Jump to content

  • Log In with Google      Sign In   
  • Create Account


Collision Response With Penetration Depth


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 ryan20fun   Members   -  Reputation: 681

Like
0Likes
Like

Posted 16 November 2011 - 12:19 PM

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.

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;

Thanks In Advance.

---edit---
i proberbly should mention that im adding the result ( "penetration" ) to the player position.
Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

Sponsor:

#2 bonus.2113   Members   -  Reputation: 630

Like
1Likes
Like

Posted 16 November 2011 - 12:55 PM

Hi, I've just had about the same problem.
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.

#3 jmastro   Members   -  Reputation: 114

Like
1Likes
Like

Posted 16 November 2011 - 01:02 PM

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.

#4 Infernal-rk   Members   -  Reputation: 135

Like
1Likes
Like

Posted 16 November 2011 - 01:40 PM

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.

#5 ryan20fun   Members   -  Reputation: 681

Like
0Likes
Like

Posted 16 November 2011 - 11:35 PM

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.
Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

#6 Krohm   Crossbones+   -  Reputation: 3017

Like
0Likes
Like

Posted 17 November 2011 - 02:30 AM

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.




#7 ryan20fun   Members   -  Reputation: 681

Like
0Likes
Like

Posted 17 November 2011 - 05:59 AM

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;

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

#8 bonus.2113   Members   -  Reputation: 630

Like
1Likes
Like

Posted 17 November 2011 - 08:01 AM

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

You need to add a second condition to your If-Branches
Imagine with your current form:

  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

#9 ryan20fun   Members   -  Reputation: 681

Like
0Likes
Like

Posted 17 November 2011 - 08:29 AM

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*/;

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

#10 ryan20fun   Members   -  Reputation: 681

Like
0Likes
Like

Posted 17 November 2011 - 08:34 AM

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

You need to add a second condition to your If-Branches
Imagine with your current form:

  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 :)
Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS