- Home /
Converting Local to World Forces in gravity simulation.
I have a semi-realistic solution to orbital gravity but it has several problems:
It uses a system that only allows body-body gravity.
It works in local space.
It makes my planets turn unrealistically.
It only works for large objects(planets, moons, stars)
I'd like to find a way to convert the local forces to world forces and store them in variables, then apply them in world space.
I think that I should describe this more in depth, so here we go.
At the moment my system has two spheres of different sizes childed to there own empty objects. The empty object is rotated (problem 3) to face the other's sphere, so the planets turn as the empty object turns and we all know this doesn't happen. Then, a second script uses a function to find the distance between them and does this equation g = (GxM1xM2)/(DxD). This gives output in Newtons, then I divide it by its imaginary mass. Imaginary mass is a mass to large to simulate with the ridgedbody. This gives a force of Newtons equal to Meters per Second Squared if the mass of the ridgedbody is 1kg. Then, I apply this as a constant force in the local Z (problem 2) direction of the empty object (Ridgedbody's mass is set to 1kg). Now, I have gravity! So as the "planet" is childed to the empty object, it will not work for an object (spaceship) that has other forces applied to it (problem 4). Also, it can only face one object at a time. Therefore the force can only be applied in one local z direction. Making it impossible to create a multi-body gravity simulation where a moon pulls on a planet while it gets pulled on by its planet's star (problem 1).
I can put up the scripts if it helps but not if you dont need them. If you need more explantion on a aspect of the system I can supply that to.
Thanks for the help.
Answer by ScroodgeM · Jul 14, 2012 at 12:56 PM
why can't you add force to rigidbodies in different directions?
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html
you can calculate force for each pair of obects and apply force every FixedUpdate() in relative directions - unique for each pair. also it's better to ignore far-placed pairs to make calculations lighter, close object's gravity is much more powerful then far-placed, so you can ignore it in usual game.
this is what i want to accomplish but i'm not sure how to calculate the forces for the individual axis
you have two objects in position pos1 and pos2 (Vector3). just take (pos2-pos1) and you'll got direction from pos1 to pos2, so you must apply force to obj1 in this direction, and to obj2 in opposite direction
http://answers.unity3d.com/questions/284173/distinguish-between-two-colliding-objects.html
just like this but inverse directions
thanks so much for your help at first i was really confused but when i figured out you needed to normalize the vector3 it worked! but now a small and probably obvious question how to you apply a world constant force because it seams i can only apply a relative force and that wont work for a spinning planet or ship? Never $$anonymous$$d i found the answer in the spelling you have to use a lower case when spelling .force facehoof /)-(\
Your answer
![](https://koobas.hobune.stream/wayback/20220613074019im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Finding "UP" while using planetary gravity? 0 Answers
Possible to create axis centered gravity? 1 Answer
Moving object along its own axis 2 Answers
Centric gravity issues... 1 Answer
Planetary based gravity 0 Answers