•      Sign In
• Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

# Bounding Box Collision

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1Cryptiik  Members   -  Reputation: 240

Like
0Likes
Like

Posted 25 August 2011 - 01:34 PM

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

### #2Ectara  Members   -  Reputation: 880

Like
1Likes
Like

Posted 25 August 2011 - 01:43 PM

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.

### #3Cryptiik  Members   -  Reputation: 240

Like
0Likes
Like

Posted 25 August 2011 - 02:30 PM

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

### #4Ectara  Members   -  Reputation: 880

Like
1Likes
Like

Posted 25 August 2011 - 02:39 PM

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.

### #5Cryptiik  Members   -  Reputation: 240

Like
0Likes
Like

Posted 25 August 2011 - 02:58 PM

Got it to work Thanks man appreciate it

### #6Ectara  Members   -  Reputation: 880

Like
1Likes
Like

Posted 25 August 2011 - 03:02 PM

No problem, glad to help.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS