forestCollision.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 "forest/forestCollision.h"
  24. #include "forest/forest.h"
  25. #include "forest/forestCell.h"
  26. #include "forest/ts/tsForestItemData.h"
  27. #include "ts/tsShapeInstance.h"
  28. #include "T3D/physics/physicsPlugin.h"
  29. #include "T3D/physics/physicsBody.h"
  30. #include "T3D/physics/physicsCollision.h"
  31. #include "collision/concretePolyList.h"
  32. #include "platform/profiler.h"
  33. /*
  34. bool Forest::castRay( const Point3F &start, const Point3F &end, RayInfo* info )
  35. {
  36. //if ( !mData )
  37. //return false;
  38. //return mData->castRay( start, end, outInfo );
  39. return false;
  40. }
  41. bool Forest::castRayI( const Point3F &start, const Point3F &end, ForestRayInfo *outInfo )
  42. {
  43. if ( !mData )
  44. return false;
  45. return mData->castRay( start, end, outInfo );
  46. }
  47. ScriptMethod( Forest, forestRayCast, const char*, 4, 4, "( Point3F start, Point3F end )"
  48. "Cast a ray from start to end, checking for collision against trees in this forest.\n\n"
  49. "@returns A string containing either null, if nothing was struck, or these fields:\n"
  50. " - The ID of the tree type datablock.\n"
  51. " - The tree ID (not a normal object id).\n"
  52. " - t, The time during the raycast at which the collision occured." )
  53. {
  54. Point3F start, end;
  55. dSscanf(argv[2], "%g %g %g", &start.x, &start.y, &start.z);
  56. dSscanf(argv[3], "%g %g %g", &end.x, &end.y, &end.z);
  57. static const U32 bufSize = 256;
  58. char *returnBuffer = Con::getReturnBuffer(bufSize);
  59. returnBuffer[0] = '0';
  60. returnBuffer[1] = '\0';
  61. ForestRayInfo rinfo;
  62. if ( object->castRayI( start, end, &rinfo ) )
  63. dSprintf( returnBuffer, bufSize, "%d %d %g", rinfo.item->getData()->getId(), rinfo.key, rinfo.t );
  64. return returnBuffer;
  65. }
  66. */
  67. void ForestConvex::calculateTransform( const MatrixF &worldXfrm )
  68. {
  69. mTransform = worldXfrm;
  70. return;
  71. }
  72. Box3F ForestConvex::getBoundingBox() const
  73. {
  74. // This is probably a bad idea? -- BJG
  75. return getBoundingBox( mTransform, Point3F(mScale,mScale,mScale) );
  76. }
  77. Box3F ForestConvex::getBoundingBox(const MatrixF& mat, const Point3F& scale) const
  78. {
  79. Box3F newBox = box;
  80. newBox.minExtents.convolve(scale);
  81. newBox.maxExtents.convolve(scale);
  82. mat.mul(newBox);
  83. return newBox;
  84. }
  85. Point3F ForestConvex::support(const VectorF& v) const
  86. {
  87. TSShapeInstance *si = mData->getShapeInstance();
  88. TSShape::ConvexHullAccelerator* pAccel =
  89. si->getShape()->getAccelerator(mData->getCollisionDetails()[hullId]);
  90. AssertFatal(pAccel != NULL, "Error, no accel!");
  91. F32 currMaxDP = mDot(pAccel->vertexList[0], v);
  92. U32 index = 0;
  93. for (U32 i = 1; i < pAccel->numVerts; i++)
  94. {
  95. F32 dp = mDot(pAccel->vertexList[i], v);
  96. if (dp > currMaxDP)
  97. {
  98. currMaxDP = dp;
  99. index = i;
  100. }
  101. }
  102. return pAccel->vertexList[index];
  103. }
  104. void ForestConvex::getFeatures( const MatrixF &mat, const VectorF &n, ConvexFeature *cf )
  105. {
  106. cf->material = 0;
  107. cf->mObject = mObject;
  108. TSShapeInstance *si = mData->getShapeInstance();
  109. TSShape::ConvexHullAccelerator* pAccel =
  110. si->getShape()->getAccelerator(mData->getCollisionDetails()[hullId]);
  111. AssertFatal(pAccel != NULL, "Error, no accel!");
  112. F32 currMaxDP = mDot(pAccel->vertexList[0], n);
  113. U32 index = 0;
  114. U32 i;
  115. for (i = 1; i < pAccel->numVerts; i++)
  116. {
  117. F32 dp = mDot(pAccel->vertexList[i], n);
  118. if (dp > currMaxDP)
  119. {
  120. currMaxDP = dp;
  121. index = i;
  122. }
  123. }
  124. const U8* emitString = pAccel->emitStrings[index];
  125. U32 currPos = 0;
  126. U32 numVerts = emitString[currPos++];
  127. for (i = 0; i < numVerts; i++)
  128. {
  129. cf->mVertexList.increment();
  130. index = emitString[currPos++];
  131. mat.mulP(pAccel->vertexList[index], &cf->mVertexList.last());
  132. }
  133. U32 numEdges = emitString[currPos++];
  134. for (i = 0; i < numEdges; i++)
  135. {
  136. U32 ev0 = emitString[currPos++];
  137. U32 ev1 = emitString[currPos++];
  138. cf->mEdgeList.increment();
  139. cf->mEdgeList.last().vertex[0] = ev0;
  140. cf->mEdgeList.last().vertex[1] = ev1;
  141. }
  142. U32 numFaces = emitString[currPos++];
  143. for (i = 0; i < numFaces; i++)
  144. {
  145. cf->mFaceList.increment();
  146. U32 plane = emitString[currPos++];
  147. mat.mulV(pAccel->normalList[plane], &cf->mFaceList.last().normal);
  148. for (U32 j = 0; j < 3; j++)
  149. cf->mFaceList.last().vertex[j] = emitString[currPos++];
  150. }
  151. }
  152. void ForestConvex::getPolyList(AbstractPolyList* list)
  153. {
  154. list->setTransform( &mTransform, Point3F(mScale,mScale,mScale));
  155. list->setObject(mObject);
  156. TSShapeInstance *si = mData->getShapeInstance();
  157. S32 detail = mData->getCollisionDetails()[hullId];
  158. si->animate(detail);
  159. si->buildPolyList(list, detail);
  160. }
  161. void Forest::buildConvex( const Box3F &box, Convex *convex )
  162. {
  163. mConvexList->collectGarbage();
  164. // Get all ForestItem(s) within the box.
  165. Vector<ForestItem> trees;
  166. mData->getItems( box, &trees );
  167. if ( trees.empty() )
  168. return;
  169. for ( U32 i = 0; i < trees.size(); i++ )
  170. {
  171. const ForestItem &forestItem = trees[i];
  172. Box3F realBox = box;
  173. mWorldToObj.mul( realBox );
  174. realBox.minExtents.convolveInverse( mObjScale );
  175. realBox.maxExtents.convolveInverse( mObjScale );
  176. // JCF: is this really necessary if we already got this ForestItem
  177. // as a result from getItems?
  178. if ( realBox.isOverlapped( getObjBox() ) == false )
  179. continue;
  180. TSForestItemData *data = (TSForestItemData*)forestItem.getData();
  181. // Find CollisionDetail(s) that are defined...
  182. const Vector<S32> &details = data->getCollisionDetails();
  183. for ( U32 j = 0; j < details.size(); j++ )
  184. {
  185. // JCFHACK: need to fix this if we want this to work with speedtree
  186. // or other cases in which we don't have a TSForestItemData.
  187. // Most likely via preventing this method and other torque collision
  188. // specific stuff from ever getting called.
  189. if ( details[j] == -1 )
  190. continue;
  191. // See if this convex exists in the working set already...
  192. Convex* cc = 0;
  193. CollisionWorkingList& wl = convex->getWorkingList();
  194. for ( CollisionWorkingList* itr = wl.wLink.mNext; itr != &wl; itr = itr->wLink.mNext )
  195. {
  196. if ( itr->mConvex->getType() == ForestConvexType )
  197. {
  198. ForestConvex *pConvex = static_cast<ForestConvex*>(itr->mConvex);
  199. if ( pConvex->mObject == this &&
  200. pConvex->mForestItemKey == forestItem.getKey() &&
  201. pConvex->hullId == j )
  202. {
  203. cc = itr->mConvex;
  204. break;
  205. }
  206. }
  207. }
  208. if (cc)
  209. continue;
  210. // Then we need to make one.
  211. ForestConvex *cp = new ForestConvex;
  212. mConvexList->registerObject(cp);
  213. convex->addToWorkingList(cp);
  214. cp->mObject = this;
  215. cp->mForestItemKey = forestItem.getKey();
  216. cp->mData = data;
  217. cp->mScale = forestItem.getScale();
  218. cp->hullId = j;
  219. cp->box = forestItem.getObjBox();
  220. cp->calculateTransform( forestItem.getTransform() );
  221. }
  222. }
  223. }
  224. bool Forest::buildPolyList( PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere )
  225. {
  226. if ( context == PLC_Decal )
  227. return false;
  228. // Get all ForestItem(s) within the box.
  229. Vector<ForestItem> trees;
  230. if ( mData->getItems( box, &trees ) == 0 )
  231. return false;
  232. polyList->setObject(this);
  233. bool gotPoly = false;
  234. for ( U32 i = 0; i < trees.size(); i++ )
  235. gotPoly |= trees[i].buildPolyList( polyList, box, sphere );
  236. return gotPoly;
  237. }
  238. bool ForestItem::buildPolyList( AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere ) const
  239. {
  240. TSForestItemData *data = (TSForestItemData*)mDataBlock;
  241. bool ret = false;
  242. MatrixF xfm = getTransform();
  243. polyList->setTransform( &xfm, Point3F(mScale,mScale,mScale) );
  244. TSShapeInstance *si = data->getShapeInstance();
  245. const Vector<S32> &details = data->getCollisionDetails();
  246. S32 detail;
  247. for (U32 i = 0; i < details.size(); i++ )
  248. {
  249. detail = details[i];
  250. if (detail != -1)
  251. ret |= si->buildPolyList( polyList, detail );
  252. }
  253. return ret;
  254. }
  255. bool Forest::collideBox( const Point3F &start, const Point3F &end, RayInfo *outInfo )
  256. {
  257. //Con::warnf( "Forest::collideBox() - not yet implemented!" );
  258. return Parent::collideBox( start, end, outInfo );
  259. }
  260. bool Forest::castRay( const Point3F &start, const Point3F &end, RayInfo *outInfo )
  261. {
  262. return castRayBase( start, end, outInfo, false );
  263. }
  264. bool Forest::castRayRendered( const Point3F &start, const Point3F &end, RayInfo *outInfo )
  265. {
  266. return castRayBase( start, end, outInfo, true );
  267. }
  268. bool Forest::castRayBase( const Point3F &start, const Point3F &end, RayInfo *outInfo, bool rendered )
  269. {
  270. if ( !getObjBox().collideLine( start, end ) )
  271. return false;
  272. if ( mData->castRay( start, end, outInfo, rendered ) )
  273. {
  274. outInfo->object = this;
  275. return true;
  276. }
  277. return false;
  278. }
  279. void Forest::updateCollision()
  280. {
  281. if ( !mData )
  282. return;
  283. mData->buildPhysicsRep( this );
  284. // Make the assumption that if collision needs
  285. // to be updated that the zoning probably changed too.
  286. if ( isClientObject() )
  287. mZoningDirty = true;
  288. }
  289. bool ForestData::castRay( const Point3F &start, const Point3F &end, RayInfo *outInfo, bool rendered ) const
  290. {
  291. RayInfo shortest;
  292. shortest.userData = outInfo->userData;
  293. shortest.t = F32_MAX;
  294. BucketTable::ConstIterator iter = mBuckets.begin();
  295. for (; iter != mBuckets.end(); ++iter)
  296. {
  297. if ( iter->value->castRay( start, end, outInfo, rendered ) )
  298. {
  299. if ( outInfo->t < shortest.t )
  300. shortest = *outInfo;
  301. }
  302. }
  303. *outInfo = shortest;
  304. return ( outInfo->t < F32_MAX );
  305. }
  306. bool ForestCell::castRay( const Point3F &start, const Point3F &end, RayInfo *outInfo, bool rendered ) const
  307. {
  308. // JCF: start/end are in object space of the Forest but mBounds
  309. // is in world space... Luckily Forest is global bounds and always at the origin
  310. // with no scale.
  311. if ( !mBounds.collideLine( start, end ) )
  312. return false;
  313. RayInfo shortest;
  314. shortest.userData = outInfo->userData;
  315. shortest.t = F32_MAX;
  316. if ( !isLeaf() )
  317. {
  318. for ( U32 i=0; i < 4; i++ )
  319. {
  320. if ( mSubCells[i]->castRay( start, end, outInfo, rendered ) )
  321. {
  322. if ( outInfo->t < shortest.t )
  323. shortest = *outInfo;
  324. }
  325. }
  326. }
  327. else
  328. {
  329. for ( U32 i = 0; i < mItems.size(); i++ )
  330. {
  331. if ( mItems[i].castRay( start, end, outInfo, rendered ) )
  332. {
  333. if ( outInfo->t < shortest.t )
  334. shortest = *outInfo;
  335. }
  336. }
  337. }
  338. *outInfo = shortest;
  339. return ( outInfo->t < F32_MAX );
  340. }
  341. bool ForestItem::castRay( const Point3F &start, const Point3F &end, RayInfo *outInfo, bool rendered ) const
  342. {
  343. if ( !mWorldBox.collideLine( start, end ) )
  344. return false;
  345. Point3F s, e;
  346. MatrixF mat = getTransform();
  347. mat.scale( Point3F(mScale) );
  348. mat.inverse();
  349. mat.mulP( start, &s );
  350. mat.mulP( end, &e );
  351. TSForestItemData *data = (TSForestItemData*)mDataBlock;
  352. TSShapeInstance *si = data->getShapeInstance();
  353. if ( !si )
  354. return false;
  355. if ( rendered )
  356. {
  357. // Always raycast against detail level zero
  358. // because it makes lots of things easier.
  359. if ( !si->castRayRendered( s, e, outInfo, 0 ) )
  360. return false;
  361. if ( outInfo->userData != NULL )
  362. *(ForestItem*)(outInfo->userData) = *this;
  363. return true;
  364. }
  365. RayInfo shortest;
  366. shortest.t = 1e8;
  367. bool gotMatch = false;
  368. S32 detail;
  369. const Vector<S32> &details = data->getLOSDetails();
  370. for (U32 i = 0; i < details.size(); i++)
  371. {
  372. detail = details[i];
  373. if (detail != -1)
  374. {
  375. //si->animate(data->mLOSDetails[i]);
  376. if ( si->castRayOpcode( detail, s, e, outInfo ) )
  377. {
  378. if (outInfo->t < shortest.t)
  379. {
  380. gotMatch = true;
  381. shortest = *outInfo;
  382. }
  383. }
  384. }
  385. }
  386. if ( !gotMatch )
  387. return false;
  388. // Copy out the shortest time...
  389. *outInfo = shortest;
  390. if ( outInfo->userData != NULL )
  391. *(ForestItem*)(outInfo->userData) = *this;
  392. return true;
  393. }