CollisionShape.cpp 39 KB

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