Simple collision detection when 2 objects push body into eachother

Started by
5 comments, last by bwhiting 10 years, 11 months ago

Probably a really easy question for you season pros, but I can't seem to think of an obvious solution to this problem.

Here it is in a simple form:

Imagine a rectangle travelling horizontally, it collides with another rectangle that is above it, overlapping it only slightly from above:

_____

___ | |

| | |-------|

| |

| | ----->

|___|_______________

This will result in the object being pushed down into the floor (the penetration in the downward direction may be the smallest) which will lead to it being pushed back up by the floor.

How do you avoid this problem and ensure that the object stops dead on collision rather than gets pushed down.

I though about separating the collision into stages, testing only one axis at a time but that feels clunky.

Hold the phones, whilst writing this I think maybe I should never check the bottom side of the top body because its normal (if dotted with the direction of the other body) would show that they should never be tested against each other.

Maybe that is more important than I though, never check a face if its normal dot the direction is greater than 0? Am I on the right lines? Am only really dabbling here so not the end of the world if I can work it out. Thanks for your time.

smile.png

Advertisement

I think this is quite a tricky problem. I see two solutions:

1. Design your levels in a way that this never happens. This is the case for most platformers. For example, if you have tile based levels, you would choose the player and tile size appropriately.

2. Take a continuos collision detection/resolution approach which prevents those objects from penetrating in the first place. E.g., speculative contacts.

Thanks for the replies, 1 was the obvious solution and a perfectly acceptable one - I just figured there was an easy solution. 2, I have read a bit about this on wildbunny and it looks great but more complex than I was hoping was needed to fix the issue. (will get round to it at some point I am sure).

Any thoughts on the idea of using normals as a guide?

Using normal vs velocity as you describe won't work if you're still allowing intersection and then trying to resolve it after the fact I'm afraid, because what if the character jumps just before he reaches the block? Even if he doesn't jump he might still have a tiny amount of upward velocity due to floating point inaccuracy. This might cause you to put an epsilon in, which could then fail in some other subtle case.

Using continuous collision would be pretty straightforward if you only have axis aligned boxes and is possibly the only general solution to this problem. However, if you also have irregular collision shapes then you're likely to get into a world of floating point rounding hurt.

Another random idea - if you're only using rectangles then you could say that the top of the moving box starts off higher than the bottom of the other box and therefore can't have hit, but this only works for aligned boxes I think.

I also actually think your other idea of checking the x axis then the y axis could have legs. Of course it'll also fail if you then drop from above very fast onto a gap that isn't wide enough to fit down!

Designing around the issue is desirable but might not be possible if you want moving platforms or sliding doors, etc.

Wish I could give you a definitive solution but this is indeed quite a tricky problem.

Continuous collision detection seems to be the only way. To avoid this you need to move your rectangle such that it never ends up clipping other geometry, so you need to know about the geometry when moving your rectangle which would seem to suit CCD naturally, which returns the minimum time until intersection or equivalently the farthest an object can travel before clipping the world, and when that happens you can handle both forces which will immediately cancel themselves out.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Just like what the above posts mentioned, CCD will help you out. Also, if you use separating axis tests, the last separating axis between the 2 boxes is going to be horizontal, and not vertical. You should be able to take the "normalized last separating axis * penetration distance" and move the box to where constraints are satisfied. Or move the box to it's "first time of contact."

Once you start having to deal with multiple contact points (e.g. box stacking), it'll get a little bit more complex....

Thanks again for some thoughts there, looks like I will have to look into CCD then.

The rectangles were just illustratory of the problem. More complex shapes and rotations should would also ideally be accounted for down the line.

I really thought there would be some logical (more obvious) solution to this kind of problem but I guess it is a well known issue.

I have some SAT collision code I wrote a while ago, seems pretty decent - not epic fast but good enough.

Which reminds me, I haven't played that much in the world of 2D, more of a 3D guy really so on that note I have another small question semi related to my problem above.

Do people generally work in local space (transforming other bodies into their own space) or in world space when dealing with collisions in 2d. Or do people just work in world space only? Just curious as I would tend to want to use transforms but might be seen as overkill for simple 2D physics.

This topic is closed to new replies.

Advertisement