b2_mouse_joint.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // MIT License
  2. // Copyright (c) 2019 Erin Catto
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #ifndef B2_MOUSE_JOINT_H
  19. #define B2_MOUSE_JOINT_H
  20. #include "b2_api.h"
  21. #include "b2_joint.h"
  22. /// Mouse joint definition. This requires a world target point,
  23. /// tuning parameters, and the time step.
  24. struct B2_API b2MouseJointDef : public b2JointDef
  25. {
  26. b2MouseJointDef()
  27. {
  28. type = e_mouseJoint;
  29. target.Set(0.0f, 0.0f);
  30. maxForce = 0.0f;
  31. stiffness = 0.0f;
  32. damping = 0.0f;
  33. }
  34. /// The initial world target point. This is assumed
  35. /// to coincide with the body anchor initially.
  36. b2Vec2 target;
  37. /// The maximum constraint force that can be exerted
  38. /// to move the candidate body. Usually you will express
  39. /// as some multiple of the weight (multiplier * mass * gravity).
  40. float maxForce;
  41. /// The linear stiffness in N/m
  42. float stiffness;
  43. /// The linear damping in N*s/m
  44. float damping;
  45. };
  46. /// A mouse joint is used to make a point on a body track a
  47. /// specified world point. This a soft constraint with a maximum
  48. /// force. This allows the constraint to stretch and without
  49. /// applying huge forces.
  50. /// NOTE: this joint is not documented in the manual because it was
  51. /// developed to be used in the testbed. If you want to learn how to
  52. /// use the mouse joint, look at the testbed.
  53. class B2_API b2MouseJoint : public b2Joint
  54. {
  55. public:
  56. /// Implements b2Joint.
  57. b2Vec2 GetAnchorA() const override;
  58. /// Implements b2Joint.
  59. b2Vec2 GetAnchorB() const override;
  60. /// Implements b2Joint.
  61. b2Vec2 GetReactionForce(float inv_dt) const override;
  62. /// Implements b2Joint.
  63. float GetReactionTorque(float inv_dt) const override;
  64. /// Use this to update the target point.
  65. void SetTarget(const b2Vec2& target);
  66. const b2Vec2& GetTarget() const;
  67. /// Set/get the maximum force in Newtons.
  68. void SetMaxForce(float force);
  69. float GetMaxForce() const;
  70. /// Set/get the linear stiffness in N/m
  71. void SetStiffness(float stiffness) { m_stiffness = stiffness; }
  72. float GetStiffness() const { return m_stiffness; }
  73. /// Set/get linear damping in N*s/m
  74. void SetDamping(float damping) { m_damping = damping; }
  75. float GetDamping() const { return m_damping; }
  76. /// The mouse joint does not support dumping.
  77. void Dump() override { b2Log("Mouse joint dumping is not supported.\n"); }
  78. /// Implement b2Joint::ShiftOrigin
  79. void ShiftOrigin(const b2Vec2& newOrigin) override;
  80. protected:
  81. friend class b2Joint;
  82. b2MouseJoint(const b2MouseJointDef* def);
  83. void InitVelocityConstraints(const b2SolverData& data) override;
  84. void SolveVelocityConstraints(const b2SolverData& data) override;
  85. bool SolvePositionConstraints(const b2SolverData& data) override;
  86. b2Vec2 m_localAnchorB;
  87. b2Vec2 m_targetA;
  88. float m_stiffness;
  89. float m_damping;
  90. float m_beta;
  91. // Solver shared
  92. b2Vec2 m_impulse;
  93. float m_maxForce;
  94. float m_gamma;
  95. // Solver temp
  96. int32 m_indexA;
  97. int32 m_indexB;
  98. b2Vec2 m_rB;
  99. b2Vec2 m_localCenterB;
  100. float m_invMassB;
  101. float m_invIB;
  102. b2Mat22 m_mass;
  103. b2Vec2 m_C;
  104. };
  105. #endif