View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Two questions about Collision Detection.

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.

### #1Jode  Members

Posted 18 April 2012 - 02:26 PM

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.

### #2TheResolute  Members

Posted 18 April 2012 - 04:11 PM

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

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