File indexing completed on 2025-08-03 03:49:58

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_FRICTION_JOINT_H
0020 #define B2_FRICTION_JOINT_H
0021 
0022 #include <Box2D/Dynamics/Joints/b2Joint.h>
0023 
0024 /// Friction joint definition.
0025 struct b2FrictionJointDef : public b2JointDef
0026 {
0027     b2FrictionJointDef()
0028     {
0029         type = e_frictionJoint;
0030         localAnchorA.SetZero();
0031         localAnchorB.SetZero();
0032         maxForce = 0.0f;
0033         maxTorque = 0.0f;
0034     }
0035 
0036     /// Initialize the bodies, anchors, axis, and reference angle using the world
0037     /// anchor and world axis.
0038     void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor);
0039 
0040     /// The local anchor point relative to bodyA's origin.
0041     b2Vec2 localAnchorA;
0042 
0043     /// The local anchor point relative to bodyB's origin.
0044     b2Vec2 localAnchorB;
0045 
0046     /// The maximum friction force in N.
0047     qreal maxForce;
0048 
0049     /// The maximum friction torque in N-m.
0050     qreal maxTorque;
0051 };
0052 
0053 /// Friction joint. This is used for top-down friction.
0054 /// It provides 2D translational friction and angular friction.
0055 class b2FrictionJoint : public b2Joint
0056 {
0057 public:
0058     b2Vec2 GetAnchorA() const override;
0059     b2Vec2 GetAnchorB() const override;
0060 
0061     b2Vec2 GetReactionForce(qreal inv_dt) const override;
0062     qreal GetReactionTorque(qreal inv_dt) const override;
0063 
0064     /// Set the maximum friction force in N.
0065     void SetMaxForce(qreal force);
0066 
0067     /// Get the maximum friction force in N.
0068     qreal GetMaxForce() const;
0069 
0070     /// Set the maximum friction torque in N*m.
0071     void SetMaxTorque(qreal torque);
0072 
0073     /// Get the maximum friction torque in N*m.
0074     qreal GetMaxTorque() const;
0075 
0076 protected:
0077 
0078     friend class b2Joint;
0079 
0080     b2FrictionJoint(const b2FrictionJointDef* def);
0081 
0082     void InitVelocityConstraints(const b2TimeStep& step) override;
0083     void SolveVelocityConstraints(const b2TimeStep& step) override;
0084     bool SolvePositionConstraints(qreal baumgarte) override;
0085 
0086     b2Vec2 m_localAnchorA;
0087     b2Vec2 m_localAnchorB;
0088 
0089     b2Mat22 m_linearMass;
0090     qreal m_angularMass;
0091 
0092     b2Vec2 m_linearImpulse;
0093     qreal m_angularImpulse;
0094 
0095     qreal m_maxForce;
0096     qreal m_maxTorque;
0097 };
0098 
0099 #endif