Page 1 of 1

Problem with rotate and get angle

Posted: Tue Mar 12, 2013 4:35 pm
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) );

Re: Problem with rotate and get angle

Posted: Tue Mar 12, 2013 5:03 pm
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;

Re: Problem with rotate and get angle

Posted: Wed Mar 13, 2013 10:15 am
by Daboog
Thanks, Iforce. This is my inattentiveness :)