first commit
[blok] / Box2D / Source / Dynamics / Joints / b2DistanceJoint.h
1 /*
2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
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_DISTANCE_JOINT_H
20 #define B2_DISTANCE_JOINT_H
21
22 #include "b2Joint.h"
23
24 /// Distance joint definition. This requires defining an
25 /// anchor point on both bodies and the non-zero length of the
26 /// distance joint. The definition uses local anchor points
27 /// so that the initial configuration can violate the constraint
28 /// slightly. This helps when saving and loading a game.
29 /// @warning Do not use a zero or short length.
30 struct b2DistanceJointDef : public b2JointDef
31 {
32         b2DistanceJointDef()
33         {
34                 type = e_distanceJoint;
35                 localAnchor1.Set(0.0f, 0.0f);
36                 localAnchor2.Set(0.0f, 0.0f);
37                 length = 1.0f;
38                 frequencyHz = 0.0f;
39                 dampingRatio = 0.0f;
40         }
41
42         /// Initialize the bodies, anchors, and length using the world
43         /// anchors.
44         void Initialize(b2Body* body1, b2Body* body2,
45                                         const b2Vec2& anchor1, const b2Vec2& anchor2);
46
47         /// The local anchor point relative to body1's origin.
48         b2Vec2 localAnchor1;
49
50         /// The local anchor point relative to body2's origin.
51         b2Vec2 localAnchor2;
52
53         /// The equilibrium length between the anchor points.
54         float32 length;
55
56         /// The response speed.
57         float32 frequencyHz;
58
59         /// The damping ratio. 0 = no damping, 1 = critical damping.
60         float32 dampingRatio;
61 };
62
63 /// A distance joint constrains two points on two bodies
64 /// to remain at a fixed distance from each other. You can view
65 /// this as a massless, rigid rod.
66 class b2DistanceJoint : public b2Joint
67 {
68 public:
69
70         b2Vec2 GetAnchor1() const;
71         b2Vec2 GetAnchor2() const;
72
73         b2Vec2 GetReactionForce() const;
74         float32 GetReactionTorque() const;
75
76         //--------------- Internals Below -------------------
77
78         b2DistanceJoint(const b2DistanceJointDef* data);
79
80         void InitVelocityConstraints(const b2TimeStep& step);
81         void SolveVelocityConstraints(const b2TimeStep& step);
82         bool SolvePositionConstraints();
83
84         b2Vec2 m_localAnchor1;
85         b2Vec2 m_localAnchor2;
86         b2Vec2 m_u;
87         float32 m_frequencyHz;
88         float32 m_dampingRatio;
89         float32 m_gamma;
90         float32 m_bias;
91         float32 m_impulse;
92         float32 m_mass;         // effective mass for the constraint.
93         float32 m_length;
94 };
95
96 #endif