- Home /
Newtonian Gravity working backwards
I'm trying to make a planet affect other objects.
It kind of resembles artificial planetary gravity, but it is wierd, because the farther the object is, the more force it excerts. Perhaps there is something wrong with my formulation?
Vector3 direction = rbUnderInfluence [i].transform.position - transform.position;
intensity = gravitationalForce * (rb.mass * planetMass / distance * distance);
rb.AddForce (direction * intensity);
How are you calculating your distance? I suspect that may be where the issue is, try tracing it out and see if it is behaving as expected.
Answer by Bunny83 · Jun 21, 2016 at 03:55 AM
You have two errors here:
This
planetMass / distance * distance
will be equal toplanetMass
. Division and multiplication have the same precedence. Operators with same precedence are executed left to right. You would need to put your "distance * distance" into brackets.The second problem is that you use your direction vector directly. This vector has a "length". The further away your objects are, the "longer" / greater the vector is. You have to normalize the vector.
All in all this should work:
Vector3 direction = rbUnderInfluence [i].transform.position - transform.position;
intensity = gravitationalForce * rb.mass * planetMass / (distance * distance);
rb.AddForce (direction.normalized * intensity);
Note that using real world values will most likely create huge errors due to the large numbers.
Finally instead of your distance * distance
you could simply use direction.sqrMagnitude
. It's the distance / length of the direction vector squared.
Thank you, very usefull.
The only thing is that I had a very strange behaviour, when I wrote (distance * distance) I was not getting any result, so I just did the multiplication before and places it where the distance was before.
Also, as you mentioned, I ended up tweaking the gravitationalforce almost to 1000 so that it acted in accordance to the world values,
Thank you again.
Your answer
Follow this Question
Related Questions
Change rigidbody's jumping speed 2 Answers
Applying force to my object has no effect. 1 Answer
Applying force to a rigidbody 2 Answers
Rigidbody--Addforce on a Spherical Platform(A Globe) 2 Answers
Simulate gravity on rigidbody 1 Answer