Collision Detection
Posted: Sat May 02, 2009 9:38 pm
I was trying to create a function that detected collision between two objects. I was trying to have it depending on what number the integer col returned it would not allow the player to move in the direction of which it collided. After it not working successfully, I kept making minor changes and now it's the closest I've gotten it to working, but I seem to still not be doing something right, for in the case of col returning 2, meaning that your on the left side of the object, it simply doesn't detect collision. I've checked for collision detection tutorials on google, and generally they are for 3d games or only for checking for collision between the object and the screen. I greatly appreciate any help!
Code: Select all
int check_collision(int ob1x, int ob1y, int ob2x, int ob2y)
{
int col = 0;
int ob1xt;
int ob1yt;
int ob2xt;
int ob2yt;
int o2x;
int o2xt;
int o2y;
int o2yt;
ob1xt = ob1x + 32;
ob1yt = ob1y + 32;
ob2xt = ob2x + 32;
ob2yt = ob2y + 32;
o2y = ob2y -5;
o2yt = ob2yt - 5;
o2x = ob2x + 5;
o2xt = ob2xt - 5;
if ((ob1x <= ob2xt) && (ob1x > o2xt) && (ob1y <= ob2yt) && (ob1yt >= ob2y))
{
//Able to move Up, Down, Right
col = 1;
}
if ((ob1xt >= ob2x) && (ob1xt < o2x) && (ob1y <= ob2yt) && (ob1yt >= ob2y))
{
//Able to move Up, Down, Left
col = 2;
}
if ((ob1xt >= ob2x) && (ob1x <= ob2xt) && (ob1y <= ob2yt) && (ob1y > o2yt))
{
//Able to move Down, Left, Right
col = 3;
}
if ((ob1xt >= ob2x) && (ob1x <= ob2xt) && (ob1yt >= ob2y) && (ob1yt < o2yt))
{
//Able to move Up, Left, Right
col = 4;
}
return (col);
}