CollisionShape.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //
  2. // Copyright (c) 2008-2015 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. #pragma once
  23. #include "../Container/ArrayPtr.h"
  24. #include "../Math/BoundingBox.h"
  25. #include "../Math/Quaternion.h"
  26. #include "../Scene/Component.h"
  27. class btBvhTriangleMeshShape;
  28. class btCollisionShape;
  29. class btCompoundShape;
  30. class btTriangleMesh;
  31. struct btTriangleInfoMap;
  32. namespace Atomic
  33. {
  34. class CustomGeometry;
  35. class Geometry;
  36. class Model;
  37. class PhysicsWorld;
  38. class RigidBody;
  39. class Terrain;
  40. class TriangleMeshInterface;
  41. /// Collision shape type.
  42. enum ShapeType
  43. {
  44. SHAPE_BOX = 0,
  45. SHAPE_SPHERE,
  46. SHAPE_STATICPLANE,
  47. SHAPE_CYLINDER,
  48. SHAPE_CAPSULE,
  49. SHAPE_CONE,
  50. SHAPE_TRIANGLEMESH,
  51. SHAPE_CONVEXHULL,
  52. SHAPE_TERRAIN
  53. };
  54. /// Base class for collision shape geometry data.
  55. struct CollisionGeometryData : public RefCounted
  56. {
  57. REFCOUNTED(CollisionGeometryData)
  58. };
  59. /// Triangle mesh geometry data.
  60. struct TriangleMeshData : public CollisionGeometryData
  61. {
  62. REFCOUNTED(TriangleMeshData)
  63. /// Construct from a model.
  64. TriangleMeshData(Model* model, unsigned lodLevel);
  65. /// Construct from a custom geometry.
  66. TriangleMeshData(CustomGeometry* custom);
  67. /// Destruct. Free geometry data.
  68. ~TriangleMeshData();
  69. /// Bullet triangle mesh interface.
  70. TriangleMeshInterface* meshInterface_;
  71. /// Bullet triangle mesh collision shape.
  72. btBvhTriangleMeshShape* shape_;
  73. /// Bullet triangle info map.
  74. btTriangleInfoMap* infoMap_;
  75. };
  76. /// Convex hull geometry data.
  77. struct ConvexData : public CollisionGeometryData
  78. {
  79. REFCOUNTED(ConvexData)
  80. /// Construct from a model.
  81. ConvexData(Model* model, unsigned lodLevel);
  82. /// Construct from a custom geometry.
  83. ConvexData(CustomGeometry* custom);
  84. /// Destruct. Free geometry data.
  85. ~ConvexData();
  86. /// Build the convex hull from vertices.
  87. void BuildHull(const PODVector<Vector3>& vertices);
  88. /// Vertex data.
  89. SharedArrayPtr<Vector3> vertexData_;
  90. /// Number of vertices.
  91. unsigned vertexCount_;
  92. /// Index data.
  93. SharedArrayPtr<unsigned> indexData_;
  94. /// Number of indices.
  95. unsigned indexCount_;
  96. };
  97. /// Heightfield geometry data.
  98. struct HeightfieldData : public CollisionGeometryData
  99. {
  100. REFCOUNTED(HeightfieldData)
  101. /// Construct from a terrain.
  102. HeightfieldData(Terrain* terrain, unsigned lodLevel);
  103. /// Destruct. Free geometry data.
  104. ~HeightfieldData();
  105. /// Height data. On LOD level 0 the original height data will be used.
  106. SharedArrayPtr<float> heightData_;
  107. /// Vertex spacing.
  108. Vector3 spacing_;
  109. /// Heightmap size.
  110. IntVector2 size_;
  111. /// Minimum height.
  112. float minHeight_;
  113. /// Maximum height.
  114. float maxHeight_;
  115. };
  116. /// Physics collision shape component.
  117. class ATOMIC_API CollisionShape : public Component
  118. {
  119. OBJECT(CollisionShape);
  120. public:
  121. /// Construct.
  122. CollisionShape(Context* context);
  123. /// Destruct. Free the geometry data and clean up unused data from the geometry data cache.
  124. virtual ~CollisionShape();
  125. /// Register object factory.
  126. static void RegisterObject(Context* context);
  127. /// Handle attribute write access.
  128. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  129. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  130. virtual void ApplyAttributes();
  131. /// Handle enabled/disabled state change.
  132. virtual void OnSetEnabled();
  133. /// Visualize the component as debug geometry.
  134. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  135. /// Set as a box.
  136. void SetBox(const Vector3& size, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  137. /// Set as a sphere.
  138. void SetSphere(float diameter, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  139. /// Set as a static plane.
  140. void SetStaticPlane(const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  141. /// Set as a cylinder.
  142. void SetCylinder
  143. (float diameter, float height, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  144. /// Set as a capsule.
  145. void SetCapsule
  146. (float diameter, float height, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  147. /// Set as a cone.
  148. void SetCone
  149. (float diameter, float height, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  150. /// Set as a triangle mesh from Model. If you update a model's geometry and want to reapply the shape, call physicsWorld->RemoveCachedGeometry(model) first.
  151. void SetTriangleMesh
  152. (Model* model, unsigned lodLevel = 0, const Vector3& scale = Vector3::ONE, const Vector3& position = Vector3::ZERO,
  153. const Quaternion& rotation = Quaternion::IDENTITY);
  154. /// Set as a triangle mesh from CustomGeometry.
  155. void SetCustomTriangleMesh(CustomGeometry* custom, const Vector3& scale = Vector3::ONE, const Vector3& position = Vector3::ZERO,
  156. const Quaternion& rotation = Quaternion::IDENTITY);
  157. /// Set as a convex hull from Model.
  158. void SetConvexHull
  159. (Model* model, unsigned lodLevel = 0, const Vector3& scale = Vector3::ONE, const Vector3& position = Vector3::ZERO,
  160. const Quaternion& rotation = Quaternion::IDENTITY);
  161. /// Set as a convex hull from CustomGeometry.
  162. void SetCustomConvexHull(CustomGeometry* custom, const Vector3& scale = Vector3::ONE, const Vector3& position = Vector3::ZERO,
  163. const Quaternion& rotation = Quaternion::IDENTITY);
  164. /// Set as a terrain. Only works if the same scene node contains a Terrain component.
  165. void SetTerrain(unsigned lodLevel = 0);
  166. /// Set shape type.
  167. void SetShapeType(ShapeType type);
  168. /// Set shape size.
  169. void SetSize(const Vector3& size);
  170. /// Set offset position.
  171. void SetPosition(const Vector3& position);
  172. /// Set offset rotation.
  173. void SetRotation(const Quaternion& rotation);
  174. /// Set offset transform.
  175. void SetTransform(const Vector3& position, const Quaternion& rotation);
  176. /// Set collision margin.
  177. void SetMargin(float margin);
  178. /// Set triangle mesh / convex hull model.
  179. void SetModel(Model* model);
  180. /// Set model LOD level.
  181. void SetLodLevel(unsigned lodLevel);
  182. /// Return Bullet collision shape.
  183. btCollisionShape* GetCollisionShape() const { return shape_; }
  184. /// Return the shared geometry data.
  185. CollisionGeometryData* GetGeometryData() const { return geometry_; }
  186. /// Return physics world.
  187. PhysicsWorld* GetPhysicsWorld() const { return physicsWorld_; }
  188. /// Return shape type.
  189. ShapeType GetShapeType() const { return shapeType_; }
  190. /// Return shape size.
  191. const Vector3& GetSize() const { return size_; }
  192. /// Return offset position.
  193. const Vector3& GetPosition() const { return position_; }
  194. /// Return offset rotation.
  195. const Quaternion& GetRotation() const { return rotation_; }
  196. /// Return collision margin.
  197. float GetMargin() const { return margin_; }
  198. /// Return triangle mesh / convex hull model.
  199. Model* GetModel() const { return model_; }
  200. /// Return model LOD level.
  201. unsigned GetLodLevel() const { return lodLevel_; }
  202. /// Return world-space bounding box.
  203. BoundingBox GetWorldBoundingBox() const;
  204. /// Update the new collision shape to the RigidBody.
  205. void NotifyRigidBody(bool updateMass = true);
  206. /// Set model attribute.
  207. void SetModelAttr(const ResourceRef& value);
  208. /// Return model attribute.
  209. ResourceRef GetModelAttr() const;
  210. /// Release the collision shape.
  211. void ReleaseShape();
  212. protected:
  213. /// Handle node being assigned.
  214. virtual void OnNodeSet(Node* node);
  215. /// Handle scene being assigned.
  216. virtual void OnSceneSet(Scene* scene);
  217. /// Handle node transform being dirtied.
  218. virtual void OnMarkedDirty(Node* node);
  219. private:
  220. /// Find the parent rigid body component and return its compound collision shape.
  221. btCompoundShape* GetParentCompoundShape();
  222. /// Update the collision shape after attribute changes.
  223. void UpdateShape();
  224. /// Update terrain collision shape from the terrain component.
  225. void HandleTerrainCreated(StringHash eventType, VariantMap& eventData);
  226. /// Update trimesh or convex shape after a model has reloaded itself.
  227. void HandleModelReloadFinished(StringHash eventType, VariantMap& eventData);
  228. /// Physics world.
  229. WeakPtr<PhysicsWorld> physicsWorld_;
  230. /// Rigid body.
  231. WeakPtr<RigidBody> rigidBody_;
  232. /// Model.
  233. SharedPtr<Model> model_;
  234. /// Shared geometry data.
  235. SharedPtr<CollisionGeometryData> geometry_;
  236. /// Bullet collision shape.
  237. btCollisionShape* shape_;
  238. /// Collision shape type.
  239. ShapeType shapeType_;
  240. /// Offset position.
  241. Vector3 position_;
  242. /// Offset rotation.
  243. Quaternion rotation_;
  244. /// Shape size.
  245. Vector3 size_;
  246. /// Cached world scale for determining if the collision shape needs update.
  247. Vector3 cachedWorldScale_;
  248. /// Model LOD level.
  249. unsigned lodLevel_;
  250. /// CustomGeometry component ID for convex hull mode. 0 if not creating the convex hull from a CustomGeometry.
  251. unsigned customGeometryID_;
  252. /// Collision margin.
  253. float margin_;
  254. /// Recreate collision shape flag.
  255. bool recreateShape_;
  256. /// Shape creation retry flag if attributes initially set without scene.
  257. bool retryCreation_;
  258. };
  259. }