Problem with rotate and get angle

General discussion about Box2D tutorials
Post Reply
Daboog
Posts: 22
Joined: Tue Jan 15, 2013 4:38 pm

Problem with rotate and get angle

Post by Daboog »

Hi!
I have some problem with manipulation. I want to rotate the body by joystick, I look the tutorial http://www.iforce2d.net/b2dtut/rotate-to-angle and use method

Code: Select all

  
  float nextAngle = bodyAngle + body->GetAngularVelocity() / 60.0;
  float totalRotation = desiredAngle - nextAngle;//use angle in next time step
  body->ApplyTorque( totalRotation < 0 ? -10 : 10 );
Joystick have 360 dec (2 PI) and Body rotate after cursor, but when joystick have 0 dec and body have angle
360, body rotate in the opposite direction.

I try to solve this problem by setTranslate, but it is does not always work...

Code: Select all

		
float bodyAngle = flBody.getAngle();
		float roundAngle = round(angle,1);

		if(roundAngle == 0 || roundAngle == (float)(6.3)){
			flBody.setTransform(flBody.getPosition(), roundAngle);
			//System.out.println(" Angle - " + roundAngle + " BodyAngle " + bodyAngle);
		}

		float nextAngle = (float) ((bodyAngle + flBody.getAngularVelocity() / 60.0));
	    float totalRotation = angle - nextAngle; //use angle in next time step

		flBody.applyTorque( (float) (totalRotation < 0 ? -0.5 : 0.5) );
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Problem with rotate and get angle

Post by iforce2d »

It's important to 'normalize' the angle so you only turn in the shortest direction, as mentioned in the tutorial:

Code: Select all

while ( totalRotation < -180 * DEGTORAD ) totalRotation += 360 * DEGTORAD;
while ( totalRotation >  180 * DEGTORAD ) totalRotation -= 360 * DEGTORAD;
Daboog
Posts: 22
Joined: Tue Jan 15, 2013 4:38 pm

Re: Problem with rotate and get angle

Post by Daboog »

Thanks, Iforce. This is my inattentiveness :)
Post Reply