Sign in to follow this  
Astra

Simple Collision Detection Issue

Recommended Posts

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:

[code]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);
}
}[/code]

and in my character and platform class:

[code]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];}[/code]


[code]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];
}[/code]

thanks for any advice

Share this post


Link to post
Share on other sites
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:
[code]
bool CheckAABBCollision(AABB one, AABB two)
{
if(one.minX < .......
.
.
.
)
return true;

return false;
}
[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this