[SOLVED] Why does not my gravitational simulation work well?


#1

I am doing a gravitational simulation of n-bodies where there is an attractor point at (0, 0, 0). But the orbits of rigid bodies change a lot for each period. What am I doing wrong? Is it a problem of the simpleUpdate? Or in the calculation of the distance to the attractor point?

@Override
    public void simpleUpdate(float tpf) {

        for (PhysicsRigidBody rbc : bulletAppState.getPhysicsSpace().getRigidBodyList()) {
            rbc.applyCentralForce(rbc.getPhysicsLocation().mult(-50f / rbc.getPhysicsLocation().distanceSquared(ZERO)));
        }
    }

#2

JME physics uses single-precision (32-bit) floating point arithmetic. For astronomical simulations, you’d probably need double precision (64-bit). Bullet Physics has double-precision capability, but accessing it from JME would require massive changes.

In the mean time, you could try a smaller physics timestep. The default timestep is 1/60 second, and you can alter it by invoking PhysicsSpace.setAccuracy(timeStep). However, since JME physics runs in real time, you’ll encounter performance issues if you make the timestep too small.


#3

Are you actually using bullet’s collision detection? If not then it’s a waste to use it at all. The “physics” part of a physics engine is dead-simple.

v = v + a * t
p = p + v * t

Apply forces as needed… which isn’t that much harder.

Edit: (and then you could pretty easily use double precision.)


#4

Thank you very much for the advice, I have already tried calling PhysicsSpace.setAccuracy (timeStep) but the bodies follow exactly the same path if timeStep is worth 1/60, 1/60000 or 0.2. I am a beginner and I would like to study where the error comes from. Interestingly, if I replace what is inside mult (…) by -5f it works very well and the orbit is stable, so I suspect that the error may come from the calculation of the distance.


#5

Thank you very much, but I would like to have the things that jBullet has like collisions between the surfaces of the bodies. I am a beginner and I would die to know why the orbits unexpectedly change and I am disappointed not to know the cause. I have noticed that if I replace what is inside mult (…) by -5f (always having an attractive force to the ZERO vector, but that remains constant with the distance) the orbits are perfect and do not alter with time . That’s why I suspect that the problem is in the calculation of distance.


#6

I notice you’re using a 1/x law for the central force instead of an inverse-square law. Also, physics forces should be applied before each timestep, not during the frame update.


#7

distance is squared in the code. How would you implement the second thing you say?


#8

Because you have the distance in the numerator, you are computing a force proportional to distance/(distance squared) which is 1/distance, not 1/(distance squared).

To apply forces before every physics step, either add a PhysicsTickListener to your PhysicsSpace or else override the prePhysicsTick method of your BulletAppState.


#9

ok, thanks, i gonna try it.


#10

I finally solved it, but I still lack the physicsTickListener. Now the orbits are stable and works well.

rbc.applyCentralForce(rbc.getPhysicsLocation().normalize().mult(-200f / rbc.getPhysicsLocation().distanceSquared(ZERO)));

I used the normalize. I would not have realized the error without your help. Thank you.