Jump to content
  • Advertisement

Archived

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

didalabu2

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!