Gravity!


Lesson learned do not try to intuit how gravity works if you want a good result.

After looking into the math a bit I ended up with this code which is a little messy but I'm pretty happy with:

Vector2 calculategravityforce()
{
    float mass = getobjectmass(gameObject); 
    Vector2 distance = getdistance();  
    Vector2 gravityforce = distance.normalized * (Power * (mass * getobjectmass(Target) * MassMultFactor)/Mathf.Pow(distance.magnitude / DistanceMultFactor, DistancePowFactor));   
    return gravityforce;     
}

Setting power to 9.8, MassMultFactor & DistanceMultFactor to 1 and DistancePowFactor to 2 gives a pretty good approximation of real gravity. I ended up changing some of the values a little to improve the gameplay but it's very easy to change so it's good for rapidly testing different variables.

I considered using an animationcurve for some of those values so, for instance, the massmultfactor could be higher for low mass objects than high mass ones. After testing it though the extra customization wasn't really worth the cumbersome UI for using it.

Leave a comment

Log in with itch.io to leave a comment.