CollisionShape.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 "ArrayPtr.h"
  26. #include "Component.h"
  27. #include "Quaternion.h"
  28. #include "PhysicsDefs.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. 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. /// Number of indices.
  65. unsigned indexCount_;
  66. };
  67. /// Heightfield geometry data.
  68. struct HeightfieldData : public CollisionGeometryData
  69. {
  70. /// Construct from a model.
  71. HeightfieldData(Model* model, IntVector2 numPoints, float thickness, unsigned lodLevel, const Vector3& scale);
  72. /// Destruct. Free geometry data.
  73. ~HeightfieldData();
  74. /// ODE heightfield geometry ID.
  75. dHeightfieldDataID heightfield_;
  76. /// Height values.
  77. SharedArrayPtr<float> heightData_;
  78. };
  79. /// Physics collision shape component.
  80. class CollisionShape : public Component
  81. {
  82. OBJECT(CollisionShape);
  83. public:
  84. /// Construct.
  85. CollisionShape(Context* context);
  86. /// Destruct. Free the geometry data and clean up unused data from the geometry data cache.
  87. virtual ~CollisionShape();
  88. /// Register object factory.
  89. static void RegisterObject(Context* context);
  90. /// Handle attribute write access.
  91. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  92. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  93. virtual void ApplyAttributes();
  94. /// Clear the collision geometry.
  95. void Clear();
  96. /// %Set as a sphere.
  97. void SetSphere(float diameter, 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 diameter, float height, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  102. /// %Set as a capsule.
  103. void SetCapsule(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& size = Vector3::ONE, const Vector3& position = Vector3::ZERO, const Quaternion& rotation = Quaternion::IDENTITY);
  106. /// %Set as a heightfield.
  107. void SetHeightfield(Model* model, unsigned xPoints, unsigned zPoints, float thickness, unsigned lodLevel, const Vector3& size = Vector3::ONE, 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, unsigned lodLevel, const Vector3& size = Vector3::ONE, 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 SetCollisionLayer(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. /// %Set phantom flag. Phantom shapes generate collision events only, but no contact joints.
  125. void SetPhantom(bool enable);
  126. /// Return physics world.
  127. PhysicsWorld* GetPhysicsWorld() const { return physicsWorld_; }
  128. /// Return model (trimesh & heightfield only.)
  129. Model* GetModel() const { return model_; }
  130. /// Return shape type.
  131. ShapeType GetShapeType() const { return shapeType_; }
  132. /// Return ODE geometry.
  133. dGeomID GetGeometry() const { return geometry_; }
  134. /// Return unscaled shape size.
  135. const Vector3& GetSize() const { return size_; }
  136. /// Return number of points in X & Z dimensions (heightfield only.)
  137. IntVector2 GetNumPoints() const { return numPoints_; }
  138. /// Return thickness (convex hull & heightfield only.)
  139. float GetThickness() const { return thickness_; }
  140. /// Return model LOD level (trimesh & heightfield only.)
  141. unsigned GetLodLevel() const { return lodLevel_; }
  142. /// Return offset position.
  143. const Vector3& GetPosition() const { return position_; }
  144. /// Return rotation.
  145. const Quaternion& GetRotation() const { return rotation_; }
  146. /// Return collision group bits.
  147. unsigned GetCollisionLayer() const { return collisionLayer_; }
  148. /// Return collision mask bits.
  149. unsigned GetCollisionMask() const { return collisionMask_; }
  150. /// Return friction coefficient.
  151. float GetFriction() const { return friction_; }
  152. /// Return bounce coefficient.
  153. float GetBounce() const { return bounce_; }
  154. /// Return phantom flag.
  155. bool IsPhantom() const { return phantom_; }
  156. /// Return the world-space bounding box
  157. BoundingBox GetWorldBoundingBox() const;
  158. /// Update geometry transform and associate with rigid body if available.
  159. void UpdateTransform(bool nodeUpdate = false);
  160. /// Add debug geometry to the debug renderer.
  161. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  162. /// %Set model attribute.
  163. void SetModelAttr(ResourceRef value);
  164. /// Return model attribute.
  165. ResourceRef GetModelAttr() const;
  166. protected:
  167. /// Handle node being assigned.
  168. virtual void OnNodeSet(Node* node);
  169. /// Handle node transform being dirtied.
  170. virtual void OnMarkedDirty(Node* node);
  171. private:
  172. /// Create new geometry.
  173. void CreateGeometry();
  174. /// Remove existing geometry.
  175. void ReleaseGeometry(bool notifyBody = true);
  176. /// Physics world.
  177. WeakPtr<PhysicsWorld> physicsWorld_;
  178. /// Model for trimesh & heightfield geometry.
  179. SharedPtr<Model> model_;
  180. /// Geometry data for trimesh & heightfield geometry.
  181. SharedPtr<CollisionGeometryData> geometryData_;
  182. /// ODE geometry.
  183. dGeomID geometry_;
  184. /// Shape type.
  185. ShapeType shapeType_;
  186. /// Unscaled size of shape.
  187. Vector3 size_;
  188. /// Number of heightfield points in X & Z dimensions.
  189. IntVector2 numPoints_;
  190. /// Thickness.
  191. float thickness_;
  192. /// LOD level.
  193. unsigned lodLevel_;
  194. /// Offset position.
  195. Vector3 position_;
  196. /// Rotation.
  197. Quaternion rotation_;
  198. /// Scene node scale used to create the geometry.
  199. Vector3 geometryScale_;
  200. /// Collision group bits.
  201. unsigned collisionLayer_;
  202. /// Collision mask bits.
  203. unsigned collisionMask_;
  204. /// Friction coefficient.
  205. float friction_;
  206. /// Bounce coefficient.
  207. float bounce_;
  208. /// Phantom flag.
  209. bool phantom_;
  210. /// Recreate geometry flag.
  211. bool recreateGeometry_;
  212. };