CollisionShape.cpp 20 KB

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