开发者

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:

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

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 a开发者_JS百科re also two diagonal springs).

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


Easiest way to model an elastic collison (where no energy is lost) by just working out total the momentum (speed * mass) of each worm in X and Y before the collision and dividing this between the two worms after the collision. The momentum would normally divide up by mass, so simplest is if the worms are the same mass then the X and Y momentum of each after the collision is simply half the total momentum of the two before.

This means if two worms going the same speed collide they bounce off as if it were a mirror, if a moving worm hist a staitonary worm they stick together and move in the same direction at half speed

If you want more realistic movement within the worm then put the new momentum into a single segment and then model how the recoil affects the rest of the worm.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