Constraint.h 7.7 KB

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