CollisionShape.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // Copyright (c) 2008-2013 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. namespace Urho3D
  32. {
  33. class Geometry;
  34. class Model;
  35. class PhysicsWorld;
  36. class RigidBody;
  37. class Terrain;
  38. /// Collision shape type.
  39. enum ShapeType
  40. {
  41. SHAPE_BOX = 0,
  42. SHAPE_SPHERE,
  43. SHAPE_STATICPLANE,
  44. SHAPE_CYLINDER,
  45. SHAPE_CAPSULE,
  46. SHAPE_CONE,
  47. SHAPE_TRIANGLEMESH,
  48. SHAPE_CONVEXHULL,
  49. SHAPE_TERRAIN
  50. };
  51. /// Base class for collision shape geometry data.
  52. struct CollisionGeometryData : public RefCounted
  53. {
  54. /// Original model name
  55. String modelName_;
  56. };
  57. /// Triangle mesh geometry data.
  58. struct TriangleMeshData : public CollisionGeometryData
  59. {
  60. /// Construct from a model.
  61. TriangleMeshData(Model* model, unsigned lodLevel);
  62. /// Destruct. Free geometry data.
  63. ~TriangleMeshData();
  64. /// Bullet triangle mesh data.
  65. btTriangleMesh* meshData_;
  66. /// Bullet triangle mesh collision shape.
  67. btBvhTriangleMeshShape* shape_;
  68. };
  69. /// Convex hull geometry data.
  70. struct ConvexData : public CollisionGeometryData
  71. {
  72. /// Construct from a model.
  73. ConvexData(Model* model, unsigned lodLevel);
  74. /// Destruct. Free geometry data.
  75. ~ConvexData();
  76. /// Vertex data.
  77. SharedArrayPtr<Vector3> vertexData_;
  78. /// Number of vertices.
  79. unsigned vertexCount_;
  80. /// Index data.
  81. SharedArrayPtr<unsigned> indexData_;
  82. /// Number of indices.
  83. unsigned indexCount_;
  84. };
  85. /// Heightfield geometry data.
  86. struct HeightfieldData : public CollisionGeometryData
  87. {
  88. /// Construct from a terrain.
  89. HeightfieldData(Terrain* terrain);
  90. /// Destruct. Free geometry data.
  91. ~HeightfieldData();
  92. /// Height data.
  93. SharedArrayPtr<float> heightData_;
  94. /// Vertex spacing.
  95. Vector3 spacing_;
  96. /// Heightmap size.
  97. IntVector2 size_;
  98. /// Minimum height.
  99. float minHeight_;
  100. /// Maximum height.
  101. float maxHeight_;
  102. };
  103. /// Physics collision shape component.
  104. class CollisionShape : public Component
  105. {
  106. OBJECT(CollisionShape);
  107. public:
  108. /// Construct.
  109. CollisionShape(Context* context);
  110. /// Destruct. Free the geometry data and clean up unused data from the geometry data cache.
  111. virtual ~CollisionShape();
  112. /// Register object factory.
  113. static void RegisterObject(Context* context);
  114. /// Handle attribute write access.
  115. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  116. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  117. virtual void ApplyAttributes();
  118. /// Handle enabled/disabled state change.
  119. virtual void OnSetEnabled();
  120. /// Visualize the component as debug geometry.
  121. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  122. /// Set as a box.
  123. void SetBox(const Vector3& size, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  124. /// Set as a sphere.
  125. void SetSphere(float diameter, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  126. /// Set as a cylinder.
  127. void SetCylinder(float diameter, float height, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  128. /// Set as a capsule.
  129. void SetCapsule(float diameter, float height, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  130. /// Set as a cone.
  131. void SetCone(float diameter, float height, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  132. /// Set as a triangle mesh.
  133. void SetTriangleMesh(Model* model, unsigned lodLevel, const Vector3& scale = Vector3::ONE, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  134. /// Set as a convex hull.
  135. void SetConvexHull(Model* model, unsigned lodLevel, const Vector3& scale = Vector3::ONE, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  136. /// Set as a terrain. Only works if the same scene node contains a Terrain component.
  137. void SetTerrain();
  138. /// Set shape type.
  139. void SetShapeType(ShapeType type);
  140. /// Set shape size.
  141. void SetSize(const Vector3& size);
  142. /// Set offset position.
  143. void SetPosition(const Vector3& position);
  144. /// Set offset rotation.
  145. void SetRotation(const Quaternion& rotation);
  146. /// Set offset transform.
  147. void SetTransform(const Vector3& position, const Quaternion& rotation);
  148. /// Set collision margin.
  149. void SetMargin(float margin);
  150. /// Set triangle mesh / convex hull model.
  151. void SetModel(Model* model);
  152. /// Set model LOD level.
  153. void SetLodLevel(unsigned lodLevel);
  154. /// Return Bullet collision shape.
  155. btCollisionShape* GetCollisionShape() const { return shape_; }
  156. /// Return the shared geometry data.
  157. CollisionGeometryData* GetGeometryData() const { return geometry_; }
  158. /// Return physics world.
  159. PhysicsWorld* GetPhysicsWorld() const { return physicsWorld_; }
  160. /// Return shape type.
  161. ShapeType GetShapeType() const { return shapeType_; }
  162. /// Return shape size.
  163. const Vector3& GetSize() const { return size_; }
  164. /// Return offset position.
  165. const Vector3& GetPosition() const { return position_; }
  166. /// Return offset rotation.
  167. const Quaternion& GetRotation() const { return rotation_; }
  168. /// Return collision margin.
  169. float GetMargin() const { return margin_; }
  170. /// Return triangle mesh / convex hull model.
  171. Model* GetModel() const { return model_; }
  172. /// Return model LOD level.
  173. unsigned GetLodLevel() const { return lodLevel_; }
  174. /// Return world-space bounding box.
  175. BoundingBox GetWorldBoundingBox() const;
  176. /// Update the new collision shape to the RigidBody, and tell it to update its mass.
  177. void NotifyRigidBody();
  178. /// Set model attribute.
  179. void SetModelAttr(ResourceRef value);
  180. /// Return model attribute.
  181. ResourceRef GetModelAttr() const;
  182. /// Release the collision shape.
  183. void ReleaseShape();
  184. protected:
  185. /// Handle node being assigned.
  186. virtual void OnNodeSet(Node* node);
  187. /// Handle node transform being dirtied.
  188. virtual void OnMarkedDirty(Node* node);
  189. private:
  190. /// Find the parent rigid body component and return its compound collision shape.
  191. btCompoundShape* GetParentCompoundShape();
  192. /// Update the collision shape after attribute changes.
  193. void UpdateShape();
  194. /// Update terrain collision shape from the terrain component.
  195. void HandleTerrainCreated(StringHash eventType, VariantMap& eventData);
  196. /// Physics world.
  197. WeakPtr<PhysicsWorld> physicsWorld_;
  198. /// Rigid body.
  199. WeakPtr<RigidBody> rigidBody_;
  200. /// Model.
  201. SharedPtr<Model> model_;
  202. /// Shared geometry data.
  203. SharedPtr<CollisionGeometryData> geometry_;
  204. /// Bullet collision shape.
  205. btCollisionShape* shape_;
  206. /// Collision shape type.
  207. ShapeType shapeType_;
  208. /// Offset position.
  209. Vector3 position_;
  210. /// Offset rotation.
  211. Quaternion rotation_;
  212. /// Shape size.
  213. Vector3 size_;
  214. /// Cached world scale for determining if the collision shape needs update.
  215. Vector3 cachedWorldScale_;
  216. /// Model LOD level.
  217. unsigned lodLevel_;
  218. /// Collision margin.
  219. float margin_;
  220. /// Recrease collision shape flag.
  221. bool recreateShape_;
  222. };
  223. }