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,554 @@
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.Mat33;
28
+ import org.jbox2d.common.MathUtils;
29
+ import org.jbox2d.common.Rot;
30
+ import org.jbox2d.common.Settings;
31
+ import org.jbox2d.common.Vec2;
32
+ import org.jbox2d.common.Vec3;
33
+ import org.jbox2d.dynamics.Body;
34
+ import org.jbox2d.dynamics.SolverData;
35
+ import org.jbox2d.pooling.IWorldPool;
36
+
37
+ //Point-to-point constraint
38
+ //C = p2 - p1
39
+ //Cdot = v2 - v1
40
+ // = v2 + cross(w2, r2) - v1 - cross(w1, r1)
41
+ //J = [-I -r1_skew I r2_skew ]
42
+ //Identity used:
43
+ //w k % (rx i + ry j) = w * (-ry i + rx j)
44
+
45
+ //Motor constraint
46
+ //Cdot = w2 - w1
47
+ //J = [0 0 -1 0 0 1]
48
+ //K = invI1 + invI2
49
+
50
+ /**
51
+ * A revolute joint constrains two bodies to share a common point while they are free to rotate
52
+ * about the point. The relative rotation about the shared point is the joint angle. You can limit
53
+ * the relative rotation with a joint limit that specifies a lower and upper angle. You can use a
54
+ * motor to drive the relative rotation about the shared point. A maximum motor torque is provided
55
+ * so that infinite forces are not generated.
56
+ *
57
+ * @author Daniel Murphy
58
+ */
59
+ public class RevoluteJoint extends Joint {
60
+
61
+ // Solver shared
62
+ protected final Vec2 m_localAnchorA = new Vec2();
63
+ protected final Vec2 m_localAnchorB = new Vec2();
64
+ private final Vec3 m_impulse = new Vec3();
65
+ private float m_motorImpulse;
66
+
67
+ private boolean m_enableMotor;
68
+ private float m_maxMotorTorque;
69
+ private float m_motorSpeed;
70
+
71
+ private boolean m_enableLimit;
72
+ protected float m_referenceAngle;
73
+ private float m_lowerAngle;
74
+ private float m_upperAngle;
75
+
76
+ // Solver temp
77
+ private int m_indexA;
78
+ private int m_indexB;
79
+ private final Vec2 m_rA = new Vec2();
80
+ private final Vec2 m_rB = new Vec2();
81
+ private final Vec2 m_localCenterA = new Vec2();
82
+ private final Vec2 m_localCenterB = new Vec2();
83
+ private float m_invMassA;
84
+ private float m_invMassB;
85
+ private float m_invIA;
86
+ private float m_invIB;
87
+ private final Mat33 m_mass = new Mat33(); // effective mass for point-to-point constraint.
88
+ private float m_motorMass; // effective mass for motor/limit angular constraint.
89
+ private LimitState m_limitState;
90
+
91
+ protected RevoluteJoint(IWorldPool argWorld, RevoluteJointDef def) {
92
+ super(argWorld, def);
93
+ m_localAnchorA.set(def.localAnchorA);
94
+ m_localAnchorB.set(def.localAnchorB);
95
+ m_referenceAngle = def.referenceAngle;
96
+
97
+ m_motorImpulse = 0;
98
+
99
+ m_lowerAngle = def.lowerAngle;
100
+ m_upperAngle = def.upperAngle;
101
+ m_maxMotorTorque = def.maxMotorTorque;
102
+ m_motorSpeed = def.motorSpeed;
103
+ m_enableLimit = def.enableLimit;
104
+ m_enableMotor = def.enableMotor;
105
+ m_limitState = LimitState.INACTIVE;
106
+ }
107
+
108
+ @Override
109
+ public void initVelocityConstraints(final SolverData data) {
110
+ m_indexA = m_bodyA.m_islandIndex;
111
+ m_indexB = m_bodyB.m_islandIndex;
112
+ m_localCenterA.set(m_bodyA.m_sweep.localCenter);
113
+ m_localCenterB.set(m_bodyB.m_sweep.localCenter);
114
+ m_invMassA = m_bodyA.m_invMass;
115
+ m_invMassB = m_bodyB.m_invMass;
116
+ m_invIA = m_bodyA.m_invI;
117
+ m_invIB = m_bodyB.m_invI;
118
+
119
+ // Vec2 cA = data.positions[m_indexA].c;
120
+ float aA = data.positions[m_indexA].a;
121
+ Vec2 vA = data.velocities[m_indexA].v;
122
+ float wA = data.velocities[m_indexA].w;
123
+
124
+ // Vec2 cB = data.positions[m_indexB].c;
125
+ float aB = data.positions[m_indexB].a;
126
+ Vec2 vB = data.velocities[m_indexB].v;
127
+ float wB = data.velocities[m_indexB].w;
128
+ final Rot qA = pool.popRot();
129
+ final Rot qB = pool.popRot();
130
+ final Vec2 temp = pool.popVec2();
131
+
132
+ qA.set(aA);
133
+ qB.set(aB);
134
+
135
+ // Compute the effective masses.
136
+ Rot.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_localCenterA), m_rA);
137
+ Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), m_rB);
138
+
139
+ // J = [-I -r1_skew I r2_skew]
140
+ // [ 0 -1 0 1]
141
+ // r_skew = [-ry; rx]
142
+
143
+ // Matlab
144
+ // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
145
+ // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
146
+ // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]
147
+
148
+ float mA = m_invMassA, mB = m_invMassB;
149
+ float iA = m_invIA, iB = m_invIB;
150
+
151
+ boolean fixedRotation = (iA + iB == 0.0f);
152
+
153
+ m_mass.ex.x = mA + mB + m_rA.y * m_rA.y * iA + m_rB.y * m_rB.y * iB;
154
+ m_mass.ey.x = -m_rA.y * m_rA.x * iA - m_rB.y * m_rB.x * iB;
155
+ m_mass.ez.x = -m_rA.y * iA - m_rB.y * iB;
156
+ m_mass.ex.y = m_mass.ey.x;
157
+ m_mass.ey.y = mA + mB + m_rA.x * m_rA.x * iA + m_rB.x * m_rB.x * iB;
158
+ m_mass.ez.y = m_rA.x * iA + m_rB.x * iB;
159
+ m_mass.ex.z = m_mass.ez.x;
160
+ m_mass.ey.z = m_mass.ez.y;
161
+ m_mass.ez.z = iA + iB;
162
+
163
+ m_motorMass = iA + iB;
164
+ if (m_motorMass > 0.0f) {
165
+ m_motorMass = 1.0f / m_motorMass;
166
+ }
167
+
168
+ if (m_enableMotor == false || fixedRotation) {
169
+ m_motorImpulse = 0.0f;
170
+ }
171
+
172
+ if (m_enableLimit && fixedRotation == false) {
173
+ float jointAngle = aB - aA - m_referenceAngle;
174
+ if (MathUtils.abs(m_upperAngle - m_lowerAngle) < 2.0f * Settings.angularSlop) {
175
+ m_limitState = LimitState.EQUAL;
176
+ } else if (jointAngle <= m_lowerAngle) {
177
+ if (m_limitState != LimitState.AT_LOWER) {
178
+ m_impulse.z = 0.0f;
179
+ }
180
+ m_limitState = LimitState.AT_LOWER;
181
+ } else if (jointAngle >= m_upperAngle) {
182
+ if (m_limitState != LimitState.AT_UPPER) {
183
+ m_impulse.z = 0.0f;
184
+ }
185
+ m_limitState = LimitState.AT_UPPER;
186
+ } else {
187
+ m_limitState = LimitState.INACTIVE;
188
+ m_impulse.z = 0.0f;
189
+ }
190
+ } else {
191
+ m_limitState = LimitState.INACTIVE;
192
+ }
193
+
194
+ if (data.step.warmStarting) {
195
+ final Vec2 P = pool.popVec2();
196
+ // Scale impulses to support a variable time step.
197
+ m_impulse.x *= data.step.dtRatio;
198
+ m_impulse.y *= data.step.dtRatio;
199
+ m_motorImpulse *= data.step.dtRatio;
200
+
201
+ P.x = m_impulse.x;
202
+ P.y = m_impulse.y;
203
+
204
+ vA.x -= mA * P.x;
205
+ vA.y -= mA * P.y;
206
+ wA -= iA * (Vec2.cross(m_rA, P) + m_motorImpulse + m_impulse.z);
207
+
208
+ vB.x += mB * P.x;
209
+ vB.y += mB * P.y;
210
+ wB += iB * (Vec2.cross(m_rB, P) + m_motorImpulse + m_impulse.z);
211
+ pool.pushVec2(1);
212
+ } else {
213
+ m_impulse.setZero();
214
+ m_motorImpulse = 0.0f;
215
+ }
216
+ // data.velocities[m_indexA].v.set(vA);
217
+ data.velocities[m_indexA].w = wA;
218
+ // data.velocities[m_indexB].v.set(vB);
219
+ data.velocities[m_indexB].w = wB;
220
+
221
+ pool.pushVec2(1);
222
+ pool.pushRot(2);
223
+ }
224
+
225
+ @Override
226
+ public void solveVelocityConstraints(final SolverData data) {
227
+ Vec2 vA = data.velocities[m_indexA].v;
228
+ float wA = data.velocities[m_indexA].w;
229
+ Vec2 vB = data.velocities[m_indexB].v;
230
+ float wB = data.velocities[m_indexB].w;
231
+
232
+ float mA = m_invMassA, mB = m_invMassB;
233
+ float iA = m_invIA, iB = m_invIB;
234
+
235
+ boolean fixedRotation = (iA + iB == 0.0f);
236
+
237
+ // Solve motor constraint.
238
+ if (m_enableMotor && m_limitState != LimitState.EQUAL && fixedRotation == false) {
239
+ float Cdot = wB - wA - m_motorSpeed;
240
+ float impulse = -m_motorMass * Cdot;
241
+ float oldImpulse = m_motorImpulse;
242
+ float maxImpulse = data.step.dt * m_maxMotorTorque;
243
+ m_motorImpulse = MathUtils.clamp(m_motorImpulse + impulse, -maxImpulse, maxImpulse);
244
+ impulse = m_motorImpulse - oldImpulse;
245
+
246
+ wA -= iA * impulse;
247
+ wB += iB * impulse;
248
+ }
249
+ final Vec2 temp = pool.popVec2();
250
+
251
+ // Solve limit constraint.
252
+ if (m_enableLimit && m_limitState != LimitState.INACTIVE && fixedRotation == false) {
253
+
254
+ final Vec2 Cdot1 = pool.popVec2();
255
+ final Vec3 Cdot = pool.popVec3();
256
+
257
+ // Solve point-to-point constraint
258
+ Vec2.crossToOutUnsafe(wA, m_rA, temp);
259
+ Vec2.crossToOutUnsafe(wB, m_rB, Cdot1);
260
+ Cdot1.addLocal(vB).subLocal(vA).subLocal(temp);
261
+ float Cdot2 = wB - wA;
262
+ Cdot.set(Cdot1.x, Cdot1.y, Cdot2);
263
+
264
+ Vec3 impulse = pool.popVec3();
265
+ m_mass.solve33ToOut(Cdot, impulse);
266
+ impulse.negateLocal();
267
+
268
+ if (m_limitState == LimitState.EQUAL) {
269
+ m_impulse.addLocal(impulse);
270
+ } else if (m_limitState == LimitState.AT_LOWER) {
271
+ float newImpulse = m_impulse.z + impulse.z;
272
+ if (newImpulse < 0.0f) {
273
+ final Vec2 rhs = pool.popVec2();
274
+ rhs.set(m_mass.ez.x, m_mass.ez.y).mulLocal(m_impulse.z).subLocal(Cdot1);
275
+ m_mass.solve22ToOut(rhs, temp);
276
+ impulse.x = temp.x;
277
+ impulse.y = temp.y;
278
+ impulse.z = -m_impulse.z;
279
+ m_impulse.x += temp.x;
280
+ m_impulse.y += temp.y;
281
+ m_impulse.z = 0.0f;
282
+ pool.pushVec2(1);
283
+ } else {
284
+ m_impulse.addLocal(impulse);
285
+ }
286
+ } else if (m_limitState == LimitState.AT_UPPER) {
287
+ float newImpulse = m_impulse.z + impulse.z;
288
+ if (newImpulse > 0.0f) {
289
+ final Vec2 rhs = pool.popVec2();
290
+ rhs.set(m_mass.ez.x, m_mass.ez.y).mulLocal(m_impulse.z).subLocal(Cdot1);
291
+ m_mass.solve22ToOut(rhs, temp);
292
+ impulse.x = temp.x;
293
+ impulse.y = temp.y;
294
+ impulse.z = -m_impulse.z;
295
+ m_impulse.x += temp.x;
296
+ m_impulse.y += temp.y;
297
+ m_impulse.z = 0.0f;
298
+ pool.pushVec2(1);
299
+ } else {
300
+ m_impulse.addLocal(impulse);
301
+ }
302
+ }
303
+ final Vec2 P = pool.popVec2();
304
+
305
+ P.set(impulse.x, impulse.y);
306
+
307
+ vA.x -= mA * P.x;
308
+ vA.y -= mA * P.y;
309
+ wA -= iA * (Vec2.cross(m_rA, P) + impulse.z);
310
+
311
+ vB.x += mB * P.x;
312
+ vB.y += mB * P.y;
313
+ wB += iB * (Vec2.cross(m_rB, P) + impulse.z);
314
+
315
+ pool.pushVec2(2);
316
+ pool.pushVec3(2);
317
+ } else {
318
+
319
+ // Solve point-to-point constraint
320
+ Vec2 Cdot = pool.popVec2();
321
+ Vec2 impulse = pool.popVec2();
322
+
323
+ Vec2.crossToOutUnsafe(wA, m_rA, temp);
324
+ Vec2.crossToOutUnsafe(wB, m_rB, Cdot);
325
+ Cdot.addLocal(vB).subLocal(vA).subLocal(temp);
326
+ m_mass.solve22ToOut(Cdot.negateLocal(), impulse); // just leave negated
327
+
328
+ m_impulse.x += impulse.x;
329
+ m_impulse.y += impulse.y;
330
+
331
+ vA.x -= mA * impulse.x;
332
+ vA.y -= mA * impulse.y;
333
+ wA -= iA * Vec2.cross(m_rA, impulse);
334
+
335
+ vB.x += mB * impulse.x;
336
+ vB.y += mB * impulse.y;
337
+ wB += iB * Vec2.cross(m_rB, impulse);
338
+
339
+ pool.pushVec2(2);
340
+ }
341
+
342
+ // data.velocities[m_indexA].v.set(vA);
343
+ data.velocities[m_indexA].w = wA;
344
+ // data.velocities[m_indexB].v.set(vB);
345
+ data.velocities[m_indexB].w = wB;
346
+
347
+ pool.pushVec2(1);
348
+ }
349
+
350
+ @Override
351
+ public boolean solvePositionConstraints(final SolverData data) {
352
+ final Rot qA = pool.popRot();
353
+ final Rot qB = pool.popRot();
354
+ Vec2 cA = data.positions[m_indexA].c;
355
+ float aA = data.positions[m_indexA].a;
356
+ Vec2 cB = data.positions[m_indexB].c;
357
+ float aB = data.positions[m_indexB].a;
358
+
359
+ qA.set(aA);
360
+ qB.set(aB);
361
+
362
+ float angularError = 0.0f;
363
+ float positionError = 0.0f;
364
+
365
+ boolean fixedRotation = (m_invIA + m_invIB == 0.0f);
366
+
367
+ // Solve angular limit constraint.
368
+ if (m_enableLimit && m_limitState != LimitState.INACTIVE && fixedRotation == false) {
369
+ float angle = aB - aA - m_referenceAngle;
370
+ float limitImpulse = 0.0f;
371
+
372
+ if (m_limitState == LimitState.EQUAL) {
373
+ // Prevent large angular corrections
374
+ float C =
375
+ MathUtils.clamp(angle - m_lowerAngle, -Settings.maxAngularCorrection,
376
+ Settings.maxAngularCorrection);
377
+ limitImpulse = -m_motorMass * C;
378
+ angularError = MathUtils.abs(C);
379
+ } else if (m_limitState == LimitState.AT_LOWER) {
380
+ float C = angle - m_lowerAngle;
381
+ angularError = -C;
382
+
383
+ // Prevent large angular corrections and allow some slop.
384
+ C = MathUtils.clamp(C + Settings.angularSlop, -Settings.maxAngularCorrection, 0.0f);
385
+ limitImpulse = -m_motorMass * C;
386
+ } else if (m_limitState == LimitState.AT_UPPER) {
387
+ float C = angle - m_upperAngle;
388
+ angularError = C;
389
+
390
+ // Prevent large angular corrections and allow some slop.
391
+ C = MathUtils.clamp(C - Settings.angularSlop, 0.0f, Settings.maxAngularCorrection);
392
+ limitImpulse = -m_motorMass * C;
393
+ }
394
+
395
+ aA -= m_invIA * limitImpulse;
396
+ aB += m_invIB * limitImpulse;
397
+ }
398
+ // Solve point-to-point constraint.
399
+ {
400
+ qA.set(aA);
401
+ qB.set(aB);
402
+
403
+ final Vec2 rA = pool.popVec2();
404
+ final Vec2 rB = pool.popVec2();
405
+ final Vec2 C = pool.popVec2();
406
+ final Vec2 impulse = pool.popVec2();
407
+
408
+ Rot.mulToOutUnsafe(qA, C.set(m_localAnchorA).subLocal(m_localCenterA), rA);
409
+ Rot.mulToOutUnsafe(qB, C.set(m_localAnchorB).subLocal(m_localCenterB), rB);
410
+ C.set(cB).addLocal(rB).subLocal(cA).subLocal(rA);
411
+ positionError = C.length();
412
+
413
+ float mA = m_invMassA, mB = m_invMassB;
414
+ float iA = m_invIA, iB = m_invIB;
415
+
416
+ final Mat22 K = pool.popMat22();
417
+ K.ex.x = mA + mB + iA * rA.y * rA.y + iB * rB.y * rB.y;
418
+ K.ex.y = -iA * rA.x * rA.y - iB * rB.x * rB.y;
419
+ K.ey.x = K.ex.y;
420
+ K.ey.y = mA + mB + iA * rA.x * rA.x + iB * rB.x * rB.x;
421
+ K.solveToOut(C, impulse);
422
+ impulse.negateLocal();
423
+
424
+ cA.x -= mA * impulse.x;
425
+ cA.y -= mA * impulse.y;
426
+ aA -= iA * Vec2.cross(rA, impulse);
427
+
428
+ cB.x += mB * impulse.x;
429
+ cB.y += mB * impulse.y;
430
+ aB += iB * Vec2.cross(rB, impulse);
431
+
432
+ pool.pushVec2(4);
433
+ pool.pushMat22(1);
434
+ }
435
+ // data.positions[m_indexA].c.set(cA);
436
+ data.positions[m_indexA].a = aA;
437
+ // data.positions[m_indexB].c.set(cB);
438
+ data.positions[m_indexB].a = aB;
439
+
440
+ pool.pushRot(2);
441
+
442
+ return positionError <= Settings.linearSlop && angularError <= Settings.angularSlop;
443
+ }
444
+
445
+ public Vec2 getLocalAnchorA() {
446
+ return m_localAnchorA;
447
+ }
448
+
449
+ public Vec2 getLocalAnchorB() {
450
+ return m_localAnchorB;
451
+ }
452
+
453
+ public float getReferenceAngle() {
454
+ return m_referenceAngle;
455
+ }
456
+
457
+ @Override
458
+ public void getAnchorA(Vec2 argOut) {
459
+ m_bodyA.getWorldPointToOut(m_localAnchorA, argOut);
460
+ }
461
+
462
+ @Override
463
+ public void getAnchorB(Vec2 argOut) {
464
+ m_bodyB.getWorldPointToOut(m_localAnchorB, argOut);
465
+ }
466
+
467
+ @Override
468
+ public void getReactionForce(float inv_dt, Vec2 argOut) {
469
+ argOut.set(m_impulse.x, m_impulse.y).mulLocal(inv_dt);
470
+ }
471
+
472
+ @Override
473
+ public float getReactionTorque(float inv_dt) {
474
+ return inv_dt * m_impulse.z;
475
+ }
476
+
477
+ public float getJointAngle() {
478
+ final Body b1 = m_bodyA;
479
+ final Body b2 = m_bodyB;
480
+ return b2.m_sweep.a - b1.m_sweep.a - m_referenceAngle;
481
+ }
482
+
483
+ public float getJointSpeed() {
484
+ final Body b1 = m_bodyA;
485
+ final Body b2 = m_bodyB;
486
+ return b2.m_angularVelocity - b1.m_angularVelocity;
487
+ }
488
+
489
+ public boolean isMotorEnabled() {
490
+ return m_enableMotor;
491
+ }
492
+
493
+ public void enableMotor(boolean flag) {
494
+ m_bodyA.setAwake(true);
495
+ m_bodyB.setAwake(true);
496
+ m_enableMotor = flag;
497
+ }
498
+
499
+ public float getMotorTorque(float inv_dt) {
500
+ return m_motorImpulse * inv_dt;
501
+ }
502
+
503
+ public void setMotorSpeed(final float speed) {
504
+ m_bodyA.setAwake(true);
505
+ m_bodyB.setAwake(true);
506
+ m_motorSpeed = speed;
507
+ }
508
+
509
+ public void setMaxMotorTorque(final float torque) {
510
+ m_bodyA.setAwake(true);
511
+ m_bodyB.setAwake(true);
512
+ m_maxMotorTorque = torque;
513
+ }
514
+
515
+ public float getMotorSpeed() {
516
+ return m_motorSpeed;
517
+ }
518
+
519
+ public float getMaxMotorTorque() {
520
+ return m_maxMotorTorque;
521
+ }
522
+
523
+ public boolean isLimitEnabled() {
524
+ return m_enableLimit;
525
+ }
526
+
527
+ public void enableLimit(final boolean flag) {
528
+ if (flag != m_enableLimit) {
529
+ m_bodyA.setAwake(true);
530
+ m_bodyB.setAwake(true);
531
+ m_enableLimit = flag;
532
+ m_impulse.z = 0.0f;
533
+ }
534
+ }
535
+
536
+ public float getLowerLimit() {
537
+ return m_lowerAngle;
538
+ }
539
+
540
+ public float getUpperLimit() {
541
+ return m_upperAngle;
542
+ }
543
+
544
+ public void setLimits(final float lower, final float upper) {
545
+ assert (lower <= upper);
546
+ if (lower != m_lowerAngle || upper != m_upperAngle) {
547
+ m_bodyA.setAwake(true);
548
+ m_bodyB.setAwake(true);
549
+ m_impulse.z = 0.0f;
550
+ m_lowerAngle = lower;
551
+ m_upperAngle = upper;
552
+ }
553
+ }
554
+ }
@@ -0,0 +1,137 @@
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
+ /*
25
+ * JBox2D - A Java Port of Erin Catto's Box2D
26
+ *
27
+ * JBox2D homepage: http://jbox2d.sourceforge.net/
28
+ * Box2D homepage: http://www.box2d.org
29
+ *
30
+ * This software is provided 'as-is', without any express or implied
31
+ * warranty. In no event will the authors be held liable for any damages
32
+ * arising from the use of this software.
33
+ *
34
+ * Permission is granted to anyone to use this software for any purpose,
35
+ * including commercial applications, and to alter it and redistribute it
36
+ * freely, subject to the following restrictions:
37
+ *
38
+ * 1. The origin of this software must not be misrepresented; you must not
39
+ * claim that you wrote the original software. If you use this software
40
+ * in a product, an acknowledgment in the product documentation would be
41
+ * appreciated but is not required.
42
+ * 2. Altered source versions must be plainly marked as such, and must not be
43
+ * misrepresented as being the original software.
44
+ * 3. This notice may not be removed or altered from any source distribution.
45
+ */
46
+
47
+ package org.jbox2d.dynamics.joints;
48
+
49
+ import org.jbox2d.common.Vec2;
50
+ import org.jbox2d.dynamics.Body;
51
+
52
+ /**
53
+ * Revolute joint definition. This requires defining an anchor point where the bodies are joined.
54
+ * The definition uses local anchor points so that the initial configuration can violate the
55
+ * constraint slightly. You also need to specify the initial relative angle for joint limits. This
56
+ * helps when saving and loading a game. The local anchor points are measured from the body's origin
57
+ * rather than the center of mass because:<br/>
58
+ * <ul>
59
+ * <li>you might not know where the center of mass will be.</li>
60
+ * <li>if you add/remove shapes from a body and recompute the mass, the joints will be broken.</li>
61
+ * </ul>
62
+ */
63
+ public class RevoluteJointDef extends JointDef {
64
+
65
+ /**
66
+ * The local anchor point relative to body1's origin.
67
+ */
68
+ public Vec2 localAnchorA;
69
+
70
+ /**
71
+ * The local anchor point relative to body2's origin.
72
+ */
73
+ public Vec2 localAnchorB;
74
+
75
+ /**
76
+ * The body2 angle minus body1 angle in the reference state (radians).
77
+ */
78
+ public float referenceAngle;
79
+
80
+ /**
81
+ * A flag to enable joint limits.
82
+ */
83
+ public boolean enableLimit;
84
+
85
+ /**
86
+ * The lower angle for the joint limit (radians).
87
+ */
88
+ public float lowerAngle;
89
+
90
+ /**
91
+ * The upper angle for the joint limit (radians).
92
+ */
93
+ public float upperAngle;
94
+
95
+ /**
96
+ * A flag to enable the joint motor.
97
+ */
98
+ public boolean enableMotor;
99
+
100
+ /**
101
+ * The desired motor speed. Usually in radians per second.
102
+ */
103
+ public float motorSpeed;
104
+
105
+ /**
106
+ * The maximum motor torque used to achieve the desired motor speed. Usually in N-m.
107
+ */
108
+ public float maxMotorTorque;
109
+
110
+ public RevoluteJointDef() {
111
+ super(JointType.REVOLUTE);
112
+ localAnchorA = new Vec2(0.0f, 0.0f);
113
+ localAnchorB = new Vec2(0.0f, 0.0f);
114
+ referenceAngle = 0.0f;
115
+ lowerAngle = 0.0f;
116
+ upperAngle = 0.0f;
117
+ maxMotorTorque = 0.0f;
118
+ motorSpeed = 0.0f;
119
+ enableLimit = false;
120
+ enableMotor = false;
121
+ }
122
+
123
+ /**
124
+ * Initialize the bodies, anchors, and reference angle using the world anchor.
125
+ *
126
+ * @param b1
127
+ * @param b2
128
+ * @param anchor
129
+ */
130
+ public void initialize(final Body b1, final Body b2, final Vec2 anchor) {
131
+ bodyA = b1;
132
+ bodyB = b2;
133
+ bodyA.getLocalPointToOut(anchor, localAnchorA);
134
+ bodyB.getLocalPointToOut(anchor, localAnchorB);
135
+ referenceAngle = bodyB.getAngle() - bodyA.getAngle();
136
+ }
137
+ }