CollisionShape.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. lodLevel_(0),
  179. cachedWorldScale_(Vector3::ONE),
  180. margin_(DEFAULT_COLLISION_MARGIN),
  181. dirty_(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. ATTRIBUTE(CollisionShape, VAR_VECTOR3, "Offset Position", position_, Vector3::ZERO, AM_DEFAULT);
  196. ATTRIBUTE(CollisionShape, VAR_QUATERNION, "Offset Rotation", rotation_, 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. dirty_ = true;
  205. }
  206. void CollisionShape::ApplyAttributes()
  207. {
  208. if (dirty_)
  209. {
  210. UpdateShape();
  211. NotifyRigidBody();
  212. dirty_ = false;
  213. }
  214. }
  215. void CollisionShape::SetBox(const Vector3& size, const Vector3& position, const Quaternion& rotation)
  216. {
  217. shapeType_ = SHAPE_BOX;
  218. size_ = size;
  219. position_ = position;
  220. rotation_ = rotation;
  221. model_.Reset();
  222. UpdateShape();
  223. NotifyRigidBody();
  224. MarkNetworkUpdate();
  225. }
  226. void CollisionShape::SetSphere(float diameter, const Vector3& position, const Quaternion& rotation)
  227. {
  228. shapeType_ = SHAPE_SPHERE;
  229. size_ = Vector3(diameter, diameter, diameter);
  230. position_ = position;
  231. rotation_ = rotation;
  232. model_.Reset();
  233. UpdateShape();
  234. NotifyRigidBody();
  235. MarkNetworkUpdate();
  236. }
  237. void CollisionShape::SetCylinder(float diameter, float height, const Vector3& position, const Quaternion& rotation)
  238. {
  239. shapeType_ = SHAPE_CYLINDER;
  240. size_ = Vector3(diameter, height, diameter);
  241. position_ = position;
  242. rotation_ = rotation;
  243. model_.Reset();
  244. UpdateShape();
  245. NotifyRigidBody();
  246. MarkNetworkUpdate();
  247. }
  248. void CollisionShape::SetCapsule(float diameter, float height, const Vector3& position, const Quaternion& rotation)
  249. {
  250. shapeType_ = SHAPE_CAPSULE;
  251. size_ = Vector3(diameter, height, diameter);
  252. position_ = position;
  253. rotation_ = rotation;
  254. model_.Reset();
  255. UpdateShape();
  256. NotifyRigidBody();
  257. MarkNetworkUpdate();
  258. }
  259. void CollisionShape::SetCone(float diameter, float height, const Vector3& position, const Quaternion& rotation)
  260. {
  261. shapeType_ = SHAPE_CONE;
  262. size_ = Vector3(diameter, height, diameter);
  263. position_ = position;
  264. rotation_ = rotation;
  265. model_.Reset();
  266. UpdateShape();
  267. NotifyRigidBody();
  268. MarkNetworkUpdate();
  269. }
  270. void CollisionShape::SetTriangleMesh(Model* model, unsigned lodLevel, const Vector3& scale, const Vector3& position, const Quaternion& rotation)
  271. {
  272. if (!model)
  273. {
  274. LOGERROR("Null model, can not set triangle mesh");
  275. return;
  276. }
  277. shapeType_ = SHAPE_TRIANGLEMESH;
  278. model_ = model;
  279. lodLevel_ = lodLevel;
  280. size_ = scale;
  281. position_ = position;
  282. rotation_ = rotation;
  283. UpdateShape();
  284. NotifyRigidBody();
  285. MarkNetworkUpdate();
  286. }
  287. void CollisionShape::SetConvexHull(Model* model, unsigned lodLevel, const Vector3& scale, const Vector3& position, const Quaternion& rotation)
  288. {
  289. if (!model)
  290. {
  291. LOGERROR("Null model, can not set convex hull");
  292. return;
  293. }
  294. shapeType_ = SHAPE_CONVEXHULL;
  295. model_ = model;
  296. lodLevel_ = lodLevel;
  297. size_ = scale;
  298. position_ = position;
  299. rotation_ = rotation;
  300. UpdateShape();
  301. NotifyRigidBody();
  302. MarkNetworkUpdate();
  303. }
  304. void CollisionShape::SetShapeType(ShapeType type)
  305. {
  306. if (type != shapeType_)
  307. {
  308. shapeType_ = type;
  309. UpdateShape();
  310. NotifyRigidBody();
  311. MarkNetworkUpdate();
  312. }
  313. }
  314. void CollisionShape::SetSize(const Vector3& size)
  315. {
  316. if (size != size_)
  317. {
  318. size_ = size;
  319. UpdateShape();
  320. NotifyRigidBody();
  321. MarkNetworkUpdate();
  322. }
  323. }
  324. void CollisionShape::SetPosition(const Vector3& position)
  325. {
  326. if (position != position_)
  327. {
  328. position_ = position;
  329. NotifyRigidBody();
  330. MarkNetworkUpdate();
  331. }
  332. }
  333. void CollisionShape::SetRotation(const Quaternion& rotation)
  334. {
  335. if (rotation != rotation_)
  336. {
  337. rotation_ = rotation;
  338. NotifyRigidBody();
  339. MarkNetworkUpdate();
  340. }
  341. }
  342. void CollisionShape::SetTransform(const Vector3& position, const Quaternion& rotation)
  343. {
  344. if (position != position_ || rotation != rotation_)
  345. {
  346. position_ = position;
  347. rotation_ = rotation;
  348. NotifyRigidBody();
  349. MarkNetworkUpdate();
  350. }
  351. }
  352. void CollisionShape::SetMargin(float margin)
  353. {
  354. margin = Max(margin, 0.0f);
  355. if (margin != margin_)
  356. {
  357. if (shape_)
  358. shape_->setMargin(margin);
  359. margin_ = margin;
  360. MarkNetworkUpdate();
  361. }
  362. }
  363. void CollisionShape::SetModel(Model* model)
  364. {
  365. if (model != model_)
  366. {
  367. model_ = model;
  368. if (shapeType_ >= SHAPE_TRIANGLEMESH)
  369. {
  370. UpdateShape();
  371. NotifyRigidBody();
  372. }
  373. MarkNetworkUpdate();
  374. }
  375. }
  376. void CollisionShape::SetLodLevel(unsigned lodLevel)
  377. {
  378. if (lodLevel != lodLevel_)
  379. {
  380. lodLevel_ = lodLevel;
  381. if (shapeType_ >= SHAPE_TRIANGLEMESH)
  382. {
  383. UpdateShape();
  384. NotifyRigidBody();
  385. }
  386. MarkNetworkUpdate();
  387. }
  388. }
  389. void CollisionShape::NotifyRigidBody()
  390. {
  391. btCompoundShape* compound = GetParentCompoundShape();
  392. if (node_ && shape_ && compound)
  393. {
  394. // Remove the shape first to ensure it is not added twice
  395. compound->removeChildShape(shape_);
  396. // Then add with updated offset
  397. btTransform offset;
  398. offset.setOrigin(ToBtVector3(node_->GetWorldScale() * position_));
  399. offset.setRotation(ToBtQuaternion(rotation_));
  400. compound->addChildShape(offset, shape_);
  401. // Finally tell the rigid body to update its mass
  402. rigidBody_->UpdateMass();
  403. }
  404. }
  405. void CollisionShape::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  406. {
  407. if (debug && physicsWorld_ && shape_ && node_)
  408. {
  409. physicsWorld_->SetDebugRenderer(debug);
  410. physicsWorld_->SetDebugDepthTest(depthTest);
  411. // Use the rigid body's world transform if possible, as it may be different from the rendering transform
  412. Matrix3x4 worldTransform;
  413. RigidBody* body = GetComponent<RigidBody>();
  414. if (body)
  415. worldTransform = Matrix3x4(body->GetPosition(), body->GetRotation(), node_->GetWorldScale());
  416. else
  417. worldTransform = node_->GetWorldTransform();
  418. Vector3 worldPosition = worldTransform * position_;
  419. Quaternion worldRotation = worldTransform.Rotation() * rotation_;
  420. btDiscreteDynamicsWorld* world = physicsWorld_->GetWorld();
  421. world->debugDrawObject(btTransform(ToBtQuaternion(worldRotation), ToBtVector3(worldPosition)), shape_, btVector3(0.0f,
  422. 1.0f, 0.0f));
  423. physicsWorld_->SetDebugRenderer(0);
  424. }
  425. }
  426. void CollisionShape::SetModelAttr(ResourceRef value)
  427. {
  428. ResourceCache* cache = GetSubsystem<ResourceCache>();
  429. model_ = cache->GetResource<Model>(value.id_);
  430. dirty_ = true;
  431. MarkNetworkUpdate();
  432. }
  433. ResourceRef CollisionShape::GetModelAttr() const
  434. {
  435. return GetResourceRef(model_, Model::GetTypeStatic());
  436. }
  437. void CollisionShape::OnNodeSet(Node* node)
  438. {
  439. if (node)
  440. {
  441. Scene* scene = GetScene();
  442. if (scene)
  443. {
  444. physicsWorld_ = scene->GetComponent<PhysicsWorld>();
  445. if (physicsWorld_)
  446. physicsWorld_->AddCollisionShape(this);
  447. }
  448. node->AddListener(this);
  449. UpdateShape();
  450. NotifyRigidBody();
  451. }
  452. }
  453. void CollisionShape::OnMarkedDirty(Node* node)
  454. {
  455. Vector3 newWorldScale = node_->GetWorldScale();
  456. if (!newWorldScale.Equals(cachedWorldScale_) && shape_)
  457. {
  458. switch (shapeType_)
  459. {
  460. case SHAPE_BOX:
  461. case SHAPE_SPHERE:
  462. case SHAPE_CYLINDER:
  463. case SHAPE_CAPSULE:
  464. case SHAPE_CONE:
  465. shape_->setLocalScaling(ToBtVector3(newWorldScale));
  466. break;
  467. case SHAPE_TRIANGLEMESH:
  468. case SHAPE_CONVEXHULL:
  469. shape_->setLocalScaling(ToBtVector3(newWorldScale * size_));
  470. break;
  471. }
  472. NotifyRigidBody();
  473. cachedWorldScale_ = newWorldScale;
  474. }
  475. }
  476. btCompoundShape* CollisionShape::GetParentCompoundShape()
  477. {
  478. if (!rigidBody_)
  479. rigidBody_ = GetComponent<RigidBody>();
  480. return rigidBody_ ? rigidBody_->GetCompoundShape() : 0;
  481. }
  482. void CollisionShape::UpdateShape()
  483. {
  484. PROFILE(UpdateCollisionShape);
  485. ReleaseShape();
  486. if (!physicsWorld_)
  487. return;
  488. if (node_)
  489. {
  490. Vector3 newWorldScale = node_->GetWorldScale();
  491. switch (shapeType_)
  492. {
  493. case SHAPE_BOX:
  494. shape_ = new btBoxShape(ToBtVector3(size_ * 0.5f));
  495. shape_->setLocalScaling(ToBtVector3(newWorldScale));
  496. break;
  497. case SHAPE_SPHERE:
  498. shape_ = new btSphereShape(size_.x_ * 0.5f);
  499. shape_->setLocalScaling(ToBtVector3(newWorldScale));
  500. break;
  501. case SHAPE_CYLINDER:
  502. shape_ = new btCylinderShape(btVector3(size_.x_ * 0.5f, size_.y_ * 0.5f, size_.x_ * 0.5f));
  503. shape_->setLocalScaling(ToBtVector3(newWorldScale));
  504. break;
  505. case SHAPE_CAPSULE:
  506. shape_ = new btCapsuleShape(size_.x_ * 0.5f, Max(size_.y_ - size_.x_, 0.0f));
  507. shape_->setLocalScaling(ToBtVector3(newWorldScale));
  508. break;
  509. case SHAPE_CONE:
  510. shape_ = new btConeShape(size_.x_ * 0.5f, size_.y_);
  511. shape_->setLocalScaling(ToBtVector3(newWorldScale));
  512. break;
  513. case SHAPE_TRIANGLEMESH:
  514. size_ = size_.Abs();
  515. if (model_)
  516. {
  517. // Check the geometry cache
  518. String id = "TriMesh_" + model_->GetName() + "_" + String(lodLevel_);
  519. Map<String, SharedPtr<CollisionGeometryData> >& cache = physicsWorld_->GetGeometryCache();
  520. Map<String, SharedPtr<CollisionGeometryData> >::Iterator j = cache.Find(id);
  521. if (j != cache.End())
  522. geometry_ = j->second_;
  523. else
  524. {
  525. geometry_ = new TriangleMeshData(model_, lodLevel_);
  526. cache[id] = geometry_;
  527. }
  528. TriangleMeshData* triMesh = static_cast<TriangleMeshData*>(geometry_.Get());
  529. shape_ = new btScaledBvhTriangleMeshShape(triMesh->shape_, ToBtVector3(newWorldScale * size_));
  530. }
  531. break;
  532. case SHAPE_CONVEXHULL:
  533. size_ = size_.Abs();
  534. if (model_)
  535. {
  536. // Check the geometry cache
  537. String id = "Convex_" + model_->GetName() + "_" + String(lodLevel_);
  538. Map<String, SharedPtr<CollisionGeometryData> >& cache = physicsWorld_->GetGeometryCache();
  539. Map<String, SharedPtr<CollisionGeometryData> >::Iterator j = cache.Find(id);
  540. if (j != cache.End())
  541. geometry_ = j->second_;
  542. else
  543. {
  544. geometry_ = new ConvexData(model_, lodLevel_);
  545. cache[id] = geometry_;
  546. }
  547. ConvexData* convex = static_cast<ConvexData*>(geometry_.Get());
  548. shape_ = new btConvexHullShape((btScalar*)convex->vertexData_.Get(), convex->vertexCount_, sizeof(Vector3));
  549. shape_->setLocalScaling(ToBtVector3(newWorldScale * size_));
  550. }
  551. break;
  552. }
  553. if (shape_)
  554. shape_->setMargin(margin_);
  555. cachedWorldScale_ = newWorldScale;
  556. }
  557. if (physicsWorld_)
  558. physicsWorld_->CleanupGeometryCache();
  559. }
  560. void CollisionShape::ReleaseShape()
  561. {
  562. btCompoundShape* compound = GetParentCompoundShape();
  563. if (shape_ && compound)
  564. {
  565. compound->removeChildShape(shape_);
  566. rigidBody_->UpdateMass();
  567. }
  568. delete shape_;
  569. shape_ = 0;
  570. geometry_.Reset();
  571. if (physicsWorld_)
  572. physicsWorld_->CleanupGeometryCache();
  573. }