#### Archived

This topic is now archived and is closed to further replies.

# Box collision detection

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

## Recommended Posts

I am trying to program box collision detection in my game. I currently have working code. The way I do it is check every line from one box, with every side (polygon) of the other box. This seems to be slow. I was wondering if I am going the long route. Is there a faster way to tell if two boxes are colliding (assuming they can be rotated on any axis)? Thanks

##### Share on other sites
Box/Box detection is simple: test to see if the space of Box 1 intersects with the space of box. Since the space is not curved detection is simple since you know that no edges can pass into the space of a box without a corner doing so as well.

##### Share on other sites
yes but what if a cube is rotated then a simple

IF X > CX+1 || X < CX-1 or Y > CY+1 or Y < CY-1

{
colision
}

or am i from another planet

##### Share on other sites
Actually, the space of a box can enter another box without its corners entering.

##### Share on other sites
"Actually, the space of a box can enter another box without its corners entering. "

That''s why you have to do it both way. A smaller box can be inside another bigger box without the bigger box touching any part of the smaller one. But when you check the smaller box, each of it''s corner will be inside the bigger box and you will register a collision.

Thorb

##### Share on other sites
There is an extreme case where even checking each box''s vertexes against the other box doesn''t work. For example:

  |------------------|-|-----------------|------| |                 |     ||-|-----------------|------  |                 |  |-----------------|

It''s an interesting case but in order for it to happen a vertex of the first box must first enter the second box... Unless one box is very fast, in which case this is basically a minor case of tunneling (where one box goes through the other one), in which case you need a swept test.

##### Share on other sites
Hey there Enokh, I haven''t found too much on the net in terms of academic principles concerning swept tests.
Could you explain to me how a swept test can be calculated generally (I know they involve working out who will collide with whom before it happens).

By the way, surely in the scenario you described, you don''t need to check each boxes vertices, but the top left and width/height of each box to determine if they cross over each other at all?

##### Share on other sites
If you are testing collisions between a certain time interval as most collisions are done and, assuming they weren''t already colliding, my idea works fine.

##### Share on other sites
I''ve never worked with swept tests myself, but the theory behind it is that you check for collisions like you normally do, but instead of once per game updation (typically once every 1/60 of a second for example if you have VSync on and your refresh rate is 60, you get the idea) then you check to see how long it took to render and update the last frame (IE you check your Delta Frame), then you pass that number to your collision check updation functions. Now you decide on a nice arbitrary number like 100 or 150, and you divide your Delta Frame by that number. Now you basically do a collision check once every (DeltaFrame / 150) of a second, so you cannot POSSIBLY have tunneling occur in your game. Ta da.

##### Share on other sites
you could try space partitioning, splitting up your scene into lots of cubes and and then only checking for collision with the objects in the cube you are currently in

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 12
• 19
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632929
• Total Posts
3009285
• ### Who's Online (See full list)

There are no registered users currently online

×