1 | /* |
2 | * Copyright (c) 2006-2007 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_MOUSE_JOINT_H |
20 | #define B2_MOUSE_JOINT_H |
21 | |
22 | #include <Box2D/Dynamics/Joints/b2Joint.h> |
23 | |
24 | /// Mouse joint definition. This requires a world target point, |
25 | /// tuning parameters, and the time step. |
26 | struct b2MouseJointDef : public b2JointDef |
27 | { |
28 | b2MouseJointDef() |
29 | { |
30 | type = e_mouseJoint; |
31 | target.Set(0.0f, 0.0f); |
32 | maxForce = 0.0f; |
33 | frequencyHz = 5.0f; |
34 | dampingRatio = 0.7f; |
35 | } |
36 | |
37 | /// The initial world target point. This is assumed |
38 | /// to coincide with the body anchor initially. |
39 | b2Vec2 target; |
40 | |
41 | /// The maximum constraint force that can be exerted |
42 | /// to move the candidate body. Usually you will express |
43 | /// as some multiple of the weight (multiplier * mass * gravity). |
44 | float32 maxForce; |
45 | |
46 | /// The response speed. |
47 | float32 frequencyHz; |
48 | |
49 | /// The damping ratio. 0 = no damping, 1 = critical damping. |
50 | float32 dampingRatio; |
51 | }; |
52 | |
53 | /// A mouse joint is used to make a point on a body track a |
54 | /// specified world point. This a soft constraint with a maximum |
55 | /// force. This allows the constraint to stretch and without |
56 | /// applying huge forces. |
57 | /// NOTE: this joint is not documented in the manual because it was |
58 | /// developed to be used in the testbed. If you want to learn how to |
59 | /// use the mouse joint, look at the testbed. |
60 | class b2MouseJoint : public b2Joint |
61 | { |
62 | public: |
63 | |
64 | /// Implements b2Joint. |
65 | b2Vec2 GetAnchorA() const; |
66 | |
67 | /// Implements b2Joint. |
68 | b2Vec2 GetAnchorB() const; |
69 | |
70 | /// Implements b2Joint. |
71 | b2Vec2 GetReactionForce(float32 inv_dt) const; |
72 | |
73 | /// Implements b2Joint. |
74 | float32 GetReactionTorque(float32 inv_dt) const; |
75 | |
76 | /// Use this to update the target point. |
77 | void SetTarget(const b2Vec2& target); |
78 | const b2Vec2& GetTarget() const; |
79 | |
80 | /// Set/get the maximum force in Newtons. |
81 | void SetMaxForce(float32 force); |
82 | float32 GetMaxForce() const; |
83 | |
84 | /// Set/get the frequency in Hertz. |
85 | void SetFrequency(float32 hz); |
86 | float32 GetFrequency() const; |
87 | |
88 | /// Set/get the damping ratio (dimensionless). |
89 | void SetDampingRatio(float32 ratio); |
90 | float32 GetDampingRatio() const; |
91 | |
92 | /// The mouse joint does not support dumping. |
93 | void Dump() { b2Log("Mouse joint dumping is not supported.\n" ); } |
94 | |
95 | /// Implement b2Joint::ShiftOrigin |
96 | void ShiftOrigin(const b2Vec2& newOrigin); |
97 | |
98 | protected: |
99 | friend class b2Joint; |
100 | |
101 | b2MouseJoint(const b2MouseJointDef* def); |
102 | |
103 | void InitVelocityConstraints(const b2SolverData& data); |
104 | void SolveVelocityConstraints(const b2SolverData& data); |
105 | bool SolvePositionConstraints(const b2SolverData& data); |
106 | |
107 | b2Vec2 m_localAnchorB; |
108 | b2Vec2 m_targetA; |
109 | float32 m_frequencyHz; |
110 | float32 m_dampingRatio; |
111 | float32 m_beta; |
112 | |
113 | // Solver shared |
114 | b2Vec2 m_impulse; |
115 | float32 m_maxForce; |
116 | float32 m_gamma; |
117 | |
118 | // Solver temp |
119 | int32 m_indexA; |
120 | int32 m_indexB; |
121 | b2Vec2 m_rB; |
122 | b2Vec2 m_localCenterB; |
123 | float32 m_invMassB; |
124 | float32 m_invIB; |
125 | b2Mat22 m_mass; |
126 | b2Vec2 m_C; |
127 | }; |
128 | |
129 | #endif |
130 | |