Relative gravity
Asked Answered
I

3

8

I've started using jMonkey engine recently, which is very nice. But I got stuck trying to implement relative gravity.

I want to make planets orbiting around each other (not necessarily in perfectly circular orbit, depends on velocity). So every object should affect other objects.

What I have right now:

turning off global gravity

bulletAppState.getPhysicsSpace().setGravity(Vector3f.ZERO);

initializing spheres and adding to physics space

Sphere sphere = new Sphere(50, 50, 5);
Geometry sun = new Geometry("Sun", sphere);

sun.setMaterial(stone_mat);
rootNode.attachChild(sun);
sun.setLocalTranslation(0, 0, 0);

sunPhysics = new RigidBodyControl((float) (50*Math.pow(10, 5)));
sun.addControl(sunPhysics);
bulletAppState.getPhysicsSpace().add(sunPhysics);

Geometry mercury = new Geometry("Mercury", sphere);

mercury.setMaterial(stone_mat);
rootNode.attachChild(mercury);
mercury.setLocalTranslation(15f, 0, 0);

mercuryPhysics = new RigidBodyControl((float) (5));
mercury.addControl(mercuryPhysics);
bulletAppState.getPhysicsSpace().add(mercuryPhysics);

I noticed that there is method setGravity in RigidBodyControl class, but it just sets the direction. So object goes that way until it disappears.

I am really looking forward for answers.

Impetuosity answered 11/9, 2012 at 13:21 Comment(0)
S
1

Gravity in bulletphysics is one direction for all objects.

You should set gravity to 0 like you did and apply force too all objects after every simulation step using following formula

F = m * a

F - force
m - objects mass
a - acceleration 

regular acceleration on earth is g == 9.8

In space acceleration may every depend on distance from planet or planets.

If you like to simulate game like Angry Birds Space then you should consider reading article about gravity in that game http://www.wired.com/wiredscience/2012/03/the-gravitational-force-in-angry-birds-space/

Shaunteshave answered 12/9, 2012 at 11:33 Comment(2)
You can apply force with applyCentralForce methodShaunteshave
Thanks, I though that there may be "more simple" way to implement that. But no problem, I will try it the way you suggested :)Unstrap
C
3

The law of gravitation says F = G (m1 * m2) / r^2 where m1 and m2 are the masses of the two bodies, and r is the distance. G is the gravitational constant.

Newton's second law says F = m * a, so if we put them together, the body with mass m1 would experience an acceleration of a = G * m2 / r^2 from gravitational pull from the body with mass m2.

Now, what you would have to do is: In each step of the simulation, for each body, sum up the as for each other body and apply that acceleration to the body's velocity.

a(body1) = G * sum[ mass(body2) / dist(body1, body2)^2 , for each body2 ]
Cyclone answered 14/9, 2012 at 10:39 Comment(0)
S
1

Gravity in bulletphysics is one direction for all objects.

You should set gravity to 0 like you did and apply force too all objects after every simulation step using following formula

F = m * a

F - force
m - objects mass
a - acceleration 

regular acceleration on earth is g == 9.8

In space acceleration may every depend on distance from planet or planets.

If you like to simulate game like Angry Birds Space then you should consider reading article about gravity in that game http://www.wired.com/wiredscience/2012/03/the-gravitational-force-in-angry-birds-space/

Shaunteshave answered 12/9, 2012 at 11:33 Comment(2)
You can apply force with applyCentralForce methodShaunteshave
Thanks, I though that there may be "more simple" way to implement that. But no problem, I will try it the way you suggested :)Unstrap
S
0

If you want stable system, don't use simulated physics. (But if system have enough significant bodies, noone knows how it should behave, so it does not matter).

For things like solar system, I would use kinematic mode for planets (move them yourself) and for ships, asteroids etc. use forces. (Unless you make biliard in solar system)

Sunn answered 14/9, 2012 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.