Constraint.h 7.2 KB

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