# Collision Response With Penetration Depth

This topic is 2589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi All.

im trying to do "proper" collision detection / response.

im trying to use the penetration depth of two axis aligned rectanges to move the player out of it.
it works okey, in the fact that the player is no longer in the other object.

but it is not working in the fact of gameplay.

ive searched and read a lot of posts about collision detection / response but i cannot get what i want done( it keeps doing what i told it and not what i wanted to happen )

this is a picture of the result.

[attachment=6118:Untitled-1 copy.jpg]

how should i do the penetration processing so that it will push the player up / left without this problem ?

here is the code that does the collision penetration:
 if( left < other_right ) penetration.x = left - other_right; else if( right > other_left ) penetration.x = other_left - right; if( bottom > other_top ) penetration.y = other_top - bottom; else if( top > other_bottom ) penetration.y = top - other_bottom; 

---edit---
i proberbly should mention that im adding the result ( "penetration" ) to the player position.

##### Share on other sites
I suppose what you want to do is to keep the player above the plattform if he falls on it, beneath it if he runs into it or under it if he jumps into it.
Now you are checking the intersection distance in Y as well as in X direction and then moving the player in respect to both distances.

This results in the problem you show in the picture. The player is moved above the plattform and then to the left as well.
I solved it by checking what the shortest distance is to move out of collision and then just applying that one.

This should work just fine for what you are doing right now.

edit: Infernal-rk is right. I forgot to mention that I test first if there is a collision.

##### Share on other sites
 if( left < other_right ) penetration.x = left - other_right; else if( right > other_left ) penetration.x = other_left - right; if( bottom > other_top ) penetration.y = other_top - bottom; else if( top > other_bottom ) penetration.y = top - other_bottom; 

Assuming your player is the variables "left", "right", etc:

It seems like you have subtraction orders reversed. Look at a simple example of penetration on the left with left = 0 and other_right = 2. penetration.x will be 0-2 = -2. If you're adding this to the player position, it's just going to push him more to the left instead of out to the right to get rid of the penetration.

##### Share on other sites
by doing the axes seperately you are getting messed up. the reason is this:

if you do the x axis penetration check you will definitely get moved outside the x axis bounds of the platform
then when you do the y axis penetration check, you will also definitely get moved outside the y axis bounds of the platform. basically, even though your platform is a long rectangle, the bounds testing ensures that you will not "penetrate" the infinite 2 surfaces you would find if you extruded the platform along each axis seperately. That is why you end up corner to corner.

first test if there is a collision : if you have penetration in the x axis, first test for penetration in the y axis as well before you calculate a correction.

I believe the separating axis theorem is what you are after.:

If there is no x overlap - there is no collision. break out early
OR If here is no y overlap - there is no collision. break out early

if there is x overlap - if there is also y overlap - collision - calculate penetration/correction- done
OR if there is y overlap - if ther eis also x overlap - collision - calculate penetration/correction - done

when you calculate the overlaps: do not just compare your left < their right.. because what if BOTH your left AND your right edges are < their right..
need to be more complete on those tests.

##### Share on other sites
Thanks All, its now fixed, but what if the two boxes were rotated ?

can i just do a line on line check and use the normal of the intersecting line ?
also how would i find the normal of the line ?, because i know of the way to do it with three points but not two.

##### Share on other sites
im trying to do "proper" collision detection / response...im trying to use the penetration depth of two axis aligned rectanges to move the player out of it...it works okey, in the fact that the player is no longer in the other object....i proberbly should mention that im adding the result ( "penetration" ) to the player position.
I also did that for a while. Unfortunately, it just does not work. The encroached vector simply cannot be trusted. It is just too much dependent on orientation of the colliding object and the algorithm used. Even topology alone might have serious trouble. Put two collision objects one on top of each other and you'll start observing more and more issues such as:
On low framerate
Player encroaches object on top
Algorithm detects encroach vector as "down"
Pushes down the player ... inside the "bottom" volume

I am growing confident the only safe way of recovering from encroaching is to keep track of a previous, valid position.

I'll keep an eye on this, in case some suggested solution emerges.

##### Share on other sites
ok, ive been trying to get this to work on all 4 sides of the cube correctly, but alas at best i can make it jump the cube above the other.

anybody got any tips on getting this to work on all 4 sides ?

