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 {
b2GearJointDefb2GearJointDef32 	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 GetAnchorA() const;
64 	b2Vec2 GetAnchorB() const;
65 
66 	b2Vec2 GetReactionForce(float32 inv_dt) const;
67 	float32 GetReactionTorque(float32 inv_dt) const;
68 
69 	/// Set/Get the gear ratio.
70 	void SetRatio(float32 ratio);
71 	float32 GetRatio() const;
72 
73 protected:
74 
75 	friend class b2Joint;
76 	b2GearJoint(const b2GearJointDef* data);
77 
78 	void InitVelocityConstraints(const b2TimeStep& step);
79 	void SolveVelocityConstraints(const b2TimeStep& step);
80 	bool SolvePositionConstraints(float32 baumgarte);
81 
82 	b2Body* m_ground1;
83 	b2Body* m_ground2;
84 
85 	// One of these is NULL.
86 	b2RevoluteJoint* m_revolute1;
87 	b2PrismaticJoint* m_prismatic1;
88 
89 	// One of these is NULL.
90 	b2RevoluteJoint* m_revolute2;
91 	b2PrismaticJoint* m_prismatic2;
92 
93 	b2Vec2 m_groundAnchor1;
94 	b2Vec2 m_groundAnchor2;
95 
96 	b2Vec2 m_localAnchor1;
97 	b2Vec2 m_localAnchor2;
98 
99 	b2Jacobian m_J;
100 
101 	float32 m_constant;
102 	float32 m_ratio;
103 
104 	// Effective mass
105 	float32 m_mass;
106 
107 	// Impulse for accumulation/warm starting.
108 	float32 m_impulse;
109 };
110 
111 #endif
112