CollisionShape.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "CollisionShape.h"
  25. #include "Context.h"
  26. #include "DebugRenderer.h"
  27. #include "Geometry.h"
  28. #include "Log.h"
  29. #include "Model.h"
  30. #include "PhysicsUtils.h"
  31. #include "PhysicsWorld.h"
  32. #include "Profiler.h"
  33. #include "ResourceCache.h"
  34. #include "RigidBody.h"
  35. #include "Scene.h"
  36. #include <BulletCollision/CollisionShapes/btBoxShape.h>
  37. #include <BulletCollision/CollisionShapes/btCapsuleShape.h>
  38. #include <BulletCollision/CollisionShapes/btCompoundShape.h>
  39. #include <BulletCollision/CollisionShapes/btConeShape.h>
  40. #include <BulletCollision/CollisionShapes/btConvexHullShape.h>
  41. #include <BulletCollision/CollisionShapes/btCylinderShape.h>
  42. #include <BulletCollision/CollisionShapes/btScaledBvhTriangleMeshShape.h>
  43. #include <BulletCollision/CollisionShapes/btSphereShape.h>
  44. #include <BulletCollision/CollisionShapes/btTriangleMesh.h>
  45. #include <BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h>
  46. #include <hull.h>
  47. static const float DEFAULT_COLLISION_MARGIN = 0.04f;
  48. static const String typeNames[] =
  49. {
  50. "Box",
  51. "Sphere",
  52. "Cylinder",
  53. "Capsule",
  54. "Cone",
  55. "TriangleMesh",
  56. "ConvexHull",
  57. ""
  58. };
  59. TriangleMeshData::TriangleMeshData(Model* model, unsigned lodLevel) :
  60. meshData_(0),
  61. shape_(0)
  62. {
  63. modelName_ = model->GetName();
  64. meshData_ = new btTriangleMesh();
  65. const Vector<Vector<SharedPtr<Geometry> > >& geometries = model->GetGeometries();
  66. for (unsigned i = 0; i < geometries.Size(); ++i)
  67. {
  68. unsigned subGeometryLodLevel = lodLevel;
  69. if (subGeometryLodLevel >= geometries[i].Size())
  70. subGeometryLodLevel = geometries[i].Size() - 1;
  71. Geometry* geom = geometries[i][subGeometryLodLevel];
  72. if (!geom)
  73. continue;
  74. const unsigned char* vertexData;
  75. const unsigned char* indexData;
  76. unsigned vertexSize;
  77. unsigned indexSize;
  78. geom->GetRawData(vertexData, vertexSize, indexData, indexSize);
  79. if (!vertexData || !indexData)
  80. continue;
  81. unsigned indexStart = geom->GetIndexStart();
  82. unsigned indexCount = geom->GetIndexCount();
  83. // 16-bit indices
  84. if (indexSize == sizeof(unsigned short))
  85. {
  86. const unsigned short* indices = (const unsigned short*)indexData;
  87. for (unsigned j = indexStart; j < indexStart + indexCount; j += 3)
  88. {
  89. const Vector3& v0 = *((const Vector3*)(&vertexData[indices[j] * vertexSize]));
  90. const Vector3& v1 = *((const Vector3*)(&vertexData[indices[j + 1] * vertexSize]));
  91. const Vector3& v2 = *((const Vector3*)(&vertexData[indices[j + 2] * vertexSize]));
  92. meshData_->addTriangle(ToBtVector3(v0), ToBtVector3(v1), ToBtVector3(v2), true);
  93. }
  94. }
  95. // 32-bit indices
  96. else
  97. {
  98. const unsigned* indices = (const unsigned*)indexData;
  99. for (unsigned j = indexStart; j < indexStart + indexCount; j += 3)
  100. {
  101. const Vector3& v0 = *((const Vector3*)(&vertexData[indices[j] * vertexSize]));
  102. const Vector3& v1 = *((const Vector3*)(&vertexData[indices[j + 1] * vertexSize]));
  103. const Vector3& v2 = *((const Vector3*)(&vertexData[indices[j + 2] * vertexSize]));
  104. meshData_->addTriangle(ToBtVector3(v0), ToBtVector3(v1), ToBtVector3(v2), true);
  105. }
  106. }
  107. }
  108. shape_ = new btBvhTriangleMeshShape(meshData_, true, true);
  109. }
  110. TriangleMeshData::~TriangleMeshData()
  111. {
  112. delete shape_;
  113. shape_ = 0;
  114. delete meshData_;
  115. meshData_ = 0;
  116. }
  117. ConvexData::ConvexData(Model* model, unsigned lodLevel)
  118. {
  119. modelName_ = model->GetName();
  120. const Vector<Vector<SharedPtr<Geometry> > >& geometries = model->GetGeometries();
  121. PODVector<Vector3> originalVertices;
  122. for (unsigned i = 0; i < geometries.Size(); ++i)
  123. {
  124. unsigned subGeometryLodLevel = lodLevel;
  125. if (subGeometryLodLevel >= geometries[i].Size())
  126. subGeometryLodLevel = geometries[i].Size() - 1;
  127. Geometry* geom = geometries[i][subGeometryLodLevel];
  128. if (!geom)
  129. continue;
  130. const unsigned char* vertexData;
  131. const unsigned char* indexData;
  132. unsigned vertexSize;
  133. unsigned indexSize;
  134. geom->GetRawData(vertexData, vertexSize, indexData, indexSize);
  135. if (!vertexData || !indexData)
  136. continue;
  137. unsigned vertexStart = geom->GetVertexStart();
  138. unsigned vertexCount = geom->GetVertexCount();
  139. // Copy vertex data
  140. for (unsigned j = 0; j < vertexCount; ++j)
  141. {
  142. const Vector3& v = *((const Vector3*)(&vertexData[(vertexStart + j) * vertexSize]));
  143. originalVertices.Push(v);
  144. }
  145. }
  146. if (originalVertices.Size())
  147. {
  148. // Build the convex hull from the raw geometry
  149. StanHull::HullDesc desc;
  150. desc.SetHullFlag(StanHull::QF_TRIANGLES);
  151. desc.mVcount = originalVertices.Size();
  152. desc.mVertices = originalVertices[0].Data();
  153. desc.mVertexStride = 3 * sizeof(float);
  154. desc.mSkinWidth = 0.0f;
  155. StanHull::HullLibrary lib;
  156. StanHull::HullResult result;
  157. lib.CreateConvexHull(desc, result);
  158. vertexCount_ = result.mNumOutputVertices;
  159. vertexData_ = new Vector3[vertexCount_];
  160. // Copy vertex data
  161. memcpy(vertexData_.Get(), result.mOutputVertices, vertexCount_ * sizeof(Vector3));
  162. lib.ReleaseResult(result);
  163. }
  164. else
  165. vertexCount_ = 0;
  166. }
  167. ConvexData::~ConvexData()
  168. {
  169. }
  170. OBJECTTYPESTATIC(CollisionShape);
  171. CollisionShape::CollisionShape(Context* context) :
  172. Component(context),
  173. shape_(0),
  174. shapeType_(SHAPE_BOX),
  175. position_(Vector3::ZERO),
  176. rotation_(Quaternion::IDENTITY),
  177. size_(Vector3::ONE),
  178. cachedWorldScale_(Vector3::ONE),
  179. lodLevel_(0),
  180. margin_(DEFAULT_COLLISION_MARGIN),
  181. recreateShape_(false)
  182. {
  183. }
  184. CollisionShape::~CollisionShape()
  185. {
  186. ReleaseShape();
  187. if (physicsWorld_)
  188. physicsWorld_->RemoveCollisionShape(this);
  189. }
  190. void CollisionShape::RegisterObject(Context* context)
  191. {
  192. context->RegisterFactory<CollisionShape>();
  193. ENUM_ATTRIBUTE(CollisionShape, "Shape Type", shapeType_, typeNames, SHAPE_BOX, AM_DEFAULT);
  194. ATTRIBUTE(CollisionShape, VAR_VECTOR3, "Size", size_, Vector3::ONE, AM_DEFAULT);
  195. REF_ACCESSOR_ATTRIBUTE(CollisionShape, VAR_VECTOR3, "Offset Position", GetPosition, SetPosition, Vector3, Vector3::ZERO, AM_DEFAULT);
  196. REF_ACCESSOR_ATTRIBUTE(CollisionShape, VAR_QUATERNION, "Offset Rotation", GetRotation, SetRotation, Quaternion, Quaternion::IDENTITY, AM_DEFAULT);
  197. ACCESSOR_ATTRIBUTE(CollisionShape, VAR_RESOURCEREF, "Model", GetModelAttr, SetModelAttr, ResourceRef, ResourceRef(Model::GetTypeStatic()), AM_DEFAULT);
  198. ATTRIBUTE(CollisionShape, VAR_INT, "LOD Level", lodLevel_, 0, AM_DEFAULT);
  199. ATTRIBUTE(CollisionShape, VAR_FLOAT, "Collision Margin", margin_, DEFAULT_COLLISION_MARGIN, AM_DEFAULT);
  200. }
  201. void CollisionShape::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  202. {
  203. Component::OnSetAttribute(attr, src);
  204. // Change of any non-accessor attribute requires recreation of the collision shape
  205. if (!attr.accessor_)
  206. recreateShape_ = true;
  207. }
  208. void CollisionShape::ApplyAttributes()
  209. {
  210. if (recreateShape_)
  211. {
  212. UpdateShape();
  213. NotifyRigidBody();
  214. recreateShape_ = false;
  215. }
  216. }
  217. void CollisionShape::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  218. {
  219. if (debug && physicsWorld_ && shape_ && node_)
  220. {
  221. physicsWorld_->SetDebugRenderer(debug);
  222. physicsWorld_->SetDebugDepthTest(depthTest);
  223. // Use the rigid body's world transform if possible, as it may be different from the rendering transform
  224. Matrix3x4 worldTransform;
  225. RigidBody* body = GetComponent<RigidBody>();
  226. if (body)
  227. worldTransform = Matrix3x4(body->GetPosition(), body->GetRotation(), node_->GetWorldScale());
  228. else
  229. worldTransform = node_->GetWorldTransform();
  230. Vector3 worldPosition = worldTransform * position_;
  231. Quaternion worldRotation = worldTransform.Rotation() * rotation_;
  232. btDiscreteDynamicsWorld* world = physicsWorld_->GetWorld();
  233. world->debugDrawObject(btTransform(ToBtQuaternion(worldRotation), ToBtVector3(worldPosition)), shape_, btVector3(0.0f,
  234. 1.0f, 0.0f));
  235. physicsWorld_->SetDebugRenderer(0);
  236. }
  237. }
  238. void CollisionShape::SetBox(const Vector3& size, const Vector3& position, const Quaternion& rotation)
  239. {
  240. shapeType_ = SHAPE_BOX;
  241. size_ = size;
  242. position_ = position;
  243. rotation_ = rotation;
  244. model_.Reset();
  245. UpdateShape();
  246. NotifyRigidBody();
  247. MarkNetworkUpdate();
  248. }
  249. void CollisionShape::SetSphere(float diameter, const Vector3& position, const Quaternion& rotation)
  250. {
  251. shapeType_ = SHAPE_SPHERE;
  252. size_ = Vector3(diameter, diameter, diameter);
  253. position_ = position;
  254. rotation_ = rotation;
  255. model_.Reset();
  256. UpdateShape();
  257. NotifyRigidBody();
  258. MarkNetworkUpdate();
  259. }
  260. void CollisionShape::SetCylinder(float diameter, float height, const Vector3& position, const Quaternion& rotation)
  261. {
  262. shapeType_ = SHAPE_CYLINDER;
  263. size_ = Vector3(diameter, height, diameter);
  264. position_ = position;
  265. rotation_ = rotation;
  266. model_.Reset();
  267. UpdateShape();
  268. NotifyRigidBody();
  269. MarkNetworkUpdate();
  270. }
  271. void CollisionShape::SetCapsule(float diameter, float height, const Vector3& position, const Quaternion& rotation)
  272. {
  273. shapeType_ = SHAPE_CAPSULE;
  274. size_ = Vector3(diameter, height, diameter);
  275. position_ = position;
  276. rotation_ = rotation;
  277. model_.Reset();
  278. UpdateShape();
  279. NotifyRigidBody();
  280. MarkNetworkUpdate();
  281. }
  282. void CollisionShape::SetCone(float diameter, float height, const Vector3& position, const Quaternion& rotation)
  283. {
  284. shapeType_ = SHAPE_CONE;
  285. size_ = Vector3(diameter, height, diameter);
  286. position_ = position;
  287. rotation_ = rotation;
  288. model_.Reset();
  289. UpdateShape();
  290. NotifyRigidBody();
  291. MarkNetworkUpdate();
  292. }
  293. void CollisionShape::SetTriangleMesh(Model* model, unsigned lodLevel, const Vector3& scale, const Vector3& position, const Quaternion& rotation)
  294. {
  295. if (!model)
  296. {
  297. LOGERROR("Null model, can not set triangle mesh");
  298. return;
  299. }
  300. shapeType_ = SHAPE_TRIANGLEMESH;
  301. model_ = model;
  302. lodLevel_ = lodLevel;
  303. size_ = scale;
  304. position_ = position;
  305. rotation_ = rotation;
  306. UpdateShape();
  307. NotifyRigidBody();
  308. MarkNetworkUpdate();
  309. }
  310. void CollisionShape::SetConvexHull(Model* model, unsigned lodLevel, const Vector3& scale, const Vector3& position, const Quaternion& rotation)
  311. {
  312. if (!model)
  313. {
  314. LOGERROR("Null model, can not set convex hull");
  315. return;
  316. }
  317. shapeType_ = SHAPE_CONVEXHULL;
  318. model_ = model;
  319. lodLevel_ = lodLevel;
  320. size_ = scale;
  321. position_ = position;
  322. rotation_ = rotation;
  323. UpdateShape();
  324. NotifyRigidBody();
  325. MarkNetworkUpdate();
  326. }
  327. void CollisionShape::SetShapeType(ShapeType type)
  328. {
  329. if (type != shapeType_)
  330. {
  331. shapeType_ = type;
  332. UpdateShape();
  333. NotifyRigidBody();
  334. MarkNetworkUpdate();
  335. }
  336. }
  337. void CollisionShape::SetSize(const Vector3& size)
  338. {
  339. if (size != size_)
  340. {
  341. size_ = size;
  342. UpdateShape();
  343. NotifyRigidBody();
  344. MarkNetworkUpdate();
  345. }
  346. }
  347. void CollisionShape::SetPosition(const Vector3& position)
  348. {
  349. if (position != position_)
  350. {
  351. position_ = position;
  352. NotifyRigidBody();
  353. MarkNetworkUpdate();
  354. }
  355. }
  356. void CollisionShape::SetRotation(const Quaternion& rotation)
  357. {
  358. if (rotation != rotation_)
  359. {
  360. rotation_ = rotation;
  361. NotifyRigidBody();
  362. MarkNetworkUpdate();
  363. }
  364. }
  365. void CollisionShape::SetTransform(const Vector3& position, const Quaternion& rotation)
  366. {
  367. if (position != position_ || rotation != rotation_)
  368. {
  369. position_ = position;
  370. rotation_ = rotation;
  371. NotifyRigidBody();
  372. MarkNetworkUpdate();
  373. }
  374. }
  375. void CollisionShape::SetMargin(float margin)
  376. {
  377. margin = Max(margin, 0.0f);
  378. if (margin != margin_)
  379. {
  380. if (shape_)
  381. shape_->setMargin(margin);
  382. margin_ = margin;
  383. MarkNetworkUpdate();
  384. }
  385. }
  386. void CollisionShape::SetModel(Model* model)
  387. {
  388. if (model != model_)
  389. {
  390. model_ = model;
  391. if (shapeType_ >= SHAPE_TRIANGLEMESH)
  392. {
  393. UpdateShape();
  394. NotifyRigidBody();
  395. }
  396. MarkNetworkUpdate();
  397. }
  398. }
  399. void CollisionShape::SetLodLevel(unsigned lodLevel)
  400. {
  401. if (lodLevel != lodLevel_)
  402. {
  403. lodLevel_ = lodLevel;
  404. if (shapeType_ >= SHAPE_TRIANGLEMESH)
  405. {
  406. UpdateShape();
  407. NotifyRigidBody();
  408. }
  409. MarkNetworkUpdate();
  410. }
  411. }
  412. void CollisionShape::NotifyRigidBody()
  413. {
  414. btCompoundShape* compound = GetParentCompoundShape();
  415. if (node_ && shape_ && compound)
  416. {
  417. // Remove the shape first to ensure it is not added twice
  418. compound->removeChildShape(shape_);
  419. // Then add with updated offset
  420. btTransform offset;
  421. offset.setOrigin(ToBtVector3(node_->GetWorldScale() * position_));
  422. offset.setRotation(ToBtQuaternion(rotation_));
  423. compound->addChildShape(offset, shape_);
  424. // Finally tell the rigid body to update its mass
  425. rigidBody_->UpdateMass();
  426. }
  427. }
  428. void CollisionShape::SetModelAttr(ResourceRef value)
  429. {
  430. ResourceCache* cache = GetSubsystem<ResourceCache>();
  431. model_ = cache->GetResource<Model>(value.id_);
  432. recreateShape_ = true;
  433. MarkNetworkUpdate();
  434. }
  435. ResourceRef CollisionShape::GetModelAttr() const
  436. {
  437. return GetResourceRef(model_, Model::GetTypeStatic());
  438. }
  439. void CollisionShape::ReleaseShape()
  440. {
  441. btCompoundShape* compound = GetParentCompoundShape();
  442. if (shape_ && compound)
  443. {
  444. compound->removeChildShape(shape_);
  445. rigidBody_->UpdateMass();
  446. }
  447. delete shape_;
  448. shape_ = 0;
  449. geometry_.Reset();
  450. if (physicsWorld_)
  451. physicsWorld_->CleanupGeometryCache();
  452. }
  453. void CollisionShape::OnNodeSet(Node* node)
  454. {
  455. if (node)
  456. {
  457. Scene* scene = GetScene();
  458. if (scene)
  459. {
  460. physicsWorld_ = scene->GetComponent<PhysicsWorld>();
  461. if (physicsWorld_)
  462. physicsWorld_->AddCollisionShape(this);
  463. else
  464. LOGERROR("No physics world component in scene, can not create collision shape");
  465. }
  466. node->AddListener(this);
  467. UpdateShape();
  468. NotifyRigidBody();
  469. }
  470. }
  471. void CollisionShape::OnMarkedDirty(Node* node)
  472. {
  473. Vector3 newWorldScale = node_->GetWorldScale();
  474. if (!newWorldScale.Equals(cachedWorldScale_) && shape_)
  475. {
  476. switch (shapeType_)
  477. {
  478. case SHAPE_BOX:
  479. case SHAPE_SPHERE:
  480. case SHAPE_CYLINDER:
  481. case SHAPE_CAPSULE:
  482. case SHAPE_CONE:
  483. shape_->setLocalScaling(ToBtVector3(newWorldScale));
  484. break;
  485. case SHAPE_TRIANGLEMESH:
  486. case SHAPE_CONVEXHULL:
  487. shape_->setLocalScaling(ToBtVector3(newWorldScale * size_));
  488. break;
  489. }
  490. NotifyRigidBody();
  491. cachedWorldScale_ = newWorldScale;
  492. }
  493. }
  494. btCompoundShape* CollisionShape::GetParentCompoundShape()
  495. {
  496. if (!rigidBody_)
  497. rigidBody_ = GetComponent<RigidBody>();
  498. return rigidBody_ ? rigidBody_->GetCompoundShape() : 0;
  499. }
  500. void CollisionShape::UpdateShape()
  501. {
  502. PROFILE(UpdateCollisionShape);
  503. ReleaseShape();
  504. if (!physicsWorld_)
  505. return;
  506. if (node_)
  507. {
  508. Vector3 newWorldScale = node_->GetWorldScale();
  509. switch (shapeType_)
  510. {
  511. case SHAPE_BOX:
  512. shape_ = new btBoxShape(ToBtVector3(size_ * 0.5f));
  513. shape_->setLocalScaling(ToBtVector3(newWorldScale));
  514. break;
  515. case SHAPE_SPHERE:
  516. shape_ = new btSphereShape(size_.x_ * 0.5f);
  517. shape_->setLocalScaling(ToBtVector3(newWorldScale));
  518. break;
  519. case SHAPE_CYLINDER:
  520. shape_ = new btCylinderShape(btVector3(size_.x_ * 0.5f, size_.y_ * 0.5f, size_.x_ * 0.5f));
  521. shape_->setLocalScaling(ToBtVector3(newWorldScale));
  522. break;
  523. case SHAPE_CAPSULE:
  524. shape_ = new btCapsuleShape(size_.x_ * 0.5f, Max(size_.y_ - size_.x_, 0.0f));
  525. shape_->setLocalScaling(ToBtVector3(newWorldScale));
  526. break;
  527. case SHAPE_CONE:
  528. shape_ = new btConeShape(size_.x_ * 0.5f, size_.y_);
  529. shape_->setLocalScaling(ToBtVector3(newWorldScale));
  530. break;
  531. case SHAPE_TRIANGLEMESH:
  532. size_ = size_.Abs();
  533. if (model_)
  534. {
  535. // Check the geometry cache
  536. String id = "TriMesh_" + model_->GetName() + "_" + String(lodLevel_);
  537. Map<String, SharedPtr<CollisionGeometryData> >& cache = physicsWorld_->GetGeometryCache();
  538. Map<String, SharedPtr<CollisionGeometryData> >::Iterator j = cache.Find(id);
  539. if (j != cache.End())
  540. geometry_ = j->second_;
  541. else
  542. {
  543. geometry_ = new TriangleMeshData(model_, lodLevel_);
  544. cache[id] = geometry_;
  545. }
  546. TriangleMeshData* triMesh = static_cast<TriangleMeshData*>(geometry_.Get());
  547. shape_ = new btScaledBvhTriangleMeshShape(triMesh->shape_, ToBtVector3(newWorldScale * size_));
  548. }
  549. break;
  550. case SHAPE_CONVEXHULL:
  551. size_ = size_.Abs();
  552. if (model_)
  553. {
  554. // Check the geometry cache
  555. String id = "Convex_" + model_->GetName() + "_" + String(lodLevel_);
  556. Map<String, SharedPtr<CollisionGeometryData> >& cache = physicsWorld_->GetGeometryCache();
  557. Map<String, SharedPtr<CollisionGeometryData> >::Iterator j = cache.Find(id);
  558. if (j != cache.End())
  559. geometry_ = j->second_;
  560. else
  561. {
  562. geometry_ = new ConvexData(model_, lodLevel_);
  563. cache[id] = geometry_;
  564. }
  565. ConvexData* convex = static_cast<ConvexData*>(geometry_.Get());
  566. shape_ = new btConvexHullShape((btScalar*)convex->vertexData_.Get(), convex->vertexCount_, sizeof(Vector3));
  567. shape_->setLocalScaling(ToBtVector3(newWorldScale * size_));
  568. }
  569. break;
  570. }
  571. if (shape_)
  572. {
  573. shape_->setUserPointer(this);
  574. shape_->setMargin(margin_);
  575. }
  576. cachedWorldScale_ = newWorldScale;
  577. }
  578. if (physicsWorld_)
  579. physicsWorld_->CleanupGeometryCache();
  580. }