This project has moved. For the latest updates, please go here.

Path/Chain Controlled by Mouse

Mar 7, 2010 at 9:12 PM

Hello Farseer community,

I've created a path which looks and acts like a chain successfully in C#/XNA using the following code:

ComplexFactory.Instance.DampingConstant = 10;
ComplexFactory.Instance.SpringConstant = 150;
chain = ComplexFactory.Instance.CreateChain(physicsSimulator, new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2), new Vector2(-GraphicsDevice.Viewport.Width, -GraphicsDevice.Viewport.Height), 20f, 20f, 30, .8f, true, false, LinkType.RevoluteJoint);
chain.CreateGeoms(physicsSimulator, 10);
foreach (Body body in chain.Bodies)
    body.IgnoreGravity = true;
    Sprite sprite = new Sprite(chainTexture, body);

Then I fix the first chain body in place using a fixed revolution joint because I want the chain to be attached to the stage. I want the other end of of the chain to follow the mouse. I've tried updating the last chain body to be positioned to the mouse position in the update method. I've also tried attaching a spring from the last chain body to the mouse position. Hmm, I even tried applying a force to the last chain body in the direction of the mouse movement (current position - last position). None the these provide the desired result (I'm assuming you can imagine the desired result - I can always elaborate if need be) and most of the time they throw a Not-a-Number exception corresponding to the variable AngularVelocity at line 880 in the Body.cs file:

_rotationalDrag = AngularVelocity * AngularVelocity * Math.Sign(AngularVelocity);

Farseer is a great engine! It is easy to use and if I have a problem normally it doesn't take long to resolve it. However, I can't seem to get this chain to follow the mouse! I tried manually creating a chain and adjusting the joints and constraints. I've tried dampening the mouse input. I've tried multiple types of constraints instead of revolution joints between the chain bodies. This Not-a-Number just keeps popping up and I'm sure it's because the mouse creates to dramatic of a force on the chain.

Can anyone help resolve this?

Thanks in advance!