Constraint.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //
  2. // Copyright (c) 2008-2020 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. /// \file
  23. #pragma once
  24. #include "../Math/Vector3.h"
  25. #include "../Scene/Component.h"
  26. class btTypedConstraint;
  27. namespace Urho3D
  28. {
  29. /// Supported constraint types.
  30. enum ConstraintType
  31. {
  32. CONSTRAINT_POINT = 0,
  33. CONSTRAINT_HINGE,
  34. CONSTRAINT_SLIDER,
  35. CONSTRAINT_CONETWIST
  36. };
  37. class PhysicsWorld;
  38. class RigidBody;
  39. /// Physics constraint component. Connects two rigid bodies together, or one rigid body to a static point.
  40. class URHO3D_API Constraint : public Component
  41. {
  42. URHO3D_OBJECT(Constraint, Component);
  43. friend class RigidBody;
  44. public:
  45. /// Construct.
  46. explicit Constraint(Context* context);
  47. /// Destruct.
  48. ~Constraint() override;
  49. /// Register object factory.
  50. static void RegisterObject(Context* context);
  51. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  52. void ApplyAttributes() override;
  53. /// Handle enabled/disabled state change.
  54. void OnSetEnabled() override;
  55. /// Return the depended on nodes to order network updates.
  56. void GetDependencyNodes(PODVector<Node*>& dest) override;
  57. /// Visualize the component as debug geometry.
  58. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest) override;
  59. /// Set constraint type and recreate the constraint.
  60. /// @property
  61. void SetConstraintType(ConstraintType type);
  62. /// Set other body to connect to. Set to null to connect to the static world.
  63. /// @property
  64. void SetOtherBody(RigidBody* body);
  65. /// Set constraint position relative to own body.
  66. /// @property
  67. void SetPosition(const Vector3& position);
  68. /// Set constraint rotation relative to own body.
  69. /// @property
  70. void SetRotation(const Quaternion& rotation);
  71. /// Set constraint rotation relative to own body by specifying the axis.
  72. /// @property
  73. void SetAxis(const Vector3& axis);
  74. /// Set constraint position relative to the other body. If connected to the static world, is a world space position.
  75. /// @property
  76. void SetOtherPosition(const Vector3& position);
  77. /// Set constraint rotation relative to the other body. If connected to the static world, is a world space rotation.
  78. /// @property
  79. void SetOtherRotation(const Quaternion& rotation);
  80. /// Set constraint rotation relative to the other body by specifying the axis.
  81. /// @property
  82. void SetOtherAxis(const Vector3& axis);
  83. /// Set constraint world space position. Resets both own and other body relative position, ie. zeroes the constraint error.
  84. /// @property
  85. void SetWorldPosition(const Vector3& position);
  86. /// Set high limit. Interpretation is constraint type specific.
  87. /// @property
  88. void SetHighLimit(const Vector2& limit);
  89. /// Set low limit. Interpretation is constraint type specific.
  90. /// @property
  91. void SetLowLimit(const Vector2& limit);
  92. /// Set constraint error reduction parameter. Zero = leave to default.
  93. /// @property{set_erp}
  94. void SetERP(float erp);
  95. /// Set constraint force mixing parameter. Zero = leave to default.
  96. /// @property{set_cfm}
  97. void SetCFM(float cfm);
  98. /// Set whether to disable collisions between connected bodies.
  99. /// @property
  100. void SetDisableCollision(bool disable);
  101. /// Return physics world.
  102. PhysicsWorld* GetPhysicsWorld() const { return physicsWorld_; }
  103. /// Return Bullet constraint.
  104. btTypedConstraint* GetConstraint() const { return constraint_.Get(); }
  105. /// Return constraint type.
  106. /// @property
  107. ConstraintType GetConstraintType() const { return constraintType_; }
  108. /// Return rigid body in own scene node.
  109. /// @property
  110. RigidBody* GetOwnBody() const { return ownBody_; }
  111. /// Return the other rigid body. May be null if connected to the static world.
  112. /// @property
  113. RigidBody* GetOtherBody() const { return otherBody_; }
  114. /// Return constraint position relative to own body.
  115. /// @property
  116. const Vector3& GetPosition() const { return position_; }
  117. /// Return constraint rotation relative to own body.
  118. /// @property
  119. const Quaternion& GetRotation() const { return rotation_; }
  120. /// Return constraint position relative to other body.
  121. /// @property
  122. const Vector3& GetOtherPosition() const { return otherPosition_; }
  123. /// Return constraint rotation relative to other body.
  124. /// @property
  125. const Quaternion& GetOtherRotation() const { return otherRotation_; }
  126. /// Return constraint world position, calculated from own body.
  127. /// @property
  128. Vector3 GetWorldPosition() const;
  129. /// Return high limit.
  130. /// @property
  131. const Vector2& GetHighLimit() const { return highLimit_; }
  132. /// Return low limit.
  133. /// @property
  134. const Vector2& GetLowLimit() const { return lowLimit_; }
  135. /// Return constraint error reduction parameter.
  136. /// @property{get_erp}
  137. float GetERP() const { return erp_; }
  138. /// Return constraint force mixing parameter.
  139. /// @property{get_cfm}
  140. float GetCFM() const { return cfm_; }
  141. /// Return whether collisions between connected bodies are disabled.
  142. /// @property
  143. bool GetDisableCollision() const { return disableCollision_; }
  144. /// Release the constraint.
  145. void ReleaseConstraint();
  146. /// Apply constraint frames.
  147. void ApplyFrames();
  148. protected:
  149. /// Handle node being assigned.
  150. void OnNodeSet(Node* node) override;
  151. /// Handle scene being assigned.
  152. void OnSceneSet(Scene* scene) override;
  153. /// Handle node transform being dirtied.
  154. void OnMarkedDirty(Node* node) override;
  155. private:
  156. /// Create the constraint.
  157. void CreateConstraint();
  158. /// Apply high and low constraint limits.
  159. void ApplyLimits();
  160. /// Adjust other body position.
  161. void AdjustOtherBodyPosition();
  162. /// Mark constraint dirty.
  163. void MarkConstraintDirty() { recreateConstraint_ = true; }
  164. /// Mark frames dirty.
  165. void MarkFramesDirty() { framesDirty_ = true; }
  166. /// Physics world.
  167. WeakPtr<PhysicsWorld> physicsWorld_;
  168. /// Own rigid body.
  169. WeakPtr<RigidBody> ownBody_;
  170. /// Other rigid body.
  171. WeakPtr<RigidBody> otherBody_;
  172. /// Bullet constraint.
  173. UniquePtr<btTypedConstraint> constraint_;
  174. /// Constraint type.
  175. ConstraintType constraintType_;
  176. /// Constraint position.
  177. Vector3 position_;
  178. /// Constraint rotation.
  179. Quaternion rotation_;
  180. /// Constraint other body position.
  181. Vector3 otherPosition_;
  182. /// Constraint other body axis.
  183. Quaternion otherRotation_;
  184. /// Cached world scale for determining if the constraint position needs update.
  185. Vector3 cachedWorldScale_;
  186. /// High limit.
  187. Vector2 highLimit_;
  188. /// Low limit.
  189. Vector2 lowLimit_;
  190. /// Error reduction parameter.
  191. float erp_;
  192. /// Constraint force mixing parameter.
  193. float cfm_;
  194. /// Other body node ID for pending constraint recreation.
  195. unsigned otherBodyNodeID_;
  196. /// Disable collision between connected bodies flag.
  197. bool disableCollision_;
  198. /// Recreate constraint flag.
  199. bool recreateConstraint_;
  200. /// Coordinate frames dirty flag.
  201. bool framesDirty_;
  202. /// Constraint creation retry flag if attributes initially set without scene.
  203. bool retryCreation_;
  204. };
  205. }