pbox2d 0.6.0-java → 0.8.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/.mvn/extensions.xml +8 -0
  3. data/.mvn/wrapper/maven-wrapper.properties +1 -0
  4. data/.travis.yml +23 -0
  5. data/CHANGELOG.md +8 -0
  6. data/README.md +7 -7
  7. data/Rakefile +1 -2
  8. data/lib/box2d.jar +0 -0
  9. data/lib/pbox2d/version.rb +1 -1
  10. data/lib/pbox2d.rb +1 -0
  11. data/pbox2d.gemspec +6 -11
  12. data/pom.rb +59 -0
  13. data/pom.xml +82 -73
  14. data/src/org/jbox2d/JBox2D.gwt.xml +12 -0
  15. data/src/org/jbox2d/callbacks/ContactAdaptor.java +27 -0
  16. data/src/org/jbox2d/callbacks/ContactFilter.java +59 -0
  17. data/src/org/jbox2d/callbacks/ContactImpulse.java +42 -0
  18. data/src/org/jbox2d/callbacks/ContactListener.java +87 -0
  19. data/src/org/jbox2d/callbacks/DebugDraw.java +297 -0
  20. data/src/org/jbox2d/callbacks/DestructionListener.java +53 -0
  21. data/src/org/jbox2d/callbacks/PairCallback.java +29 -0
  22. data/src/org/jbox2d/callbacks/ParticleDestructionListener.java +20 -0
  23. data/src/org/jbox2d/callbacks/ParticleQueryCallback.java +19 -0
  24. data/src/org/jbox2d/callbacks/ParticleRaycastCallback.java +19 -0
  25. data/src/org/jbox2d/callbacks/QueryCallback.java +45 -0
  26. data/src/org/jbox2d/callbacks/RayCastCallback.java +55 -0
  27. data/src/org/jbox2d/callbacks/TreeCallback.java +42 -0
  28. data/src/org/jbox2d/callbacks/TreeRayCastCallback.java +44 -0
  29. data/src/org/jbox2d/collision/AABB.java +338 -0
  30. data/src/org/jbox2d/collision/Collision.java +1444 -0
  31. data/src/org/jbox2d/collision/ContactID.java +106 -0
  32. data/src/org/jbox2d/collision/Distance.java +773 -0
  33. data/src/org/jbox2d/collision/DistanceInput.java +41 -0
  34. data/src/org/jbox2d/collision/DistanceOutput.java +43 -0
  35. data/src/org/jbox2d/collision/Manifold.java +116 -0
  36. data/src/org/jbox2d/collision/ManifoldPoint.java +104 -0
  37. data/src/org/jbox2d/collision/RayCastInput.java +47 -0
  38. data/src/org/jbox2d/collision/RayCastOutput.java +46 -0
  39. data/src/org/jbox2d/collision/TimeOfImpact.java +526 -0
  40. data/src/org/jbox2d/collision/WorldManifold.java +200 -0
  41. data/src/org/jbox2d/collision/broadphase/BroadPhase.java +92 -0
  42. data/src/org/jbox2d/collision/broadphase/BroadPhaseStrategy.java +88 -0
  43. data/src/org/jbox2d/collision/broadphase/DefaultBroadPhaseBuffer.java +268 -0
  44. data/src/org/jbox2d/collision/broadphase/DynamicTree.java +883 -0
  45. data/src/org/jbox2d/collision/broadphase/DynamicTreeFlatNodes.java +873 -0
  46. data/src/org/jbox2d/collision/broadphase/DynamicTreeNode.java +54 -0
  47. data/src/org/jbox2d/collision/broadphase/Pair.java +46 -0
  48. data/src/org/jbox2d/collision/shapes/ChainShape.java +264 -0
  49. data/src/org/jbox2d/collision/shapes/CircleShape.java +207 -0
  50. data/src/org/jbox2d/collision/shapes/EdgeShape.java +254 -0
  51. data/src/org/jbox2d/collision/shapes/MassData.java +105 -0
  52. data/src/org/jbox2d/collision/shapes/PolygonShape.java +718 -0
  53. data/src/org/jbox2d/collision/shapes/Shape.java +136 -0
  54. data/src/org/jbox2d/collision/shapes/ShapeType.java +32 -0
  55. data/src/org/jbox2d/common/BufferUtils.java +209 -0
  56. data/src/org/jbox2d/common/Color3f.java +88 -0
  57. data/src/org/jbox2d/common/IViewportTransform.java +133 -0
  58. data/src/org/jbox2d/common/Mat22.java +609 -0
  59. data/src/org/jbox2d/common/Mat33.java +290 -0
  60. data/src/org/jbox2d/common/MathUtils.java +335 -0
  61. data/src/org/jbox2d/common/OBBViewportTransform.java +174 -0
  62. data/src/org/jbox2d/common/PlatformMathUtils.java +46 -0
  63. data/src/org/jbox2d/common/RaycastResult.java +37 -0
  64. data/src/org/jbox2d/common/Rot.java +150 -0
  65. data/src/org/jbox2d/common/Settings.java +246 -0
  66. data/src/org/jbox2d/common/Sweep.java +116 -0
  67. data/src/org/jbox2d/common/Timer.java +46 -0
  68. data/src/org/jbox2d/common/Transform.java +203 -0
  69. data/src/org/jbox2d/common/Vec2.java +388 -0
  70. data/src/org/jbox2d/common/Vec3.java +170 -0
  71. data/src/org/jbox2d/dynamics/Body.java +1246 -0
  72. data/src/org/jbox2d/dynamics/BodyDef.java +382 -0
  73. data/src/org/jbox2d/dynamics/BodyType.java +41 -0
  74. data/src/org/jbox2d/dynamics/ContactManager.java +293 -0
  75. data/src/org/jbox2d/dynamics/Filter.java +62 -0
  76. data/src/org/jbox2d/dynamics/Fixture.java +454 -0
  77. data/src/org/jbox2d/dynamics/FixtureDef.java +214 -0
  78. data/src/org/jbox2d/dynamics/FixtureProxy.java +38 -0
  79. data/src/org/jbox2d/dynamics/Island.java +602 -0
  80. data/src/org/jbox2d/dynamics/Profile.java +97 -0
  81. data/src/org/jbox2d/dynamics/SolverData.java +33 -0
  82. data/src/org/jbox2d/dynamics/TimeStep.java +46 -0
  83. data/src/org/jbox2d/dynamics/World.java +2075 -0
  84. data/src/org/jbox2d/dynamics/contacts/ChainAndCircleContact.java +57 -0
  85. data/src/org/jbox2d/dynamics/contacts/ChainAndPolygonContact.java +57 -0
  86. data/src/org/jbox2d/dynamics/contacts/CircleContact.java +50 -0
  87. data/src/org/jbox2d/dynamics/contacts/Contact.java +365 -0
  88. data/src/org/jbox2d/dynamics/contacts/ContactCreator.java +35 -0
  89. data/src/org/jbox2d/dynamics/contacts/ContactEdge.java +56 -0
  90. data/src/org/jbox2d/dynamics/contacts/ContactPositionConstraint.java +49 -0
  91. data/src/org/jbox2d/dynamics/contacts/ContactRegister.java +31 -0
  92. data/src/org/jbox2d/dynamics/contacts/ContactSolver.java +1104 -0
  93. data/src/org/jbox2d/dynamics/contacts/ContactVelocityConstraint.java +60 -0
  94. data/src/org/jbox2d/dynamics/contacts/EdgeAndCircleContact.java +52 -0
  95. data/src/org/jbox2d/dynamics/contacts/EdgeAndPolygonContact.java +52 -0
  96. data/src/org/jbox2d/dynamics/contacts/PolygonAndCircleContact.java +51 -0
  97. data/src/org/jbox2d/dynamics/contacts/PolygonContact.java +50 -0
  98. data/src/org/jbox2d/dynamics/contacts/Position.java +31 -0
  99. data/src/org/jbox2d/dynamics/contacts/Velocity.java +31 -0
  100. data/src/org/jbox2d/dynamics/joints/ConstantVolumeJoint.java +258 -0
  101. data/src/org/jbox2d/dynamics/joints/ConstantVolumeJointDef.java +75 -0
  102. data/src/org/jbox2d/dynamics/joints/DistanceJoint.java +356 -0
  103. data/src/org/jbox2d/dynamics/joints/DistanceJointDef.java +106 -0
  104. data/src/org/jbox2d/dynamics/joints/FrictionJoint.java +294 -0
  105. data/src/org/jbox2d/dynamics/joints/FrictionJointDef.java +78 -0
  106. data/src/org/jbox2d/dynamics/joints/GearJoint.java +520 -0
  107. data/src/org/jbox2d/dynamics/joints/GearJointDef.java +58 -0
  108. data/src/org/jbox2d/dynamics/joints/Jacobian.java +32 -0
  109. data/src/org/jbox2d/dynamics/joints/Joint.java +235 -0
  110. data/src/org/jbox2d/dynamics/joints/JointDef.java +65 -0
  111. data/src/org/jbox2d/dynamics/joints/JointEdge.java +57 -0
  112. data/src/org/jbox2d/dynamics/joints/JointType.java +28 -0
  113. data/src/org/jbox2d/dynamics/joints/LimitState.java +28 -0
  114. data/src/org/jbox2d/dynamics/joints/MotorJoint.java +339 -0
  115. data/src/org/jbox2d/dynamics/joints/MotorJointDef.java +55 -0
  116. data/src/org/jbox2d/dynamics/joints/MouseJoint.java +262 -0
  117. data/src/org/jbox2d/dynamics/joints/MouseJointDef.java +62 -0
  118. data/src/org/jbox2d/dynamics/joints/PrismaticJoint.java +808 -0
  119. data/src/org/jbox2d/dynamics/joints/PrismaticJointDef.java +120 -0
  120. data/src/org/jbox2d/dynamics/joints/PulleyJoint.java +393 -0
  121. data/src/org/jbox2d/dynamics/joints/PulleyJointDef.java +105 -0
  122. data/src/org/jbox2d/dynamics/joints/RevoluteJoint.java +554 -0
  123. data/src/org/jbox2d/dynamics/joints/RevoluteJointDef.java +137 -0
  124. data/src/org/jbox2d/dynamics/joints/RopeJoint.java +276 -0
  125. data/src/org/jbox2d/dynamics/joints/RopeJointDef.java +34 -0
  126. data/src/org/jbox2d/dynamics/joints/WeldJoint.java +424 -0
  127. data/src/org/jbox2d/dynamics/joints/WeldJointDef.java +85 -0
  128. data/src/org/jbox2d/dynamics/joints/WheelJoint.java +498 -0
  129. data/src/org/jbox2d/dynamics/joints/WheelJointDef.java +98 -0
  130. data/src/org/jbox2d/particle/ParticleBodyContact.java +17 -0
  131. data/src/org/jbox2d/particle/ParticleColor.java +52 -0
  132. data/src/org/jbox2d/particle/ParticleContact.java +14 -0
  133. data/src/org/jbox2d/particle/ParticleDef.java +24 -0
  134. data/src/org/jbox2d/particle/ParticleGroup.java +154 -0
  135. data/src/org/jbox2d/particle/ParticleGroupDef.java +62 -0
  136. data/src/org/jbox2d/particle/ParticleGroupType.java +8 -0
  137. data/src/org/jbox2d/particle/ParticleSystem.java +2172 -0
  138. data/src/org/jbox2d/particle/ParticleType.java +28 -0
  139. data/src/org/jbox2d/particle/StackQueue.java +44 -0
  140. data/src/org/jbox2d/particle/VoronoiDiagram.java +209 -0
  141. data/src/org/jbox2d/pooling/IDynamicStack.java +47 -0
  142. data/src/org/jbox2d/pooling/IOrderedStack.java +57 -0
  143. data/src/org/jbox2d/pooling/IWorldPool.java +101 -0
  144. data/src/org/jbox2d/pooling/arrays/FloatArray.java +50 -0
  145. data/src/org/jbox2d/pooling/arrays/GeneratorArray.java +33 -0
  146. data/src/org/jbox2d/pooling/arrays/IntArray.java +53 -0
  147. data/src/org/jbox2d/pooling/arrays/Vec2Array.java +57 -0
  148. data/src/org/jbox2d/pooling/normal/CircleStack.java +77 -0
  149. data/src/org/jbox2d/pooling/normal/DefaultWorldPool.java +331 -0
  150. data/src/org/jbox2d/pooling/normal/MutableStack.java +72 -0
  151. data/src/org/jbox2d/pooling/normal/OrderedStack.java +73 -0
  152. data/src/org/jbox2d/pooling/stacks/DynamicIntStack.java +60 -0
  153. metadata +161 -14
  154. data/lib/jbox2d-library-2.3.1-SNAPSHOT.jar +0 -0
