Simple Collision Detection Issue

Started by
1 comment, last by Astra 12 years, 9 months ago
Hi guys. I'm writing a 3D game, similar to Super Smash Bro's the characters only move along the X and Y axis, so I'm doing my collision detection only on the X and Y; and ignoring Z axis collisions.
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
Advertisement
Looks like you should get a book on C++ ..

Look into member variables for your character/platform classes..

and do something more generalized with your collision checking..

like:

bool CheckAABBCollision(AABB one, AABB two)
{
if(one.minX < .......
.
.
.
)
return true;

return false;
}
i've got member variables, xpos, ypos, zpos. Not sure what specifically i should change about the way i've accessed the variables?

also with the code you suggested, as i've seen in many tutorials - i dont understand AABB one and AABB two. Is this psudo code for my character1. and mainstage? or are there actual commands known as AABB?

thanks for your patience

This topic is closed to new replies.

Advertisement