Public Group

# 2D Collision with Allegro

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

## 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 on other sites
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 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            224bool 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

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633695
• Total Posts
3013374
×