@@ -0,0 +1,339 @@
1
+ package org.jbox2d.dynamics.joints;
2
+
3
+ import org.jbox2d.common.Mat22;
4
+ import org.jbox2d.common.MathUtils;
5
+ import org.jbox2d.common.Rot;
6
+ import org.jbox2d.common.Vec2;
7
+ import org.jbox2d.dynamics.SolverData;
8
+ import org.jbox2d.pooling.IWorldPool;
9
+
10
+ //Point-to-point constraint
11
+ //Cdot = v2 - v1
12
+ // = v2 + cross(w2, r2) - v1 - cross(w1, r1)
13
+ //J = [-I -r1_skew I r2_skew ]
14
+ //Identity used:
15
+ //w k % (rx i + ry j) = w * (-ry i + rx j)
16
+
17
+ //Angle constraint
18
+ //Cdot = w2 - w1
19
+ //J = [0 0 -1 0 0 1]
20
+ //K = invI1 + invI2
21
+
22
+ /**
23
+ * A motor joint is used to control the relative motion between two bodies. A typical usage is to
24
+ * control the movement of a dynamic body with respect to the ground.
25
+ *
26
+ * @author dmurph
27
+ */
28
+ public class MotorJoint extends Joint {
29
+
30
+ // Solver shared
31
+ private final Vec2 m_linearOffset = new Vec2();
32
+ private float m_angularOffset;
33
+ private final Vec2 m_linearImpulse = new Vec2();
34
+ private float m_angularImpulse;
35
+ private float m_maxForce;
36
+ private float m_maxTorque;
37
+ private float m_correctionFactor;
38
+
39
+ // Solver temp
40
+ private int m_indexA;
41
+ private int m_indexB;
42
+ private final Vec2 m_rA = new Vec2();
43
+ private final Vec2 m_rB = new Vec2();
44
+ private final Vec2 m_localCenterA = new Vec2();
45
+ private final Vec2 m_localCenterB = new Vec2();
46
+ private final Vec2 m_linearError = new Vec2();
47
+ private float m_angularError;
48
+ private float m_invMassA;
49
+ private float m_invMassB;
50
+ private float m_invIA;
51
+ private float m_invIB;
52
+ private final Mat22 m_linearMass = new Mat22();
53
+ private float m_angularMass;
54
+
55
+ public MotorJoint(IWorldPool pool, MotorJointDef def) {
56
+ super(pool, def);
57
+ m_linearOffset.set(def.linearOffset);
58
+ m_angularOffset = def.angularOffset;
59
+
60
+ m_angularImpulse = 0.0f;
61
+
62
+ m_maxForce = def.maxForce;
63
+ m_maxTorque = def.maxTorque;
64
+ m_correctionFactor = def.correctionFactor;
65
+ }
66
+
67
+ @Override
68
+ public void getAnchorA(Vec2 out) {
69
+ out.set(m_bodyA.getPosition());
70
+ }
71
+
72
+ @Override
73
+ public void getAnchorB(Vec2 out) {
74
+ out.set(m_bodyB.getPosition());
75
+ }
76
+
77
+ public void getReactionForce(float inv_dt, Vec2 out) {
78
+ out.set(m_linearImpulse).mulLocal(inv_dt);
79
+ }
80
+
81
+ public float getReactionTorque(float inv_dt) {
82
+ return m_angularImpulse * inv_dt;
83
+ }
84
+
85
+ public float getCorrectionFactor() {
86
+ return m_correctionFactor;
87
+ }
88
+
89
+ public void setCorrectionFactor(float correctionFactor) {
90
+ this.m_correctionFactor = correctionFactor;
91
+ }
92
+
93
+ /**
94
+ * Set the target linear offset, in frame A, in meters.
95
+ */
96
+ public void setLinearOffset(Vec2 linearOffset) {
97
+ if (linearOffset.x != m_linearOffset.x || linearOffset.y != m_linearOffset.y) {
98
+ m_bodyA.setAwake(true);
99
+ m_bodyB.setAwake(true);
100
+ m_linearOffset.set(linearOffset);
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Get the target linear offset, in frame A, in meters.
106
+ */
107
+ public void getLinearOffset(Vec2 out) {
108
+ out.set(m_linearOffset);
109
+ }
110
+
111
+ /**
112
+ * Get the target linear offset, in frame A, in meters. Do not modify.
113
+ */
114
+ public Vec2 getLinearOffset() {
115
+ return m_linearOffset;
116
+ }
117
+
118
+ /**
119
+ * Set the target angular offset, in radians.
120
+ *
121
+ * @param angularOffset
122
+ */
123
+ public void setAngularOffset(float angularOffset) {
124
+ if (angularOffset != m_angularOffset) {
125
+ m_bodyA.setAwake(true);
126
+ m_bodyB.setAwake(true);
127
+ m_angularOffset = angularOffset;
128
+ }
129
+ }
130
+
131
+ public float getAngularOffset() {
132
+ return m_angularOffset;
133
+ }
134
+
135
+ /**
136
+ * Set the maximum friction force in N.
137
+ *
138
+ * @param force
139
+ */
140
+ public void setMaxForce(float force) {
141
+ assert (force >= 0.0f);
142
+ m_maxForce = force;
143
+ }
144
+
145
+ /**
146
+ * Get the maximum friction force in N.
147
+ */
148
+ public float getMaxForce() {
149
+ return m_maxForce;
150
+ }
151
+
152
+ /**
153
+ * Set the maximum friction torque in N*m.
154
+ */
155
+ public void setMaxTorque(float torque) {
156
+ assert (torque >= 0.0f);
157
+ m_maxTorque = torque;
158
+ }
159
+
160
+ /**
161
+ * Get the maximum friction torque in N*m.
162
+ */
163
+ public float getMaxTorque() {
164
+ return m_maxTorque;
165
+ }
166
+
167
+ @Override
168
+ public void initVelocityConstraints(SolverData data) {
169
+ m_indexA = m_bodyA.m_islandIndex;
170
+ m_indexB = m_bodyB.m_islandIndex;
171
+ m_localCenterA.set(m_bodyA.m_sweep.localCenter);
172
+ m_localCenterB.set(m_bodyB.m_sweep.localCenter);
173
+ m_invMassA = m_bodyA.m_invMass;
174
+ m_invMassB = m_bodyB.m_invMass;
175
+ m_invIA = m_bodyA.m_invI;
176
+ m_invIB = m_bodyB.m_invI;
177
+
178
+ final Vec2 cA = data.positions[m_indexA].c;
179
+ float aA = data.positions[m_indexA].a;
180
+ final Vec2 vA = data.velocities[m_indexA].v;
181
+ float wA = data.velocities[m_indexA].w;
182
+
183
+ final Vec2 cB = data.positions[m_indexB].c;
184
+ float aB = data.positions[m_indexB].a;
185
+ final Vec2 vB = data.velocities[m_indexB].v;
186
+ float wB = data.velocities[m_indexB].w;
187
+
188
+ final Rot qA = pool.popRot();
189
+ final Rot qB = pool.popRot();
190
+ final Vec2 temp = pool.popVec2();
191
+ Mat22 K = pool.popMat22();
192
+
193
+ qA.set(aA);
194
+ qB.set(aB);
195
+
196
+ // Compute the effective mass matrix.
197
+ // m_rA = b2Mul(qA, -m_localCenterA);
198
+ // m_rB = b2Mul(qB, -m_localCenterB);
199
+ m_rA.x = qA.c * -m_localCenterA.x - qA.s * -m_localCenterA.y;
200
+ m_rA.y = qA.s * -m_localCenterA.x + qA.c * -m_localCenterA.y;
201
+ m_rB.x = qB.c * -m_localCenterB.x - qB.s * -m_localCenterB.y;
202
+ m_rB.y = qB.s * -m_localCenterB.x + qB.c * -m_localCenterB.y;
203
+
204
+ // J = [-I -r1_skew I r2_skew]
205
+ // [ 0 -1 0 1]
206
+ // r_skew = [-ry; rx]
207
+
208
+ // Matlab
209
+ // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
210
+ // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
211
+ // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]
212
+ float mA = m_invMassA, mB = m_invMassB;
213
+ float iA = m_invIA, iB = m_invIB;
214
+
215
+ K.ex.x = mA + mB + iA * m_rA.y * m_rA.y + iB * m_rB.y * m_rB.y;
216
+ K.ex.y = -iA * m_rA.x * m_rA.y - iB * m_rB.x * m_rB.y;
217
+ K.ey.x = K.ex.y;
218
+ K.ey.y = mA + mB + iA * m_rA.x * m_rA.x + iB * m_rB.x * m_rB.x;
219
+
220
+ K.invertToOut(m_linearMass);
221
+
222
+ m_angularMass = iA + iB;
223
+ if (m_angularMass > 0.0f) {
224
+ m_angularMass = 1.0f / m_angularMass;
225
+ }
226
+
227
+ // m_linearError = cB + m_rB - cA - m_rA - b2Mul(qA, m_linearOffset);
228
+ Rot.mulToOutUnsafe(qA, m_linearOffset, temp);
229
+ m_linearError.x = cB.x + m_rB.x - cA.x - m_rA.x - temp.x;
230
+ m_linearError.y = cB.y + m_rB.y - cA.y - m_rA.y - temp.y;
231
+ m_angularError = aB - aA - m_angularOffset;
232
+
233
+ if (data.step.warmStarting) {
234
+ // Scale impulses to support a variable time step.
235
+ m_linearImpulse.x *= data.step.dtRatio;
236
+ m_linearImpulse.y *= data.step.dtRatio;
237
+ m_angularImpulse *= data.step.dtRatio;
238
+
239
+ final Vec2 P = m_linearImpulse;
240
+ vA.x -= mA * P.x;
241
+ vA.y -= mA * P.y;
242
+ wA -= iA * (m_rA.x * P.y - m_rA.y * P.x + m_angularImpulse);
243
+ vB.x += mB * P.x;
244
+ vB.y += mB * P.y;
245
+ wB += iB * (m_rB.x * P.y - m_rB.y * P.x + m_angularImpulse);
246
+ } else {
247
+ m_linearImpulse.setZero();
248
+ m_angularImpulse = 0.0f;
249
+ }
250
+
251
+ pool.pushVec2(1);
252
+ pool.pushMat22(1);
253
+ pool.pushRot(2);
254
+
255
+ // data.velocities[m_indexA].v = vA;
256
+ data.velocities[m_indexA].w = wA;
257
+ // data.velocities[m_indexB].v = vB;
258
+ data.velocities[m_indexB].w = wB;
259
+ }
260
+
261
+ @Override
262
+ public void solveVelocityConstraints(SolverData data) {
263
+ final Vec2 vA = data.velocities[m_indexA].v;
264
+ float wA = data.velocities[m_indexA].w;
265
+ final Vec2 vB = data.velocities[m_indexB].v;
266
+ float wB = data.velocities[m_indexB].w;
267
+
268
+ float mA = m_invMassA, mB = m_invMassB;
269
+ float iA = m_invIA, iB = m_invIB;
270
+
271
+ float h = data.step.dt;
272
+ float inv_h = data.step.inv_dt;
273
+
274
+ final Vec2 temp = pool.popVec2();
275
+
276
+ // Solve angular friction
277
+ {
278
+ float Cdot = wB - wA + inv_h * m_correctionFactor * m_angularError;
279
+ float impulse = -m_angularMass * Cdot;
280
+
281
+ float oldImpulse = m_angularImpulse;
282
+ float maxImpulse = h * m_maxTorque;
283
+ m_angularImpulse = MathUtils.clamp(m_angularImpulse + impulse, -maxImpulse, maxImpulse);
284
+ impulse = m_angularImpulse - oldImpulse;
285
+
286
+ wA -= iA * impulse;
287
+ wB += iB * impulse;
288
+ }
289
+
290
+ final Vec2 Cdot = pool.popVec2();
291
+
292
+ // Solve linear friction
293
+ {
294
+ // Cdot = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA) + inv_h * m_correctionFactor *
295
+ // m_linearError;
296
+ Cdot.x =
297
+ vB.x + -wB * m_rB.y - vA.x - -wA * m_rA.y + inv_h * m_correctionFactor * m_linearError.x;
298
+ Cdot.y =
299
+ vB.y + wB * m_rB.x - vA.y - wA * m_rA.x + inv_h * m_correctionFactor * m_linearError.y;
300
+
301
+ final Vec2 impulse = temp;
302
+ Mat22.mulToOutUnsafe(m_linearMass, Cdot, impulse);
303
+ impulse.negateLocal();
304
+ final Vec2 oldImpulse = pool.popVec2();
305
+ oldImpulse.set(m_linearImpulse);
306
+ m_linearImpulse.addLocal(impulse);
307
+
308
+ float maxImpulse = h * m_maxForce;
309
+
310
+ if (m_linearImpulse.lengthSquared() > maxImpulse * maxImpulse) {
311
+ m_linearImpulse.normalize();
312
+ m_linearImpulse.mulLocal(maxImpulse);
313
+ }
314
+
315
+ impulse.x = m_linearImpulse.x - oldImpulse.x;
316
+ impulse.y = m_linearImpulse.y - oldImpulse.y;
317
+
318
+ vA.x -= mA * impulse.x;
319
+ vA.y -= mA * impulse.y;
320
+ wA -= iA * (m_rA.x * impulse.y - m_rA.y * impulse.x);
321
+
322
+ vB.x += mB * impulse.x;
323
+ vB.y += mB * impulse.y;
324
+ wB += iB * (m_rB.x * impulse.y - m_rB.y * impulse.x);
325
+ }
326
+
327
+ pool.pushVec2(3);
328
+
329
+ // data.velocities[m_indexA].v.set(vA);
330
+ data.velocities[m_indexA].w = wA;
331
+ // data.velocities[m_indexB].v.set(vB);
332
+ data.velocities[m_indexB].w = wB;
333
+ }
334
+
335
+ @Override
336
+ public boolean solvePositionConstraints(SolverData data) {
337
+ return true;
338
+ }
339
+ }
@@ -0,0 +1,55 @@
1
+ package org.jbox2d.dynamics.joints;
2
+
3
+ import org.jbox2d.common.Vec2;
4
+ import org.jbox2d.dynamics.Body;
5
+
6
+ /**
7
+ * Motor joint definition.
8
+ *
9
+ * @author dmurph
10
+ */
11
+ public class MotorJointDef extends JointDef {
12
+ /**
13
+ * Position of bodyB minus the position of bodyA, in bodyA's frame, in meters.
14
+ */
15
+ public final Vec2 linearOffset = new Vec2();
16
+
17
+ /**
18
+ * The bodyB angle minus bodyA angle in radians.
19
+ */
20
+ public float angularOffset;
21
+
22
+ /**
23
+ * The maximum motor force in N.
24
+ */
25
+ public float maxForce;
26
+
27
+ /**
28
+ * The maximum motor torque in N-m.
29
+ */
30
+ public float maxTorque;
31
+
32
+ /**
33
+ * Position correction factor in the range [0,1].
34
+ */
35
+ public float correctionFactor;
36
+
37
+ public MotorJointDef() {
38
+ super(JointType.MOTOR);
39
+ angularOffset = 0;
40
+ maxForce = 1;
41
+ maxTorque = 1;
42
+ correctionFactor = 0.3f;
43
+ }
44
+
45
+ public void initialize(Body bA, Body bB) {
46
+ bodyA = bA;
47
+ bodyB = bB;
48
+ Vec2 xB = bodyB.getPosition();
49
+ bodyA.getLocalPointToOut(xB, linearOffset);
50
+
51
+ float angleA = bodyA.getAngle();
52
+ float angleB = bodyB.getAngle();
53
+ angularOffset = angleB - angleA;
54
+ }
55
+ }
@@ -0,0 +1,262 @@
1
+ /*******************************************************************************
2
+ * Copyright (c) 2013, Daniel Murphy
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without modification,
6
+ * are permitted provided that the following conditions are met:
7
+ * * Redistributions of source code must retain the above copyright notice,
8
+ * this list of conditions and the following disclaimer.
9
+ * * Redistributions in binary form must reproduce the above copyright notice,
10
+ * this list of conditions and the following disclaimer in the documentation
11
+ * and/or other materials provided with the distribution.
12
+ *
13
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16
+ * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
19
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
+ * POSSIBILITY OF SUCH DAMAGE.
23
+ ******************************************************************************/
24
+ package org.jbox2d.dynamics.joints;
25
+
26
+ import org.jbox2d.common.Mat22;
27
+ import org.jbox2d.common.MathUtils;
28
+ import org.jbox2d.common.Rot;
29
+ import org.jbox2d.common.Settings;
30
+ import org.jbox2d.common.Transform;
31
+ import org.jbox2d.common.Vec2;
32
+ import org.jbox2d.dynamics.SolverData;
33
+ import org.jbox2d.pooling.IWorldPool;
34
+
35
+ /**
36
+ * A mouse joint is used to make a point on a body track a specified world point. This a soft
37
+ * constraint with a maximum force. This allows the constraint to stretch and without applying huge
38
+ * forces. NOTE: this joint is not documented in the manual because it was developed to be used in
39
+ * the testbed. If you want to learn how to use the mouse joint, look at the testbed.
40
+ *
41
+ * @author Daniel
42
+ */
43
+ public class MouseJoint extends Joint {
44
+
45
+ private final Vec2 m_localAnchorB = new Vec2();
46
+ private final Vec2 m_targetA = new Vec2();
47
+ private float m_frequencyHz;
48
+ private float m_dampingRatio;
49
+ private float m_beta;
50
+
51
+ // Solver shared
52
+ private final Vec2 m_impulse = new Vec2();
53
+ private float m_maxForce;
54
+ private float m_gamma;
55
+
56
+ // Solver temp
57
+ private int m_indexB;
58
+ private final Vec2 m_rB = new Vec2();
59
+ private final Vec2 m_localCenterB = new Vec2();
60
+ private float m_invMassB;
61
+ private float m_invIB;
62
+ private final Mat22 m_mass = new Mat22();
63
+ private final Vec2 m_C = new Vec2();
64
+
65
+ protected MouseJoint(IWorldPool argWorld, MouseJointDef def) {
66
+ super(argWorld, def);
67
+ assert (def.target.isValid());
68
+ assert (def.maxForce >= 0);
69
+ assert (def.frequencyHz >= 0);
70
+ assert (def.dampingRatio >= 0);
71
+
72
+ m_targetA.set(def.target);
73
+ Transform.mulTransToOutUnsafe(m_bodyB.getTransform(), m_targetA, m_localAnchorB);
74
+
75
+ m_maxForce = def.maxForce;
76
+ m_impulse.setZero();
77
+
78
+ m_frequencyHz = def.frequencyHz;
79
+ m_dampingRatio = def.dampingRatio;
80
+
81
+ m_beta = 0;
82
+ m_gamma = 0;
83
+ }
84
+
85
+ @Override
86
+ public void getAnchorA(Vec2 argOut) {
87
+ argOut.set(m_targetA);
88
+ }
89
+
90
+ @Override
91
+ public void getAnchorB(Vec2 argOut) {
92
+ m_bodyB.getWorldPointToOut(m_localAnchorB, argOut);
93
+ }
94
+
95
+ @Override
96
+ public void getReactionForce(float invDt, Vec2 argOut) {
97
+ argOut.set(m_impulse).mulLocal(invDt);
98
+ }
99
+
100
+ @Override
101
+ public float getReactionTorque(float invDt) {
102
+ return invDt * 0.0f;
103
+ }
104
+
105
+
106
+ public void setTarget(Vec2 target) {
107
+ if (m_bodyB.isAwake() == false) {
108
+ m_bodyB.setAwake(true);
109
+ }
110
+ m_targetA.set(target);
111
+ }
112
+
113
+ public Vec2 getTarget() {
114
+ return m_targetA;
115
+ }
116
+
117
+ // / set/get the maximum force in Newtons.
118
+ public void setMaxForce(float force) {
119
+ m_maxForce = force;
120
+ }
121
+
122
+ public float getMaxForce() {
123
+ return m_maxForce;
124
+ }
125
+
126
+ // / set/get the frequency in Hertz.
127
+ public void setFrequency(float hz) {
128
+ m_frequencyHz = hz;
129
+ }
130
+
131
+ public float getFrequency() {
132
+ return m_frequencyHz;
133
+ }
134
+
135
+ // / set/get the damping ratio (dimensionless).
136
+ public void setDampingRatio(float ratio) {
137
+ m_dampingRatio = ratio;
138
+ }
139
+
140
+ public float getDampingRatio() {
141
+ return m_dampingRatio;
142
+ }
143
+
144
+ @Override
145
+ public void initVelocityConstraints(final SolverData data) {
146
+ m_indexB = m_bodyB.m_islandIndex;
147
+ m_localCenterB.set(m_bodyB.m_sweep.localCenter);
148
+ m_invMassB = m_bodyB.m_invMass;
149
+ m_invIB = m_bodyB.m_invI;
150
+
151
+ Vec2 cB = data.positions[m_indexB].c;
152
+ float aB = data.positions[m_indexB].a;
153
+ Vec2 vB = data.velocities[m_indexB].v;
154
+ float wB = data.velocities[m_indexB].w;
155
+
156
+ final Rot qB = pool.popRot();
157
+
158
+ qB.set(aB);
159
+
160
+ float mass = m_bodyB.getMass();
161
+
162
+ // Frequency
163
+ float omega = 2.0f * MathUtils.PI * m_frequencyHz;
164
+
165
+ // Damping coefficient
166
+ float d = 2.0f * mass * m_dampingRatio * omega;
167
+
168
+ // Spring stiffness
169
+ float k = mass * (omega * omega);
170
+
171
+ // magic formulas
172
+ // gamma has units of inverse mass.
173
+ // beta has units of inverse time.
174
+ float h = data.step.dt;
175
+ assert (d + h * k > Settings.EPSILON);
176
+ m_gamma = h * (d + h * k);
177
+ if (m_gamma != 0.0f) {
178
+ m_gamma = 1.0f / m_gamma;
179
+ }
180
+ m_beta = h * k * m_gamma;
181
+
182
+ Vec2 temp = pool.popVec2();
183
+
184
+ // Compute the effective mass matrix.
185
+ Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), m_rB);
186
+
187
+ // K = [(1/m1 + 1/m2) * eye(2) - skew(r1) * invI1 * skew(r1) - skew(r2) * invI2 * skew(r2)]
188
+ // = [1/m1+1/m2 0 ] + invI1 * [r1.y*r1.y -r1.x*r1.y] + invI2 * [r1.y*r1.y -r1.x*r1.y]
189
+ // [ 0 1/m1+1/m2] [-r1.x*r1.y r1.x*r1.x] [-r1.x*r1.y r1.x*r1.x]
190
+ final Mat22 K = pool.popMat22();
191
+ K.ex.x = m_invMassB + m_invIB * m_rB.y * m_rB.y + m_gamma;
192
+ K.ex.y = -m_invIB * m_rB.x * m_rB.y;
193
+ K.ey.x = K.ex.y;
194
+ K.ey.y = m_invMassB + m_invIB * m_rB.x * m_rB.x + m_gamma;
195
+
196
+ K.invertToOut(m_mass);
197
+
198
+ m_C.set(cB).addLocal(m_rB).subLocal(m_targetA);
199
+ m_C.mulLocal(m_beta);
200
+
201
+ // Cheat with some damping
202
+ wB *= 0.98f;
203
+
204
+ if (data.step.warmStarting) {
205
+ m_impulse.mulLocal(data.step.dtRatio);
206
+ vB.x += m_invMassB * m_impulse.x;
207
+ vB.y += m_invMassB * m_impulse.y;
208
+ wB += m_invIB * Vec2.cross(m_rB, m_impulse);
209
+ } else {
210
+ m_impulse.setZero();
211
+ }
212
+
213
+ // data.velocities[m_indexB].v.set(vB);
214
+ data.velocities[m_indexB].w = wB;
215
+
216
+ pool.pushVec2(1);
217
+ pool.pushMat22(1);
218
+ pool.pushRot(1);
219
+ }
220
+
221
+ @Override
222
+ public boolean solvePositionConstraints(final SolverData data) {
223
+ return true;
224
+ }
225
+
226
+ @Override
227
+ public void solveVelocityConstraints(final SolverData data) {
228
+
229
+ Vec2 vB = data.velocities[m_indexB].v;
230
+ float wB = data.velocities[m_indexB].w;
231
+
232
+ // Cdot = v + cross(w, r)
233
+ final Vec2 Cdot = pool.popVec2();
234
+ Vec2.crossToOutUnsafe(wB, m_rB, Cdot);
235
+ Cdot.addLocal(vB);
236
+
237
+ final Vec2 impulse = pool.popVec2();
238
+ final Vec2 temp = pool.popVec2();
239
+
240
+ temp.set(m_impulse).mulLocal(m_gamma).addLocal(m_C).addLocal(Cdot).negateLocal();
241
+ Mat22.mulToOutUnsafe(m_mass, temp, impulse);
242
+
243
+ Vec2 oldImpulse = temp;
244
+ oldImpulse.set(m_impulse);
245
+ m_impulse.addLocal(impulse);
246
+ float maxImpulse = data.step.dt * m_maxForce;
247
+ if (m_impulse.lengthSquared() > maxImpulse * maxImpulse) {
248
+ m_impulse.mulLocal(maxImpulse / m_impulse.length());
249
+ }
250
+ impulse.set(m_impulse).subLocal(oldImpulse);
251
+
252
+ vB.x += m_invMassB * impulse.x;
253
+ vB.y += m_invMassB * impulse.y;
254
+ wB += m_invIB * Vec2.cross(m_rB, impulse);
255
+
256
+ // data.velocities[m_indexB].v.set(vB);
257
+ data.velocities[m_indexB].w = wB;
258
+
259
+ pool.pushVec2(3);
260
+ }
261
+
262
+ }
@@ -0,0 +1,62 @@
1
+ /*******************************************************************************
2
+ * Copyright (c) 2013, Daniel Murphy
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without modification,
6
+ * are permitted provided that the following conditions are met:
7
+ * * Redistributions of source code must retain the above copyright notice,
8
+ * this list of conditions and the following disclaimer.
9
+ * * Redistributions in binary form must reproduce the above copyright notice,
10
+ * this list of conditions and the following disclaimer in the documentation
11
+ * and/or other materials provided with the distribution.
12
+ *
13
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16
+ * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
19
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
+ * POSSIBILITY OF SUCH DAMAGE.
23
+ ******************************************************************************/
24
+ package org.jbox2d.dynamics.joints;
25
+
26
+ import org.jbox2d.common.Vec2;
27
+
28
+ /**
29
+ * Mouse joint definition. This requires a world target point, tuning parameters, and the time step.
30
+ *
31
+ * @author Daniel
32
+ */
33
+ public class MouseJointDef extends JointDef {
34
+ /**
35
+ * The initial world target point. This is assumed to coincide with the body anchor initially.
36
+ */
37
+ public final Vec2 target = new Vec2();
38
+
39
+ /**
40
+ * The maximum constraint force that can be exerted to move the candidate body. Usually you will
41
+ * express as some multiple of the weight (multiplier * mass * gravity).
42
+ */
43
+ public float maxForce;
44
+
45
+ /**
46
+ * The response speed.
47
+ */
48
+ public float frequencyHz;
49
+
50
+ /**
51
+ * The damping ratio. 0 = no damping, 1 = critical damping.
52
+ */
53
+ public float dampingRatio;
54
+
55
+ public MouseJointDef() {
56
+ super(JointType.MOUSE);
57
+ target.set(0, 0);
58
+ maxForce = 0;
59
+ frequencyHz = 5;
60
+ dampingRatio = .7f;
61
+ }
62
+ }