Hi guys,
I just encountered a problem with the gravitation force. I’m applying a “anti-gravity force” in order to compensate the gravity. These two forces should cancel each other out, but my character raises from the ground and starts to float upwards.
Does anybody have an idea where the resulting upward movement comes from?
I printed the applied forces and they were exactly equal…
Additionally, I printed the current velocity and against the expectation it increased.
Here is a small test class to demonstrate the problem:
package start;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AppState;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.PhysicsTickListener;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.objects.PhysicsRigidBody;
import com.jme3.math.Vector3f;
public class GravityTest extends SimpleApplication implements
PhysicsTickListener {
public GravityTest() {
}
public GravityTest(AppState... initialStates) {
super(initialStates);
}
public static void main(String[] args) {
GravityTest app = new GravityTest();
app.start();
}
private CollisionShape shape = new BoxCollisionShape(new Vector3f(1, 1, 1));
private float mass = 100;
private PhysicsRigidBody player = new PhysicsRigidBody(shape, mass);
private Vector3f gravity = new Vector3f(0f, -9.81f, 0f);
// force = acceleration * mass
private Vector3f antiGravForce = gravity.mult(-mass);
private boolean antigravActive = true;
@Override
public void simpleInitApp() {
player.setGravity(gravity);
player.setAngularFactor(0);
BulletAppState bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
bulletAppState.getPhysicsSpace().addTickListener(this);
bulletAppState.getPhysicsSpace().add(player);
}
@Override
public void physicsTick(PhysicsSpace space, float tpf) {
System.out.println("V post physics: " + player.getLinearVelocity());
}
@Override
public void prePhysicsTick(PhysicsSpace space, float tpf) {
System.out.println("==========================");
System.out.println("V pre physics: " + player.getLinearVelocity());
if (antigravActive) {
// counter gravitational force
player.applyCentralForce(antiGravForce);
System.out.println("countering gravity " + player.getGravity()
+ " with antigrav force " + antiGravForce);
// getGravity() actually returns gravity * mass
}
}
}
Thx 4 help