# Need help with collision detection/resolution in a 2D simulation of 'worm-like' creatures

(originally posted this on regular stack overflow, somebody pointed this forum out :-) )

Hi there,

I'm implementing a physics engine for the simulation of worm-like creatures. Problem is, is that they're able to swim over each other and occupy the same point in space. So you have some idea of the environment, I provide a video here: http://www.box.net/shared/mlddkslszbxd59cfdhpc

So what I need to do is incorporate a collision detection and resolution algorithm that would overcome this. For the detection part, I've been utilising bounding circles; each worm is given an outer coarse circle which if crossed then triggers detection around smaller fine grain detection circles as placed around each body segment.

That bit is relatively easy. The more difficult bit is to decide what to do given a collision. My problem is how to derive and correctly set the velocities and forces of the point masses after a collision has taken place.

Just in case its not entirely obvious, each worm is made out of springs and point masses. Each body segment of a worm assumes a rectangular shape at rest with 4 point masses and 6 springs (since there are also two diagonal springs).

Now it was suggested over on regular stack overflow that I implement an elastic collision by summing the momentum of worm A and the momentum of worm B (when they collide). The way I do this is on a body segment by body segment basis: if two body segments from two different worms are close enough (i.e., colliding), then I sum the momentum of each and divide the result by 2. This yields a new velocity value which I then apply to the velocity of each body segment. This is meant to have the effect of 'springing' the colliding body segments away from each other.

However, I don't think I'm implementing it correctly. The issues are: when worm A hits worm B, if they are both swimming strongly enough, they both still swim through each other albeit at a slower pace. An example of this is shown in this video here: http://www.box.net/shared/sankz3i7xeenilhzhqfo

The second issue is that if two worms are within collision range and worm A swims away from worm B, then worm A proceeds to pull worm B along with it although at a slower speed than normal un-hindered swimming.

I'm really not sure how to go about this. I've read about requiring the setting of angular velocity for two colliding bodies but I'm not sure whether this only applies to solid bodies or not..An elastic collision seems the most sensible given that I'm using a spring based model.

If anyone can help, I'd greatly appreciate it

Thanks,

Ben.