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_GEAR_JOINT_H |
20 | #define B2_GEAR_JOINT_H |
21 | |
22 | #include <Box2D/Dynamics/Joints/b2Joint.h> |
23 | |
24 | /// Gear joint definition. This definition requires two existing |
25 | /// revolute or prismatic joints (any combination will work). |
26 | struct b2GearJointDef : public b2JointDef |
27 | { |
28 | b2GearJointDef() |
29 | { |
30 | type = e_gearJoint; |
31 | joint1 = NULL; |
32 | joint2 = NULL; |
33 | ratio = 1.0f; |
34 | } |
35 | |
36 | /// The first revolute/prismatic joint attached to the gear joint. |
37 | b2Joint* joint1; |
38 | |
39 | /// The second revolute/prismatic joint attached to the gear joint. |
40 | b2Joint* joint2; |
41 | |
42 | /// The gear ratio. |
43 | /// @see b2GearJoint for explanation. |
44 | float32 ratio; |
45 | }; |
46 | |
47 | /// A gear joint is used to connect two joints together. Either joint |
48 | /// can be a revolute or prismatic joint. You specify a gear ratio |
49 | /// to bind the motions together: |
50 | /// coordinate1 + ratio * coordinate2 = constant |
51 | /// The ratio can be negative or positive. If one joint is a revolute joint |
52 | /// and the other joint is a prismatic joint, then the ratio will have units |
53 | /// of length or units of 1/length. |
54 | /// @warning You have to manually destroy the gear joint if joint1 or joint2 |
55 | /// is destroyed. |
56 | class b2GearJoint : public b2Joint |
57 | { |
58 | public: |
59 | b2Vec2 GetAnchorA() const; |
60 | b2Vec2 GetAnchorB() const; |
61 | |
62 | b2Vec2 GetReactionForce(float32 inv_dt) const; |
63 | float32 GetReactionTorque(float32 inv_dt) const; |
64 | |
65 | /// Get the first joint. |
66 | b2Joint* GetJoint1() { return m_joint1; } |
67 | |
68 | /// Get the second joint. |
69 | b2Joint* GetJoint2() { return m_joint2; } |
70 | |
71 | /// Set/Get the gear ratio. |
72 | void SetRatio(float32 ratio); |
73 | float32 GetRatio() const; |
74 | |
75 | /// Dump joint to dmLog |
76 | void Dump(); |
77 | |
78 | protected: |
79 | |
80 | friend class b2Joint; |
81 | b2GearJoint(const b2GearJointDef* data); |
82 | |
83 | void InitVelocityConstraints(const b2SolverData& data); |
84 | void SolveVelocityConstraints(const b2SolverData& data); |
85 | bool SolvePositionConstraints(const b2SolverData& data); |
86 | |
87 | b2Joint* m_joint1; |
88 | b2Joint* m_joint2; |
89 | |
90 | b2JointType m_typeA; |
91 | b2JointType m_typeB; |
92 | |
93 | // Body A is connected to body C |
94 | // Body B is connected to body D |
95 | b2Body* m_bodyC; |
96 | b2Body* m_bodyD; |
97 | |
98 | // Solver shared |
99 | b2Vec2 m_localAnchorA; |
100 | b2Vec2 m_localAnchorB; |
101 | b2Vec2 m_localAnchorC; |
102 | b2Vec2 m_localAnchorD; |
103 | |
104 | b2Vec2 m_localAxisC; |
105 | b2Vec2 m_localAxisD; |
106 | |
107 | float32 m_referenceAngleA; |
108 | float32 m_referenceAngleB; |
109 | |
110 | float32 m_constant; |
111 | float32 m_ratio; |
112 | |
113 | float32 m_impulse; |
114 | |
115 | // Solver temp |
116 | int32 m_indexA, m_indexB, m_indexC, m_indexD; |
117 | b2Vec2 m_lcA, m_lcB, m_lcC, m_lcD; |
118 | float32 m_mA, m_mB, m_mC, m_mD; |
119 | float32 m_iA, m_iB, m_iC, m_iD; |
120 | b2Vec2 m_JvAC, m_JvBD; |
121 | float32 m_JwA, m_JwB, m_JwC, m_JwD; |
122 | float32 m_mass; |
123 | }; |
124 | |
125 | #endif |
126 | |