I tried implementing an AABB method of collision detection but I think I got it a bit wrong. I'm testing the min.x min.y of my character against the min.x min.y max.x max.y of the ''stage'' (that is, one big platform and 3 suspended platforms). So as you would expect only when the bottom left front corner (min) of my "character" rectangle goes into the platform does a collision occur.
What I want to know is if there's a more efficient way of doing it than checking each individual vertex with the platform min/max.
Heres the code for my collision function:
void checkCollision()
{
if((Character1.min(0) > mainStage.min(0)) &&
(Character1.min(0) < mainStage.max(0)) &&
(Character1.min(1) > mainStage.min(1)) &&
(Character1.min(1) < mainStage.max(1)))
{
exit(0);
}
if((Character1.min(0) > platform1.min(0)) &&
(Character1.min(0) < platform1.max(0)) &&
(Character1.min(1) > platform1.min(1)) &&
(Character1.min(1) < platform1.max(1)))
{
exit(0);
}
if((Character1.min(0) > platform2.min(0)) &&
(Character1.min(0) < platform2.max(0)) &&
(Character1.min(1) > platform2.min(1)) &&
(Character1.min(1) < platform2.max(1)))
{
exit(0);
}
if((Character1.min(0) > platform3.min(0)) &&
(Character1.min(0) < platform3.max(0)) &&
(Character1.min(1) > platform3.min(1)) &&
(Character1.min(1) < platform3.max(1)))
{
exit(0);
}
}
and in my character and platform class:
float Character::min(int index)
{
float minimum[3] = {xpos-5.0f, ypos-10.0f, zpos-0.02f};
return minimum[index];
}
float Character::max(int index)
{
float maximum[3] = {xpos+5.0f, ypos+10.0f, zpos+0.02f};
return maximum[index];}
float Block::min(int index)
{
float minimum[3] = {getxCoords()-(getxWidth()/2), getyCoords()-(getyHeight()/2), getzCoords()-(getzLength()/2)};
return minimum[index];
}
float Block::max(int index)
{
float maximum[3] = {getxCoords()+(getxWidth()/2), getyCoords()+(getyHeight()/2), getzCoords()+(getzLength()/2)};
return maximum[index];
}
thanks for any advice