CollisionShape.h 7.5 KB

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