File indexing completed on 2025-08-03 03:49:59
0001 /* 0002 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com 0003 * 0004 * This software is provided 'as-is', without any express or implied 0005 * warranty. In no event will the authors be held liable for any damages 0006 * arising from the use of this software. 0007 * Permission is granted to anyone to use this software for any purpose, 0008 * including commercial applications, and to alter it and redistribute it 0009 * freely, subject to the following restrictions: 0010 * 1. The origin of this software must not be misrepresented; you must not 0011 * claim that you wrote the original software. If you use this software 0012 * in a product, an acknowledgment in the product documentation would be 0013 * appreciated but is not required. 0014 * 2. Altered source versions must be plainly marked as such, and must not be 0015 * misrepresented as being the original software. 0016 * 3. This notice may not be removed or altered from any source distribution. 0017 */ 0018 0019 #ifndef B2_MOUSE_JOINT_H 0020 #define B2_MOUSE_JOINT_H 0021 0022 #include <Box2D/Dynamics/Joints/b2Joint.h> 0023 0024 /// Mouse joint definition. This requires a world target point, 0025 /// tuning parameters, and the time step. 0026 struct b2MouseJointDef : public b2JointDef 0027 { 0028 b2MouseJointDef() 0029 { 0030 type = e_mouseJoint; 0031 target.Set(0.0f, 0.0f); 0032 maxForce = 0.0f; 0033 frequencyHz = 5.0f; 0034 dampingRatio = 0.7f; 0035 } 0036 0037 /// The initial world target point. This is assumed 0038 /// to coincide with the body anchor initially. 0039 b2Vec2 target; 0040 0041 /// The maximum constraint force that can be exerted 0042 /// to move the candidate body. Usually you will express 0043 /// as some multiple of the weight (multiplier * mass * gravity). 0044 qreal maxForce; 0045 0046 /// The response speed. 0047 qreal frequencyHz; 0048 0049 /// The damping ratio. 0 = no damping, 1 = critical damping. 0050 qreal dampingRatio; 0051 }; 0052 0053 /// A mouse joint is used to make a point on a body track a 0054 /// specified world point. This a soft constraint with a maximum 0055 /// force. This allows the constraint to stretch and without 0056 /// applying huge forces. 0057 /// NOTE: this joint is not documented in the manual because it was 0058 /// developed to be used in the testbed. If you want to learn how to 0059 /// use the mouse joint, look at the testbed. 0060 class b2MouseJoint : public b2Joint 0061 { 0062 public: 0063 0064 /// Implements b2Joint. 0065 b2Vec2 GetAnchorA() const override; 0066 0067 /// Implements b2Joint. 0068 b2Vec2 GetAnchorB() const override; 0069 0070 /// Implements b2Joint. 0071 b2Vec2 GetReactionForce(qreal inv_dt) const override; 0072 0073 /// Implements b2Joint. 0074 qreal GetReactionTorque(qreal inv_dt) const override; 0075 0076 /// Use this to update the target point. 0077 void SetTarget(const b2Vec2& target); 0078 const b2Vec2& GetTarget() const; 0079 0080 /// Set/get the maximum force in Newtons. 0081 void SetMaxForce(qreal force); 0082 qreal GetMaxForce() const; 0083 0084 /// Set/get the frequency in Hertz. 0085 void SetFrequency(qreal hz); 0086 qreal GetFrequency() const; 0087 0088 /// Set/get the damping ratio (dimensionless). 0089 void SetDampingRatio(qreal ratio); 0090 qreal GetDampingRatio() const; 0091 0092 protected: 0093 friend class b2Joint; 0094 0095 b2MouseJoint(const b2MouseJointDef* def); 0096 0097 void InitVelocityConstraints(const b2TimeStep& step) override; 0098 void SolveVelocityConstraints(const b2TimeStep& step) override; 0099 bool SolvePositionConstraints(qreal baumgarte) override { B2_NOT_USED(baumgarte); return true; } 0100 0101 b2Vec2 m_localAnchor; 0102 b2Vec2 m_target; 0103 b2Vec2 m_impulse; 0104 0105 b2Mat22 m_mass; // effective mass for point-to-point constraint. 0106 b2Vec2 m_C; // position error 0107 qreal m_maxForce; 0108 qreal m_frequencyHz; 0109 qreal m_dampingRatio; 0110 qreal m_beta; 0111 qreal m_gamma; 0112 }; 0113 0114 #endif