AABB 2D Collision math problem from example

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

Recommended Posts

I'm implementing 2D collision detection using AABB from the tutorial on http://www.harveycartel.org/metanet/tutorials/tutorialA.html I'm writing my own version and it works, sorta. The problem i'm having is this section of code from the example. When they talk about penetration depth are they talking about how much each box is into each other? Because what I'm seeing doesn't not working with 2 rectangles of (10x1 - ground) and (2x2 - player)
var dx = pos.x - tx;//tile->obj delta
var px = (txw + this.xw) - Math.abs(dx);//penetration depth in x


lets just say this
dx = 5 - 0;
px = (5 + 1) - 5 = 1

px is > 0 so we have penetration there

but! if its

dx = -5 - 0;
px = (5 + 1) - 5 = 1


px is > 0 so this says we have penetration but the player is in the 5 units away from the ground. the ground would start at 0 and have a width of 10. I've been wrestling with this for a day or so and I cant seem to figure out why they do this. Any help / insite would be appreciated. code from the example
function CollideAABBVsTile(tile)
{
var pos = this.pos;
var c = tile;

var tx = c.pos.x;
var ty = c.pos.y;
var txw = c.xw;
var tyw = c.yw;

var dx = pos.x - tx;//tile->obj delta
var px = (txw + this.xw) - Math.abs(dx);//penetration depth in x

if(0 < px)
{
var dy = pos.y - ty;//tile->obj delta
var py = (tyw + this.yw) - Math.abs(dy);//pen depth in y

if(0 < py)
{
//object may be colliding with tile; call tile-specific collision function

//calculate projection vectors
if(px < py)
{
//project in x
if(dx < 0)
{
//project to the left
px *= -1;
py = 0;
}
else
{
//proj to right
py = 0;
}
}
else
{
//project in y
if(dy < 0)
{
//project up
px = 0;
py *= -1;
}
else
{
//project down
px = 0;
}
}

ResolveBoxTile(px,py,this,c);

}
}
}



Share on other sites
the ground can't "start at 0" -- you should reread the section on halfwidths.

(5 + 1) means that the halfwidth of one object is 5, the other is 1; in other words, one is 10pix wide and the other is 2pix. so, if the centers of the objects are only 5pix from each other, there is a penetration of 1pix, which is the result you're getting.

Share on other sites
Quote:
 Original post by raiganthe ground can't "start at 0" -- you should reread the section on halfwidths.(5 + 1) means that the halfwidth of one object is 5, the other is 1; in other words, one is 10pix wide and the other is 2pix. so, if the centers of the objects are only 5pix from each other, there is a penetration of 1pix, which is the result you're getting.i recommend downloading this presentation, it contains a better explanation of halfwidths: http://www.harveycartel.org/metanet/tutorials/fitc05.zip

Thank you for the suggestion! Figures I missed something like that. Always those mundane details ;)

Share on other sites

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

Create an account

Register a new account

• Forum Statistics

• Total Topics
628648
• Total Posts
2984036

• 10
• 9
• 9
• 10
• 21