Jump to content
  • Advertisement
Sign in to follow this  
lordcorm

2D Collision with Allegro

This topic is 4075 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi im making a small 2D game with Allegro for the heck of it :P and i was wondering if any of you guys had any good collision detection methods/algorithms, all the ones i have tried have failed. Thanks a bunch, Lordcorm

Share this post


Link to post
Share on other sites
Advertisement
Well, it depends on what kind of objects you are working with. For each case theres always a most suitable method, like the Separation Axis Theorem for convex polygons/polyhedras, AABBs or even circle/sphere distance for very simple objects and so on . . . but basically, the idea behind collision detection is check at every frame if the objects are intersecting and if they do, do something to separate them . . . then, please specify what kinds of objects you are working.

Share this post


Link to post
Share on other sites
Hi, thanks for the reply. Im working with little tanks, and i got collision to work when i hit something from the x axis but not the y axis, here is some code:


#define North 0
#define NorthEast 32
#define East 64
#define SouthEast 96
#define South 128
#define SouthWest 160
#define West 192
#define NorthWest 224

bool Tank::inside(float x, float y, float left, float top, float right, float bottom)
{
if(x > left && x < right && y > top && y < bottom)
return true;
else
return false;
}

void Tank::CheckCollision(BITMAP* B,float x1,float y1, int boarder)
{
float width1 = x + g_Tank->w;
float height1 = y + g_Tank->h;
float width2 = x1 + B->w;
float height2 = y1 + B->h;

if(inside(x,y,x1 + boarder,y1 + boarder,width2 - boarder, height2 - boarder))
{
if(angle == East)
{
x =x - 1;
y =y;
}
if(angle == West)
{
x = x + 1;
y = y;
}
if(angle == South)
{
x = x;
y = y - 1;
}
if(angle == North)
{
x = x;
y = y + 1;
}
}
if(inside(x, height1, x1 + boarder, y1 + boarder, width2 - boarder, height2 - boarder))
{
if(angle == East)
{
x =x - 1;
y =y;
}
if(angle == West)
{
x = x + 1;
y = y;
}
if(angle == South)
{
x = x;
y = y - 1;
}
if(angle == North)
{
x = x;
y = y + 1;
}
}
if(inside(width1, y, x1 + boarder, y1 + boarder, width2 - boarder, height2 - boarder))
{
if(angle == East)
{
x =x - 1;
y =y;
}
if(angle == West)
{
x = x + 1;
y = y;
}
if(angle == South)
{
x = x;
y = y - 1;
}
if(angle == North)
{
x = x;
y = y + 1;
}
}
if(inside(width1, height1, x1 + boarder, y1 + boarder, width2 - boarder, height2 - boarder))
{
if(angle == East)
{
x =x - 1;
y =y;
}
if(angle == West)
{
x = x + 1;
y = y;
}
if(angle == South)
{
x = x;
y = y - 1;
}
if(angle == North)
{
x = x;
y = y + 1;
}
}
return;
}



Thanks for the help!

Lordcorm

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!