2
0

extrudedPolyList.cpp 15 KB

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