Simulating a missile (using the AS3 version)

...in which case, you'd probably want to go with 1) above.
I will definitely try the method of applying a correction torque every time step and post my results here.

So, applying a correction torque every time step won't break continuous collision detection?

Thanks a lot to both of you.
Torque should always be safe - all engines should be able to easily support user defined accelerations and torques, but setting positions and velocities directly is always less safe because the engine might assume consistency in these variables from step to step.
Here is my function that corrects the missile body's torque / rotation.
Code:
      public function CorrectMissileRotation(): void
      {
         var velocityAngle: Number;
         var velocity: b2Vec2;
         var torque: Number;
         var torqueFactor: Number = -0.1;
         
         velocity = missile.GetLinearVelocity();
         
         velocityAngle = Math.atan2( velocity.y, velocity.x ) + (Math.PI / 2);
         
         torque = torqueFactor * ( missile.m_rotation - velocityAngle );
         missile.ApplyTorque(torque);

         //missile.m_rotation = velocityAngle;
      }


I think I have done exactly what you suggested. On playing around, a value of -0.1 for torqueFactor seemed to do decently well. But still, I found that, instead of correcting the torque, if I set the rotation, the flight of the missile looks so much more realistic. Maybe it is because I'm not doing the torque part right?

Thanks.
Anjan
Yup, that looks about right. Unfortunately I forgot a couple things, namely to add friction, to make it dependent on the magnitude of the velocity (things have a greater tendency to align the faster they go), and to deal with a wrap-around issue.

I think this looks better - I'm posting the Java code that I tested, should be an easy conversion to AS3:
Code:
           float velocityAngle;
           Vec2 velocity;
           float torque;
           float torqueFactor = -100.0f; //set this to whatever works for your units
           float torqueDamping = -torqueFactor; //this is a good starting place, but experiment
           
           velocity = missile.getLinearVelocity();
           
           velocityAngle = (float) (Math.atan2( velocity.y, velocity.x ) + (Math.PI / 2));
           float angVelDelta =  missile.m_rotation - velocityAngle;
           
           //Constrain to -PI -> PI
           while (angVelDelta > Math.PI){
               angVelDelta -= 2*Math.PI;
           }
           while (angVelDelta < -Math.PI){
               angVelDelta += 2*Math.PI;
           }
           
           float absVel = velocity.length();
           torque = absVel* torqueFactor * (angVelDelta) - absVel*missile.m_angularVelocity * torqueDamping;
           missile.applyTorque(torque);



With this code you can make things pretty "stiff," almost as if you were just setting the angle. The damping really damps angular motion, which makes for bizarre physics, but for missile motion it looks pretty good (to fix it up so that normal motion worked right, too, would require some additional velocity dependence and a whole lot of tuning - real life aerodynamics models tend to be messy). I wasn't sure about adding the absVel multiply to the damping term in the second to last line; in any case, you can alter this to suit your needs.
😀 Thank you. You are so helpful. I'll try this out and get back to you.

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