Homing Missile

Hi,

Does anyone have any advice on a basic homing missile mechanic? I have tried to understand OpenSteer but it is way too complex for me, and isnt physics based.

Regards

i
Well, you could calculate the angle between the missile's direction and the target, and then rotate the missile a tiny bit if required. Do this on each step.
Hi,

How do I rotate a Box2D body which is one shape, I have to work out some torque to apply to get the necessary rotation? You cant just set the rotation of a body can you?

Regards

i
Actually, yes you can.
Code:
   /// Set the position of the body's origin and rotation.
   /// This breaks any contacts and wakes the other bodies.
   /// Manipulating a body's transform may cause non-physical behavior.
   /// @param position the world position of the body's local origin.
   /// @param angle the world rotation in radians.
   void SetTransform(const b2Vec2& position, float32 angle);


So, you could use
Code:
myBody->SetTransform(myBody->GetPosition(), newAngle);
Hi,

Thanks...will this spoil the simulation at all? Setting the position manually rather than via forces?

Regards

i
I'd apply torques. You can compute the current 'error', the angle between the current direction and the desired direction (missile position to target position vector) and apply a torque using this error as 'weight'. Also cap the torque to a maximum value.
Thanks,

How do you clamp the maximum steering force? OpenSteer talks about a capped steering force, I guess this is linked to the turning radius you desire,? Is it just capping the size of the steering angle, or capping the adjustment vector??

Sorry for the silly questions!

Regards

i
It's simpler and more accurate to apply a force in the given direction, rather then just rotating the velocity.
You can essentially treat the body as a particle (as previously suggested, I believe) by disabling rotation for the body. (This option is exposed via the Box2D API, IIRC.) You can then use whatever orientation you want for rendering purposes; a logical solution would be to orient the object so that its forward direction vector is parallel to the velocity vector.

This assumes that the object can be reasonably represented with (e.g.) a circle for the purpose of collision detection. If you need greater accuracy than that, you may need to orient the body using physics as described previously (you might also be able to use a kinematic body for this, although I haven't tried that method myself).

If you go the physics route, I imagine you'd want to apply corrective torque based on the body's current orientation and the velocity vector. Or, you could try to model a guided projectile more accurately by applying a constant thrust in the forward direction, and apply lateral thrusts as needed to guide the projectile (at least I assume this is how it works in actuality, more or less).
Hey,

there shouldn't actually be a problem if you just manually set its position; because the nature of a missile is, as soon as it touches something it'll explode right? So the simulation won't break because you will be destroying the missile as soon as it has a collision with anything, so theres no physics simulation to mess up.

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