We are developing a 2D top down game with libgdx using the included box2d as physics engine.
We don't want our bodies to accelerate but to instantly move with the wanted velocity. The bodies are moved like this, which works perfectly for our purpose:
Vector2 currentVelocity = body.getLinearVelocity();
Vector2 targetVelocity = velocity.velocityApplyVector.cpy();
targetVelocity.sub(currentVelocity);
targetVelocity.scl(body.getMass());
body.applyLinearImpulse(targetVelocity, body.getWorldCenter(), true);
The problem now is, that we also want the bodies to instantly rotate to the direction they are facing. The direction of the player is determined by the mouse position, so it is possible that the body is moving in another direction than it is facing. I tried to accomplish it like this:
float bodyAngle = body.getAngle() * MathUtils.radDeg;
float totalRotation = (inputRotation - bodyAngle) % 360;
if(Math.abs(totalRotation) > 1) {
if ( totalRotation < -180) totalRotation += 360;
if ( totalRotation > 180) totalRotation -= 360;
float impulse = body.getInertia() * totalRotation;
body.applyTorque(impulse, true);
}
This causes the body to rotate in the wanted direction but it needs multiple frames until it totally reaches the wanted angle.
The body mass is set to 70 and the angular damping to 8 (I tried some values for the damping and 8 was the best value for my cause, but that's, of course, not a final solution...).
I guess I have to somehow increase the needed impulse depending on the body mass and the angular damping (we want to be able to change it) but I can't figure out how to get the values right. I don't want to use setTransform
since it totally destroys the physically correct behaviour of the bodies. How can I calculate the needed values? Is there any better way to solve my problem?