## Detecting crushing

Official forum for the Chipmunk2D Physics Library.

### Detecting crushing

Hi,

In my setup, I'm using a body+rectangular shape to represent a character moving around.
I'm using static bodies+shapes to represent moving platforms. I manually position these static bodies and rehash them along each step during movement.

I would like to detect when my character is about to be 'crushed' by a moving platform - for example, he's standing on one which is going up and about to hit another. Currently the body just slides around until it comes out which is understandable considering I'm skirting the laws of physics with my moving platform.

Is there anyway to detect when the body is getting forced between 2 other bodies?
statictype

Posts: 2
Joined: Wed Mar 16, 2011 4:30 am

### Re: Detecting crushing

I think so, although it wouldn't be trivial to do. I actually started writing a function to do exactly that a couple years ago: http://code.google.com/p/chipmunk-physi ... biter.c#74 To be clear, that function does not work because it only looks at one arbiter at a time. That's why it's not declared in the header files, but I figured I'd want to come back to it someday.

To make something that would work, you'd have to do something like this:
Code: Select all
`postsolve(...){  ...  cpVect impulse = cpArbiterTotalImpulse(arb);  myObject->vsum = cpvadd(myObject->vsum, impulse);  myObject->fsum += cpvlength(impulse);}myObject->vsum = cpvzero;myObject->fsum = 0.0f;cpSpaceStep();cpFloat crushing = 1.0f - cpvlength(myObject->vsum)/myObject->fsum;`

Basically, you are looking for when the sum of the impulses is very small compared to the sum of their magnitudes. When an object is being crushed between two platforms, the impulses from both collisions will be very nearly equal but in opposite directions (depending on the number of iterations anyway). The should almost cancel each other out, meaning that their vector sum will be very small.

I've never actually had a chance to try this, so I'd be interested to see if it works reliably.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/

slembcke

Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

### Re: Detecting crushing

Thanks! Will play around with this and see what I can do.

I thought of two other approaches but not sure if they are viable within Chipmunk's architecture:

1) Crushing mostly leads to a body significantly overlapping with one or more other bodies in space. Is it possible to detect this?

2) Instead of a single body (or may be in addition to,) use a spring with bodies attached at either end. Detect the compression on the spring.
I assume this second method won't work because non-rigid bodies are outside of Chipmunk's scope. Is that correct?
statictype

Posts: 2
Joined: Wed Mar 16, 2011 4:30 am

### Re: Detecting crushing

If you want to try #1, you can get the depth of a collision point. It's in the cpArbiter documentation. #2 would work just fine too, Chipmunk has springs.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/

slembcke

Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

### Re: Detecting crushing

Just letting you know that the solution suggested by slembcke works perfectly when you take into account all arbiters affecting a single body.

It works best if you check for a certain crushing value during several frames in a row to rule out any sudden and short "crushing" impacts. And also make sure the overall magnitude is big enough to cause crushing.
rrakkola

Posts: 9
Joined: Thu Jun 27, 2013 3:04 pm