b2MouseJoint.cpp
Go to the documentation of this file.
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 
20 #include <Box2D/Dynamics/b2Body.h>
22 
23 // p = attached point, m = mouse point
24 // C = p - m
25 // Cdot = v
26 // = v + cross(w, r)
27 // J = [I r_skew]
28 // Identity used:
29 // w k % (rx i + ry j) = w * (-ry i + rx j)
30 
32 : b2Joint(def)
33 {
34  b2Assert(def->target.IsValid());
35  b2Assert(b2IsValid(def->maxForce) && def->maxForce >= 0.0f);
36  b2Assert(b2IsValid(def->frequencyHz) && def->frequencyHz >= 0.0f);
37  b2Assert(b2IsValid(def->dampingRatio) && def->dampingRatio >= 0.0f);
38 
39  m_targetA = def->target;
41 
42  m_maxForce = def->maxForce;
44 
47 
48  m_beta = 0.0f;
49  m_gamma = 0.0f;
50 }
51 
53 {
54  if (m_bodyB->IsAwake() == false)
55  {
56  m_bodyB->SetAwake(true);
57  }
58  m_targetA = target;
59 }
60 
62 {
63  return m_targetA;
64 }
65 
67 {
68  m_maxForce = force;
69 }
70 
72 {
73  return m_maxForce;
74 }
75 
77 {
78  m_frequencyHz = hz;
79 }
80 
82 {
83  return m_frequencyHz;
84 }
85 
87 {
88  m_dampingRatio = ratio;
89 }
90 
92 {
93  return m_dampingRatio;
94 }
95 
97 {
102 
103  b2Vec2 cB = data.positions[m_indexB].c;
104  float32 aB = data.positions[m_indexB].a;
105  b2Vec2 vB = data.velocities[m_indexB].v;
106  float32 wB = data.velocities[m_indexB].w;
107 
108  b2Rot qB(aB);
109 
110  float32 mass = m_bodyB->GetMass();
111 
112  // Frequency
113  float32 omega = 2.0f * b2_pi * m_frequencyHz;
114 
115  // Damping coefficient
116  float32 d = 2.0f * mass * m_dampingRatio * omega;
117 
118  // Spring stiffness
119  float32 k = mass * (omega * omega);
120 
121  // magic formulas
122  // gamma has units of inverse mass.
123  // beta has units of inverse time.
124  float32 h = data.step.dt;
125  b2Assert(d + h * k > b2_epsilon);
126  m_gamma = h * (d + h * k);
127  if (m_gamma != 0.0f)
128  {
129  m_gamma = 1.0f / m_gamma;
130  }
131  m_beta = h * k * m_gamma;
132 
133  // Compute the effective mass matrix.
135 
136  // K = [(1/m1 + 1/m2) * eye(2) - skew(r1) * invI1 * skew(r1) - skew(r2) * invI2 * skew(r2)]
137  // = [1/m1+1/m2 0 ] + invI1 * [r1.y*r1.y -r1.x*r1.y] + invI2 * [r1.y*r1.y -r1.x*r1.y]
138  // [ 0 1/m1+1/m2] [-r1.x*r1.y r1.x*r1.x] [-r1.x*r1.y r1.x*r1.x]
139  b2Mat22 K;
140  K.ex.x = m_invMassB + m_invIB * m_rB.y * m_rB.y + m_gamma;
141  K.ex.y = -m_invIB * m_rB.x * m_rB.y;
142  K.ey.x = K.ex.y;
143  K.ey.y = m_invMassB + m_invIB * m_rB.x * m_rB.x + m_gamma;
144 
145  m_mass = K.GetInverse();
146 
147  m_C = cB + m_rB - m_targetA;
148  m_C *= m_beta;
149 
150  // Cheat with some damping
151  wB *= 0.98f;
152 
153  if (data.step.warmStarting)
154  {
155  m_impulse *= data.step.dtRatio;
156  vB += m_invMassB * m_impulse;
157  wB += m_invIB * b2Cross(m_rB, m_impulse);
158  }
159  else
160  {
161  m_impulse.SetZero();
162  }
163 
164  data.velocities[m_indexB].v = vB;
165  data.velocities[m_indexB].w = wB;
166 }
167 
169 {
170  b2Vec2 vB = data.velocities[m_indexB].v;
171  float32 wB = data.velocities[m_indexB].w;
172 
173  // Cdot = v + cross(w, r)
174  b2Vec2 Cdot = vB + b2Cross(wB, m_rB);
175  b2Vec2 impulse = b2Mul(m_mass, -(Cdot + m_C + m_gamma * m_impulse));
176 
177  b2Vec2 oldImpulse = m_impulse;
178  m_impulse += impulse;
179  float32 maxImpulse = data.step.dt * m_maxForce;
180  if (m_impulse.LengthSquared() > maxImpulse * maxImpulse)
181  {
182  m_impulse *= maxImpulse / m_impulse.Length();
183  }
184  impulse = m_impulse - oldImpulse;
185 
186  vB += m_invMassB * impulse;
187  wB += m_invIB * b2Cross(m_rB, impulse);
188 
189  data.velocities[m_indexB].v = vB;
190  data.velocities[m_indexB].w = wB;
191 }
192 
194 {
195  B2_NOT_USED(data);
196  return true;
197 }
198 
200 {
201  return m_targetA;
202 }
203 
205 {
207 }
208 
210 {
211  return inv_dt * m_impulse;
212 }
213 
215 {
216  return inv_dt * 0.0f;
217 }
218 
219 void b2MouseJoint::ShiftOrigin(const b2Vec2& newOrigin)
220 {
221  m_targetA -= newOrigin;
222 }
b2Vec2 m_targetA
Definition: b2MouseJoint.h:108
d
b2Velocity * velocities
Definition: b2TimeStep.h:67
float32 m_invMass
Definition: b2Body.h:455
int32 m_islandIndex
Definition: b2Body.h:434
b2Vec2 b2Mul(const b2Mat22 &A, const b2Vec2 &v)
Definition: b2Math.h:432
float32 m_invIB
Definition: b2MouseJoint.h:124
float32 m_dampingRatio
Definition: b2MouseJoint.h:110
float32 a
Definition: b2TimeStep.h:52
float32 dampingRatio
The damping ratio. 0 = no damping, 1 = critical damping.
Definition: b2MouseJoint.h:50
#define b2_pi
Definition: b2Settings.h:40
b2Vec2 m_localAnchorB
Definition: b2MouseJoint.h:107
b2Vec2 GetAnchorA() const
Implements b2Joint.
b2TimeStep step
Definition: b2TimeStep.h:65
b2Vec2 c
Definition: b2TimeStep.h:51
float32 w
Definition: b2TimeStep.h:59
float32 GetDampingRatio() const
#define B2_NOT_USED(x)
Definition: b2Settings.h:26
float32 dtRatio
Definition: b2TimeStep.h:42
bool IsAwake() const
Definition: b2Body.h:654
#define b2_epsilon
Definition: b2Settings.h:39
b2Vec2 GetWorldPoint(const b2Vec2 &localPoint) const
Definition: b2Body.h:556
Solver Data.
Definition: b2TimeStep.h:63
void SetZero()
Set this vector to all zeros.
Definition: b2Math.h:61
A 2D column vector.
Definition: b2Math.h:52
b2Vec2 ey
Definition: b2Math.h:252
float32 GetReactionTorque(float32 inv_dt) const
Implements b2Joint.
bool IsValid() const
Does this vector contain finite coordinates?
Definition: b2Math.h:128
b2Vec2 localCenter
local center of mass position
Definition: b2Math.h:392
float32 b2Cross(const b2Vec2 &a, const b2Vec2 &b)
Perform the cross product on two vectors. In 2D this produces a scalar.
Definition: b2Math.h:411
b2Vec2 m_localCenterB
Definition: b2MouseJoint.h:122
b2Vec2 GetReactionForce(float32 inv_dt) const
Implements b2Joint.
b2Vec2 m_impulse
Definition: b2MouseJoint.h:114
b2Vec2 v
Definition: b2TimeStep.h:58
bool b2IsValid(float32 x)
This function is used to ensure that a floating point number is not a NaN or infinity.
Definition: b2Math.h:26
float32 frequencyHz
The response speed.
Definition: b2MouseJoint.h:47
float32 GetMaxForce() const
void SetMaxForce(float32 force)
Set/get the maximum force in Newtons.
float32 m_maxForce
Definition: b2MouseJoint.h:115
float32 m_invI
Definition: b2Body.h:458
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
float32 m_invMassB
Definition: b2MouseJoint.h:123
GLenum target
b2Vec2 b2MulT(const b2Mat22 &A, const b2Vec2 &v)
Definition: b2Math.h:439
float32 m_frequencyHz
Definition: b2MouseJoint.h:109
const b2Vec2 & GetTarget() const
float32 y
Definition: b2Math.h:139
b2Position * positions
Definition: b2TimeStep.h:66
float32 GetMass() const
Definition: b2Body.h:539
void SetFrequency(float32 hz)
Set/get the frequency in Hertz.
#define b2Assert(A)
Definition: b2Settings.h:27
float32 maxForce
Definition: b2MouseJoint.h:44
b2Mat22 m_mass
Definition: b2MouseJoint.h:125
float32 m_gamma
Definition: b2MouseJoint.h:116
bool SolvePositionConstraints(const b2SolverData &data)
void SolveVelocityConstraints(const b2SolverData &data)
b2Vec2 GetAnchorB() const
Implements b2Joint.
b2Vec2 ex
Definition: b2Math.h:252
A 2-by-2 matrix. Stored in column-major order.
Definition: b2Math.h:182
float32 GetFrequency() const
void ShiftOrigin(const b2Vec2 &newOrigin)
Implement b2Joint::ShiftOrigin.
bool warmStarting
Definition: b2TimeStep.h:45
Rotation.
Definition: b2Math.h:298
float32 m_beta
Definition: b2MouseJoint.h:111
void InitVelocityConstraints(const b2SolverData &data)
b2MouseJoint(const b2MouseJointDef *def)
void SetDampingRatio(float32 ratio)
Set/get the damping ratio (dimensionless).
const b2Transform & GetTransform() const
Definition: b2Body.h:474
float32 x
Definition: b2Math.h:139
void SetTarget(const b2Vec2 &target)
Use this to update the target point.
float32 dt
Definition: b2TimeStep.h:40
b2Mat22 GetInverse() const
Definition: b2Math.h:222
void SetAwake(bool flag)
Definition: b2Body.h:633
b2Body * m_bodyB
Definition: b2Joint.h:176
b2Sweep m_sweep
Definition: b2Body.h:437
float float32
Definition: b2Settings.h:35
GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble f


mvsim
Author(s):
autogenerated on Fri May 7 2021 03:05:51