CollisionShape.h 9.7 KB

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