1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| bool intersectObbWithObb(OBB &first, OBB &second) { auto fRes = computerOBBIntersecte(first,second); auto sRes = computerOBBIntersecte(second,first); return (fRes&sRes); }
bool computerOBBIntersecte(OBB first, OBB second) { first.m_trans = glm::translate(glm::mat4(1.0f),-first.m_center); auto rotate = glm::mat4(glm::vec4(first.m_u, 0.0), glm::vec4(first.m_v, 0.0), glm::vec4(first.m_w, 0.0), glm::vec4(0, 0, 0, 1.0)); rotate = glm::transpose(rotate); first.m_rotate = rotate;
first.m_center = first.m_rotate * first.m_trans * glm::vec4(first.m_center,1.0); first.m_u = first.m_rotate * first.m_trans * glm::vec4(first.m_u,0.0); first.m_v = first.m_rotate * first.m_trans * glm::vec4(first.m_v,0.0); first.m_w = first.m_rotate * first.m_trans * glm::vec4(first.m_w,0.0);
auto trans2Origin = glm::translate(glm::mat4(1.0),-second.m_center); second.m_trans = trans2Origin; auto transRestore = glm::translate(glm::mat4(1.0),second.m_center); second.m_center = first.m_trans * transRestore * first.m_rotate * second.m_trans * glm::vec4(second.m_center,1.0); second.m_u = first.m_trans * transRestore * first.m_rotate * second.m_trans * glm::vec4(second.m_u,0.0); second.m_v = first.m_trans * transRestore * first.m_rotate * second.m_trans * glm::vec4(second.m_v,0.0); second.m_w = first.m_trans * transRestore * first.m_rotate * second.m_trans * glm::vec4(second.m_w,0.0);
glm::vec3 fMinPos = first.m_center - first.m_size.x/2.0f * first.m_u - first.m_size.y / 2.0f * first.m_v - first.m_size.z/2.0f * first.m_w; glm::vec3 fMaxPos = first.m_center + first.m_size.x/2.0f * first.m_u + first.m_size.y / 2.0f * first.m_v + first.m_size.z/2.0f * first.m_w;
std::array<glm::vec3,8> secondPos; { secondPos[0] = second.m_center - second.m_u * second.m_size.x * 0.5f - second.m_v * second.m_size.y * 0.5f - second.m_w * second.m_size.z * 0.5f;
secondPos[1] = second.m_center - second.m_u * second.m_size.x * 0.5f - second.m_v * second.m_size.y * 0.5f + second.m_w * second.m_size.z * 0.5f;
secondPos[2] = second.m_center - second.m_u * second.m_size.x * 0.5f + second.m_v * second.m_size.y * 0.5f - second.m_w * second.m_size.z * 0.5f;
secondPos[3] = second.m_center - second.m_u * second.m_size.x * 0.5f + second.m_v * second.m_size.y * 0.5f + second.m_w * second.m_size.z * 0.5f;
secondPos[4] = second.m_center + second.m_u * second.m_size.x * 0.5f - second.m_v * second.m_size.y * 0.5f - second.m_w * second.m_size.z * 0.5f;
secondPos[5] = second.m_center + second.m_u * second.m_size.x * 0.5f - second.m_v * second.m_size.y * 0.5f + second.m_w * second.m_size.z * 0.5f;
secondPos[6] = second.m_center + second.m_u * second.m_size.x * 0.5f + second.m_v * second.m_size.y * 0.5f - second.m_w * second.m_size.z * 0.5f;
secondPos[7] = second.m_center + second.m_u * second.m_size.x * 0.5f + second.m_v * second.m_size.y * 0.5f + second.m_w * second.m_size.z * 0.5f; }
glm::vec3 sMinPos{secondPos[0]}; glm::vec3 sMaxPos{secondPos[0]};
for(auto & it : secondPos) { sMinPos.x = sMinPos.x > it.x? it.x:sMinPos.x; sMinPos.y = sMinPos.y > it.y? it.y:sMinPos.y; sMinPos.z = sMinPos.z > it.z? it.z:sMinPos.z;
sMaxPos.x = sMaxPos.x < it.x? it.x:sMaxPos.x; sMaxPos.y = sMaxPos.y < it.y? it.y:sMaxPos.y; sMaxPos.z = sMaxPos.z < it.z? it.z:sMaxPos.z; }
if( (fMinPos.x > sMaxPos.x || sMinPos.x > fMaxPos.x) || (fMinPos.y > sMaxPos.y || sMinPos.y > fMaxPos.y) || (fMinPos.z > sMaxPos.z || sMinPos.z > fMaxPos.z) ) { return false; }
return true; }
|