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

Two questions about Collision Detection.

Started by Jode Apr 18, 2012 at 8:26 PM 0 replies 1.5k views
Original Post
Jode
Jode
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).

Questions:
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.

Thanks.
Hobbyist game developer, game designer, and gamer.
TheResolute
TheResolute
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

Topic Locked

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

Sign in to reply to this topic.