CollisionShape.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Core/Profiler.h"
  6. #include "../Graphics/CustomGeometry.h"
  7. #include "../Graphics/DebugRenderer.h"
  8. #include "../Graphics/DrawableEvents.h"
  9. #include "../Graphics/Geometry.h"
  10. #include "../Graphics/Model.h"
  11. #include "../Graphics/Terrain.h"
  12. #include "../GraphicsAPI/IndexBuffer.h"
  13. #include "../GraphicsAPI/VertexBuffer.h"
  14. #include "../IO/Log.h"
  15. #include "../Physics/CollisionShape.h"
  16. #include "../Physics/PhysicsUtils.h"
  17. #include "../Physics/PhysicsWorld.h"
  18. #include "../Physics/RigidBody.h"
  19. #include "../Resource/ResourceCache.h"
  20. #include "../Resource/ResourceEvents.h"
  21. #include "../Scene/Scene.h"
  22. #include <Bullet/BulletCollision/CollisionDispatch/btInternalEdgeUtility.h>
  23. #include <Bullet/BulletCollision/CollisionShapes/btBoxShape.h>
  24. #include <Bullet/BulletCollision/CollisionShapes/btCapsuleShape.h>
  25. #include <Bullet/BulletCollision/CollisionShapes/btCompoundShape.h>
  26. #include <Bullet/BulletCollision/CollisionShapes/btConeShape.h>
  27. #include <Bullet/BulletCollision/CollisionShapes/btConvexHullShape.h>
  28. #include <Bullet/BulletCollision/CollisionShapes/btCylinderShape.h>
  29. #include <Bullet/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h>
  30. #include <Bullet/BulletCollision/CollisionShapes/btScaledBvhTriangleMeshShape.h>
  31. #include <Bullet/BulletCollision/CollisionShapes/btSphereShape.h>
  32. #include <Bullet/BulletCollision/CollisionShapes/btTriangleIndexVertexArray.h>
  33. #include <Bullet/BulletCollision/CollisionShapes/btStaticPlaneShape.h>
  34. #include <Bullet/BulletCollision/Gimpact/btGImpactShape.h>
  35. #include <Bullet/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h>
  36. #include <StanHull/hull.h>
  37. using namespace std;
  38. namespace Urho3D
  39. {
  40. static const float DEFAULT_COLLISION_MARGIN = 0.04f;
  41. static const unsigned QUANTIZE_MAX_TRIANGLES = 1000000;
  42. static const btVector3 WHITE(1.0f, 1.0f, 1.0f);
  43. static const btVector3 GREEN(0.0f, 1.0f, 0.0f);
  44. static const char* typeNames[] =
  45. {
  46. "Box",
  47. "Sphere",
  48. "StaticPlane",
  49. "Cylinder",
  50. "Capsule",
  51. "Cone",
  52. "TriangleMesh",
  53. "ConvexHull",
  54. "Terrain",
  55. "GImpactMesh",
  56. nullptr
  57. };
  58. extern const char* PHYSICS_CATEGORY;
  59. class TriangleMeshInterface : public btTriangleIndexVertexArray
  60. {
  61. public:
  62. TriangleMeshInterface(Model* model, i32 lodLevel) :
  63. btTriangleIndexVertexArray()
  64. {
  65. assert(lodLevel >= 0);
  66. unsigned numGeometries = model->GetNumGeometries();
  67. unsigned totalTriangles = 0;
  68. for (unsigned i = 0; i < numGeometries; ++i)
  69. {
  70. Geometry* geometry = model->GetGeometry(i, lodLevel);
  71. if (!geometry)
  72. {
  73. URHO3D_LOGWARNING("Skipping null geometry for triangle mesh collision");
  74. continue;
  75. }
  76. SharedArrayPtr<byte> vertexData;
  77. SharedArrayPtr<byte> indexData;
  78. i32 vertexSize;
  79. i32 indexSize;
  80. const Vector<VertexElement>* elements;
  81. geometry->GetRawDataShared(vertexData, vertexSize, indexData, indexSize, elements);
  82. if (!vertexData || !indexData || !elements || VertexBuffer::GetElementOffset(*elements, TYPE_VECTOR3, SEM_POSITION) != 0)
  83. {
  84. URHO3D_LOGWARNING("Skipping geometry with no or unsuitable CPU-side geometry data for triangle mesh collision");
  85. continue;
  86. }
  87. // Keep shared pointers to the vertex/index data so that if it's unloaded or changes size, we don't crash
  88. dataArrays_.Push(vertexData);
  89. dataArrays_.Push(indexData);
  90. i32 indexStart = geometry->GetIndexStart();
  91. i32 indexCount = geometry->GetIndexCount();
  92. btIndexedMesh meshIndex;
  93. meshIndex.m_numTriangles = indexCount / 3;
  94. meshIndex.m_triangleIndexBase = reinterpret_cast<unsigned char*>(&indexData[indexStart * indexSize]);
  95. meshIndex.m_triangleIndexStride = 3 * indexSize;
  96. meshIndex.m_numVertices = 0;
  97. meshIndex.m_vertexBase = reinterpret_cast<unsigned char*>(vertexData.Get());
  98. meshIndex.m_vertexStride = vertexSize;
  99. meshIndex.m_indexType = (indexSize == sizeof(unsigned short)) ? PHY_SHORT : PHY_INTEGER;
  100. meshIndex.m_vertexType = PHY_FLOAT;
  101. m_indexedMeshes.push_back(meshIndex);
  102. totalTriangles += meshIndex.m_numTriangles;
  103. }
  104. // Bullet will not work properly with quantized AABB compression, if the triangle count is too large. Use a conservative
  105. // threshold value
  106. useQuantize_ = totalTriangles <= QUANTIZE_MAX_TRIANGLES;
  107. }
  108. explicit TriangleMeshInterface(CustomGeometry* custom) :
  109. btTriangleIndexVertexArray()
  110. {
  111. const Vector<Vector<CustomGeometryVertex>>& srcVertices = custom->GetVertices();
  112. unsigned totalVertexCount = 0;
  113. unsigned totalTriangles = 0;
  114. for (i32 i = 0; i < srcVertices.Size(); ++i)
  115. totalVertexCount += srcVertices[i].Size();
  116. if (totalVertexCount)
  117. {
  118. // CustomGeometry vertex data is unindexed, so build index data here
  119. SharedArrayPtr<byte> vertexData(new byte[totalVertexCount * sizeof(Vector3)]);
  120. SharedArrayPtr<byte> indexData(new byte[totalVertexCount * sizeof(unsigned)]);
  121. dataArrays_.Push(vertexData);
  122. dataArrays_.Push(indexData);
  123. auto* destVertex = reinterpret_cast<Vector3*>(&vertexData[0]);
  124. auto* destIndex = reinterpret_cast<unsigned*>(&indexData[0]);
  125. unsigned k = 0;
  126. for (i32 i = 0; i < srcVertices.Size(); ++i)
  127. {
  128. for (i32 j = 0; j < srcVertices[i].Size(); ++j)
  129. {
  130. *destVertex++ = srcVertices[i][j].position_;
  131. *destIndex++ = k++;
  132. }
  133. }
  134. btIndexedMesh meshIndex;
  135. meshIndex.m_numTriangles = totalVertexCount / 3;
  136. meshIndex.m_triangleIndexBase = reinterpret_cast<unsigned char*>(indexData.Get());
  137. meshIndex.m_triangleIndexStride = 3 * sizeof(unsigned);
  138. meshIndex.m_numVertices = totalVertexCount;
  139. meshIndex.m_vertexBase = reinterpret_cast<unsigned char*>(vertexData.Get());
  140. meshIndex.m_vertexStride = sizeof(Vector3);
  141. meshIndex.m_indexType = PHY_INTEGER;
  142. meshIndex.m_vertexType = PHY_FLOAT;
  143. m_indexedMeshes.push_back(meshIndex);
  144. totalTriangles += meshIndex.m_numTriangles;
  145. }
  146. useQuantize_ = totalTriangles <= QUANTIZE_MAX_TRIANGLES;
  147. }
  148. /// OK to use quantization flag.
  149. bool useQuantize_;
  150. private:
  151. /// Shared vertex/index data used in the collision.
  152. Vector<SharedArrayPtr<byte>> dataArrays_;
  153. };
  154. TriangleMeshData::TriangleMeshData(Model* model, i32 lodLevel)
  155. {
  156. assert(lodLevel >= 0);
  157. meshInterface_ = make_unique<TriangleMeshInterface>(model, lodLevel);
  158. shape_ = make_unique<btBvhTriangleMeshShape>(meshInterface_.get(), meshInterface_->useQuantize_, true);
  159. infoMap_ = make_unique<btTriangleInfoMap>();
  160. btGenerateInternalEdgeInfo(shape_.get(), infoMap_.get());
  161. }
  162. TriangleMeshData::TriangleMeshData(CustomGeometry* custom)
  163. {
  164. meshInterface_ = make_unique<TriangleMeshInterface>(custom);
  165. shape_ = make_unique<btBvhTriangleMeshShape>(meshInterface_.get(), meshInterface_->useQuantize_, true);
  166. infoMap_ = make_unique<btTriangleInfoMap>();
  167. btGenerateInternalEdgeInfo(shape_.get(), infoMap_.get());
  168. }
  169. GImpactMeshData::GImpactMeshData(Model* model, i32 lodLevel)
  170. {
  171. assert(lodLevel >= 0);
  172. meshInterface_ = make_unique<TriangleMeshInterface>(model, lodLevel);
  173. }
  174. GImpactMeshData::GImpactMeshData(CustomGeometry* custom)
  175. {
  176. meshInterface_ = make_unique<TriangleMeshInterface>(custom);
  177. }
  178. ConvexData::ConvexData(Model* model, i32 lodLevel)
  179. {
  180. assert(lodLevel >= 0);
  181. Vector<Vector3> vertices;
  182. unsigned numGeometries = model->GetNumGeometries();
  183. for (unsigned i = 0; i < numGeometries; ++i)
  184. {
  185. Geometry* geometry = model->GetGeometry(i, lodLevel);
  186. if (!geometry)
  187. {
  188. URHO3D_LOGWARNING("Skipping null geometry for convex hull collision");
  189. continue;
  190. };
  191. const byte* vertexData;
  192. const byte* indexData;
  193. i32 vertexSize;
  194. i32 indexSize;
  195. const Vector<VertexElement>* elements;
  196. geometry->GetRawData(vertexData, vertexSize, indexData, indexSize, elements);
  197. if (!vertexData || VertexBuffer::GetElementOffset(*elements, TYPE_VECTOR3, SEM_POSITION) != 0)
  198. {
  199. URHO3D_LOGWARNING("Skipping geometry with no or unsuitable CPU-side geometry data for convex hull collision");
  200. continue;
  201. }
  202. unsigned vertexStart = geometry->GetVertexStart();
  203. unsigned vertexCount = geometry->GetVertexCount();
  204. // Copy vertex data
  205. for (unsigned j = 0; j < vertexCount; ++j)
  206. {
  207. const Vector3& v = *((const Vector3*)(&vertexData[(vertexStart + j) * vertexSize]));
  208. vertices.Push(v);
  209. }
  210. }
  211. BuildHull(vertices);
  212. }
  213. ConvexData::ConvexData(CustomGeometry* custom)
  214. {
  215. const Vector<Vector<CustomGeometryVertex>>& srcVertices = custom->GetVertices();
  216. Vector<Vector3> vertices;
  217. for (i32 i = 0; i < srcVertices.Size(); ++i)
  218. {
  219. for (i32 j = 0; j < srcVertices[i].Size(); ++j)
  220. vertices.Push(srcVertices[i][j].position_);
  221. }
  222. BuildHull(vertices);
  223. }
  224. void ConvexData::BuildHull(const Vector<Vector3>& vertices)
  225. {
  226. if (vertices.Size())
  227. {
  228. // Build the convex hull from the raw geometry
  229. StanHull::HullDesc desc;
  230. desc.SetHullFlag(StanHull::QF_TRIANGLES);
  231. desc.mVcount = vertices.Size();
  232. desc.mVertices = vertices[0].Data();
  233. desc.mVertexStride = 3 * sizeof(float);
  234. desc.mSkinWidth = 0.0f;
  235. StanHull::HullLibrary lib;
  236. StanHull::HullResult result;
  237. lib.CreateConvexHull(desc, result);
  238. vertexCount_ = result.mNumOutputVertices;
  239. vertexData_ = new Vector3[vertexCount_];
  240. indexCount_ = result.mNumIndices;
  241. indexData_ = new unsigned[indexCount_];
  242. // Copy vertex data & index data
  243. memcpy(vertexData_.Get(), result.mOutputVertices, vertexCount_ * sizeof(Vector3));
  244. memcpy(indexData_.Get(), result.mIndices, indexCount_ * sizeof(unsigned));
  245. lib.ReleaseResult(result);
  246. }
  247. else
  248. {
  249. vertexCount_ = 0;
  250. indexCount_ = 0;
  251. }
  252. }
  253. HeightfieldData::HeightfieldData(Terrain* terrain, i32 lodLevel) :
  254. heightData_(terrain->GetHeightData()),
  255. spacing_(terrain->GetSpacing()),
  256. size_(terrain->GetNumVertices()),
  257. minHeight_(0.0f),
  258. maxHeight_(0.0f)
  259. {
  260. assert(lodLevel >= 0);
  261. if (heightData_)
  262. {
  263. if (lodLevel > 0)
  264. {
  265. IntVector2 lodSize = size_;
  266. Vector3 lodSpacing = spacing_;
  267. unsigned skip = 1;
  268. for (i32 i = 0; i < lodLevel; ++i)
  269. {
  270. skip *= 2;
  271. lodSpacing.x_ *= 2.0f;
  272. lodSpacing.z_ *= 2.0f;
  273. int rX = lodSize.x_ & 1u;
  274. int rY = lodSize.y_ & 1u;
  275. lodSize.x_ >>= 1;
  276. lodSize.y_ >>= 1;
  277. lodSize.x_ += rX;
  278. lodSize.y_ += rY;
  279. if (lodSize.x_ <= 2 || lodSize.y_ <= 2)
  280. break;
  281. }
  282. SharedArrayPtr<float> lodHeightData(new float[lodSize.x_ * lodSize.y_]);
  283. for (int y = 0, dY = 0; y < size_.y_ && dY < lodSize.y_; y += skip, ++dY)
  284. {
  285. for (int x = 0, dX = 0; x < size_.x_ && dX < lodSize.x_; x += skip, ++dX)
  286. lodHeightData[dY * lodSize.x_ + dX] = heightData_[y * size_.x_ + x];
  287. }
  288. size_ = lodSize;
  289. spacing_ = lodSpacing;
  290. heightData_ = lodHeightData;
  291. }
  292. auto points = (unsigned)(size_.x_ * size_.y_);
  293. float* data = heightData_.Get();
  294. minHeight_ = maxHeight_ = data[0];
  295. for (unsigned i = 1; i < points; ++i)
  296. {
  297. minHeight_ = Min(minHeight_, data[i]);
  298. maxHeight_ = Max(maxHeight_, data[i]);
  299. }
  300. }
  301. }
  302. bool HasDynamicBuffers(Model* model, i32 lodLevel)
  303. {
  304. assert(lodLevel >= 0);
  305. unsigned numGeometries = model->GetNumGeometries();
  306. for (unsigned i = 0; i < numGeometries; ++i)
  307. {
  308. Geometry* geometry = model->GetGeometry(i, lodLevel);
  309. if (!geometry)
  310. continue;
  311. unsigned numVertexBuffers = geometry->GetNumVertexBuffers();
  312. for (unsigned j = 0; j < numVertexBuffers; ++j)
  313. {
  314. VertexBuffer* buffer = geometry->GetVertexBuffer(j);
  315. if (!buffer)
  316. continue;
  317. if (buffer->IsDynamic())
  318. return true;
  319. }
  320. IndexBuffer* buffer = geometry->GetIndexBuffer();
  321. if (buffer && buffer->IsDynamic())
  322. return true;
  323. }
  324. return false;
  325. }
  326. CollisionGeometryData* CreateCollisionGeometryData(ShapeType shapeType, Model* model, i32 lodLevel)
  327. {
  328. assert(lodLevel >= 0);
  329. switch (shapeType)
  330. {
  331. case SHAPE_TRIANGLEMESH:
  332. return new TriangleMeshData(model, lodLevel);
  333. case SHAPE_CONVEXHULL:
  334. return new ConvexData(model, lodLevel);
  335. case SHAPE_GIMPACTMESH:
  336. return new GImpactMeshData(model, lodLevel);
  337. default:
  338. return nullptr;
  339. }
  340. }
  341. CollisionGeometryData* CreateCollisionGeometryData(ShapeType shapeType, CustomGeometry* custom)
  342. {
  343. switch (shapeType)
  344. {
  345. case SHAPE_TRIANGLEMESH:
  346. return new TriangleMeshData(custom);
  347. case SHAPE_CONVEXHULL:
  348. return new ConvexData(custom);
  349. case SHAPE_GIMPACTMESH:
  350. return new GImpactMeshData(custom);
  351. default:
  352. return nullptr;
  353. }
  354. }
  355. btCollisionShape* CreateCollisionGeometryDataShape(ShapeType shapeType, CollisionGeometryData* geometry, const Vector3& scale)
  356. {
  357. switch (shapeType)
  358. {
  359. case SHAPE_TRIANGLEMESH:
  360. {
  361. auto* triMesh = static_cast<TriangleMeshData*>(geometry);
  362. return new btScaledBvhTriangleMeshShape(triMesh->shape_.get(), ToBtVector3(scale));
  363. }
  364. case SHAPE_CONVEXHULL:
  365. {
  366. auto* convex = static_cast<ConvexData*>(geometry);
  367. auto* shape = new btConvexHullShape((btScalar*)convex->vertexData_.Get(), convex->vertexCount_, sizeof(Vector3));
  368. shape->setLocalScaling(ToBtVector3(scale));
  369. return shape;
  370. }
  371. case SHAPE_GIMPACTMESH:
  372. {
  373. auto* gimpactMesh = static_cast<GImpactMeshData*>(geometry);
  374. auto* shape = new btGImpactMeshShape(gimpactMesh->meshInterface_.get());
  375. shape->setLocalScaling(ToBtVector3(scale));
  376. shape->updateBound();
  377. return shape;
  378. }
  379. default:
  380. return nullptr;
  381. }
  382. }
  383. CollisionShape::CollisionShape(Context* context) :
  384. Component(context),
  385. shapeType_(SHAPE_BOX),
  386. position_(Vector3::ZERO),
  387. rotation_(Quaternion::IDENTITY),
  388. size_(Vector3::ONE),
  389. cachedWorldScale_(Vector3::ONE),
  390. lodLevel_(0),
  391. customGeometryID_(0),
  392. margin_(DEFAULT_COLLISION_MARGIN),
  393. recreateShape_(true),
  394. retryCreation_(false)
  395. {
  396. }
  397. CollisionShape::~CollisionShape()
  398. {
  399. ReleaseShape();
  400. if (physicsWorld_)
  401. physicsWorld_->RemoveCollisionShape(this);
  402. }
  403. void CollisionShape::RegisterObject(Context* context)
  404. {
  405. context->RegisterFactory<CollisionShape>(PHYSICS_CATEGORY);
  406. URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, true, AM_DEFAULT);
  407. URHO3D_ENUM_ATTRIBUTE_EX("Shape Type", shapeType_, MarkShapeDirty, typeNames, SHAPE_BOX, AM_DEFAULT);
  408. URHO3D_ATTRIBUTE_EX("Size", size_, MarkShapeDirty, Vector3::ONE, AM_DEFAULT);
  409. URHO3D_ACCESSOR_ATTRIBUTE("Offset Position", GetPosition, SetPosition, Vector3::ZERO, AM_DEFAULT);
  410. URHO3D_ACCESSOR_ATTRIBUTE("Offset Rotation", GetRotation, SetRotation, Quaternion::IDENTITY, AM_DEFAULT);
  411. URHO3D_ACCESSOR_ATTRIBUTE("Model", GetModelAttr, SetModelAttr, ResourceRef(Model::GetTypeStatic()), AM_DEFAULT);
  412. URHO3D_ATTRIBUTE_EX("LOD Level", lodLevel_, MarkShapeDirty, 0, AM_DEFAULT);
  413. URHO3D_ATTRIBUTE_EX("Collision Margin", margin_, MarkShapeDirty, DEFAULT_COLLISION_MARGIN, AM_DEFAULT);
  414. URHO3D_ATTRIBUTE_EX("CustomGeometry ComponentID", customGeometryID_, MarkShapeDirty, 0, AM_DEFAULT | AM_COMPONENTID);
  415. }
  416. void CollisionShape::ApplyAttributes()
  417. {
  418. if (recreateShape_)
  419. {
  420. UpdateShape();
  421. NotifyRigidBody();
  422. }
  423. }
  424. void CollisionShape::OnSetEnabled()
  425. {
  426. NotifyRigidBody();
  427. }
  428. void CollisionShape::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  429. {
  430. if (debug && physicsWorld_ && shape_ && node_ && IsEnabledEffective())
  431. {
  432. // Use the rigid body's world transform if possible, as it may be different from the rendering transform
  433. Matrix3x4 worldTransform;
  434. auto* body = GetComponent<RigidBody>();
  435. bool bodyActive = false;
  436. if (body)
  437. {
  438. worldTransform = Matrix3x4(body->GetPosition(), body->GetRotation(), node_->GetWorldScale());
  439. bodyActive = body->IsActive();
  440. }
  441. else
  442. worldTransform = node_->GetWorldTransform();
  443. // Special case code for convex hull: bypass Bullet's own rendering to draw triangles correctly, not just edges
  444. if (shapeType_ == SHAPE_CONVEXHULL)
  445. {
  446. auto*convexData = static_cast<ConvexData*>(GetGeometryData());
  447. auto* body = GetComponent<RigidBody>();
  448. Color color = bodyActive ? Color::WHITE : Color::GREEN;
  449. Matrix3x4 shapeTransform(worldTransform * position_, worldTransform.Rotation() * rotation_, worldTransform.Scale());
  450. if (convexData)
  451. {
  452. for (unsigned i = 0; i < convexData->indexCount_; i += 3)
  453. {
  454. Vector3 a = shapeTransform * convexData->vertexData_[convexData->indexData_[i + 0]];
  455. Vector3 b = shapeTransform * convexData->vertexData_[convexData->indexData_[i + 1]];
  456. Vector3 c = shapeTransform * convexData->vertexData_[convexData->indexData_[i + 2]];
  457. debug->AddLine(a, b, color, depthTest);
  458. debug->AddLine(b, c, color, depthTest);
  459. debug->AddLine(a, c, color, depthTest);
  460. }
  461. }
  462. }
  463. else
  464. {
  465. physicsWorld_->SetDebugRenderer(debug);
  466. physicsWorld_->SetDebugDepthTest(depthTest);
  467. Vector3 position = position_;
  468. // For terrains, undo the height centering performed automatically by Bullet
  469. if (shapeType_ == SHAPE_TERRAIN && geometry_)
  470. {
  471. auto* heightfield = static_cast<HeightfieldData*>(geometry_.Get());
  472. position.y_ += (heightfield->minHeight_ + heightfield->maxHeight_) * 0.5f;
  473. }
  474. Vector3 worldPosition(worldTransform * position);
  475. Quaternion worldRotation(worldTransform.Rotation() * rotation_);
  476. btDiscreteDynamicsWorld* world = physicsWorld_->GetWorld();
  477. world->debugDrawObject(btTransform(ToBtQuaternion(worldRotation), ToBtVector3(worldPosition)), shape_.get(), bodyActive ?
  478. WHITE : GREEN);
  479. physicsWorld_->SetDebugRenderer(nullptr);
  480. }
  481. }
  482. }
  483. void CollisionShape::SetBox(const Vector3& size, const Vector3& position, const Quaternion& rotation)
  484. {
  485. if (model_)
  486. UnsubscribeFromEvent(model_, E_RELOADFINISHED);
  487. shapeType_ = SHAPE_BOX;
  488. size_ = size;
  489. position_ = position;
  490. rotation_ = rotation;
  491. model_.Reset();
  492. customGeometryID_ = 0;
  493. UpdateShape();
  494. NotifyRigidBody();
  495. MarkNetworkUpdate();
  496. }
  497. void CollisionShape::SetSphere(float diameter, const Vector3& position, const Quaternion& rotation)
  498. {
  499. if (model_)
  500. UnsubscribeFromEvent(model_, E_RELOADFINISHED);
  501. shapeType_ = SHAPE_SPHERE;
  502. size_ = Vector3(diameter, diameter, diameter);
  503. position_ = position;
  504. rotation_ = rotation;
  505. model_.Reset();
  506. customGeometryID_ = 0;
  507. UpdateShape();
  508. NotifyRigidBody();
  509. MarkNetworkUpdate();
  510. }
  511. void CollisionShape::SetStaticPlane(const Vector3& position, const Quaternion& rotation)
  512. {
  513. if (model_)
  514. UnsubscribeFromEvent(model_, E_RELOADFINISHED);
  515. shapeType_ = SHAPE_STATICPLANE;
  516. position_ = position;
  517. rotation_ = rotation;
  518. model_.Reset();
  519. customGeometryID_ = 0;
  520. UpdateShape();
  521. NotifyRigidBody();
  522. MarkNetworkUpdate();
  523. }
  524. void CollisionShape::SetCylinder(float diameter, float height, const Vector3& position, const Quaternion& rotation)
  525. {
  526. if (model_)
  527. UnsubscribeFromEvent(model_, E_RELOADFINISHED);
  528. shapeType_ = SHAPE_CYLINDER;
  529. size_ = Vector3(diameter, height, diameter);
  530. position_ = position;
  531. rotation_ = rotation;
  532. model_.Reset();
  533. customGeometryID_ = 0;
  534. UpdateShape();
  535. NotifyRigidBody();
  536. MarkNetworkUpdate();
  537. }
  538. void CollisionShape::SetCapsule(float diameter, float height, const Vector3& position, const Quaternion& rotation)
  539. {
  540. if (model_)
  541. UnsubscribeFromEvent(model_, E_RELOADFINISHED);
  542. shapeType_ = SHAPE_CAPSULE;
  543. size_ = Vector3(diameter, height, diameter);
  544. position_ = position;
  545. rotation_ = rotation;
  546. model_.Reset();
  547. customGeometryID_ = 0;
  548. UpdateShape();
  549. NotifyRigidBody();
  550. MarkNetworkUpdate();
  551. }
  552. void CollisionShape::SetCone(float diameter, float height, const Vector3& position, const Quaternion& rotation)
  553. {
  554. if (model_)
  555. UnsubscribeFromEvent(model_, E_RELOADFINISHED);
  556. shapeType_ = SHAPE_CONE;
  557. size_ = Vector3(diameter, height, diameter);
  558. position_ = position;
  559. rotation_ = rotation;
  560. model_.Reset();
  561. customGeometryID_ = 0;
  562. UpdateShape();
  563. NotifyRigidBody();
  564. MarkNetworkUpdate();
  565. }
  566. void CollisionShape::SetTriangleMesh(Model* model, i32 lodLevel, const Vector3& scale, const Vector3& position,
  567. const Quaternion& rotation)
  568. {
  569. assert(lodLevel >= 0);
  570. SetModelShape(SHAPE_TRIANGLEMESH, model, lodLevel, scale, position, rotation);
  571. }
  572. void CollisionShape::SetCustomTriangleMesh(CustomGeometry* custom, const Vector3& scale, const Vector3& position,
  573. const Quaternion& rotation)
  574. {
  575. SetCustomShape(SHAPE_TRIANGLEMESH, custom, scale, position, rotation);
  576. }
  577. void CollisionShape::SetConvexHull(Model* model, i32 lodLevel, const Vector3& scale, const Vector3& position,
  578. const Quaternion& rotation)
  579. {
  580. assert(lodLevel >= 0);
  581. SetModelShape(SHAPE_CONVEXHULL, model, lodLevel, scale, position, rotation);
  582. }
  583. void CollisionShape::SetCustomConvexHull(CustomGeometry* custom, const Vector3& scale, const Vector3& position,
  584. const Quaternion& rotation)
  585. {
  586. SetCustomShape(SHAPE_CONVEXHULL, custom, scale, position, rotation);
  587. }
  588. void CollisionShape::SetGImpactMesh(Model* model, i32 lodLevel, const Vector3& scale, const Vector3& position,
  589. const Quaternion& rotation)
  590. {
  591. assert(lodLevel >= 0);
  592. SetModelShape(SHAPE_GIMPACTMESH, model, lodLevel, scale, position, rotation);
  593. }
  594. void CollisionShape::SetCustomGImpactMesh(CustomGeometry* custom, const Vector3& scale, const Vector3& position,
  595. const Quaternion& rotation)
  596. {
  597. SetCustomShape(SHAPE_GIMPACTMESH, custom, scale, position, rotation);
  598. }
  599. void CollisionShape::SetTerrain(i32 lodLevel)
  600. {
  601. assert(lodLevel >= 0);
  602. Terrain* terrain = GetComponent<Terrain>();
  603. if (!terrain)
  604. {
  605. URHO3D_LOGERROR("No terrain component, can not set terrain shape");
  606. return;
  607. }
  608. if (model_)
  609. UnsubscribeFromEvent(model_, E_RELOADFINISHED);
  610. shapeType_ = SHAPE_TERRAIN;
  611. lodLevel_ = lodLevel;
  612. UpdateShape();
  613. NotifyRigidBody();
  614. MarkNetworkUpdate();
  615. }
  616. void CollisionShape::SetShapeType(ShapeType type)
  617. {
  618. if (type != shapeType_)
  619. {
  620. shapeType_ = type;
  621. UpdateShape();
  622. NotifyRigidBody();
  623. MarkNetworkUpdate();
  624. }
  625. }
  626. void CollisionShape::SetSize(const Vector3& size)
  627. {
  628. if (size != size_)
  629. {
  630. size_ = size;
  631. UpdateShape();
  632. NotifyRigidBody();
  633. MarkNetworkUpdate();
  634. }
  635. }
  636. void CollisionShape::SetPosition(const Vector3& position)
  637. {
  638. if (position != position_)
  639. {
  640. position_ = position;
  641. NotifyRigidBody();
  642. MarkNetworkUpdate();
  643. }
  644. }
  645. void CollisionShape::SetRotation(const Quaternion& rotation)
  646. {
  647. if (rotation != rotation_)
  648. {
  649. rotation_ = rotation;
  650. NotifyRigidBody();
  651. MarkNetworkUpdate();
  652. }
  653. }
  654. void CollisionShape::SetTransform(const Vector3& position, const Quaternion& rotation)
  655. {
  656. if (position != position_ || rotation != rotation_)
  657. {
  658. position_ = position;
  659. rotation_ = rotation;
  660. NotifyRigidBody();
  661. MarkNetworkUpdate();
  662. }
  663. }
  664. void CollisionShape::SetMargin(float margin)
  665. {
  666. margin = Max(margin, 0.0f);
  667. if (margin != margin_)
  668. {
  669. if (shape_)
  670. shape_->setMargin(margin);
  671. margin_ = margin;
  672. MarkNetworkUpdate();
  673. }
  674. }
  675. void CollisionShape::SetModel(Model* model)
  676. {
  677. if (model != model_)
  678. {
  679. if (model_)
  680. UnsubscribeFromEvent(model_, E_RELOADFINISHED);
  681. model_ = model;
  682. if (shapeType_ >= SHAPE_TRIANGLEMESH)
  683. {
  684. UpdateShape();
  685. NotifyRigidBody();
  686. }
  687. MarkNetworkUpdate();
  688. }
  689. }
  690. void CollisionShape::SetLodLevel(i32 lodLevel)
  691. {
  692. assert(lodLevel >= 0);
  693. if (lodLevel != lodLevel_)
  694. {
  695. lodLevel_ = lodLevel;
  696. if (shapeType_ >= SHAPE_TRIANGLEMESH)
  697. {
  698. UpdateShape();
  699. NotifyRigidBody();
  700. }
  701. MarkNetworkUpdate();
  702. }
  703. }
  704. BoundingBox CollisionShape::GetWorldBoundingBox() const
  705. {
  706. if (shape_ && node_)
  707. {
  708. // Use the rigid body's world transform if possible, as it may be different from the rendering transform
  709. auto* body = GetComponent<RigidBody>();
  710. Matrix3x4 worldTransform = body ? Matrix3x4(body->GetPosition(), body->GetRotation(), node_->GetWorldScale()) :
  711. node_->GetWorldTransform();
  712. Vector3 worldPosition(worldTransform * position_);
  713. Quaternion worldRotation(worldTransform.Rotation() * rotation_);
  714. btTransform shapeWorldTransform(ToBtQuaternion(worldRotation), ToBtVector3(worldPosition));
  715. btVector3 aabbMin, aabbMax;
  716. shape_->getAabb(shapeWorldTransform, aabbMin, aabbMax);
  717. return BoundingBox(ToVector3(aabbMin), ToVector3(aabbMax));
  718. }
  719. else
  720. return BoundingBox();
  721. }
  722. void CollisionShape::NotifyRigidBody(bool updateMass)
  723. {
  724. btCompoundShape* compound = GetParentCompoundShape();
  725. if (node_ && shape_ && compound)
  726. {
  727. // Remove the shape first to ensure it is not added twice
  728. compound->removeChildShape(shape_.get());
  729. if (IsEnabledEffective())
  730. {
  731. // Then add with updated offset
  732. Vector3 position = position_;
  733. // For terrains, undo the height centering performed automatically by Bullet
  734. if (shapeType_ == SHAPE_TERRAIN && geometry_)
  735. {
  736. auto* heightfield = static_cast<HeightfieldData*>(geometry_.Get());
  737. position.y_ += (heightfield->minHeight_ + heightfield->maxHeight_) * 0.5f;
  738. }
  739. btTransform offset;
  740. offset.setOrigin(ToBtVector3(node_->GetWorldScale() * position));
  741. offset.setRotation(ToBtQuaternion(rotation_));
  742. compound->addChildShape(offset, shape_.get());
  743. }
  744. // Finally tell the rigid body to update its mass
  745. if (updateMass)
  746. rigidBody_->UpdateMass();
  747. }
  748. }
  749. void CollisionShape::SetModelAttr(const ResourceRef& value)
  750. {
  751. auto* cache = GetSubsystem<ResourceCache>();
  752. model_ = cache->GetResource<Model>(value.name_);
  753. recreateShape_ = true;
  754. MarkNetworkUpdate();
  755. }
  756. ResourceRef CollisionShape::GetModelAttr() const
  757. {
  758. return GetResourceRef(model_, Model::GetTypeStatic());
  759. }
  760. void CollisionShape::ReleaseShape()
  761. {
  762. btCompoundShape* compound = GetParentCompoundShape();
  763. if (shape_ && compound)
  764. {
  765. compound->removeChildShape(shape_.get());
  766. rigidBody_->UpdateMass();
  767. }
  768. shape_.reset();
  769. geometry_.Reset();
  770. if (physicsWorld_)
  771. physicsWorld_->CleanupGeometryCache();
  772. }
  773. void CollisionShape::OnNodeSet(Node* node)
  774. {
  775. if (node)
  776. {
  777. node->AddListener(this);
  778. cachedWorldScale_ = node->GetWorldScale();
  779. // Terrain collision shape depends on the terrain component's geometry updates. Subscribe to them
  780. SubscribeToEvent(node, E_TERRAINCREATED, URHO3D_HANDLER(CollisionShape, HandleTerrainCreated));
  781. }
  782. }
  783. void CollisionShape::OnSceneSet(Scene* scene)
  784. {
  785. if (scene)
  786. {
  787. if (scene == node_)
  788. URHO3D_LOGWARNING(GetTypeName() + " should not be created to the root scene node");
  789. physicsWorld_ = scene->GetOrCreateComponent<PhysicsWorld>();
  790. physicsWorld_->AddCollisionShape(this);
  791. // Create shape now if necessary (attributes modified before adding to scene)
  792. if (retryCreation_)
  793. {
  794. UpdateShape();
  795. NotifyRigidBody();
  796. }
  797. }
  798. else
  799. {
  800. ReleaseShape();
  801. if (physicsWorld_)
  802. physicsWorld_->RemoveCollisionShape(this);
  803. // Recreate when moved to a scene again
  804. retryCreation_ = true;
  805. }
  806. }
  807. void CollisionShape::OnMarkedDirty(Node* node)
  808. {
  809. Vector3 newWorldScale = node_->GetWorldScale();
  810. if (HasWorldScaleChanged(cachedWorldScale_, newWorldScale) && shape_)
  811. {
  812. // Physics operations are not safe from worker threads
  813. Scene* scene = GetScene();
  814. if (scene && scene->IsThreadedUpdate())
  815. {
  816. scene->DelayedMarkedDirty(this);
  817. return;
  818. }
  819. switch (shapeType_)
  820. {
  821. case SHAPE_BOX:
  822. case SHAPE_SPHERE:
  823. case SHAPE_CYLINDER:
  824. case SHAPE_CAPSULE:
  825. case SHAPE_CONE:
  826. shape_->setLocalScaling(ToBtVector3(newWorldScale));
  827. break;
  828. case SHAPE_TRIANGLEMESH:
  829. case SHAPE_CONVEXHULL:
  830. shape_->setLocalScaling(ToBtVector3(newWorldScale * size_));
  831. break;
  832. case SHAPE_TERRAIN:
  833. {
  834. auto* heightfield = static_cast<HeightfieldData*>(geometry_.Get());
  835. shape_->setLocalScaling(ToBtVector3(Vector3(heightfield->spacing_.x_, 1.0f, heightfield->spacing_.z_) *
  836. newWorldScale * size_));
  837. }
  838. break;
  839. default:
  840. break;
  841. }
  842. NotifyRigidBody();
  843. cachedWorldScale_ = newWorldScale;
  844. }
  845. }
  846. btCompoundShape* CollisionShape::GetParentCompoundShape()
  847. {
  848. if (!rigidBody_)
  849. rigidBody_ = GetComponent<RigidBody>();
  850. return rigidBody_ ? rigidBody_->GetCompoundShape() : nullptr;
  851. }
  852. void CollisionShape::UpdateShape()
  853. {
  854. URHO3D_PROFILE(UpdateCollisionShape);
  855. ReleaseShape();
  856. // If no physics world available now mark for retry later
  857. if (!physicsWorld_)
  858. {
  859. retryCreation_ = true;
  860. return;
  861. }
  862. if (node_)
  863. {
  864. cachedWorldScale_ = node_->GetWorldScale();
  865. switch (shapeType_)
  866. {
  867. case SHAPE_BOX:
  868. shape_ = make_unique<btBoxShape>(ToBtVector3(size_ * 0.5f));
  869. shape_->setLocalScaling(ToBtVector3(cachedWorldScale_));
  870. break;
  871. case SHAPE_SPHERE:
  872. shape_ = make_unique<btSphereShape>(size_.x_ * 0.5f);
  873. shape_->setLocalScaling(ToBtVector3(cachedWorldScale_));
  874. break;
  875. case SHAPE_STATICPLANE:
  876. shape_ = make_unique<btStaticPlaneShape>(btVector3(0.0f, 1.0f, 0.0f), 0.0f);
  877. break;
  878. case SHAPE_CYLINDER:
  879. shape_ = make_unique<btCylinderShape>(btVector3(size_.x_ * 0.5f, size_.y_ * 0.5f, size_.x_ * 0.5f));
  880. shape_->setLocalScaling(ToBtVector3(cachedWorldScale_));
  881. break;
  882. case SHAPE_CAPSULE:
  883. shape_ = make_unique<btCapsuleShape>(size_.x_ * 0.5f, Max(size_.y_ - size_.x_, 0.0f));
  884. shape_->setLocalScaling(ToBtVector3(cachedWorldScale_));
  885. break;
  886. case SHAPE_CONE:
  887. shape_ = make_unique<btConeShape>(size_.x_ * 0.5f, size_.y_);
  888. shape_->setLocalScaling(ToBtVector3(cachedWorldScale_));
  889. break;
  890. case SHAPE_TRIANGLEMESH:
  891. UpdateCachedGeometryShape(physicsWorld_->GetTriMeshCache());
  892. break;
  893. case SHAPE_CONVEXHULL:
  894. UpdateCachedGeometryShape(physicsWorld_->GetConvexCache());
  895. break;
  896. case SHAPE_GIMPACTMESH:
  897. UpdateCachedGeometryShape(physicsWorld_->GetGImpactTrimeshCache());
  898. break;
  899. case SHAPE_TERRAIN:
  900. size_ = size_.Abs();
  901. {
  902. Terrain* terrain = GetComponent<Terrain>();
  903. if (terrain && terrain->GetHeightData())
  904. {
  905. geometry_ = new HeightfieldData(terrain, lodLevel_);
  906. auto* heightfield = static_cast<HeightfieldData*>(geometry_.Get());
  907. shape_ = make_unique<btHeightfieldTerrainShape>(
  908. heightfield->size_.x_, heightfield->size_.y_, heightfield->heightData_.Get(),
  909. 1.0f, heightfield->minHeight_, heightfield->maxHeight_, 1, PHY_FLOAT, false);
  910. shape_->setLocalScaling(
  911. ToBtVector3(Vector3(heightfield->spacing_.x_, 1.0f, heightfield->spacing_.z_) * cachedWorldScale_ * size_));
  912. }
  913. }
  914. break;
  915. default:
  916. shape_.reset(this->UpdateDerivedShape(shapeType_, cachedWorldScale_));
  917. break;
  918. }
  919. if (shape_)
  920. {
  921. shape_->setUserPointer(this);
  922. shape_->setMargin(margin_);
  923. }
  924. }
  925. if (physicsWorld_)
  926. physicsWorld_->CleanupGeometryCache();
  927. recreateShape_ = false;
  928. retryCreation_ = false;
  929. }
  930. void CollisionShape::UpdateCachedGeometryShape(CollisionGeometryDataCache& cache)
  931. {
  932. Scene* scene = GetScene();
  933. size_ = size_.Abs();
  934. if (customGeometryID_ && scene)
  935. {
  936. auto* custom = dynamic_cast<CustomGeometry*>(scene->GetComponent(customGeometryID_));
  937. if (custom)
  938. {
  939. geometry_ = CreateCollisionGeometryData(shapeType_, custom);
  940. assert(geometry_);
  941. shape_.reset(CreateCollisionGeometryDataShape(shapeType_, geometry_.Get(), cachedWorldScale_ * size_));
  942. assert(shape_);
  943. }
  944. else
  945. URHO3D_LOGWARNING("Could not find custom geometry component ID " + String(customGeometryID_) +
  946. " for collision shape creation");
  947. }
  948. else if (model_ && model_->GetNumGeometries())
  949. {
  950. // Check the geometry cache
  951. Pair<Model*, i32> id = MakePair(model_.Get(), lodLevel_);
  952. auto cachedGeometry = cache.Find(id);
  953. if (cachedGeometry != cache.End())
  954. geometry_ = cachedGeometry->second_;
  955. else
  956. {
  957. geometry_ = CreateCollisionGeometryData(shapeType_, model_, lodLevel_);
  958. assert(geometry_);
  959. // Check if model has dynamic buffers, do not cache in that case
  960. if (!HasDynamicBuffers(model_, lodLevel_))
  961. cache[id] = geometry_;
  962. }
  963. shape_.reset(CreateCollisionGeometryDataShape(shapeType_, geometry_.Get(), cachedWorldScale_ * size_));
  964. assert(shape_);
  965. // Watch for live reloads of the collision model to reload the geometry if necessary
  966. SubscribeToEvent(model_, E_RELOADFINISHED, URHO3D_HANDLER(CollisionShape, HandleModelReloadFinished));
  967. }
  968. }
  969. void CollisionShape::SetModelShape(ShapeType shapeType, Model* model, i32 lodLevel,
  970. const Vector3& scale, const Vector3& position, const Quaternion& rotation)
  971. {
  972. assert(lodLevel >= 0);
  973. if (!model)
  974. {
  975. URHO3D_LOGERROR("Null model, can not set collsion shape");
  976. return;
  977. }
  978. if (model_)
  979. UnsubscribeFromEvent(model_, E_RELOADFINISHED);
  980. shapeType_ = shapeType;
  981. model_ = model;
  982. lodLevel_ = lodLevel;
  983. size_ = scale;
  984. position_ = position;
  985. rotation_ = rotation;
  986. customGeometryID_ = 0;
  987. UpdateShape();
  988. NotifyRigidBody();
  989. MarkNetworkUpdate();
  990. }
  991. void CollisionShape::SetCustomShape(ShapeType shapeType, CustomGeometry* custom,
  992. const Vector3& scale, const Vector3& position, const Quaternion& rotation)
  993. {
  994. if (!custom)
  995. {
  996. URHO3D_LOGERROR("Null custom geometry, can not set collsion shape");
  997. return;
  998. }
  999. if (custom->GetScene() != GetScene())
  1000. {
  1001. URHO3D_LOGERROR("Custom geometry is not in the same scene as the collision shape, can not set collsion shape");
  1002. return;
  1003. }
  1004. if (model_)
  1005. UnsubscribeFromEvent(model_, E_RELOADFINISHED);
  1006. shapeType_ = shapeType;
  1007. model_.Reset();
  1008. lodLevel_ = 0;
  1009. size_ = scale;
  1010. position_ = position;
  1011. rotation_ = rotation;
  1012. customGeometryID_ = custom->GetID();
  1013. UpdateShape();
  1014. NotifyRigidBody();
  1015. MarkNetworkUpdate();
  1016. }
  1017. btCollisionShape* CollisionShape::UpdateDerivedShape(int shapeType, const Vector3& newWorldScale)
  1018. {
  1019. // To be overridden in derived classes.
  1020. return nullptr;
  1021. }
  1022. void CollisionShape::HandleTerrainCreated(StringHash eventType, VariantMap& eventData)
  1023. {
  1024. if (shapeType_ == SHAPE_TERRAIN)
  1025. {
  1026. UpdateShape();
  1027. NotifyRigidBody();
  1028. }
  1029. }
  1030. void CollisionShape::HandleModelReloadFinished(StringHash eventType, VariantMap& eventData)
  1031. {
  1032. if (physicsWorld_)
  1033. physicsWorld_->RemoveCachedGeometry(model_);
  1034. if (shapeType_ == SHAPE_TRIANGLEMESH || shapeType_ == SHAPE_CONVEXHULL)
  1035. {
  1036. UpdateShape();
  1037. NotifyRigidBody();
  1038. }
  1039. }
  1040. }