CollisionShape.h 8.4 KB

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