CollisionShape.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "BoundingBox.h"
  25. #include "ArrayPtr.h"
  26. #include "Component.h"
  27. #include "Quaternion.h"
  28. class DebugRenderer;
  29. class Geometry;
  30. class Model;
  31. class PhysicsWorld;
  32. class RigidBody;
  33. class btBvhTriangleMeshShape;
  34. class btCollisionShape;
  35. class btCompoundShape;
  36. class btTriangleMesh;
  37. /// Collision shape type.
  38. enum ShapeType
  39. {
  40. SHAPE_BOX = 0,
  41. SHAPE_SPHERE,
  42. SHAPE_CYLINDER,
  43. SHAPE_CAPSULE,
  44. SHAPE_CONE,
  45. SHAPE_TRIANGLEMESH,
  46. SHAPE_CONVEXHULL
  47. };
  48. /// Base class for collision shape geometry data.
  49. struct CollisionGeometryData : public RefCounted
  50. {
  51. /// Original model name
  52. String modelName_;
  53. };
  54. /// Triangle mesh geometry data.
  55. struct TriangleMeshData : public CollisionGeometryData
  56. {
  57. /// Construct from a model.
  58. TriangleMeshData(Model* model, unsigned lodLevel);
  59. /// Destruct. Free geometry data.
  60. ~TriangleMeshData();
  61. /// Bullet triangle mesh data.
  62. btTriangleMesh* meshData_;
  63. /// Bullet triangle mesh collision shape.
  64. btBvhTriangleMeshShape* shape_;
  65. };
  66. /// Convex hull geometry data.
  67. struct ConvexData : public CollisionGeometryData
  68. {
  69. /// Construct from a model.
  70. ConvexData(Model* model, unsigned lodLevel);
  71. /// Destruct. Free geometry data.
  72. ~ConvexData();
  73. /// Vertex data.
  74. SharedArrayPtr<Vector3> vertexData_;
  75. /// Number of vertices.
  76. unsigned vertexCount_;
  77. };
  78. /// Physics collision shape component.
  79. class CollisionShape : public Component
  80. {
  81. OBJECT(CollisionShape);
  82. public:
  83. /// Construct.
  84. CollisionShape(Context* context);
  85. /// Destruct. Free the geometry data and clean up unused data from the geometry data cache.
  86. virtual ~CollisionShape();
  87. /// Register object factory.
  88. static void RegisterObject(Context* context);
  89. /// Handle attribute write access.
  90. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  91. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  92. virtual void ApplyAttributes();
  93. /// %Set as a sphere.
  94. void SetSphere(float diameter, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  95. /// %Set as a box.
  96. void SetBox(const Vector3& size, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  97. /// %Set as a cylinder.
  98. void SetCylinder(float diameter, float height, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  99. /// %Set as a capsule.
  100. void SetCapsule(float diameter, float height, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  101. /// %Set as a cone.
  102. void SetCone(float diameter, float height, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  103. /// %Set as a triangle mesh.
  104. void SetTriangleMesh(Model* model, unsigned lodLevel, const Vector3& scale = Vector3::ONE, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  105. /// %Set as a convex hull.
  106. void SetConvexHull(Model* model, unsigned lodLevel, const Vector3& scale = Vector3::ONE, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  107. /// %Set shape type.
  108. void SetShapeType(ShapeType type);
  109. /// %Set shape size.
  110. void SetSize(const Vector3& size);
  111. /// %Set offset position.
  112. void SetPosition(const Vector3& position);
  113. /// %Set offset rotation.
  114. void SetRotation(const Quaternion& rotation);
  115. /// %Set offset transform.
  116. void SetTransform(const Vector3& position, const Quaternion& rotation);
  117. /// %Set collision margin.
  118. void SetMargin(float margin);
  119. /// %Set triangle mesh / convex hull model.
  120. void SetModel(Model* model);
  121. /// %Set model LOD level.
  122. void SetLodLevel(unsigned lodLevel);
  123. /// Return Bullet collision shape.
  124. btCollisionShape* GetCollisionShape() const { return shape_; }
  125. /// Return physics world.
  126. PhysicsWorld* GetPhysicsWorld() const { return physicsWorld_; }
  127. /// Return shape type.
  128. ShapeType GetShapeType() const { return shapeType_; }
  129. /// Return shape size.
  130. const Vector3& GetSize() const { return size_; }
  131. /// Return offset position.
  132. const Vector3& GetPosition() const { return position_; }
  133. /// Return offset rotation.
  134. const Quaternion& GetRotation() const { return rotation_; }
  135. /// Return collision margin.
  136. float GetMargin() const { return margin_; }
  137. /// Return triangle mesh / convex hull model.
  138. Model* GetModel() const { return model_; }
  139. /// Return model LOD level.
  140. unsigned GetLodLevel() const { return lodLevel_; }
  141. /// Update the new collision shape to the RigidBody, and tell it to update its mass.
  142. void NotifyRigidBody();
  143. /// Add debug geometry to the debug renderer.
  144. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  145. /// %Set model attribute.
  146. void SetModelAttr(ResourceRef value);
  147. /// Return model attribute.
  148. ResourceRef GetModelAttr() const;
  149. protected:
  150. /// Handle node being assigned.
  151. virtual void OnNodeSet(Node* node);
  152. /// Handle node transform being dirtied.
  153. virtual void OnMarkedDirty(Node* node);
  154. private:
  155. /// Find the parent rigid body component and return its compound collision shape.
  156. btCompoundShape* GetParentCompoundShape();
  157. /// Update the collision shape after attribute changes.
  158. void UpdateShape();
  159. /// Release the collision shape.
  160. void ReleaseShape();
  161. /// Physics world.
  162. WeakPtr<PhysicsWorld> physicsWorld_;
  163. /// Rigid body.
  164. WeakPtr<RigidBody> rigidBody_;
  165. /// Model.
  166. SharedPtr<Model> model_;
  167. /// Shared geometry data.
  168. SharedPtr<CollisionGeometryData> geometry_;
  169. /// Bullet collision shape.
  170. btCollisionShape* shape_;
  171. /// Collision shape type.
  172. ShapeType shapeType_;
  173. /// Offset position.
  174. Vector3 position_;
  175. /// Offset rotation.
  176. Quaternion rotation_;
  177. /// Shape size.
  178. Vector3 size_;
  179. /// Cached world scale for determining if the collision shape needs update.
  180. Vector3 cachedWorldScale_;
  181. /// Model LOD level.
  182. unsigned lodLevel_;
  183. /// Collision margin.
  184. float margin_;
  185. /// Dirty flag.
  186. bool dirty_;
  187. };