bool BoundingBoxes::boxesAreWithin(shared_ptr<BoundingBoxes> otherBoxes) { bool collides = false; glm::mat4 rotationMatrix = rotateZ(otherBoxes->rotation.z) * rotateX(otherBoxes->rotation.x) * rotateY(otherBoxes->rotation.y); for (vector<vector<float> >::iterator vertex = otherBoxes->vertices.begin(); vertex != otherBoxes->vertices.end(); ++vertex) { glm::vec4 otherCoords(vertex->at(0), vertex->at(1), vertex->at(2), 1.0f); glm::vec4 rotatedOtherCoords(0.0f, 0.0f, 0.0f, 1.0f); rotatedOtherCoords = rotationMatrix * otherCoords; rotatedOtherCoords.x += otherBoxes->offset.x; rotatedOtherCoords.y += otherBoxes->offset.y; rotatedOtherCoords.z += otherBoxes->offset.z; /*cout << "Checking " << rotatedOtherCoords.x << ", " << rotatedOtherCoords.y << ", " << rotatedOtherCoords.z << " with " << boxesX << ", " << boxesY << ", " << boxesZ << " rotation " << boxesRotation << endl;*/ if (pointIsWithin(rotatedOtherCoords.x, rotatedOtherCoords.y, rotatedOtherCoords.z)) { collides = true; break; } } return collides; }
bool BoundingBoxSet::collidesWith(const BoundingBoxSet otherBoxSet, const glm::vec3 thisOffset, const glm::vec3 thisRotation, const glm::vec3 otherOffset, const glm::vec3 otherRotation) const { bool collides = false; glm::mat4 rotationMatrix = glm::rotate( glm::rotate( glm::rotate(glm::mat4x4(1.0f), otherRotation.z, glm::vec3(0.0f, 0.0f, -1.0f)), otherRotation.x, glm::vec3(-1.0f, 0.0f, 0.0f)), otherRotation.y, glm::vec3(0.0f, -1.0f, 0.0f)); for (auto vertex = otherBoxSet.vertices.begin(); vertex != otherBoxSet.vertices.end(); ++vertex) { glm::vec4 otherCoords(vertex->at(0), vertex->at(1), vertex->at(2), 1.0f); glm::vec4 rotatedOtherCoords; rotatedOtherCoords = rotationMatrix * otherCoords; rotatedOtherCoords.x += otherOffset.x; rotatedOtherCoords.y += otherOffset.y; rotatedOtherCoords.z += otherOffset.z; if (collidesWith(glm::vec3(rotatedOtherCoords.x, rotatedOtherCoords.y, rotatedOtherCoords.z), thisOffset, thisRotation)) { collides = true; break; } } return collides; }