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);
}
}
}