Search code examples
collision-detectiongame-physicsphysics-engine

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


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:

Example of worms interacting with coarse grain and fine grain collision circles

That bit is relatively easy. The more difficult bit is to decide what to do given a collision. My first thought was to simply force the offending point masses away from each other but this had some fairly strange effects. I guess a more realistic approach would alter the velocities of the offending point masses to essentially knock them off course. My problem then is how to derive these velocities and associated forces.

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).

Cheers for any help, I really appreciate it! :-) Ben.


Solution

  • Martin was def on the right track in terms of modelling an elastic collision. The thing I didn't initially realise however was that I also needed to check the direction that each worm would be moving in and only apply the velocity change if they were moving towards each other. Secondly the positions of the offending point masses would also have to be updated by subtracting the amount by which they would penetrate each others bounding circles. Generally I found the answer here to be extremely useful:

    https://gamedev.stackexchange.com/questions/12059/why-do-objects-interpenetrate-in-this-simple-collision-solver

    I'm pretty much using the same code and the worms are bouncing off and around each other as expected.

    Cheers,

    Ben.