2
0

CollisionShape.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "Component.h"
  26. #include "Quaternion.h"
  27. #include "PhysicsDefs.h"
  28. #include "SharedArrayPtr.h"
  29. class DebugRenderer;
  30. class Geometry;
  31. class Model;
  32. class PhysicsWorld;
  33. /// Collision shape type
  34. enum ShapeType
  35. {
  36. SHAPE_NONE = 0,
  37. SHAPE_BOX,
  38. SHAPE_SPHERE,
  39. SHAPE_CAPSULE,
  40. SHAPE_CYLINDER,
  41. SHAPE_TRIANGLEMESH,
  42. SHAPE_HEIGHTFIELD,
  43. SHAPE_CONVEXHULL
  44. };
  45. /// Base class for collision shape geometry data
  46. struct CollisionGeometryData : public RefCounted
  47. {
  48. /// Original model name
  49. std::string modelName_;
  50. };
  51. /// Triangle mesh geometry data
  52. struct TriangleMeshData : public CollisionGeometryData
  53. {
  54. /// Construct from a model
  55. TriangleMeshData(Model* model, bool makeConvexHull, float thickness, unsigned lodLevel, const Vector3& scale);
  56. /// Destruct. Free geometry data
  57. ~TriangleMeshData();
  58. /// ODE trimesh geometry ID
  59. dTriMeshDataID triMesh_;
  60. /// Vertex data
  61. SharedArrayPtr<Vector3> vertexData_;
  62. /// Index data
  63. SharedArrayPtr<unsigned> indexData_;
  64. };
  65. /// Heightfield geometry data
  66. struct HeightfieldData : public CollisionGeometryData
  67. {
  68. /// Construct from a model
  69. HeightfieldData(Model* model, IntVector2 numPoints, float thickness, unsigned lodLevel, const Vector3& scale);
  70. /// Destruct. Free geometry data
  71. ~HeightfieldData();
  72. /// ODE heightfield geometry ID
  73. dHeightfieldDataID heightfield_;
  74. /// Height values
  75. SharedArrayPtr<float> heightData_;
  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& value);
  90. /// Handle attribute read access
  91. virtual Variant OnGetAttribute(const AttributeInfo& attr);
  92. /// Perform post-load after the whole scene has been loaded
  93. virtual void PostLoad();
  94. /// Clear the collision geometry
  95. void Clear();
  96. /// Set as a sphere
  97. void SetSphere(float radius, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  98. /// Set as a box
  99. void SetBox(const Vector3& size, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  100. /// Set as a cylinder
  101. void SetCylinder(float radius, float height, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  102. /// Set as a capsule
  103. void SetCapsule(float radius, 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 = M_MAX_UNSIGNED, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  106. /// Set as a heightfield
  107. void SetHeightfield(Model* model, const IntVector2& pointSize = IntVector2::ZERO, float thickness = 1.0f, unsigned lodLevel = M_MAX_UNSIGNED, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  108. /// Set as a convex hull (internally an ODE trimesh as well)
  109. void SetConvexHull(Model* model, float skinWidth = 0.0f, unsigned lodLevel = M_MAX_UNSIGNED, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  110. /// Set offset position
  111. void SetPosition(const Vector3& position);
  112. /// Set rotation
  113. void SetRotation(const Quaternion& rotation);
  114. /// Set offset transform
  115. void SetTransform(const Vector3& position, const Quaternion& rotation);
  116. /// Set collision group bits
  117. void SetCollisionGroup(unsigned group);
  118. /// Set collision mask bits
  119. void SetCollisionMask(unsigned mask);
  120. /// Set friction coefficient
  121. void SetFriction(float friction);
  122. /// Set bounce coefficient
  123. void SetBounce(float bounce);
  124. /// Return physics world
  125. PhysicsWorld* GetPhysicsWorld() const { return physicsWorld_; }
  126. /// Return model (trimesh & heightfield only)
  127. Model* GetModel() const { return model_; }
  128. /// Return shape type
  129. ShapeType GetShapeType() const { return shapeType_; }
  130. /// Return ODE geometry
  131. dGeomID GetGeometry() const { return geometry_; }
  132. /// Return unscaled shape size
  133. const Vector3& GetSize() const { return size_; }
  134. /// Return number of points in X & Z dimensions (heightfield only)
  135. IntVector2 GetNumPoints() const { return numPoints_; }
  136. /// Return thickness (convex hull & heightfield only)
  137. float GetThickness() const { return thickness_; }
  138. /// Return model LOD level (trimesh & heightfield only)
  139. unsigned GetLodLevel() const { return lodLevel_; }
  140. /// Return offset position
  141. const Vector3& GetPosition() const { return position_; }
  142. /// Return rotation
  143. const Quaternion& GetRotation() const { return rotation_; }
  144. /// Return collision group bits
  145. unsigned GetCollisionGroup() const { return collisionGroup_; }
  146. /// Return collision mask bits
  147. unsigned GetCollisionMask() const { return collisionMask_; }
  148. /// Return friction coefficient
  149. float GetFriction() const { return friction_; }
  150. /// Return bounce coefficient
  151. float GetBounce() const { return bounce_; }
  152. /// Update geometry transform and associate with rigid body if available
  153. void UpdateTransform();
  154. /// Add debug geometry to the debug graphics
  155. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  156. protected:
  157. /// Handle node being assigned
  158. virtual void OnNodeSet(Node* node);
  159. /// Handle node transform being dirtied
  160. virtual void OnMarkedDirty(Node* node);
  161. private:
  162. /// Create new geometry
  163. void CreateGeometry();
  164. /// Remove existing geometry
  165. void ReleaseGeometry(bool notifyBody = true);
  166. /// Physics world
  167. SharedPtr<PhysicsWorld> physicsWorld_;
  168. /// Model for trimesh & heightfield geometry
  169. SharedPtr<Model> model_;
  170. /// Geometry data for trimesh & heightfield geometry
  171. SharedPtr<CollisionGeometryData> geometryData_;
  172. /// ODE geometry
  173. dGeomID geometry_;
  174. /// Shape type
  175. ShapeType shapeType_;
  176. /// Unscaled size of shape
  177. Vector3 size_;
  178. /// Number of heightfield points in X & Z dimensions
  179. IntVector2 numPoints_;
  180. /// Thickness
  181. float thickness_;
  182. /// LOD level
  183. unsigned lodLevel_;
  184. /// Offset position
  185. Vector3 position_;
  186. /// Rotation
  187. Quaternion rotation_;
  188. /// Scene node scale used to create the geometry
  189. Vector3 geometryScale_;
  190. /// Collision group bits
  191. unsigned collisionGroup_;
  192. /// Collision mask bits
  193. unsigned collisionMask_;
  194. /// Friction coefficient
  195. float friction_;
  196. /// Bounce coefficient
  197. float bounce_;
  198. };