I want to implement a collision detection system for my graphic engine.
I don't know if it is the common way to do it but my idea was to bound any solid object (like a mesh or the camera) within a 3D box, which would give me much more accurate results than a sphere.
This box is defined by eight vertices
x0 = min(vertices.x)-off // parsing mesh's vertices for the minimum x
y0 = min(vertices.y)-off
z0 = min(vertices.z)-off
x1 = max(vertices.x)+off // off avoids 2D bounding on 2D objects
y1 = max(vertices.y)+off
z1 = max(vertices.z)+off
boundingBox[0] = vec3(x0, y0, z0);
boundingBox[1] = vec3(x0, y1, z0);
boundingBox[2] = vec3(x1, y0, z0);
boundingBox[3] = vec3(x1, y1, z0);
boundingBox[4] = vec3(x0, y0, z1);
boundingBox[5] = vec3(x0, y1, z1);
boundingBox[6] = vec3(x1, y0, z1);
boundingBox[7] = vec3(x1, y1, z1);
After having transformed the bounding box to world coordinates, I'm looking for a way to check if there is an intersection between two of them, but I don't know how to do it with linear algebra.
I thought that if I was sure that all the boxes were parallel to XZ plane I could simply check all the vertices of box1 against min/max coordinates of box2, like this:
for(int i = 0; i < 8; i++) {
if(box1[i].x >= box2.minX && box1[i].x <= box2.maxX) &&
(box1[i].y >= box2.minY && box1[i].y <= box2.maxY) &&
(box1[i].z >= box2.minZ && box1[i].z <= box2.maxZ) {
// collision here
}
}
But this is not going to work since meshes could have been rotated. Is there a mathematical formula that I can use?