OPC_OBBCollider.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /*
  3. * OPCODE - Optimized Collision Detection
  4. * Copyright (C) 2001 Pierre Terdiman
  5. * Homepage: http://www.codercorner.com/Opcode.htm
  6. */
  7. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. /**
  10. * Contains code for an OBB collider.
  11. * \file OPC_OBBCollider.cpp
  12. * \author Pierre Terdiman
  13. * \date January, 1st, 2002
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. /**
  18. * Contains an OBB-vs-tree collider.
  19. *
  20. * \class OBBCollider
  21. * \author Pierre Terdiman
  22. * \version 1.3
  23. * \date January, 1st, 2002
  24. */
  25. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. // Precompiled Header
  28. #include "Stdafx.h"
  29. using namespace Opcode;
  30. #include "OPC_BoxBoxOverlap.h"
  31. #include "OPC_TriBoxOverlap.h"
  32. #define SET_CONTACT(prim_index, flag) \
  33. /* Set contact status */ \
  34. mFlags |= flag; \
  35. mTouchedPrimitives->Add(udword(prim_index));
  36. //! OBB-triangle test
  37. #define OBB_PRIM(prim_index, flag) \
  38. /* Request vertices from the app */ \
  39. VertexPointers VP; ConversionArea VC; mIMesh->GetTriangle(VP, prim_index, VC); \
  40. /* Transform them in a common space */ \
  41. TransformPoint(mLeafVerts[0], *VP.Vertex[0], mRModelToBox, mTModelToBox); \
  42. TransformPoint(mLeafVerts[1], *VP.Vertex[1], mRModelToBox, mTModelToBox); \
  43. TransformPoint(mLeafVerts[2], *VP.Vertex[2], mRModelToBox, mTModelToBox); \
  44. /* Perform triangle-box overlap test */ \
  45. if(TriBoxOverlap()) \
  46. { \
  47. SET_CONTACT(prim_index, flag) \
  48. }
  49. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  50. /**
  51. * Constructor.
  52. */
  53. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  54. OBBCollider::OBBCollider() : mFullBoxBoxTest(true)
  55. {
  56. }
  57. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. /**
  59. * Destructor.
  60. */
  61. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  62. OBBCollider::~OBBCollider()
  63. {
  64. }
  65. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. /**
  67. * Validates current settings. You should call this method after all the settings and callbacks have been defined.
  68. * \return null if everything is ok, else a string describing the problem
  69. */
  70. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  71. const char* OBBCollider::ValidateSettings()
  72. {
  73. if(TemporalCoherenceEnabled() && !FirstContactEnabled()) return "Temporal coherence only works with ""First contact"" mode!";
  74. return VolumeCollider::ValidateSettings();
  75. }
  76. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  77. /**
  78. * Generic collision query for generic OPCODE models. After the call, access the results:
  79. * - with GetContactStatus()
  80. * - with GetNbTouchedPrimitives()
  81. * - with GetTouchedPrimitives()
  82. *
  83. * \param cache [in/out] a box cache
  84. * \param box [in] collision OBB in local space
  85. * \param model [in] Opcode model to collide with
  86. * \param worldb [in] OBB's world matrix, or null
  87. * \param worldm [in] model's world matrix, or null
  88. * \return true if success
  89. * \warning SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
  90. */
  91. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  92. bool OBBCollider::Collide(OBBCache& cache, const OBB& box, const Model& model, const Matrix4x4* worldb, const Matrix4x4* worldm)
  93. {
  94. // Checkings
  95. if(!Setup(&model)) return false;
  96. // Init collision query
  97. if(InitQuery(cache, box, worldb, worldm)) return true;
  98. if(!model.HasLeafNodes())
  99. {
  100. if(model.IsQuantized())
  101. {
  102. const AABBQuantizedNoLeafTree* Tree = (const AABBQuantizedNoLeafTree*)model.GetTree();
  103. // Setup dequantization coeffs
  104. mCenterCoeff = Tree->mCenterCoeff;
  105. mExtentsCoeff = Tree->mExtentsCoeff;
  106. // Perform collision query
  107. if(SkipPrimitiveTests()) _CollideNoPrimitiveTest(Tree->GetNodes());
  108. else _Collide(Tree->GetNodes());
  109. }
  110. else
  111. {
  112. const AABBNoLeafTree* Tree = (const AABBNoLeafTree*)model.GetTree();
  113. // Perform collision query
  114. if(SkipPrimitiveTests()) _CollideNoPrimitiveTest(Tree->GetNodes());
  115. else _Collide(Tree->GetNodes());
  116. }
  117. }
  118. else
  119. {
  120. if(model.IsQuantized())
  121. {
  122. const AABBQuantizedTree* Tree = (const AABBQuantizedTree*)model.GetTree();
  123. // Setup dequantization coeffs
  124. mCenterCoeff = Tree->mCenterCoeff;
  125. mExtentsCoeff = Tree->mExtentsCoeff;
  126. // Perform collision query
  127. if(SkipPrimitiveTests()) _CollideNoPrimitiveTest(Tree->GetNodes());
  128. else _Collide(Tree->GetNodes());
  129. }
  130. else
  131. {
  132. const AABBCollisionTree* Tree = (const AABBCollisionTree*)model.GetTree();
  133. // Perform collision query
  134. if(SkipPrimitiveTests()) _CollideNoPrimitiveTest(Tree->GetNodes());
  135. else _Collide(Tree->GetNodes());
  136. }
  137. }
  138. return true;
  139. }
  140. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  141. /**
  142. * Initializes a collision query :
  143. * - reset stats & contact status
  144. * - setup matrices
  145. * - check temporal coherence
  146. *
  147. * \param cache [in/out] a box cache
  148. * \param box [in] obb in local space
  149. * \param worldb [in] obb's world matrix, or null
  150. * \param worldm [in] model's world matrix, or null
  151. * \return TRUE if we can return immediately
  152. * \warning SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
  153. */
  154. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  155. BOOL OBBCollider::InitQuery(OBBCache& cache, const OBB& box, const Matrix4x4* worldb, const Matrix4x4* worldm)
  156. {
  157. // 1) Call the base method
  158. VolumeCollider::InitQuery();
  159. // 2) Compute obb in world space
  160. mBoxExtents = box.mExtents;
  161. Matrix4x4 WorldB;
  162. if(worldb)
  163. {
  164. WorldB = Matrix4x4( box.mRot * Matrix3x3(*worldb) );
  165. WorldB.SetTrans(box.mCenter * *worldb);
  166. }
  167. else
  168. {
  169. WorldB = box.mRot;
  170. WorldB.SetTrans(box.mCenter);
  171. }
  172. // Setup matrices
  173. Matrix4x4 InvWorldB;
  174. InvertPRMatrix(InvWorldB, WorldB);
  175. if(worldm)
  176. {
  177. Matrix4x4 InvWorldM;
  178. InvertPRMatrix(InvWorldM, *worldm);
  179. Matrix4x4 WorldBtoM = WorldB * InvWorldM;
  180. Matrix4x4 WorldMtoB = *worldm * InvWorldB;
  181. mRModelToBox = WorldMtoB; WorldMtoB.GetTrans(mTModelToBox);
  182. mRBoxToModel = WorldBtoM; WorldBtoM.GetTrans(mTBoxToModel);
  183. }
  184. else
  185. {
  186. mRModelToBox = InvWorldB; InvWorldB.GetTrans(mTModelToBox);
  187. mRBoxToModel = WorldB; WorldB.GetTrans(mTBoxToModel);
  188. }
  189. // 3) Setup destination pointer
  190. mTouchedPrimitives = &cache.TouchedPrimitives;
  191. // 4) Special case: 1-triangle meshes [Opcode 1.3]
  192. if(mCurrentModel && mCurrentModel->HasSingleNode())
  193. {
  194. if(!SkipPrimitiveTests())
  195. {
  196. // We simply perform the BV-Prim overlap test each time. We assume single triangle has index 0.
  197. mTouchedPrimitives->Reset();
  198. // Perform overlap test between the unique triangle and the box (and set contact status if needed)
  199. OBB_PRIM(udword(0), OPC_CONTACT)
  200. // Return immediately regardless of status
  201. return TRUE;
  202. }
  203. }
  204. // 5) Check temporal coherence:
  205. if(TemporalCoherenceEnabled())
  206. {
  207. // Here we use temporal coherence
  208. // => check results from previous frame before performing the collision query
  209. if(FirstContactEnabled())
  210. {
  211. // We're only interested in the first contact found => test the unique previously touched face
  212. if(mTouchedPrimitives->GetNbEntries())
  213. {
  214. // Get index of previously touched face = the first entry in the array
  215. udword PreviouslyTouchedFace = mTouchedPrimitives->GetEntry(0);
  216. // Then reset the array:
  217. // - if the overlap test below is successful, the index we'll get added back anyway
  218. // - if it isn't, then the array should be reset anyway for the normal query
  219. mTouchedPrimitives->Reset();
  220. // Perform overlap test between the cached triangle and the box (and set contact status if needed)
  221. OBB_PRIM(PreviouslyTouchedFace, OPC_TEMPORAL_CONTACT)
  222. // Return immediately if possible
  223. if(GetContactStatus()) return TRUE;
  224. }
  225. // else no face has been touched during previous query
  226. // => we'll have to perform a normal query
  227. }
  228. else
  229. {
  230. // ### rewrite this
  231. OBB TestBox(mTBoxToModel, mBoxExtents, mRBoxToModel);
  232. // We're interested in all contacts =>test the new real box N(ew) against the previous fat box P(revious):
  233. if(IsCacheValid(cache) && TestBox.IsInside(cache.FatBox))
  234. {
  235. // - if N is included in P, return previous list
  236. // => we simply leave the list (mTouchedFaces) unchanged
  237. // Set contact status if needed
  238. if(mTouchedPrimitives->GetNbEntries()) mFlags |= OPC_TEMPORAL_CONTACT;
  239. // In any case we don't need to do a query
  240. return TRUE;
  241. }
  242. else
  243. {
  244. // - else do the query using a fat N
  245. // Reset cache since we'll about to perform a real query
  246. mTouchedPrimitives->Reset();
  247. // Make a fat box so that coherence will work for subsequent frames
  248. TestBox.mExtents *= cache.FatCoeff;
  249. mBoxExtents *= cache.FatCoeff;
  250. // Update cache with query data (signature for cached faces)
  251. cache.FatBox = TestBox;
  252. }
  253. }
  254. }
  255. else
  256. {
  257. // Here we don't use temporal coherence => do a normal query
  258. mTouchedPrimitives->Reset();
  259. }
  260. // Now we can precompute box-box data
  261. // Precompute absolute box-to-model rotation matrix
  262. for(udword i=0;i<3;i++)
  263. {
  264. for(udword j=0;j<3;j++)
  265. {
  266. // Epsilon value prevents floating-point inaccuracies (strategy borrowed from RAPID)
  267. mAR.m[i][j] = 1e-6f + fabsf(mRBoxToModel.m[i][j]);
  268. }
  269. }
  270. // Precompute bounds for box-in-box test
  271. mB0 = mBoxExtents - mTModelToBox;
  272. mB1 = - mBoxExtents - mTModelToBox;
  273. // Precompute box-box data - Courtesy of Erwin de Vries
  274. mBBx1 = mBoxExtents.x*mAR.m[0][0] + mBoxExtents.y*mAR.m[1][0] + mBoxExtents.z*mAR.m[2][0];
  275. mBBy1 = mBoxExtents.x*mAR.m[0][1] + mBoxExtents.y*mAR.m[1][1] + mBoxExtents.z*mAR.m[2][1];
  276. mBBz1 = mBoxExtents.x*mAR.m[0][2] + mBoxExtents.y*mAR.m[1][2] + mBoxExtents.z*mAR.m[2][2];
  277. mBB_1 = mBoxExtents.y*mAR.m[2][0] + mBoxExtents.z*mAR.m[1][0];
  278. mBB_2 = mBoxExtents.x*mAR.m[2][0] + mBoxExtents.z*mAR.m[0][0];
  279. mBB_3 = mBoxExtents.x*mAR.m[1][0] + mBoxExtents.y*mAR.m[0][0];
  280. mBB_4 = mBoxExtents.y*mAR.m[2][1] + mBoxExtents.z*mAR.m[1][1];
  281. mBB_5 = mBoxExtents.x*mAR.m[2][1] + mBoxExtents.z*mAR.m[0][1];
  282. mBB_6 = mBoxExtents.x*mAR.m[1][1] + mBoxExtents.y*mAR.m[0][1];
  283. mBB_7 = mBoxExtents.y*mAR.m[2][2] + mBoxExtents.z*mAR.m[1][2];
  284. mBB_8 = mBoxExtents.x*mAR.m[2][2] + mBoxExtents.z*mAR.m[0][2];
  285. mBB_9 = mBoxExtents.x*mAR.m[1][2] + mBoxExtents.y*mAR.m[0][2];
  286. return FALSE;
  287. }
  288. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  289. /**
  290. * Checks the OBB completely contains the box. In which case we can end the query sooner.
  291. * \param bc [in] box center
  292. * \param be [in] box extents
  293. * \return true if the OBB contains the whole box
  294. */
  295. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  296. inline_ BOOL OBBCollider::OBBContainsBox(const Point& bc, const Point& be)
  297. {
  298. // I assume if all 8 box vertices are inside the OBB, so does the whole box.
  299. // Sounds ok but maybe there's a better way?
  300. /*
  301. #define TEST_PT(a,b,c) \
  302. p.x=a; p.y=b; p.z=c; p+=bc; \
  303. f = p.x * mRModelToBox.m[0][0] + p.y * mRModelToBox.m[1][0] + p.z * mRModelToBox.m[2][0]; if(f>mB0.x || f<mB1.x) return FALSE;\
  304. f = p.x * mRModelToBox.m[0][1] + p.y * mRModelToBox.m[1][1] + p.z * mRModelToBox.m[2][1]; if(f>mB0.y || f<mB1.y) return FALSE;\
  305. f = p.x * mRModelToBox.m[0][2] + p.y * mRModelToBox.m[1][2] + p.z * mRModelToBox.m[2][2]; if(f>mB0.z || f<mB1.z) return FALSE;
  306. Point p;
  307. float f;
  308. TEST_PT(be.x, be.y, be.z)
  309. TEST_PT(-be.x, be.y, be.z)
  310. TEST_PT(be.x, -be.y, be.z)
  311. TEST_PT(-be.x, -be.y, be.z)
  312. TEST_PT(be.x, be.y, -be.z)
  313. TEST_PT(-be.x, be.y, -be.z)
  314. TEST_PT(be.x, -be.y, -be.z)
  315. TEST_PT(-be.x, -be.y, -be.z)
  316. return TRUE;
  317. */
  318. // Yes there is:
  319. // - compute model-box's AABB in OBB space
  320. // - test AABB-in-AABB
  321. float NCx = bc.x * mRModelToBox.m[0][0] + bc.y * mRModelToBox.m[1][0] + bc.z * mRModelToBox.m[2][0];
  322. float NEx = fabsf(mRModelToBox.m[0][0] * be.x) + fabsf(mRModelToBox.m[1][0] * be.y) + fabsf(mRModelToBox.m[2][0] * be.z);
  323. if(mB0.x < NCx+NEx) return FALSE;
  324. if(mB1.x > NCx-NEx) return FALSE;
  325. float NCy = bc.x * mRModelToBox.m[0][1] + bc.y * mRModelToBox.m[1][1] + bc.z * mRModelToBox.m[2][1];
  326. float NEy = fabsf(mRModelToBox.m[0][1] * be.x) + fabsf(mRModelToBox.m[1][1] * be.y) + fabsf(mRModelToBox.m[2][1] * be.z);
  327. if(mB0.y < NCy+NEy) return FALSE;
  328. if(mB1.y > NCy-NEy) return FALSE;
  329. float NCz = bc.x * mRModelToBox.m[0][2] + bc.y * mRModelToBox.m[1][2] + bc.z * mRModelToBox.m[2][2];
  330. float NEz = fabsf(mRModelToBox.m[0][2] * be.x) + fabsf(mRModelToBox.m[1][2] * be.y) + fabsf(mRModelToBox.m[2][2] * be.z);
  331. if(mB0.z < NCz+NEz) return FALSE;
  332. if(mB1.z > NCz-NEz) return FALSE;
  333. return TRUE;
  334. }
  335. #define TEST_BOX_IN_OBB(center, extents) \
  336. if(OBBContainsBox(center, extents)) \
  337. { \
  338. /* Set contact status */ \
  339. mFlags |= OPC_CONTACT; \
  340. _Dump(node); \
  341. return; \
  342. }
  343. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  344. /**
  345. * Recursive collision query for normal AABB trees.
  346. * \param node [in] current collision node
  347. */
  348. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  349. void OBBCollider::_Collide(const AABBCollisionNode* node)
  350. {
  351. // Perform OBB-AABB overlap test
  352. if(!BoxBoxOverlap(node->mAABB.mExtents, node->mAABB.mCenter)) return;
  353. TEST_BOX_IN_OBB(node->mAABB.mCenter, node->mAABB.mExtents)
  354. if(node->IsLeaf())
  355. {
  356. OBB_PRIM(node->GetPrimitive(), OPC_CONTACT)
  357. }
  358. else
  359. {
  360. _Collide(node->GetPos());
  361. if(ContactFound()) return;
  362. _Collide(node->GetNeg());
  363. }
  364. }
  365. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  366. /**
  367. * Recursive collision query for normal AABB trees, without primitive tests.
  368. * \param node [in] current collision node
  369. */
  370. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  371. void OBBCollider::_CollideNoPrimitiveTest(const AABBCollisionNode* node)
  372. {
  373. // Perform OBB-AABB overlap test
  374. if(!BoxBoxOverlap(node->mAABB.mExtents, node->mAABB.mCenter)) return;
  375. TEST_BOX_IN_OBB(node->mAABB.mCenter, node->mAABB.mExtents)
  376. if(node->IsLeaf())
  377. {
  378. SET_CONTACT(node->GetPrimitive(), OPC_CONTACT)
  379. }
  380. else
  381. {
  382. _CollideNoPrimitiveTest(node->GetPos());
  383. if(ContactFound()) return;
  384. _CollideNoPrimitiveTest(node->GetNeg());
  385. }
  386. }
  387. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  388. /**
  389. * Recursive collision query for quantized AABB trees.
  390. * \param node [in] current collision node
  391. */
  392. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  393. void OBBCollider::_Collide(const AABBQuantizedNode* node)
  394. {
  395. // Dequantize box
  396. const QuantizedAABB& Box = node->mAABB;
  397. const Point Center(float(Box.mCenter[0]) * mCenterCoeff.x, float(Box.mCenter[1]) * mCenterCoeff.y, float(Box.mCenter[2]) * mCenterCoeff.z);
  398. const Point Extents(float(Box.mExtents[0]) * mExtentsCoeff.x, float(Box.mExtents[1]) * mExtentsCoeff.y, float(Box.mExtents[2]) * mExtentsCoeff.z);
  399. // Perform OBB-AABB overlap test
  400. if(!BoxBoxOverlap(Extents, Center)) return;
  401. TEST_BOX_IN_OBB(Center, Extents)
  402. if(node->IsLeaf())
  403. {
  404. OBB_PRIM(node->GetPrimitive(), OPC_CONTACT)
  405. }
  406. else
  407. {
  408. _Collide(node->GetPos());
  409. if(ContactFound()) return;
  410. _Collide(node->GetNeg());
  411. }
  412. }
  413. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  414. /**
  415. * Recursive collision query for quantized AABB trees, without primitive tests.
  416. * \param node [in] current collision node
  417. */
  418. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  419. void OBBCollider::_CollideNoPrimitiveTest(const AABBQuantizedNode* node)
  420. {
  421. // Dequantize box
  422. const QuantizedAABB& Box = node->mAABB;
  423. const Point Center(float(Box.mCenter[0]) * mCenterCoeff.x, float(Box.mCenter[1]) * mCenterCoeff.y, float(Box.mCenter[2]) * mCenterCoeff.z);
  424. const Point Extents(float(Box.mExtents[0]) * mExtentsCoeff.x, float(Box.mExtents[1]) * mExtentsCoeff.y, float(Box.mExtents[2]) * mExtentsCoeff.z);
  425. // Perform OBB-AABB overlap test
  426. if(!BoxBoxOverlap(Extents, Center)) return;
  427. TEST_BOX_IN_OBB(Center, Extents)
  428. if(node->IsLeaf())
  429. {
  430. SET_CONTACT(node->GetPrimitive(), OPC_CONTACT)
  431. }
  432. else
  433. {
  434. _CollideNoPrimitiveTest(node->GetPos());
  435. if(ContactFound()) return;
  436. _CollideNoPrimitiveTest(node->GetNeg());
  437. }
  438. }
  439. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  440. /**
  441. * Recursive collision query for no-leaf AABB trees.
  442. * \param node [in] current collision node
  443. */
  444. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  445. void OBBCollider::_Collide(const AABBNoLeafNode* node)
  446. {
  447. // Perform OBB-AABB overlap test
  448. if(!BoxBoxOverlap(node->mAABB.mExtents, node->mAABB.mCenter)) return;
  449. TEST_BOX_IN_OBB(node->mAABB.mCenter, node->mAABB.mExtents)
  450. if(node->HasPosLeaf()) { OBB_PRIM(node->GetPosPrimitive(), OPC_CONTACT) }
  451. else _Collide(node->GetPos());
  452. if(ContactFound()) return;
  453. if(node->HasNegLeaf()) { OBB_PRIM(node->GetNegPrimitive(), OPC_CONTACT) }
  454. else _Collide(node->GetNeg());
  455. }
  456. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  457. /**
  458. * Recursive collision query for no-leaf AABB trees, without primitive tests.
  459. * \param node [in] current collision node
  460. */
  461. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  462. void OBBCollider::_CollideNoPrimitiveTest(const AABBNoLeafNode* node)
  463. {
  464. // Perform OBB-AABB overlap test
  465. if(!BoxBoxOverlap(node->mAABB.mExtents, node->mAABB.mCenter)) return;
  466. TEST_BOX_IN_OBB(node->mAABB.mCenter, node->mAABB.mExtents)
  467. if(node->HasPosLeaf()) { SET_CONTACT(node->GetPosPrimitive(), OPC_CONTACT) }
  468. else _CollideNoPrimitiveTest(node->GetPos());
  469. if(ContactFound()) return;
  470. if(node->HasNegLeaf()) { SET_CONTACT(node->GetNegPrimitive(), OPC_CONTACT) }
  471. else _CollideNoPrimitiveTest(node->GetNeg());
  472. }
  473. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  474. /**
  475. * Recursive collision query for quantized no-leaf AABB trees.
  476. * \param node [in] current collision node
  477. */
  478. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  479. void OBBCollider::_Collide(const AABBQuantizedNoLeafNode* node)
  480. {
  481. // Dequantize box
  482. const QuantizedAABB& Box = node->mAABB;
  483. const Point Center(float(Box.mCenter[0]) * mCenterCoeff.x, float(Box.mCenter[1]) * mCenterCoeff.y, float(Box.mCenter[2]) * mCenterCoeff.z);
  484. const Point Extents(float(Box.mExtents[0]) * mExtentsCoeff.x, float(Box.mExtents[1]) * mExtentsCoeff.y, float(Box.mExtents[2]) * mExtentsCoeff.z);
  485. // Perform OBB-AABB overlap test
  486. if(!BoxBoxOverlap(Extents, Center)) return;
  487. TEST_BOX_IN_OBB(Center, Extents)
  488. if(node->HasPosLeaf()) { OBB_PRIM(node->GetPosPrimitive(), OPC_CONTACT) }
  489. else _Collide(node->GetPos());
  490. if(ContactFound()) return;
  491. if(node->HasNegLeaf()) { OBB_PRIM(node->GetNegPrimitive(), OPC_CONTACT) }
  492. else _Collide(node->GetNeg());
  493. }
  494. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  495. /**
  496. * Recursive collision query for quantized no-leaf AABB trees, without primitive tests.
  497. * \param node [in] current collision node
  498. */
  499. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  500. void OBBCollider::_CollideNoPrimitiveTest(const AABBQuantizedNoLeafNode* node)
  501. {
  502. // Dequantize box
  503. const QuantizedAABB& Box = node->mAABB;
  504. const Point Center(float(Box.mCenter[0]) * mCenterCoeff.x, float(Box.mCenter[1]) * mCenterCoeff.y, float(Box.mCenter[2]) * mCenterCoeff.z);
  505. const Point Extents(float(Box.mExtents[0]) * mExtentsCoeff.x, float(Box.mExtents[1]) * mExtentsCoeff.y, float(Box.mExtents[2]) * mExtentsCoeff.z);
  506. // Perform OBB-AABB overlap test
  507. if(!BoxBoxOverlap(Extents, Center)) return;
  508. TEST_BOX_IN_OBB(Center, Extents)
  509. if(node->HasPosLeaf()) { SET_CONTACT(node->GetPosPrimitive(), OPC_CONTACT) }
  510. else _CollideNoPrimitiveTest(node->GetPos());
  511. if(ContactFound()) return;
  512. if(node->HasNegLeaf()) { SET_CONTACT(node->GetNegPrimitive(), OPC_CONTACT) }
  513. else _CollideNoPrimitiveTest(node->GetNeg());
  514. }
  515. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  516. /**
  517. * Constructor.
  518. */
  519. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  520. HybridOBBCollider::HybridOBBCollider()
  521. {
  522. }
  523. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  524. /**
  525. * Destructor.
  526. */
  527. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  528. HybridOBBCollider::~HybridOBBCollider()
  529. {
  530. }
  531. bool HybridOBBCollider::Collide(OBBCache& cache, const OBB& box, const HybridModel& model, const Matrix4x4* worldb, const Matrix4x4* worldm)
  532. {
  533. // We don't want primitive tests here!
  534. mFlags |= OPC_NO_PRIMITIVE_TESTS;
  535. // Checkings
  536. if(!Setup(&model)) return false;
  537. // Init collision query
  538. if(InitQuery(cache, box, worldb, worldm)) return true;
  539. // Special case for 1-leaf trees
  540. if(mCurrentModel && mCurrentModel->HasSingleNode())
  541. {
  542. // Here we're supposed to perform a normal query, except our tree has a single node, i.e. just a few triangles
  543. udword Nb = mIMesh->GetNbTriangles();
  544. // Loop through all triangles
  545. for(udword i=0;i<Nb;i++)
  546. {
  547. OBB_PRIM(i, OPC_CONTACT)
  548. }
  549. return true;
  550. }
  551. // Override destination array since we're only going to get leaf boxes here
  552. mTouchedBoxes.Reset();
  553. mTouchedPrimitives = &mTouchedBoxes;
  554. // Now, do the actual query against leaf boxes
  555. if(!model.HasLeafNodes())
  556. {
  557. if(model.IsQuantized())
  558. {
  559. const AABBQuantizedNoLeafTree* Tree = (const AABBQuantizedNoLeafTree*)model.GetTree();
  560. // Setup dequantization coeffs
  561. mCenterCoeff = Tree->mCenterCoeff;
  562. mExtentsCoeff = Tree->mExtentsCoeff;
  563. // Perform collision query - we don't want primitive tests here!
  564. _CollideNoPrimitiveTest(Tree->GetNodes());
  565. }
  566. else
  567. {
  568. const AABBNoLeafTree* Tree = (const AABBNoLeafTree*)model.GetTree();
  569. // Perform collision query - we don't want primitive tests here!
  570. _CollideNoPrimitiveTest(Tree->GetNodes());
  571. }
  572. }
  573. else
  574. {
  575. if(model.IsQuantized())
  576. {
  577. const AABBQuantizedTree* Tree = (const AABBQuantizedTree*)model.GetTree();
  578. // Setup dequantization coeffs
  579. mCenterCoeff = Tree->mCenterCoeff;
  580. mExtentsCoeff = Tree->mExtentsCoeff;
  581. // Perform collision query - we don't want primitive tests here!
  582. _CollideNoPrimitiveTest(Tree->GetNodes());
  583. }
  584. else
  585. {
  586. const AABBCollisionTree* Tree = (const AABBCollisionTree*)model.GetTree();
  587. // Perform collision query - we don't want primitive tests here!
  588. _CollideNoPrimitiveTest(Tree->GetNodes());
  589. }
  590. }
  591. // We only have a list of boxes so far
  592. if(GetContactStatus())
  593. {
  594. // Reset contact status, since it currently only reflects collisions with leaf boxes
  595. Collider::InitQuery();
  596. // Change dest container so that we can use built-in overlap tests and get collided primitives
  597. cache.TouchedPrimitives.Reset();
  598. mTouchedPrimitives = &cache.TouchedPrimitives;
  599. // Read touched leaf boxes
  600. udword Nb = mTouchedBoxes.GetNbEntries();
  601. const udword* Touched = mTouchedBoxes.GetEntries();
  602. const LeafTriangles* LT = model.GetLeafTriangles();
  603. const udword* Indices = model.GetIndices();
  604. // Loop through touched leaves
  605. while(Nb--)
  606. {
  607. const LeafTriangles& CurrentLeaf = LT[*Touched++];
  608. // Each leaf box has a set of triangles
  609. udword NbTris = CurrentLeaf.GetNbTriangles();
  610. if(Indices)
  611. {
  612. const udword* T = &Indices[CurrentLeaf.GetTriangleIndex()];
  613. // Loop through triangles and test each of them
  614. while(NbTris--)
  615. {
  616. udword TriangleIndex = *T++;
  617. OBB_PRIM(TriangleIndex, OPC_CONTACT)
  618. }
  619. }
  620. else
  621. {
  622. udword BaseIndex = CurrentLeaf.GetTriangleIndex();
  623. // Loop through triangles and test each of them
  624. while(NbTris--)
  625. {
  626. udword TriangleIndex = BaseIndex++;
  627. OBB_PRIM(TriangleIndex, OPC_CONTACT)
  628. }
  629. }
  630. }
  631. }
  632. return true;
  633. }