Simple question - turning a Body to face the mouse

Topics: User Forum
May 20, 2010 at 7:34 PM

I'm just getting started with Farseer Physics (and with game physics in general), so forgive me for my ignorance. How would I go about making a Body turn to face the mouse? (Or any other point for that matter.) I mean in a way that doesn't directly set the Body's Rotation to atan2(vecToMouse.Y, vecToMouse.X), but by turning gradually - going the short way round, of course! I assume that it would involve figuring out the Body's direction vector and using its dot product with the vector to the mouse, but I don't know how I would best implement this with Farseer.

May 21, 2010 at 12:10 AM

In Farseer 3, if the body is connected with a revolutejoint you can use the motor to track the angle. It is explained in the Box2D Manual.

Otherwise you would just gradually increase the angle by a certain amount until it reaches the required angle. If you want it to look smooth you can use a Cos function.

In Farseer 2 there is an AngleJoint and a FixedAngleJoint which keep an object at a certain angle, which might be usefull to you.

To get the short way around I think you can use MathHelper.WrapAngle, which wraps an angle between -Pi and Pi.