• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ryan20fun

Collision Response With Penetration Depth

9 posts in this topic

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:
[code]
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;
[/code]

Thanks In Advance.

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

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
[code]
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;
[/code]

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.
1

Share this post


Link to post
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.
1

Share this post


Link to post
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.
0

Share this post


Link to post
Share on other sites
[quote name='ryan20fun' timestamp='1321467554' post='4884632']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.[/quote]I also did that for a while. Unfortunately, it just does not work. The [i]encroached [/i]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.


0

Share this post


Link to post
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 )
[code]
// 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;
[/code]
0

Share this post


Link to post
Share on other sites
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:

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

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:

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

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
1

Share this post


Link to post
Share on other sites
i solved that by making sure its not too far in the other direction like so:
[code]
// 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*/;
[/code]
0

Share this post


Link to post
Share on other sites
[quote name='bonus.2113' timestamp='1321538503' post='4884956']
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:

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

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:

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

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
[/quote]

will use that :)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0