here is the current logic im using ( with diagrams to help visualize what should happen, but it still does not work correctly )
 // return if there is no collision if( left > other_right || right < other_left || bottom < other_top || top > other_bottom ) return penetration; // work out the penetration /* A = Other. B = Volume. |-------| | A | |----|-| | | B | | | |----|-| | |-------| A->left = 200; B->right = 250; */ // collides on the left side of the Other. // need to push volume left. // results in negative value. if( right > other_left ) penetration.x = other_left/*200*/ - right/*250*/; /* A = Other. B = Volume. |-------| | A |--|---| | | | | | | | B | | |--|---| |-------| A->right = 250; B->left = 200; */ // collides on the right side of the Other. // right to push volume left. // results in positive value. else if( left < other_right ) penetration.x = left/*200*/ - other_right/*250*/; /* A = Other. B = Volume. |-----| | B | |--|-----|-| | |-----| | | | | | | A | |----------| A->top = 200; B->bottom = 250; */ // collides on the top side of the Other. // up to push volume left. // results in negative value. if( bottom > other_top ) penetration.y = other_top/*200*/ - bottom/*250*/; /* A = Other. B = Volume. |----------| | A | | | | |-----| | | | | | |--|-----|-| | B | |-----| A->bottom = 250; B->top = 200; */ // collides on the bottom side of the Other. // down to push volume left. // results in positive value. else if( top < other_bottom ) penetration.y = other_bottom/*250*/ - top/*200*/; return penetration; 

##### Share on other sites
As I had the same problem I looked into it and found the mistake:

 if( right > other_left ) penetration.x = other_left/*200*/ - right/*250*/; 

What happens if the box is on the right of the other box? So right > other_right.
You still go in this if-branch and push the volume all the way to the left, which results in "jumping over the box".
What you need to do is add a second condition to all your if's:

 if( right > other_left && right <= other_right) penetration.x = other_left/*200*/ - right/*250*/; 

like this.

And remember to check the penetration distance in absolute values, otherwise a overlap like this:

left = 250
other_left = 100

right = 750
other_right = 600

top = 0
other_top = 45

bottom = 50
other_bottom = 95

Would be resolved by moving the object to the left, because
left - other_right = -350 is smaller than
top - other_top = -5
but the actual distance you want it to move is bigger.

I hope this helps.
bonus.2113

##### Share on other sites
i solved that by making sure its not too far in the other direction like so:
 // work out the penetration /* A = Other. B = Volume. |-------| | A | |----|-| | | B | | | |----|-| | |-------| A->left = 200; B->right = 250; */ // collides on the left side of the Other. // need to push volume left. // results in negative value. if( ( right > other_left ) && ( right < other_right ) ) penetration.x = other_left/*200*/ - right/*250*/; /* A = Other. B = Volume. |-------| | A |--|---| | | | | | | | B | | |--|---| |-------| A->right = 250; B->left = 200; */ // collides on the right side of the Other. // right to push volume left. // results in positive value. else if( ( left < other_right ) && ( left > other_left ) ) penetration.x = other_right/*250*/ - left/*200*/; /* A = Other. B = Volume. |-----| | B | |--|-----|-| | |-----| | | | | | | A | |----------| A->top = 200; B->bottom = 250; */ // collides on the top side of the Other. // up to push volume left. // results in negative value. if( ( bottom > other_top ) && ( bottom < other_bottom ) ) penetration.y = other_top/*200*/ - bottom/*250*/; /* A = Other. B = Volume. |----------| | A | | | | |-----| | | | | | |--|-----|-| | B | |-----| A->bottom = 250; B->top = 200; */ // collides on the bottom side of the Other. // down to push volume left. // results in positive value. else if( ( top < other_bottom ) && ( top > other_top ) ) penetration.y = other_bottom/*250*/ - top/*200*/; 

##### Share on other sites

As I had the same problem I looked into it and found the mistake:

 if( right > other_left ) penetration.x = other_left/*200*/ - right/*250*/; 

What happens if the box is on the right of the other box? So right > other_right.
You still go in this if-branch and push the volume all the way to the left, which results in "jumping over the box".
What you need to do is add a second condition to all your if's:

 if( right > other_left && right <= other_right) penetration.x = other_left/*200*/ - right/*250*/; 

like this.

And remember to check the penetration distance in absolute values, otherwise a overlap like this:

left = 250
other_left = 100

right = 750
other_right = 600

top = 0
other_top = 45

bottom = 50
other_bottom = 95

Would be resolved by moving the object to the left, because
left - other_right = -350 is smaller than
top - other_top = -5
but the actual distance you want it to move is bigger.

I hope this helps.
bonus.2113

will use that

1. 1
2. 2
Rutin
19
3. 3
khawk
19
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633771
• Total Posts
3013762
×