Constraint.h 7.9 KB

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