I'm trying to code some intersection codes for Oriented Bounding Boxes (OBBs) and axis-aligned bounding boxes (AABBs). I have the OBB-OBB intersection using the Separating Axis Theorem (SAT) and I'm trying to figure out how can I improve this code to the specific case where I know I'm comparing an OBB with an AABB.
This is the code of my function:
bool Collider::isColliding(OBB * one, OBB * other)
{
TestManager::collisionTests++;
glm::vec3 oneCenter = one->m_gameObject->getWorldPosition(); // object's pos = collider center
glm::mat4 oneTransform = glm::scale(one->m_gameObject->getTransform(), one->m_halfSize); // scaling for halfsize
glm::vec3 otherCenter = other->m_gameObject->getWorldPosition();
glm::mat4 otherTransform = glm::scale(other->m_gameObject->getTransform(), other->m_halfSize);
for (int a = 0; a < 3; a++) {
glm::vec3 l = glm::vec3(oneTransform[a]); // one axis to project on
float tl = std::abs(glm::dot(l, otherCenter) - glm::dot(l, oneCenter)); // center distance
float ra = std::abs(glm::dot(l, glm::vec3(oneTransform[0]))) + std::abs(glm::dot(l, glm::vec3(oneTransform[1]))) + std::abs(glm::dot(l, glm::vec3(oneTransform[2])));
float rb = std::abs(glm::dot(l, glm::vec3(otherTransform[0]))) + std::abs(glm::dot(l, glm::vec3(otherTransform[1]))) + std::abs(glm::dot(l, glm::vec3(otherTransform[2])));
float penetration = (ra + rb) - tl;
if (penetration <= 0) { // no overlap
return false;
}
}
for (int b = 0; b < 3; b++) {
glm::vec3 l = glm::vec3(otherTransform[b]); // other axis to project on
float tl = std::abs(glm::dot(l, otherCenter) - glm::dot(l, oneCenter)); // center distance
float ra = std::abs(glm::dot(l, glm::vec3(oneTransform[0]))) + std::abs(glm::dot(l, glm::vec3(oneTransform[1]))) + std::abs(glm::dot(l, glm::vec3(oneTransform[2])));
float rb = std::abs(glm::dot(l, glm::vec3(otherTransform[0]))) + std::abs(glm::dot(l, glm::vec3(otherTransform[1]))) + std::abs(glm::dot(l, glm::vec3(otherTransform[2])));
float penetration = (ra + rb) - tl;
if (penetration <= 0) { // no overlap
return false;
}
}
for (int a = 0; a < 3; a++) {
glm::vec3 aAxis = glm::vec3(oneTransform[a]);
for (int b = 0; b < 3; b++) {
glm::vec3 bAxis = glm::vec3(otherTransform[b]);
if (aAxis != bAxis) {
glm::vec3 l = glm::cross(aAxis, bAxis); // has flaw when axis are same, result in (0,0,0), solved by if
float tl = std::abs(glm::dot(l, otherCenter) - glm::dot(l, oneCenter)); // center distance
float ra = std::abs(glm::dot(l, glm::vec3(oneTransform[0]))) + std::abs(glm::dot(l, glm::vec3(oneTransform[1]))) + std::abs(glm::dot(l, glm::vec3(oneTransform[2])));
float rb = std::abs(glm::dot(l, glm::vec3(otherTransform[0]))) + std::abs(glm::dot(l, glm::vec3(otherTransform[1]))) + std::abs(glm::dot(l, glm::vec3(otherTransform[2])));
float penetration = (ra + rb) - tl;
if (penetration <= 0) { // no overlap
return false;
}
}
}
}
return true;
}
Hope you can help me.