# Creating a Collision Detection system [duplicate]

I'm currently working on 3D project (C++) and I have now come to writing the collision detection systems.

Basically my question is regarding how to architect it.

All the collisions are either between AABB-AABB or AABB-circle. I'm perfectly aware how to detect whether any two objects are colliding, but I don't know where to do the checks.

Currently I am trying to make a CollisioManager class that contains a list of all the AABBs in the level and then the player object makes a call to the CollisionManager to ask whether it is currently colliding with any (then acts accordingly).

I think I can do it this way but are there any other ways of organizing it as it feels a bit messy and not the cleanest of solutions.

If you have an array of all objects in the simulation, you can do something like this:

Collider[] Colliders = new Collider[]{col1, col2, col3, col4...};

void Simulate ()
{
for (int i = 0; i < Colliders.Length; i++)
{
Collider ColA = Colliders[i];
if (ColA == null) continue;
for (int j = i + 1; j < Colliders.Length; j++)
{
Collider ColB = Colliders[j];
if (ColB == null) continue;
CheckCollision (ColA, ColB);
}
}
}


These loops will iterate over all possible combinations once, skipping combinations of the same object and any objects that don't exist.

If you're doing spatial partitioning, you can use the same loops for iterating over the partitioned collection or you can include the indicator that the pair of objects are in the same partition in the CheckCollision() call (which is generally easier and more centralized).

Note that you can still do this for HashSets and other collections that don't use indexers but you have to store 'i' and 'j' somewhere else. I.e.

int i = 0;
foreach (Collider ColA in Colliders)
{
int j = 1;
foreach (Collider ColB in Colliders)
{
if (j > i)
{
SetCollisionPossible (ColA, ColB);
}
j++
}
i++;
}