CollisionShape.h 10 KB

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