I’m using AndEngine and Box2d to develop a game. I’m applying a force to a body to keep it “aloft” equal to that of gravity. I have the scene set up so that:
public void onAccelerationChanged(final AccelerationData pAccelerationData) {
gravity = Vector2Pool.obtain(pAccelerationData.getX(), pAccelerationData.getY());
this.mPhysicsWorld.setGravity(gravity);
Vector2Pool.recycle(gravity);
}
Now I need to set my force applied:
body.applyForce(new Vector2(0,-*gravity*), new Vector2(body.getWorldCenter()));
How do I get the value of the gravity so that I can apply it as a force when the screen is tilted?
If you are trying to have an object which isn’t affected by gravity, you can set its body as static or kinetic. It is more efficient and avoids you extra processing.
BTW, I guess you are getting null because you have already recycled the vector with this line
Edit:
Why you don’t simply use something like this?