extrudedPolyList.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "math/mMath.h"
  24. #include "console/console.h"
  25. #include "collision/extrudedPolyList.h"
  26. #include "math/mPolyhedron.h"
  27. #include "collision/collision.h"
  28. // Minimum distance from a face
  29. F32 ExtrudedPolyList::FaceEpsilon = 0.01f;
  30. // Value used to compare collision times
  31. F32 ExtrudedPolyList::EqualEpsilon = 0.0001f;
  32. ExtrudedPolyList::ExtrudedPolyList()
  33. {
  34. VECTOR_SET_ASSOCIATION(mVertexList);
  35. VECTOR_SET_ASSOCIATION(mIndexList);
  36. VECTOR_SET_ASSOCIATION(mExtrudedList);
  37. VECTOR_SET_ASSOCIATION(mPlaneList);
  38. VECTOR_SET_ASSOCIATION(mPolyPlaneList);
  39. mVelocity.set(0.0f,0.0f,0.0f);
  40. mIndexList.reserve(128);
  41. mVertexList.reserve(64);
  42. mPolyPlaneList.reserve(64);
  43. mPlaneList.reserve(64);
  44. mCollisionList = 0;
  45. }
  46. ExtrudedPolyList::~ExtrudedPolyList()
  47. {
  48. }
  49. //----------------------------------------------------------------------------
  50. bool ExtrudedPolyList::isEmpty() const
  51. {
  52. return mCollisionList->getCount() == 0;
  53. }
  54. //----------------------------------------------------------------------------
  55. void ExtrudedPolyList::extrude(const Polyhedron& pt, const VectorF& vector)
  56. {
  57. // Clear state
  58. mIndexList.clear();
  59. mVertexList.clear();
  60. mPlaneList.clear();
  61. mPolyPlaneList.clear();
  62. // Determine which faces will be extruded.
  63. mExtrudedList.setSize(pt.planeList.size());
  64. for (U32 f = 0; f < pt.planeList.size(); f++)
  65. {
  66. const PlaneF& face = pt.planeList[f];
  67. ExtrudedFace& eface = mExtrudedList[f];
  68. F32 dot = mDot(face,vector);
  69. eface.active = dot > EqualEpsilon;
  70. if (eface.active)
  71. {
  72. eface.maxDistance = dot;
  73. eface.plane = face;
  74. eface.planeMask = BIT(mPlaneList.size());
  75. // Add the face as a plane to clip against.
  76. mPlaneList.increment(2);
  77. PlaneF* plane = mPlaneList.end() - 2;
  78. plane[0] = plane[1] = face;
  79. plane[0].invert();
  80. }
  81. }
  82. // Produce extruded planes for bounding and internal edges
  83. for (U32 e = 0; e < pt.edgeList.size(); e++)
  84. {
  85. Polyhedron::Edge const& edge = pt.edgeList[e];
  86. ExtrudedFace& ef1 = mExtrudedList[edge.face[0]];
  87. ExtrudedFace& ef2 = mExtrudedList[edge.face[1]];
  88. if (ef1.active || ef2.active)
  89. {
  90. // Assumes that the edge points are clockwise
  91. // for face[0].
  92. const Point3F& p1 = pt.pointList[edge.vertex[1]];
  93. const Point3F &p2 = pt.pointList[edge.vertex[0]];
  94. Point3F p3 = p2 + vector;
  95. mPlaneList.increment(2);
  96. PlaneF* plane = mPlaneList.end() - 2;
  97. plane[0].set(p3,p2,p1);
  98. plane[1] = plane[0];
  99. plane[1].invert();
  100. U32 pmask = BIT(mPlaneList.size()-2);
  101. ef1.planeMask |= pmask;
  102. ef2.planeMask |= pmask << 1;
  103. }
  104. }
  105. }
  106. //----------------------------------------------------------------------------
  107. void ExtrudedPolyList::setCollisionList(CollisionList* info)
  108. {
  109. mCollisionList = info;
  110. mCollisionList->clear();
  111. mCollisionList->setTime( 2.0f );
  112. }
  113. //----------------------------------------------------------------------------
  114. void ExtrudedPolyList::adjustCollisionTime()
  115. {
  116. if( !mCollisionList->getCount() )
  117. return;
  118. mCollisionList->setTime( mClampF( mCollisionList->getTime(), 0.f, 1.f ) );
  119. }
  120. //----------------------------------------------------------------------------
  121. U32 ExtrudedPolyList::addPoint(const Point3F& p)
  122. {
  123. mVertexList.increment();
  124. Vertex& v = mVertexList.last();
  125. v.point.x = p.x * mScale.x;
  126. v.point.y = p.y * mScale.y;
  127. v.point.z = p.z * mScale.z;
  128. mMatrix.mulP(v.point);
  129. // Build the plane mask, planes come in pairs
  130. v.mask = 0;
  131. for (U32 i = 0; i < mPlaneList.size(); i ++)
  132. if (mPlaneList[i].distToPlane(v.point) >= 0.f)
  133. v.mask |= BIT(i);
  134. return mVertexList.size() - 1;
  135. }
  136. U32 ExtrudedPolyList::addPlane(const PlaneF& plane)
  137. {
  138. mPolyPlaneList.increment();
  139. mPlaneTransformer.transform(plane, mPolyPlaneList.last());
  140. return mPolyPlaneList.size() - 1;
  141. }
  142. //----------------------------------------------------------------------------
  143. void ExtrudedPolyList::begin(BaseMatInstance* material, U32 /*surfaceKey*/)
  144. {
  145. mPoly.object = mCurrObject;
  146. mPoly.material = material;
  147. mIndexList.clear();
  148. }
  149. void ExtrudedPolyList::plane(U32 v1, U32 v2, U32 v3)
  150. {
  151. mPoly.plane.set(mVertexList[v1].point,
  152. mVertexList[v2].point,
  153. mVertexList[v3].point);
  154. // We hope this isn't needed but we're leaving it in anyway -- BJG/EGH
  155. mPoly.plane.normalizeSafe();
  156. }
  157. void ExtrudedPolyList::plane(const PlaneF& p)
  158. {
  159. mPlaneTransformer.transform(p, mPoly.plane);
  160. }
  161. void ExtrudedPolyList::plane(const U32 index)
  162. {
  163. AssertFatal(index < mPolyPlaneList.size(), "Out of bounds index!");
  164. mPoly.plane = mPolyPlaneList[index];
  165. }
  166. const PlaneF& ExtrudedPolyList::getIndexedPlane(const U32 index)
  167. {
  168. AssertFatal(index < mPolyPlaneList.size(), "Out of bounds index!");
  169. return mPolyPlaneList[index];
  170. }
  171. void ExtrudedPolyList::vertex(U32 vi)
  172. {
  173. mIndexList.push_back(vi);
  174. }
  175. void ExtrudedPolyList::end()
  176. {
  177. // Anything facing away from the mVelocity is rejected (and also
  178. // cap to max collisions)
  179. if (mDot(mPoly.plane, mNormalVelocity) > 0.f ||
  180. mCollisionList->getCount() >= CollisionList::MaxCollisions)
  181. return;
  182. // Test the built up poly (stored in mPoly) against all our extruded
  183. // faces.
  184. U32 cFaceCount = 0;
  185. ExtrudedFace* cFace[30];
  186. bool cEdgeColl[30];
  187. ExtrudedFace* face = mExtrudedList.begin();
  188. ExtrudedFace* end = mExtrudedList.end();
  189. for (; face != end; face++)
  190. {
  191. // Skip inactive..
  192. if (!face->active)
  193. continue;
  194. // Update the dot product.
  195. face->faceDot = -mDot(face->plane,mPoly.plane);
  196. // Skip it if we're facing towards...
  197. if(face->faceDot <= 0.f)
  198. continue;
  199. // Test, and skip if colliding.
  200. if (!testPoly(*face))
  201. continue;
  202. // Note collision.
  203. cFace[cFaceCount] = face;
  204. cEdgeColl[cFaceCount++] = false;
  205. }
  206. if (!cFaceCount)
  207. {
  208. face = mExtrudedList.begin();
  209. end = mExtrudedList.end();
  210. for (; face != end; face++)
  211. {
  212. // Don't need to do dot product second time, so just check if it's
  213. // active (we already did the dot product in the previous loop).
  214. if (!face->active)
  215. continue;
  216. // Skip it if we're facing away...
  217. if(face->faceDot > 0.f)
  218. continue;
  219. // Do collision as above.
  220. if (!testPoly(*face))
  221. continue;
  222. // Note the collision.
  223. cFace[cFaceCount] = face;
  224. cEdgeColl[cFaceCount++] = true;
  225. }
  226. }
  227. // If we STILL don't have any collisions, just skip out.
  228. if (!cFaceCount)
  229. return;
  230. // Pick the best collision face based on best alignment with respective
  231. // face.
  232. face = cFace[0];
  233. bool edge = cEdgeColl[0];
  234. for (U32 f = 1; f < cFaceCount; f++)
  235. {
  236. if (cFace[f]->faceDot <= face->faceDot)
  237. continue;
  238. face = cFace[f];
  239. edge = cEdgeColl[f];
  240. }
  241. // Add it to the collision list if it's better and/or equal
  242. // to our current best.
  243. // Don't add it to the collision list if it's too far away.
  244. if (face->time > mCollisionList->getTime() + EqualEpsilon || face->time >= 1.0)
  245. return;
  246. if (face->time < mCollisionList->getTime() - EqualEpsilon)
  247. {
  248. // If this is significantly closer than before, then clear out the
  249. // list, as it's a better match than the old stuff.
  250. mCollisionList->clear();
  251. mCollisionList->setTime( face->time );
  252. mCollisionList->setMaxHeight( face->height );
  253. }
  254. else
  255. {
  256. // Otherwise, just update some book-keeping stuff.
  257. if ( face->height > mCollisionList->getMaxHeight() )
  258. mCollisionList->setMaxHeight( face->height );
  259. }
  260. // Note the collision in our collision list.
  261. Collision& collision = mCollisionList->increment();
  262. collision.point = face->point;
  263. collision.faceDot = face->faceDot;
  264. collision.face = face - mExtrudedList.begin();
  265. collision.object = mPoly.object;
  266. collision.normal = mPoly.plane;
  267. collision.material = mPoly.material;
  268. }
  269. //----------------------------------------------------------------------------
  270. bool ExtrudedPolyList::testPoly(ExtrudedFace& face)
  271. {
  272. // Build intial inside/outside plane masks
  273. U32 indexStart = 0;
  274. U32 indexEnd = mIndexList.size();
  275. U32 oVertexSize = mVertexList.size();
  276. U32 oIndexSize = mIndexList.size();
  277. U32 frontMask = 0,backMask = 0;
  278. for (U32 i = indexStart; i < indexEnd; i++)
  279. {
  280. U32 mask = mVertexList[mIndexList[i]].mask & face.planeMask;
  281. frontMask |= mask;
  282. backMask |= ~mask;
  283. }
  284. // Clip the mPoly against the planes that bound the face...
  285. // Trivial accept if all the vertices are on the backsides of
  286. // all the planes.
  287. if (frontMask)
  288. {
  289. // Trivial reject if any plane not crossed has all it's points
  290. // on the front.
  291. U32 crossMask = frontMask & backMask;
  292. if (~crossMask & frontMask)
  293. return false;
  294. // Need to do some clipping
  295. for (U32 p=0; p < mPlaneList.size(); p++)
  296. {
  297. U32 pmask = BIT(p);
  298. U32 newStart = mIndexList.size();
  299. // Only test against this plane if we have something
  300. // on both sides - otherwise skip.
  301. if (!(face.planeMask & crossMask & pmask))
  302. continue;
  303. U32 i1 = indexEnd - 1;
  304. U32 mask1 = mVertexList[mIndexList[i1]].mask;
  305. for (U32 i2 = indexStart; i2 < indexEnd; i2++)
  306. {
  307. const U32 mask2 = mVertexList[mIndexList[i2]].mask;
  308. if ((mask1 ^ mask2) & pmask)
  309. {
  310. // Clip the edge against the plane.
  311. mVertexList.increment();
  312. VectorF& v1 = mVertexList[mIndexList[i1]].point;
  313. VectorF& v2 = mVertexList[mIndexList[i2]].point;
  314. VectorF vv = v2 - v1;
  315. F32 t = -mPlaneList[p].distToPlane(v1) / mDot(mPlaneList[p],vv);
  316. mIndexList.push_back(mVertexList.size() - 1);
  317. Vertex& iv = mVertexList.last();
  318. iv.point.x = v1.x + vv.x * t;
  319. iv.point.y = v1.y + vv.y * t;
  320. iv.point.z = v1.z + vv.z * t;
  321. iv.mask = 0;
  322. // Test against the remaining planes
  323. for (U32 i = p+1; i < mPlaneList.size(); i ++)
  324. {
  325. if (mPlaneList[i].distToPlane(iv.point) > 0.f)
  326. iv.mask |= BIT(i);
  327. }
  328. }
  329. if (!(mask2 & pmask))
  330. {
  331. U32 index = mIndexList[i2];
  332. mIndexList.push_back(index);
  333. }
  334. mask1 = mask2;
  335. i1 = i2;
  336. }
  337. // Check for degenerate
  338. indexStart = newStart;
  339. indexEnd = mIndexList.size();
  340. if (mIndexList.size() - indexStart < 3)
  341. {
  342. mVertexList.setSize(oVertexSize);
  343. mIndexList.setSize(oIndexSize);
  344. return false;
  345. }
  346. }
  347. }
  348. // Find closest point on the mPoly
  349. Point3F bp(0.0f, 0.0f, 0.0f);
  350. F32 bd = 1E30f;
  351. F32 height = -1E30f;
  352. for (U32 b = indexStart; b < indexEnd; b++)
  353. {
  354. Vertex& vertex = mVertexList[mIndexList[b]];
  355. F32 dist = face.plane.distToPlane(vertex.point);
  356. if (dist <= bd)
  357. {
  358. bd = (dist < 0)? 0: dist;
  359. bp = vertex.point;
  360. }
  361. // Since we don't clip against the back plane, we'll
  362. // only include vertex heights that are within range.
  363. if (vertex.point.z > height && dist < face.maxDistance)
  364. height = vertex.point.z;
  365. }
  366. // hack for not jetting up through the cieling
  367. F32 fudge = 0.01f;
  368. F32 fudgeB = 0.2f;
  369. if(mNormalVelocity.z > 0.0)
  370. {
  371. fudge = 0.01f; //0.015;
  372. fudgeB = 0.2f;
  373. }
  374. // Do extruded points for back-off.
  375. F32 oldBd=bd;
  376. for (U32 b = indexStart; b < indexEnd; b++)
  377. {
  378. Vertex& vertex = mVertexList[mIndexList[b]];
  379. // Extrude out just a tad to make sure we don't end up getting too close to the
  380. // geometry and getting stuck - but cap it so we don't introduce error into long
  381. // sweeps.
  382. F32 dist = face.plane.distToPlane( vertex.point
  383. + Point3F(mPoly.plane) * getMin(face.maxDistance * fudgeB, fudge));
  384. if (dist <= bd)
  385. {
  386. bd = (dist < 0)? 0: dist;
  387. bp = vertex.point;
  388. }
  389. }
  390. // Remove temporary data
  391. mVertexList.setSize(oVertexSize);
  392. mIndexList.setSize(oIndexSize);
  393. // Don't add it to the collision list if it's worse then our current best.
  394. if (oldBd >= face.maxDistance)
  395. return false;
  396. // Update our info and indicate we should add to the model.
  397. F32 oldT = oldBd / face.maxDistance;
  398. F32 pushBackT = bd / face.maxDistance;
  399. if(oldT - pushBackT > 0.1)
  400. face.time = oldT - fudge;
  401. else
  402. face.time = pushBackT;
  403. face.height = height;
  404. face.point = bp;
  405. return true;
  406. }
  407. //--------------------------------------------------------------------------
  408. void ExtrudedPolyList::setVelocity(const VectorF& velocity)
  409. {
  410. mVelocity = velocity;
  411. if (velocity.isZero() == false)
  412. {
  413. mNormalVelocity = velocity;
  414. mNormalVelocity.normalize();
  415. setInterestNormal(mNormalVelocity);
  416. }
  417. else
  418. {
  419. mNormalVelocity.set(0.0f, 0.0f, 0.0f);
  420. clearInterestNormal();
  421. }
  422. }