Curved velocity

Is there any way to set the angular velocity of an object so that it curves in flight? For example, a bullet fired from a gun, if you shot it straight up, once it stops flying and starts going downwards, the bullet should do a 180 degree turn.

Any way of replicating this feature?

-P
You would need some wind model to achieve this.
I'm not sure about needing a wind model to accomplish this. Isn't it simply a matter of having the front of the bullet as the center of mass?
angular_velocity = ( atan2( linear_velocity.y, linear_velocity.x ) - angle ) * turn_speed; 😎
Could work.. 😀
P.S. turn_speed - how fast you want it to turn in the right direction. You'll have to find the best value for this variable yourself.
In a vacuum we will have conservation of angular momentum. A rigid body won't start turning on it's own.

http://en.wikipedia.org/wiki/Angular_momentum

An arrow will stay tangent to it's trajectory because of the feathers.
You're all overcomplicating things.

I don't want wind, I just want the bullet to be angled the same as his linear velocity; just by using SetXForm to set the angle. I don't need angular velocity or anything.
I'm not looking for realism, I just want it so that if a bullet gets fired it will turn with it's angle (it looks odd seeing a fired grenade not angle)

EDIT: Sorry, just read snake5's post, I'll test it out in a little bit!

-P
Alright, I tested your idea snake5, it half worked; the bullet sometimes rotates like 4 times before aiming towards it's destination.
Any ideas?

-P
Try changing the turn_speed value. What value do you have there now?

EDIT: Oh I finally understood why.. you have to normalize the angles (0-360) and then find the fastest way to the destination angle.
So, I use the normalized linear velocity?
Code example? I suck at 2d math!

-P
No, you only have to normalize the angles.
The easiest approach for that would be like this:
Code:
//deg2rad - angle unit conversion from degrees to radians
float normalize_angle( float a )
{
while( a < 0 ) a += deg2rad( 360 );
while( a >= 360 ) a -= deg2rad( 360 );
return a;
}

A bit more advanced code would be like this (I'm not sure that it works but it should 😀)
Code:
float normalize_angle( float a )
{
a = fmodf( a, deg2rad( 360 ) );
if( a < 0 ) a += deg2rad( 360 );
}

And for the fastest way to destination angle you'll have to look at three cases
1. src_angle -> dest_angle
2. src_angle -> dest_angle + 360 degrees
3. src_angle -> dest_angle - 360 degrees
find the shortest way of those -- abs( dest_angle_variation - src_angle )
and then use this code:
angular_velocity = ( dest_angle_variation - src_angle ) * turn_speed;
Now it should work.. 😀

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