Skip to main content
GameDev.net gamedev.net
🔒 Locked

Bounding Box Collision

Started by sarim Aug 25, 2011 at 7:34 PM 4 replies 1.3k views
Original Post
sarim
sarim
Hi, im having trouble with creating simple bounding box collision detection in my game...here's the code :



void Entity::TestCollision(Entity *one, Entity *two)
{
RECT rect1;
RECT rect2;

int x;
int y;

int x2;
int y2;

x = one -> pos.x;
y = one -> pos.y;

x2 = two -> pos.x;
y2 = two -> pos.y;

rect1.left = (long)one -> pos.x;
rect1.top = (long)one -> pos.y;
rect1.right = (long)rect1.left + one -> width;
rect1.bottom = (long)rect1.top + one -> height;

rect2.left = (long)two -> pos.x;
rect2.top = (long)two -> pos.y;
rect2.right = (long)rect2.left + two -> width;
rect2.bottom = (long)rect2.top + two -> height;

if (rect1.right > rect2.left && rect1.left < rect2.right && rect1.bottom > rect2.top && rect1.top < rect2.bottom)
{
one -> pos.x = x;
one -> pos.y = y;

two -> pos.x = x2;
two -> pos.y = y2;
}
}



i tested the collision by making the second object redraw itself in another random location when it came in contact with the first object...i did this just to see if the collision system was working..which it is. Now however i want there to be actual collision..meaning when the object come into contact with one another they should not be able to pass through eachother. I know i probably did something wrong in my code but i cant figure out how to go about doing this. Any help? Thanks
Ectara
Ectara
You might be able to get away with a cheaper comparison by doing:

void Entity::TestCollision(Entity *one, Entity *two)
{
int x;
int y;

int x2;
int y2;

x = one -> pos.x;
y = one -> pos.y;

x2 = two -> pos.x;
y2 = two -> pos.y;

if(absi(one->pos.x - two->pos.x) <= ((one->width + two->width) / 2) && absi(one->pos.y - two->pos.y) <= ((one->height + two->height) / 2))
{
one -> pos.x = x;
one -> pos.y = y;

two -> pos.x = x2;
two -> pos.y = y2;
}
}


if you can define a fast absi() function or macro. Divides could be shifts.

Now, on to the topic. One would usually detect if collision is occurring, or will occur, and move the entity back to the last known position, or calculate the farthest it can go without colliding.

For instance, if you move your entity 4 blocks and it winds up in a wall, you can keep it where it was before it was moved, or move it back until it is no longer colliding.
sarim
sarim
thats what i want it to do..just move back the bare minimum so that its not colliding...i thought since i put in the variable x,y,x2,and y2 it would record the last known position before the collision and move the character back to that cooridinate. But for some reason that doesnt work
Ectara
Ectara
Ah, I figured that was just a placeholder for something removed in the example. No, you need to keep track of this yourself, outside of the function. Within this function, nothing moves, so the variables are equal. So, you would keep track of the position, move the entity, if it collides, move it back to the previous position, or optionally, determine some point between the two. One could repeatedly divide the distance by two until it becomes acceptable, or zero.
sarim
sarim
Got it to work Thanks man appreciate it
Ectara
Ectara
No problem, glad to help.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.