Resolved Finding a way to debug

Napivo

Game Addict
Joined
Oct 17, 2020
Messages
8
Location
Belgium (GMT+1)
Programming Experience
1-3
I am working on a physics-based game as a hobby in my free time, I am far from a professional programmer.

I don't expect anyone to make any improvements to this silly procedure that is not working as expected.

What I would like to learn is how to find what is wrong and how you would find the error.

I think the way to go would be to create a new program in c# outside unity, that will allow any input(range) and display a graph in that case.

Is that the way?

Any input would be appreciated.


C#:
 private void OnCollisionEnter2D(Collision2D collision)
    {
        // Get the Rigidbodies from that are coliding
        Rigidbody2D other = collision.gameObject.GetComponent<Rigidbody2D>();
        Rigidbody2D myself = gameObject.GetComponent<Rigidbody2D>();

        // Get the direction of Movement od the Bodies
        Vector2 MyMovement = myself.velocity;
        Vector2 OtherMovement = other.velocity;

        // Get the movement between bodies
        Vector2 InpactMovement = MyMovement - OtherMovement;

        // Get mass of the bodies
        float MyMass = myself.mass;
        float OtherMass = other.mass;

        // Get how soft a body is
        float SoftnessMe = myself.sharedMaterial.bounciness;
        float SoftnessOther = other.sharedMaterial.bounciness;

        // Set  things if we hit floor
        if (other.name == "Floor")
        {
            SoftnessOther = Napivo.Constants.GroundSoftness;
            OtherMass = Napivo.Constants.GroundMass;
        }

        // Get the inpact delta 1-Soft = hardness
        float InpactD = ((1 - SoftnessOther) * (1 - SoftnessMe));

        // Get the impact force
        // Force = Mass * Velocity
        float InpactForceMe = (InpactMovement * OtherMass).magnitude * InpactD;

        // From now on we decide the dilimiters of the health lost

        float DamageMe = InpactForceMe < HealthThresholdMin ? InpactForceMe : 0;

        float Quota = 1;

        if ((DamageMe > HealthThresholdMin) && (DamageMe < HealthThresholdMax))
        {
             Quota = Mathf.Max(DamageMe, 0) / (HealthThresholdMax - HealthThresholdMax);
            DamageMe *= Quota;
        }

        if (!((other.name == "Floor") || (myself.name == "Floor")))
        {
            if (DamageMe > HealthThresholdMin)
            {
                Debug.Log("** Me " + myself.name + " Inpacted with " + other.name + " **");
                Debug.Log("SoftnessOther = " + SoftnessOther + " SoftnessMe = " + SoftnessMe);
                Debug.Log("InpactD = " + InpactD);
                Debug.Log("Inpact = " + Mathf.Round(InpactForceMe));
            }
        }

        if (InpactForceMe > HealthThresholdMin)
        {
            Health -= InpactForceMe;
        }
    }
 
Yes, there is. The idea is to have your core logic and data models in one or more assemblies that do not have any dependencies on Unity. The only dependencies it should have should be the .NET Framework. Then on the Unity side, you would reference these assemblies and your Unity specific classes are simple thin wrappers that translate Unity specific data into your data model, and invoke your core logic. And for that other application you are thinking of building to, you do the same thing: you just setup the data models and call the core logic. Build all the graphing that you want in this other application.
 
In the end, I only had 3 errors in my code :(

I took Skydiver's advice to heart.

OnCollisionEnter2D now collects the data I need to make calculations
GetDamage Calculates the damage independent of unity (and is static)

In my case, I still want to keep it in my class. but in the future, I might move GetDamage to a global static class.

C#:
private void OnCollisionEnter2D(Collision2D collision)
    {
        // Get the Rigidbodies from that are coliding
        Rigidbody2D other = collision.gameObject.GetComponent<Rigidbody2D>();
        Rigidbody2D myself = gameObject.GetComponent<Rigidbody2D>();

        // Get the directionof Movement od the Bodies
        Vector2 MyMovement = myself.velocity;
        Vector2 OtherMovement = other.velocity;

        // Get the movement between bodies
        Vector2 InpactMovement = MyMovement - OtherMovement;

        InpactMovement = collision.relativeVelocity;

        // Get mass of the bodies
        float MyMass = myself.mass;
        float OtherMass = other.mass;

        // Get how soft a body is
        float SoftnessMe = myself.sharedMaterial.bounciness;
        float SoftnessOther = other.sharedMaterial.bounciness;

        // Set things if we hit floor
        if (other.name == "Floor")
        {
            SoftnessOther = Napivo.Constants.GroundSoftness;
            OtherMass = Napivo.Constants.GroundMass;
        }

        float DamageMe = GetDamage(InpactMovement, OtherMass, SoftnessMe, SoftnessOther, HealthThresholdMin, HealthThresholdMax);

        if (DamageMe > HealthThresholdMin)
        {
            Health -= DamageMe;
        }
    }

    public static float GetDamage(Vector2 InpactMovement, float OtherMass, float SoftnessMe, float SoftnessOther, float HealthThresholdMin, float HealthThresholdMax)
    {
        // Get the inpact delta 1-Soft = hardness
        float InpactD = ((1 - SoftnessOther) * (1 - SoftnessMe));
        float InpactForceMe;

        // Get the impact force
        // Force = Mass * Velocity
        InpactForceMe = (InpactMovement * OtherMass).magnitude * InpactD;

        // From now on we decide the delimiters of the health lost
     
        float DamageMe = InpactForceMe > HealthThresholdMin ? InpactForceMe : 0;
        float Quota = 1;

        if ((DamageMe > HealthThresholdMin) && (DamageMe < HealthThresholdMax))
        {
            Quota = Mathf.Max(DamageMe - HealthThresholdMin, 0) / (HealthThresholdMax - HealthThresholdMin);
            if (Quota == float.PositiveInfinity)
            {
                Debug.Break();
            }
            DamageMe *= Quota;
        }

        return DamageMe;
    }
 
Last edited:
Back
Top Bottom