Followers 0

Bounding box collision in a platformer game

11 posts in this topic

Hello devs,

I am making a prototype for my first game (when I say "first" I mean that I camed up with the ideea myself, not games like pong or tic-tac-toe, which by the way, you can see here and here).

I had some problems with gravity but fixed it after a while. Next problem that I faced was bounding box collision detection...

[attachment=19567:Capture.PNG]

With red are drawn the bounding boxes.

I can stay on the top the ground. But like in any other platformer, you will hit the platform from down, left and right too.

I don't really know how to code collision when player hit the platform from other sides.

Here is the code to stay above the platform.

if(player.bbox.Intersect(&platform1.bbox))
{
player.y = platform1.y1 - player.width; //platform1.y1 - top-left corner Y coordinate
}


My bounding box class give me acces just to: x1, y1, x2, y2 coordinates of the bounding box, Intersect function (that checks if 2 bounding boxes intersects) and TestPoint function(that checks if a point is inside a bounding box).

How I can do the collision with the other sides of the platform not only the top of it?

Edited by Bratie Fanut
0

Share on other sites
The logic is just the same, you could just swap X and Y for that matter...

Using the same approach you used on your own function:
if(player.bbox.Intersect(&platform1.bbox))
{
//Y axis
if (player.y1 <= platform1.y1) //assuming y1 to be the "bigger-y coordinate"
player.y1 = platform1.y1 - player.height; //To place it on top of the platform.
else
player.y1 = platform1.y2 + 1; //To place it under the platform when hit from below.
//X axis
if (player.x1 >= platform1.x1) //assuming x1 to be the "smaller-x coordinate"
player.x1 = platform1.x2 + 1;
else
player.x1 = platform1.x1 - player.width; //To move it aside from the intersected platform.
}

This could probably work on general situations, with moderate speeds and all that.
I am assuming that (y1, x1) is the top-left coordinate, and (x2, y2) is the bottom-right one.
You'll probably have to adjust the code.

I guess I've been using Box2D for too long, getting lazy. Edited by dejaime
1

Share on other sites

The logic is just the same, you could just swap X and Y for that matter...

Using the same approach you used on your own function:

if(player.bbox.Intersect(&platform1.bbox))
{
//Y axis
if (player.y1 >= platform1.y1) //assuming y1 to be the "bigger-y coordinate"
player.y1 = platform1.y1 - player.height; //To place it on top of the platform.
else
player.y1 = platform1.y2 + 1; //To place it under the platform when hit from below.
//X axis
if (player.x2 >= platform1.x1) //assuming x1 to be the "smaller-x coordinate"
player.x1 = platform1.x1 - player.width; //To move it aside from the intersected platform.
else
player.x1 = platform1.x2 + 1;
}


This could probably work on general situations, with moderate speeds and all that.

I am assuming that (y1, x1) is the top-left coordinate, and (x2, y2) is the bottom-right one.

You'll probably have to adjust the code.

Remember to reset the speeds and all that when it hit an immovable object!

I guess I've been using Box2D for too long, getting lazy.

You are assuming everything right. I am getting lazy too... I should think of another checking in that if statement..

I will try it to see if it works.

0

Share on other sites

I cover the topic in detail in this tutorial

Here is the code to detect a collision from any direction:

return (this.x <= rect.x + rect.width &&
rect.x <= this.x + this.width &&
this.y <= rect.y + rect.height &&
rect.y <= this.y + this.height);


The explanation of why it works is in the tutorial.  I actually did a whole series of gamdev math tutorials that you may find useful.

Let me know if you have any questions.

I will check your tutorials. Thank you:)

0

Share on other sites

You are assuming everything right. I am getting lazy too... I should think of another checking in that if statement..
I will try it to see if it works.

My last code is wrong, the horizontal part, I have just fixed it, please take another look. Edited by dejaime
1

Share on other sites

Its not working.. the condition for y axis and x axis are true at the same time.. so when I jump on the platform I stay on top of it but because x axis conditions are true I am stucked.

0

Share on other sites
Yes, it needs another test level... Be back in a jiffy.

if(player.bbox.Intersect(&platform1.bbox))
{
//Y axis
if (player.y1 <= platform1.y1) {//assuming y1 to be the "bigger-y coordinate"
player.y1 = platform1.y1 - player.height; //To place it on top of the platform.
}
else if (player.y1 >= platform1.y2) {
player.y1 = platform1.y2 + 1; //To place it under the platform when hit from below.
}
//X axis
else {
if (player.x1 >= platform1.x1) //assuming x1 to be the "smaller-x coordinate"
player.x1 = platform1.x2 + 1;
else
player.x1 = platform1.x1 - player.width; //To move it aside from the intersected platform.
}
}

This should prevent it from updating both at the same time... It could also be something like this:

if(player.bbox.Intersect(&platform1.bbox))
{
//Y axis
if (/*vertical collision*/) {
if (player.y1 <= platform1.y1) {//assuming y1 to be the "bigger-y coordinate"
player.y1 = platform1.y1 - player.height; //To place it on top of the platform.
}
else {
player.y1 = platform1.y2 + 1; //To place it under the platform when hit from below.
}
//X axis
if (/*horizontal collision*/) {
if (player.x1 >= platform1.x1) //assuming x1 to be the "smaller-x coordinate"
player.x1 = platform1.x2 + 1;
else
player.x1 = platform1.x1 - player.width; //To move it aside from the intersected platform.
}
}
Sorry for anything wrong, I'm just not used to code without obsessively testing every added code block. I guess I am not used to think ahead...
2

Share on other sites

I finally made it work! Thanks @dejaime and @Kryzon. Thumbs up.

0

Share on other sites

I finally made it work! Thanks @dejaime and @Kryzon. Thumbs up.

0

Create an account

Register a new account