Curved velocity

Quote:
float normalize_angle( float a )
{
while( a < 0 ) a += deg2rad( 360 );
while( a >= 360 ) a -= deg2rad( 360 );
return a;
}


Isn't this code wrong? shouldn't the 360 in "a >= 360" be deg2rad(360)?

EDIT: As always, my knowledge of 2d math is a standstill for this. I'm not good with theories, since I don't know trig or anything offhand.

Code:
inline void DoBulletAngling (BulletBody *B)
{
   float dest_angle = atan2( B->PhysicsBody->GetLinearVelocity().y, B->PhysicsBody->GetLinearVelocity().x );
   // Find the shortest variation
   float var1 = (abs(dest_angle - B->PhysicsBody->GetAngle()));
   float var2 = (abs((dest_angle + DEG2RAD(360)) - B->PhysicsBody->GetAngle()));
   float var3 = (abs((dest_angle - DEG2RAD(360)) - B->PhysicsBody->GetAngle()));

   if (var1 < var2 && var1 < var3)
      B->PhysicsBody->SetAngularVelocity (( var1 - B->PhysicsBody->GetAngle() ) * 4);
   else if (var2 < var1 && var2 < var3)
      B->PhysicsBody->SetAngularVelocity (( var2 - B->PhysicsBody->GetAngle() ) * 4);
   else if (var3 < var1 && var3 < var2)
      B->PhysicsBody->SetAngularVelocity (( var3 - B->PhysicsBody->GetAngle() ) * 4);
}


I'm missing something. I can't see in your previous example where you get the derivative from or what -> is implying

-P
Quote:
Isn't this code wrong? shouldn't the 360 in "a >= 360" be deg2rad(360)?

Yeah, you're right. 🙂
And looks like there's a bug in your code.
Instead of "B->PhysicsBody->SetAngularVelocity (( var1 - B->PhysicsBody->GetAngle() ) * 4);"
you have to use the signed version of var1:
"B->PhysicsBody->SetAngularVelocity (( dest_angle + DEG2RAD(360) - B->PhysicsBody->GetAngle()) ) * 4);"
Then the code should be correct.
Hmm, but that confuses me because we aren't using var1 now.. the + DEG2RAD(360) is for var2.. also I realized I forgot to normalize the angle but I don't know what angle I have to normalize..

Code:
inline void DoBulletAngling (BulletBody *B)
{
   float dest_angle = atan2( B->PhysicsBody->GetLinearVelocity().y, B->PhysicsBody->GetLinearVelocity().x );
   // Find the shortest variation
   float var1 = (abs(dest_angle - B->PhysicsBody->GetAngle()));
   float var2 = (abs((dest_angle + DEG2RAD(360)) - B->PhysicsBody->GetAngle()));
   float var3 = (abs((dest_angle - DEG2RAD(360)) - B->PhysicsBody->GetAngle()));

   if (var1 < var2 && var1 < var3)
      B->PhysicsBody->SetAngularVelocity ((( dest_angle + normalize_angle(B->PhysicsBody->GetAngle())) ) * 4);
   else if (var2 < var1 && var2 < var3)
      B->PhysicsBody->SetAngularVelocity ((( dest_angle - DEG2RAD(360) + normalize_angle(B->PhysicsBody->GetAngle())) ) * 4);
   else if (var3 < var1 && var3 < var2)
      B->PhysicsBody->SetAngularVelocity ((( dest_angle + DEG2RAD(360) + normalize_angle(B->PhysicsBody->GetAngle())) ) * 4);
}


😕

Signed version of var1? In what?

-P
Whoa, sorry. It was very late at night when I looked at the source code. :/
You have to normalize the source and destination angles.
Otherwise, the source code should be ok now.. 😀

And the signed version means without the "abs". Sorry for my terms, I'm not good at remembering the correct terms.. 😛
Haha, s'okay, I have moments like that at night as well.

We seem to be getting somewhere; the bullet is rotated 180 deg in the wrong direction though, easy fix to change the dest angles, then we should be good!

Thanks for the help!
I'll post the finished code.

It's unoptimized at the moment because I just wanted to get it working, but here it is:

Code:
inline void DoBulletAngling (BulletBody *B)
{
   float dest_angle = normalize_angle(atan2( B->PhysicsBody->GetLinearVelocity().y, B->PhysicsBody->GetLinearVelocity().x ) - b2_pi);
   float src_angle = normalize_angle(B->PhysicsBody->GetAngle());
   // Find the shortest variation
   float var1 = (abs(dest_angle - src_angle));
   float var2 = (abs((dest_angle + DEG2RAD(360)) - src_angle));
   float var3 = (abs((dest_angle - DEG2RAD(360)) - src_angle));

   if (var1 < var2 && var1 < var3)
      B->PhysicsBody->SetAngularVelocity (( dest_angle - src_angle)  * 4);
   else if (var2 < var1 && var2 < var3)
      B->PhysicsBody->SetAngularVelocity (( dest_angle + DEG2RAD(360) - src_angle ) * 4);
   else if (var3 < var1 && var3 < var2)
      B->PhysicsBody->SetAngularVelocity (( dest_angle - DEG2RAD(360) - src_angle) * 4);
}


-P

Original thread: https://box2d.org/forum/viewtopic.php?t=3577