LibGDX Box2D Trying to reset body rotation.

I am trying to get a bodies rotation angle to stop incrementing to huge numbers. Usually if numbers get too high the program crashes. Not sure if Box2D deals with this. If it does then all I really need to worry about is a way to get a usable angle. Which should be quite easy to work out.

This is the only line that I’m using for rotating the body:


if(GameInputs.keyHolding(Keys.Z)) body1.applyTorque(0.01f, true);

These are the only lines I have for resetting the rotation:


if(body1.getAngle() > 359 * MathUtils.degreesToRadians){
	body1.setTransform(body1.getPosition(), 0);
}
if(body1.getAngle() < 0){
	body1.setTransform(body1.getPosition(), 359 * MathUtils.degreesToRadians);
}

As you can see every time it resets, the body jumps a little. I am not moving the body any other way, only with those lines. You’d expect that by giving it the same position before stepping the world, would have no effect. How can I reset the body rotation value?

q_RwtDMF3tk

Thanks.

I can not see why the position of the object changes when you reset it.

But you can debug the position after you setTransform the body1.
Might is there being another method called what resets the position of the body.

Or you can prevent the body from rotating instead of reset. and if you want to let it rotate again.
You start the rotation again.

take a look at:

After doing some more testing. I’ve found that it only jumps around when the body has multiple fixtures :s Even if the fixtures are far apart. I’ve also changed it so instead of going from 0° to 359° it is -179° to 179°. Although didn’t fix anything.

This is the whole render method:


@Override
public void render(float delta) {
	Gdx.gl.glClearColor(0, 0, 0, 1f);
	Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
	if(GameInputs.keyHolding(Keys.Z)) body1.applyTorque(0.01f, true);
		
	if(body1.getAngle() > 179 * MathUtils.degreesToRadians){
		body1.setTransform(body1.getPosition(), -179 * MathUtils.degreesToRadians);
		System.out.println("changed1");
	}
	if(body1.getAngle() < -179 * MathUtils.degreesToRadians){
		System.out.println("changed2");
		body1.setTransform(body1.getPosition(), 179 * MathUtils.degreesToRadians);
	}
		
	pos.text = ""+(int)body1.getPosition().x + ", " + (int)body1.getPosition().y + "\n " + (int) (body1.getAngle() * MathUtils.radiansToDegrees);
		
	batch2D.setProjectionMatrix(camera2D.combined);
	camera2D.update();
		
	batch2D.begin();
	debugRenderer.render(world, camera2D.combined);
	batch2D.end();
		
	world.step(timeStep,velocityIterations,positionIterations);
		
	batch.setProjectionMatrix(camera.combined);
	camera.update();
		
	if(GameInputs.keyHolding(Keys.Z)){
		batch.begin();
		buttonZ.render(batch);
		batch.end();
	}
		
	batch.begin();
	pos.render(batch);
	ps.draw(batch);
	batch.end();
}

Here’s an update of what it looks like. Ignore the green dot:

LULofL8pUB0

Turns out. If I add the fixtures from the center of the body equally like this

-323,0,32,32
-32
2,0,32,32
-32,0,32,32
0,0,32,32
32,0,32,32
322,0,32,32
32
3,0,32,32

It rotates perfectly. Left it rotating for around 10 minutes.

This doesn’t fix my problem though. I’m going to have random block fixtures all over the place.

First, you should be rotating between -180 and +180. Also, you’re doing this in the render method which could result in some snapping (since you aren’t interpolating)

I’ve always wondered about interpolation. I think the problem is to do with how Box2D does things. I fixed the 179 and -179 haha. I will just have to use generalized angles for each body I guess. I can calculate a normalized angle within the objects class.