Advertisement
Guest User

Untitled

a guest
Jul 15th, 2015
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. struct PROJECTION {
  2. float fMin;
  3. float fMax;
  4. bool Overlap(const PROJECTION& _pOther) const {
  5. return !(fMax < _pOther.fMin || _pOther.fMax < fMin);
  6. }
  7. };
  8.  
  9. bool CSat::TestFaces(const CShapeInstance* _psiShape0, const CShapeInstance* _psiShape1) const {
  10. const CConvexPolyhedron* pcpConvex0 = static_cast<const CConvexPolyhedron*>(_psiShape0->m_psShape);
  11. const CConvexPolyhedron* pcpConvex1 = static_cast<const CConvexPolyhedron*>(_psiShape1->m_psShape);
  12.  
  13. for (unsigned int I = 0; I < pcpConvex0->m_ui32TotalFaces; ++I) {
  14. CVector3 vAxis = _psiShape0->m_mTransform * pcpConvex0->m_pvFaces[I];
  15.  
  16. PROJECTION pProj0;
  17. PROJECTION pProj1;
  18. Project(pcpConvex0, _psiShape0->m_mTransform, vAxis, pProj0);
  19. Project(pcpConvex1, _psiShape1->m_mTransform, vAxis, pProj1);
  20.  
  21. if (!pProj0.Overlap(pProj1)) {
  22. return false;
  23. }
  24. }
  25.  
  26. return true;
  27. }
  28.  
  29. void CSat::Project(const CConvexPolyhedron* _pcpHull, const CMatrix4x4& _mTransform, const CVector3& _vAxis, PROJECTION& _pProj) const {
  30. _pProj.fMin = _pProj.fMax = _vAxis.Dot(_mTransform * _pcpHull->m_pvVerts[0]);
  31. for (unsigned int I = 1; I < _pcpHull->m_ui32TotalVerts; ++I) {
  32. float fProj = _vAxis.Dot(_mTransform * _pcpHull->m_pvVerts[I]);
  33. if (fProj < _pProj.fMin) {
  34. _pProj.fMin = fProj;
  35. }
  36. if (fProj > _pProj.fMax) {
  37. _pProj.fMax = fProj;
  38. }
  39. }
  40. }
  41.  
  42. bool CSat::TestEdges(const CShapeInstance* _psiShape0, const CShapeInstance* _psiShape1) const {
  43. const CConvexPolyhedron* pcpConvex0 = static_cast<const CConvexPolyhedron*>(_psiShape0->m_psShape);
  44. const CConvexPolyhedron* pcpConvex1 = static_cast<const CConvexPolyhedron*>(_psiShape1->m_psShape);
  45.  
  46. for (unsigned int I = 0; I < pcpConvex0->m_ui32TotalEdges; ++I) {
  47. CVector3 vAxis0 = _psiShape0->m_mTransform * pcpConvex0->m_pvEdges[I].vNormal;
  48.  
  49. for (unsigned int J = 0; J < pcpConvex1->m_ui32TotalEdges; ++J) {
  50. CVector3 vAxis1 = _psiShape1->m_mTransform * pcpConvex1->m_pvEdges[J].vNormal;
  51.  
  52. CVector3 vAxis = vAxis0.Cross(vAxis1);
  53.  
  54. PROJECTION pProj0;
  55. PROJECTION pProj1;
  56. Project(pcpConvex0, _psiShape0->m_mTransform, vAxis, pProj0);
  57. Project(pcpConvex1, _psiShape1->m_mTransform, vAxis, pProj1);
  58.  
  59. if (!pProj0.Overlap(pProj1)) {
  60. return false;
  61. }
  62. }
  63. }
  64.  
  65. return true;
  66. }
  67.  
  68. bool CSat::Intersect(const CShapeInstance* _psiShape0, const CShapeInstance* _psiShape1) {
  69. if (TestFaces(_psiShape0, _psiShape1)) {
  70. return true;
  71. }
  72. if (TestFaces(_psiShape1, _psiShape0)) {
  73.          return true;
  74.      }
  75. if (TestEdges(_psiShape0, _psiShape1)) {
  76. return true;
  77. }
  78.  
  79. return false;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement