Problems with Chain - shortening and tightening

Topics: Developer Forum, User Forum
Aug 21, 2009 at 12:08 PM
Edited Aug 21, 2009 at 12:12 PM


I have been using the code from the advanced sample 5 to try and make myself a chain for my game. I have managed to create the chain, attach it the a point and then create a PinJoint between the last of the chain bodies and the square that is currently my object.

It seems to work okay - but i do have a few problems, so I hope you guys can help!

1) I would like to generate the chain when an object created at mouseclick, hits the first geometry in its way. I have worked out the collision bit, but i can't seem to get rid of the previously created chain. It draws the new chain just fine but, but it is still attached to the now invisible chain of before, Is there anything i need to do when i dispose of a chain, before creating the new one?

2) The chain is always a lot longer than distance between the gemoetry and the box! Or maybe it isnt and just has so much give that it extends a lot further : / Any ideas on how to counter this? I wont the chain to have more elements and be less stretchy...

Here is my code:

                        ComplexFactory.Instance.SpringConstant = 150;
                        chain = ComplexFactory.Instance.CreateChain(_physicsSimulator, collisionVector, guyBody.Position,
                                                                    20f, 10f, 1f, true, false, LinkType.RevoluteJoint);
ComplexFactory.Instance.SpringConstant = 150;
chain = ComplexFactory.Instance.CreateChain(_physicsSimulator, collisionVector, guyBody.Position, 20f, 10f, 1f, true, false, LinkType.RevoluteJoint);
chain.CreateGeoms(_physicsSimulator, 0);


Anyone got any ideas?



Aug 22, 2009 at 4:55 PM

1) After you're finished using a Path, you need to do the reverse of Path.AddToPhysicsSimulator(). If you want to use it again you could set Enabled = false on every element of the path. I guess both a "RemoveFromPhysicsSimulator" and an Enabled property should be added to the Path class.

2) I've never had stretchiness issues with LinkType.RevoluteJoint. Maybe the PinJoint attaching it is contributing - I used revolute joints for the attaching - manually created rather than using the pinStart and pinEnd parameters.

On a related matter, if you use LinkType.LinearSpring, there is another overload for CreateChain with a linkWidth parameter to control the gap between geoms in the chain. Use this in conjuction with ComplexFactory.Instance.SpringConstant and ComplexFactory.Instance.SpringRestLengthFactor for a stiff springy chain that doesn't sag after creation.