externals
box2d
src
dynamics
b2_mouse_joint.cpp
Go to the documentation of this file.
1
// MIT License
2
3
// Copyright (c) 2019 Erin Catto
4
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
12
// The above copyright notice and this permission notice shall be included in all
13
// copies or substantial portions of the Software.
14
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
// SOFTWARE.
22
23
#include "
box2d/b2_body.h
"
24
#include "
box2d/b2_mouse_joint.h
"
25
#include "
box2d/b2_time_step.h
"
26
27
// p = attached point, m = mouse point
28
// C = p - m
29
// Cdot = v
30
// = v + cross(w, r)
31
// J = [I r_skew]
32
// Identity used:
33
// w k % (rx i + ry j) = w * (-ry i + rx j)
34
35
b2MouseJoint::b2MouseJoint
(
const
b2MouseJointDef
* def)
36
:
b2Joint
(def)
37
{
38
m_targetA
= def->
target
;
39
m_localAnchorB
=
b2MulT
(
m_bodyB
->
GetTransform
(),
m_targetA
);
40
m_maxForce
= def->
maxForce
;
41
m_stiffness
= def->
stiffness
;
42
m_damping
= def->
damping
;
43
44
m_impulse
.
SetZero
();
45
m_beta
= 0.0f;
46
m_gamma
= 0.0f;
47
}
48
49
void
b2MouseJoint::SetTarget
(
const
b2Vec2
& target)
50
{
51
if
(target !=
m_targetA
)
52
{
53
m_bodyB
->
SetAwake
(
true
);
54
m_targetA
= target;
55
}
56
}
57
58
const
b2Vec2
&
b2MouseJoint::GetTarget
()
const
59
{
60
return
m_targetA
;
61
}
62
63
void
b2MouseJoint::SetMaxForce
(
float
force)
64
{
65
m_maxForce
= force;
66
}
67
68
float
b2MouseJoint::GetMaxForce
()
const
69
{
70
return
m_maxForce
;
71
}
72
73
void
b2MouseJoint::InitVelocityConstraints
(
const
b2SolverData
& data)
74
{
75
m_indexB
=
m_bodyB
->
m_islandIndex
;
76
m_localCenterB
=
m_bodyB
->
m_sweep
.
localCenter
;
77
m_invMassB
=
m_bodyB
->
m_invMass
;
78
m_invIB
=
m_bodyB
->
m_invI
;
79
80
b2Vec2
cB = data.
positions
[
m_indexB
].
c
;
81
float
aB = data.
positions
[
m_indexB
].
a
;
82
b2Vec2
vB = data.
velocities
[
m_indexB
].
v
;
83
float
wB = data.
velocities
[
m_indexB
].
w
;
84
85
b2Rot
qB(aB);
86
87
float
mass =
m_bodyB
->
GetMass
();
88
89
float
d
=
m_damping
;
90
float
k =
m_stiffness
;
91
92
// magic formulas
93
// gamma has units of inverse mass.
94
// beta has units of inverse time.
95
float
h = data.
step
.
dt
;
96
m_gamma
= h * (
d
+ h * k);
97
if
(
m_gamma
!= 0.0
f
)
98
{
99
m_gamma
= 1.0f /
m_gamma
;
100
}
101
m_beta
= h * k *
m_gamma
;
102
103
// Compute the effective mass matrix.
104
m_rB
=
b2Mul
(qB,
m_localAnchorB
-
m_localCenterB
);
105
106
// K = [(1/m1 + 1/m2) * eye(2) - skew(r1) * invI1 * skew(r1) - skew(r2) * invI2 * skew(r2)]
107
// = [1/m1+1/m2 0 ] + invI1 * [r1.y*r1.y -r1.x*r1.y] + invI2 * [r1.y*r1.y -r1.x*r1.y]
108
// [ 0 1/m1+1/m2] [-r1.x*r1.y r1.x*r1.x] [-r1.x*r1.y r1.x*r1.x]
109
b2Mat22
K;
110
K.
ex
.
x
=
m_invMassB
+
m_invIB
*
m_rB
.
y
*
m_rB
.
y
+
m_gamma
;
111
K.
ex
.
y
= -
m_invIB
*
m_rB
.
x
*
m_rB
.
y
;
112
K.
ey
.
x
= K.
ex
.
y
;
113
K.
ey
.
y
=
m_invMassB
+
m_invIB
*
m_rB
.
x
*
m_rB
.
x
+
m_gamma
;
114
115
m_mass
= K.
GetInverse
();
116
117
m_C
= cB +
m_rB
-
m_targetA
;
118
m_C
*=
m_beta
;
119
120
// Cheat with some damping
121
wB *= 0.98f;
122
123
if
(data.
step
.
warmStarting
)
124
{
125
m_impulse
*= data.
step
.
dtRatio
;
126
vB +=
m_invMassB
*
m_impulse
;
127
wB +=
m_invIB
*
b2Cross
(
m_rB
,
m_impulse
);
128
}
129
else
130
{
131
m_impulse
.
SetZero
();
132
}
133
134
data.
velocities
[
m_indexB
].
v
= vB;
135
data.
velocities
[
m_indexB
].
w
= wB;
136
}
137
138
void
b2MouseJoint::SolveVelocityConstraints
(
const
b2SolverData
& data)
139
{
140
b2Vec2
vB = data.
velocities
[
m_indexB
].
v
;
141
float
wB = data.
velocities
[
m_indexB
].
w
;
142
143
// Cdot = v + cross(w, r)
144
b2Vec2
Cdot = vB +
b2Cross
(wB,
m_rB
);
145
b2Vec2
impulse =
b2Mul
(
m_mass
, -(Cdot +
m_C
+
m_gamma
*
m_impulse
));
146
147
b2Vec2
oldImpulse =
m_impulse
;
148
m_impulse
+= impulse;
149
float
maxImpulse = data.
step
.
dt
*
m_maxForce
;
150
if
(
m_impulse
.
LengthSquared
() > maxImpulse * maxImpulse)
151
{
152
m_impulse
*= maxImpulse /
m_impulse
.
Length
();
153
}
154
impulse =
m_impulse
- oldImpulse;
155
156
vB +=
m_invMassB
* impulse;
157
wB +=
m_invIB
*
b2Cross
(
m_rB
, impulse);
158
159
data.
velocities
[
m_indexB
].
v
= vB;
160
data.
velocities
[
m_indexB
].
w
= wB;
161
}
162
163
bool
b2MouseJoint::SolvePositionConstraints
(
const
b2SolverData
& data)
164
{
165
B2_NOT_USED
(data);
166
return
true
;
167
}
168
169
b2Vec2
b2MouseJoint::GetAnchorA
()
const
170
{
171
return
m_targetA
;
172
}
173
174
b2Vec2
b2MouseJoint::GetAnchorB
()
const
175
{
176
return
m_bodyB
->
GetWorldPoint
(
m_localAnchorB
);
177
}
178
179
b2Vec2
b2MouseJoint::GetReactionForce
(
float
inv_dt)
const
180
{
181
return
inv_dt *
m_impulse
;
182
}
183
184
float
b2MouseJoint::GetReactionTorque
(
float
inv_dt)
const
185
{
186
return
inv_dt * 0.0f;
187
}
188
189
void
b2MouseJoint::ShiftOrigin
(
const
b2Vec2
& newOrigin)
190
{
191
m_targetA
-= newOrigin;
192
}
b2Cross
float b2Cross(const b2Vec2 &a, const b2Vec2 &b)
Perform the cross product on two vectors. In 2D this produces a scalar.
Definition:
b2_math.h:401
b2MouseJoint::b2MouseJoint
b2MouseJoint(const b2MouseJointDef *def)
Definition:
b2_mouse_joint.cpp:35
b2MouseJoint::GetAnchorB
b2Vec2 GetAnchorB() const override
Implements b2Joint.
Definition:
b2_mouse_joint.cpp:174
b2Vec2::y
float y
Definition:
b2_math.h:128
B2_NOT_USED
#define B2_NOT_USED(x)
Definition:
b2_common.h:36
b2Body::SetAwake
void SetAwake(bool flag)
Definition:
b2_body.h:638
b2MouseJoint::GetMaxForce
float GetMaxForce() const
Definition:
b2_mouse_joint.cpp:68
b2MouseJoint::SolvePositionConstraints
bool SolvePositionConstraints(const b2SolverData &data) override
Definition:
b2_mouse_joint.cpp:163
b2MouseJoint::m_invIB
float m_invIB
Definition:
b2_mouse_joint.h:129
b2MouseJoint::GetTarget
const b2Vec2 & GetTarget() const
Definition:
b2_mouse_joint.cpp:58
b2Position::c
b2Vec2 c
Definition:
b2_time_step.h:55
b2MouseJoint::SetMaxForce
void SetMaxForce(float force)
Set/get the maximum force in Newtons.
Definition:
b2_mouse_joint.cpp:63
b2MulT
b2Vec2 b2MulT(const b2Mat22 &A, const b2Vec2 &v)
Definition:
b2_math.h:429
b2MouseJointDef::damping
float damping
The linear damping in N*s/m.
Definition:
b2_mouse_joint.h:55
b2Sweep::localCenter
b2Vec2 localCenter
local center of mass position
Definition:
b2_math.h:382
b2MouseJoint::m_indexB
int32 m_indexB
Definition:
b2_mouse_joint.h:125
b2SolverData
Solver Data.
Definition:
b2_time_step.h:67
b2MouseJoint::m_mass
b2Mat22 m_mass
Definition:
b2_mouse_joint.h:130
b2Body::m_invMass
float m_invMass
Definition:
b2_body.h:460
b2Mat22::ex
b2Vec2 ex
Definition:
b2_math.h:241
b2MouseJoint::SolveVelocityConstraints
void SolveVelocityConstraints(const b2SolverData &data) override
Definition:
b2_mouse_joint.cpp:138
b2Mat22
A 2-by-2 matrix. Stored in column-major order.
Definition:
b2_math.h:171
b2Vec2::SetZero
void SetZero()
Set this vector to all zeros.
Definition:
b2_math.h:50
b2Mul
b2Vec2 b2Mul(const b2Mat22 &A, const b2Vec2 &v)
Definition:
b2_math.h:422
b2Velocity::v
b2Vec2 v
Definition:
b2_time_step.h:62
b2MouseJoint::SetTarget
void SetTarget(const b2Vec2 &target)
Use this to update the target point.
Definition:
b2_mouse_joint.cpp:49
b2Vec2
A 2D column vector.
Definition:
b2_math.h:41
b2MouseJoint::m_targetA
b2Vec2 m_targetA
Definition:
b2_mouse_joint.h:113
b2_mouse_joint.h
b2MouseJoint::ShiftOrigin
void ShiftOrigin(const b2Vec2 &newOrigin) override
Implement b2Joint::ShiftOrigin.
Definition:
b2_mouse_joint.cpp:189
f
f
b2Mat22::GetInverse
b2Mat22 GetInverse() const
Definition:
b2_math.h:211
b2MouseJoint::m_localAnchorB
b2Vec2 m_localAnchorB
Definition:
b2_mouse_joint.h:112
b2SolverData::positions
b2Position * positions
Definition:
b2_time_step.h:70
b2Joint
Definition:
b2_joint.h:110
b2MouseJointDef::target
b2Vec2 target
Definition:
b2_mouse_joint.h:44
b2MouseJoint::m_rB
b2Vec2 m_rB
Definition:
b2_mouse_joint.h:126
b2SolverData::step
b2TimeStep step
Definition:
b2_time_step.h:69
b2MouseJoint::GetReactionTorque
float GetReactionTorque(float inv_dt) const override
Implements b2Joint.
Definition:
b2_mouse_joint.cpp:184
b2MouseJoint::m_maxForce
float m_maxForce
Definition:
b2_mouse_joint.h:120
b2MouseJoint::GetReactionForce
b2Vec2 GetReactionForce(float inv_dt) const override
Implements b2Joint.
Definition:
b2_mouse_joint.cpp:179
b2SolverData::velocities
b2Velocity * velocities
Definition:
b2_time_step.h:71
b2MouseJoint::m_C
b2Vec2 m_C
Definition:
b2_mouse_joint.h:131
b2TimeStep::dt
float dt
Definition:
b2_time_step.h:44
d
d
b2MouseJointDef
Definition:
b2_mouse_joint.h:31
b2Vec2::x
float x
Definition:
b2_math.h:128
b2Vec2::LengthSquared
float LengthSquared() const
Definition:
b2_math.h:96
b2MouseJoint::m_beta
float m_beta
Definition:
b2_mouse_joint.h:116
b2Body::m_islandIndex
int32 m_islandIndex
Definition:
b2_body.h:439
b2MouseJoint::m_localCenterB
b2Vec2 m_localCenterB
Definition:
b2_mouse_joint.h:127
b2MouseJoint::InitVelocityConstraints
void InitVelocityConstraints(const b2SolverData &data) override
Definition:
b2_mouse_joint.cpp:73
b2Velocity::w
float w
Definition:
b2_time_step.h:63
b2MouseJoint::m_gamma
float m_gamma
Definition:
b2_mouse_joint.h:121
b2MouseJointDef::stiffness
float stiffness
The linear stiffness in N/m.
Definition:
b2_mouse_joint.h:52
b2MouseJoint::m_stiffness
float m_stiffness
Definition:
b2_mouse_joint.h:114
b2MouseJoint::m_damping
float m_damping
Definition:
b2_mouse_joint.h:115
b2Position::a
float a
Definition:
b2_time_step.h:56
b2MouseJoint::m_invMassB
float m_invMassB
Definition:
b2_mouse_joint.h:128
b2MouseJointDef::maxForce
float maxForce
Definition:
b2_mouse_joint.h:49
b2TimeStep::warmStarting
bool warmStarting
Definition:
b2_time_step.h:49
b2_time_step.h
b2Mat22::ey
b2Vec2 ey
Definition:
b2_math.h:241
b2MouseJoint::m_impulse
b2Vec2 m_impulse
Definition:
b2_mouse_joint.h:119
b2Body::m_sweep
b2Sweep m_sweep
Definition:
b2_body.h:442
b2Rot
Rotation.
Definition:
b2_math.h:287
b2MouseJoint::GetAnchorA
b2Vec2 GetAnchorA() const override
Implements b2Joint.
Definition:
b2_mouse_joint.cpp:169
b2Body::GetWorldPoint
b2Vec2 GetWorldPoint(const b2Vec2 &localPoint) const
Definition:
b2_body.h:561
b2Body::GetMass
float GetMass() const
Definition:
b2_body.h:544
b2Joint::m_bodyB
b2Body * m_bodyB
Definition:
b2_joint.h:183
b2Body::GetTransform
const b2Transform & GetTransform() const
Definition:
b2_body.h:479
b2Vec2::Length
float Length() const
Get the length of this vector (the norm).
Definition:
b2_math.h:89
b2Body::m_invI
float m_invI
Definition:
b2_body.h:463
b2TimeStep::dtRatio
float dtRatio
Definition:
b2_time_step.h:46
b2_body.h
mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:07