OPC_LSSCollider.cpp 26 KB

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