1/*
2* Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
3*
4* This software is provided 'as-is', without any express or implied
5* warranty. In no event will the authors be held liable for any damages
6* arising from the use of this software.
7* Permission is granted to anyone to use this software for any purpose,
8* including commercial applications, and to alter it and redistribute it
9* freely, subject to the following restrictions:
10* 1. The origin of this software must not be misrepresented; you must not
11* claim that you wrote the original software. If you use this software
12* in a product, an acknowledgment in the product documentation would be
13* appreciated but is not required.
14* 2. Altered source versions must be plainly marked as such, and must not be
15* misrepresented as being the original software.
16* 3. This notice may not be removed or altered from any source distribution.
17*/
18
19#ifndef B2_WELD_JOINT_H
20#define B2_WELD_JOINT_H
21
22#include <Box2D/Dynamics/Joints/b2Joint.h>
23
24/// Weld joint definition. You need to specify local anchor points
25/// where they are attached and the relative body angle. The position
26/// of the anchor points is important for computing the reaction torque.
27struct b2WeldJointDef : public b2JointDef
28{
29 b2WeldJointDef()
30 {
31 type = e_weldJoint;
32 localAnchorA.Set(0.0f, 0.0f);
33 localAnchorB.Set(0.0f, 0.0f);
34 referenceAngle = 0.0f;
35 frequencyHz = 0.0f;
36 dampingRatio = 0.0f;
37 }
38
39 /// Initialize the bodies, anchors, and reference angle using a world
40 /// anchor point.
41 void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor);
42
43 /// The local anchor point relative to bodyA's origin.
44 b2Vec2 localAnchorA;
45
46 /// The local anchor point relative to bodyB's origin.
47 b2Vec2 localAnchorB;
48
49 /// The bodyB angle minus bodyA angle in the reference state (radians).
50 float32 referenceAngle;
51
52 /// The mass-spring-damper frequency in Hertz. Rotation only.
53 /// Disable softness with a value of 0.
54 float32 frequencyHz;
55
56 /// The damping ratio. 0 = no damping, 1 = critical damping.
57 float32 dampingRatio;
58};
59
60/// A weld joint essentially glues two bodies together. A weld joint may
61/// distort somewhat because the island constraint solver is approximate.
62class b2WeldJoint : public b2Joint
63{
64public:
65 b2Vec2 GetAnchorA() const;
66 b2Vec2 GetAnchorB() const;
67
68 b2Vec2 GetReactionForce(float32 inv_dt) const;
69 float32 GetReactionTorque(float32 inv_dt) const;
70
71 /// The local anchor point relative to bodyA's origin.
72 const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
73
74 /// The local anchor point relative to bodyB's origin.
75 const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
76
77 /// Get the reference angle.
78 float32 GetReferenceAngle() const { return m_referenceAngle; }
79
80 /// Set/get frequency in Hz.
81 void SetFrequency(float32 hz) { m_frequencyHz = hz; }
82 float32 GetFrequency() const { return m_frequencyHz; }
83
84 /// Set/get damping ratio.
85 void SetDampingRatio(float32 ratio) { m_dampingRatio = ratio; }
86 float32 GetDampingRatio() const { return m_dampingRatio; }
87
88 /// Dump to b2Log
89 void Dump();
90
91protected:
92
93 friend class b2Joint;
94
95 b2WeldJoint(const b2WeldJointDef* def);
96
97 void InitVelocityConstraints(const b2SolverData& data);
98 void SolveVelocityConstraints(const b2SolverData& data);
99 bool SolvePositionConstraints(const b2SolverData& data);
100
101 float32 m_frequencyHz;
102 float32 m_dampingRatio;
103 float32 m_bias;
104
105 // Solver shared
106 b2Vec2 m_localAnchorA;
107 b2Vec2 m_localAnchorB;
108 float32 m_referenceAngle;
109 float32 m_gamma;
110 b2Vec3 m_impulse;
111
112 // Solver temp
113 int32 m_indexA;
114 int32 m_indexB;
115 b2Vec2 m_rA;
116 b2Vec2 m_rB;
117 b2Vec2 m_localCenterA;
118 b2Vec2 m_localCenterB;
119 float32 m_invMassA;
120 float32 m_invMassB;
121 float32 m_invIA;
122 float32 m_invIB;
123 b2Mat33 m_mass;
124};
125
126#endif
127