Homing Missile physics

Jul 6, 2007 at 3:37 PM
Hello everyone,

In great anticipation for the next Farseer release, I am going through some little algorithms for homing missiles using this engine. I am able to get the bullets to "home" without the engine by simply moving the sprites x and y components based on its velocity vector and the position of the target. However, I would like for this to be done within Farseer as all other motion at this point is handled by the engine. Is there a precise way to achieve this? My current code is similar to this:

this.userEntity.sprPos.X += speed * (Math.Cos(Math.Atan2(targetPos.Y-this.userEntitysprPos.Y, targetPosX-this.userEntitysprPosX)));
this.userEntitysprPos.Y += speed * (Math.Sin(Math.Atan2(targetPos.Y-this.userEntityprPos.Y, targetPosX-this.userEntitysprPosX)));

As I said, this works, but what I would like to do is something like:

this.userEntity.EntPhysicsBody.ApplyForce(speed*New Vector2(Math.Cos(...),Math.Sin(...)));

I know this probably isn't correct, but something to that effect. Does anyone know if there is a solution for this? Also, I want to rotate the bullet so that the tip of the missile is always facing the position of the target. I know there is a method to apply Torque, but is there a way to directly rotate a sprite by a specified angle in Farseer?

Thanks again Jeff for all the time you're putting into this engine; it is such a huge benefit to the community. If you're taking donations, I'd definitely like to contribute (although it wouldn't be a ton :)) Thanks for any and all replies!
Coordinator
Jul 6, 2007 at 7:15 PM
I don't have a lot of time to answer right now but...

Look here for some great info on steering behaviors: http://www.red3d.com/cwr/steer/

You will need to modify it a bit to use forces and torques.

To "steer" your missle, try calculating the angle difference between where the missle is pointing and the target, then apply a torque proportional to the angle difference. For the force, just always apply it in the direction the missle is heading and let the steering torque control the direction.

You may need to increase the Angular Drag Coeficeint a bit to keep the missle from over rotating.

Hope this helps... sounds like a cool project. I hope to someday add behaviors to the engine.. but not for this next release..

-Jeff
Jul 7, 2007 at 12:54 AM
Edited Jul 7, 2007 at 12:58 AM
Thanks for taking the time to reply! I read through part of that document as was able to get the force portion working without a problem. I haven't spent much time with the torque just yet though. What I ended up with was:

userEntity.EntityPhysicsBody.ApplyForce(new Vector2(Math.Cos(Math.Atan2(targY - userEntity.tangibleForm.sprPosition.Y, targX - userEntity.tangibleForm.sprPosition.X)), Math.Sin (Math.Atan2(targY - userEntity.tangibleForm.sprPosition.Y, targX - userEntity.tangibleForm.sprPosition.X))));

This is pretty quick and dirty, but just for the sake of finding a solution, it works. Alternatively, if I would have gone the route of not using forces I could have done the following:

this.userEntity.EntityPhysicsBody.Move(new Vector2(Math.Cos(Math.Atan2(targY - userEntity.tangibleForm.sprPosition.Y, targX - userEntity.tangibleForm.sprPosition.X)), Math.Sin (Math.Atan2(targY - userEntity.tangibleForm.sprPosition.Y, targX - userEntity.tangibleForm.sprPosition.X))));

Where .EntityPhysicsBody.Move(posVector) is a method I added that manually adjusts the entity body position based on the posVector parameter. I know this somewhat defeats the purpose of the engine, but it does allow for less "inertia-like" movement if it is desired. This way the missile can still be affected by outward forces while having a sort of constant speed to it. Thanks again for the help Jeff!