As you can tell from the title, bounding box collision detection is not working out for me....*sigh* Here's my problem:
bool EntityManager::checkCollision(Entity* ent1, Entity* ent2)
{
bool cholided = false;
AABB* box1 = ent1->getBoundingBox();
AABB* box2 = ent2->getBoundingBox();
if(box1->getMin().getX() > box2->getMax().getX()) return cholided;
if(box1->getMax().getX() < box2->getMin().getX()) return cholided;
if(box1->getMin().getY() > box2->getMax().getY()) return cholided;
if(box1->getMax().getY() < box2->getMin().getY()) return cholided;
if(box1->getMin().getZ() > box2->getMax().getZ()) return cholided;
if(box1->getMax().getZ() < box2->getMin().getZ()) return cholided;
return (cholided == true);
}
this code seems to be OK since I got this from a programming math book. link: http://gamemath.com/ now I'm not sure if there's a problem with my code in the bounding box, but i don't think so since it renders fine. Maybe I'm doing something weird? anyways, the book also talked about checking a box against a moving box (a player for instance) and gave this code.
double EntityManager::checkCollisionAgainstMovingEnt(Entity* movingEnt, Entity* staticEnt, Vector3 & d)
{
// large number to return if no intersection
const double noIntesection = 1e30f;
AABB* otherAABB = staticEnt->getBoundingBox();
AABB* movingBox = movingEnt->getBoundingBox();
// interval for time consideration
double dEnter = 0.0;
double dLeave = 1.0;
if(d.getX() == 0.0f)
{
if((otherAABB->getMin().getX() >= movingBox->getMax().getX()) ||
(otherAABB->getMax().getX() <= movingBox->getMax().getX()))
{
return noIntesection;
}
else
{
// divide once
bool divByZero = (d.getX() != 0);
double oneOverD = 1.0 / d.getX();
double xEnter = (otherAABB->getMin().getX() - movingBox->getMin().getX()) * oneOverD;
double xLeave = (otherAABB->getMax().getX() - movingBox->getMax().getX()) * oneOverD;
if(xEnter > xLeave)
swap(xEnter,xLeave);
if(xEnter > dEnter) dEnter = xEnter;
if(xLeave < dLeave) dLeave = xLeave;
if(dEnter > dLeave)
return noIntesection;
}
}
// check y
if(d.getY() == 0.0f)
{
if((otherAABB->getMin().getY() >= movingBox->getMax().getY()) ||
otherAABB->getMax().getY() <= movingBox->getMin().getY())
{
return noIntesection;
}
else
{
// divide by one on the y axis.
double oneOverD = 1.0 / d.getY();
double yEnter = (otherAABB->getMin().getY() - movingBox->getMax().getY()) * oneOverD;
double yLeave = (otherAABB->getMax().getY() - movingBox->getMin().getY()) * oneOverD;
if(yEnter > yLeave)
swap(yEnter,yLeave);
if(yEnter > dEnter) dEnter = yEnter;
if(yLeave < dLeave) dLeave = yLeave;
if(dEnter > dLeave)
return noIntesection;
}
}
// check z-axis
if(d.getZ() == 0.0f)
{
if((otherAABB->getMin().getZ() >= movingBox->getMax().getZ()) ||
otherAABB->getMax().getZ() <= movingBox->getMin().getZ())
{
return noIntesection;
}
else
{
// divide by one on the y axis.
double oneOverD = 1.0 / d.getZ();
double zEnter = (otherAABB->getMin().getZ() - movingBox->getMax().getZ()) * oneOverD;
double zLeave = (otherAABB->getMax().getZ() - movingBox->getMin().getZ()) * oneOverD;
if(zEnter > zLeave)
swap(zEnter,zLeave);
if(zEnter > dEnter) dEnter = zEnter;
if(zLeave < dLeave) dLeave = zLeave;
if(dEnter > dLeave)
return noIntesection;
}
}
return dEnter;
}
here's a screen shot also: http://puu.sh/2zKM0
I'm not really sure why collision detection doesn't work for me....here's how i'm using it:
for(int i = 0; i < 10; i++) {
// if(pack->getPosition().getX() > pallets->getPosition().getX())
// {
// pallets->setColor(0.5,1,0,1);
// pallets->draw();
// pack->setColor(1,1,0,1);
// pack->setDebugColor(1,0.5,1,0.5);
// pack->draw();
// }
// if(pack->getPosition().getY() > pallets->getPosition().getY())
// {
// pallets->draw();
// pack->draw();
// }
bool collide = entMgr->checkCollision(pack,wall);
char buffer3[256];
sprintf_s(buffer3,256,"Collision: %d",collide);
Vector3 d;
d.setVector(2,1,2);
double interval = entMgr->checkCollisionAgainstMovingEnt(pack,wall,d);
char buffer4[256];
interval += 0.04;
sprintf_s(buffer4,256,"interval: %.2f",interval);
if(interval <= d.getX() && interval <= d.getY() && interval <= d.getZ())
{
pack->setColor(interval,1,0,1);
}
output(0,300,buffer4);
output(0,500,buffer3);
if(collide)
{
pack->setColor(1,0,0,0.5);
AABB* pbox = pack->getBoundingBox();
AABB* wbox = wall->getBoundingBox();
bool contains = pbox->containsPoint(0,0,0);
bool contains2 = wbox->containsPoint(-100,100,0);
char buffer[256];
sprintf_s(buffer,256,"collision: %d",collide);
output(100,100,buffer);
}
}
I don't know maybe I need to think this through. this is for a school project keep in mine I'm not asking for you to do it for me and the collision detection is all me, the teacher just wanted a simple pack man.