first commit
[blok] / Box2D / Source / Dynamics / Joints / b2GearJoint.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_GEAR_JOINT_H
20 #define B2_GEAR_JOINT_H
21
22 #include "b2Joint.h"
23
24 class b2RevoluteJoint;
25 class b2PrismaticJoint;
26
27 /// Gear joint definition. This definition requires two existing
28 /// revolute or prismatic joints (any combination will work).
29 /// The provided joints must attach a dynamic body to a static body.
30 struct b2GearJointDef : public b2JointDef
31 {
32         b2GearJointDef()
33         {
34                 type = e_gearJoint;
35                 joint1 = NULL;
36                 joint2 = NULL;
37                 ratio = 1.0f;
38         }
39
40         /// The first revolute/prismatic joint attached to the gear joint.
41         b2Joint* joint1;
42
43         /// The second revolute/prismatic joint attached to the gear joint.
44         b2Joint* joint2;
45
46         /// The gear ratio.
47         /// @see b2GearJoint for explanation.
48         float32 ratio;
49 };
50
51 /// A gear joint is used to connect two joints together. Either joint
52 /// can be a revolute or prismatic joint. You specify a gear ratio
53 /// to bind the motions together:
54 /// coordinate1 + ratio * coordinate2 = constant
55 /// The ratio can be negative or positive. If one joint is a revolute joint
56 /// and the other joint is a prismatic joint, then the ratio will have units
57 /// of length or units of 1/length.
58 /// @warning The revolute and prismatic joints must be attached to
59 /// fixed bodies (which must be body1 on those joints).
60 class b2GearJoint : public b2Joint
61 {
62 public:
63         b2Vec2 GetAnchor1() const;
64         b2Vec2 GetAnchor2() const;
65
66         b2Vec2 GetReactionForce() const;
67         float32 GetReactionTorque() const;
68
69         /// Get the gear ratio.
70         float32 GetRatio() const;
71
72         //--------------- Internals Below -------------------
73
74         b2GearJoint(const b2GearJointDef* data);
75
76         void InitVelocityConstraints(const b2TimeStep& step);
77         void SolveVelocityConstraints(const b2TimeStep& step);
78         bool SolvePositionConstraints();
79
80         b2Body* m_ground1;
81         b2Body* m_ground2;
82
83         // One of these is NULL.
84         b2RevoluteJoint* m_revolute1;
85         b2PrismaticJoint* m_prismatic1;
86
87         // One of these is NULL.
88         b2RevoluteJoint* m_revolute2;
89         b2PrismaticJoint* m_prismatic2;
90
91         b2Vec2 m_groundAnchor1;
92         b2Vec2 m_groundAnchor2;
93
94         b2Vec2 m_localAnchor1;
95         b2Vec2 m_localAnchor2;
96
97         b2Jacobian m_J;
98
99         float32 m_constant;
100         float32 m_ratio;
101
102         // Effective mass
103         float32 m_mass;
104
105         // Impulse for accumulation/warm starting.
106         float32 m_force;
107 };
108
109 #endif