first commit
[blok] / Box2D / Source / Dynamics / Joints / b2MouseJoint.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_MOUSE_JOINT_H
20 #define B2_MOUSE_JOINT_H
21
22 #include "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                 timeStep = 1.0f / 60.0f;
36         }
37
38         /// The initial world target point. This is assumed
39         /// to coincide with the body anchor initially.
40         b2Vec2 target;
41
42         /// The maximum constraint force that can be exerted
43         /// to move the candidate body. Usually you will express
44         /// as some multiple of the weight (multiplier * mass * gravity).
45         float32 maxForce;
46
47         /// The response speed.
48         float32 frequencyHz;
49
50         /// The damping ratio. 0 = no damping, 1 = critical damping.
51         float32 dampingRatio;
52
53         /// The time step used in the simulation.
54         float32 timeStep;
55 };
56
57 /// A mouse joint is used to make a point on a body track a
58 /// specified world point. This a soft constraint with a maximum
59 /// force. This allows the constraint to stretch and without
60 /// applying huge forces.
61 class b2MouseJoint : public b2Joint
62 {
63 public:
64
65         /// Implements b2Joint.
66         b2Vec2 GetAnchor1() const;
67
68         /// Implements b2Joint.
69         b2Vec2 GetAnchor2() const;
70
71         /// Implements b2Joint.
72         b2Vec2 GetReactionForce() const;
73
74         /// Implements b2Joint.
75         float32 GetReactionTorque() const;
76
77         /// Use this to update the target point.
78         void SetTarget(const b2Vec2& target);
79
80         //--------------- Internals Below -------------------
81
82         b2MouseJoint(const b2MouseJointDef* def);
83
84         void InitVelocityConstraints(const b2TimeStep& step);
85         void SolveVelocityConstraints(const b2TimeStep& step);
86         bool SolvePositionConstraints()
87         {
88                 return true;
89         }
90
91         b2Vec2 m_localAnchor;
92         b2Vec2 m_target;
93         b2Vec2 m_impulse;
94
95         b2Mat22 m_mass;         // effective mass for point-to-point constraint.
96         b2Vec2 m_C;                             // position error
97         float32 m_maxForce;
98         float32 m_beta;                 // bias factor
99         float32 m_gamma;                // softness
100 };
101
102 #endif