Jump to content
  • Advertisement
Sign in to follow this  

Two questions about Collision Detection.

This topic is 2375 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

First of all, the code.

bool Positionable::intersectsWith(Positionable other) const {
if ((position.x + size.x >= other.getPosition().x) &&
(position.x <= other.getPosition().x + other.getSize().x) &&
(position.y + size.y >= other.getPosition().y) &&
(position.y <= other.getPosition().y + other.getPosition().y) &&
(position.z + size.z >= other.getPosition().z) &&
(position.z <= other.getPosition().z + other.getSize().z)) {
return true;
} else {
return false;

Positionable is a class that has a position, a size, and a rotation (all of which are of another class called Vector3).

a) I've heard that when you start adding lots of objects, this method of collision detection will slow down the game drastically. Is that true? Does anyone have a better method?

b) How would I have this account for the rotation as well? The rotation is stored as 3 angles, one for each axis.


Share this post

Link to post
Share on other sites
Yes, it is due to the fact that collision checking happens exponentially in relation to the number of objects you're dealing with
You could partition the entire area in which objects exist that need collision checking into several regions and have objects only check for collisions with other objects in their same region
That will drastically reduce the amount of collision checking you do, assuming your objects are moderately distributed throughout your area
Researching quadtrees (http://en.wikipedia.org/wiki/Quadtree) could be useful to your for that purpose

As far as rotation goes, you will have to use a slightly complicated algorithm that basically checks the distance between all the points for each object when aligned to several axis based on each of the edges of your objects
If it would be acceptable to have a bit less accurate collision detection algorithm, you should really consider merely checking the distance between two objects, which is basically encasing each in a sphere and determining whether or not those intersect
In 2D, it's as simple as: if (object0.x - object1.x) * (object0.x - object1.x) + (object0.y - object1.y) * (object0.y - object1.y) < (object0.collisionRadius + object1.collisionRadius) * (object0.collisionRadius + object1.collisionRadius) there's a collision
Otherwise, read this article (http://www.gamedev.n...collision-r2604) and you might be able to apply it to 3D with some thought and possible additional research

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • 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!