-
Notifications
You must be signed in to change notification settings - Fork 208
/
ConstantVolumeJoint.java
258 lines (227 loc) · 9.38 KB
/
ConstantVolumeJoint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*******************************************************************************
* Copyright (c) 2013, Daniel Murphy
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package org.jbox2d.dynamics.joints;
import org.jbox2d.common.MathUtils;
import org.jbox2d.common.Settings;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.SolverData;
import org.jbox2d.dynamics.World;
import org.jbox2d.dynamics.contacts.Position;
import org.jbox2d.dynamics.contacts.Velocity;
public class ConstantVolumeJoint extends Joint {
private final Body[] bodies;
private float[] targetLengths;
private float targetVolume;
private Vec2[] normals;
private float m_impulse = 0.0f;
private World world;
private DistanceJoint[] distanceJoints;
public Body[] getBodies() {
return bodies;
}
public DistanceJoint[] getJoints() {
return distanceJoints;
}
public void inflate(float factor) {
targetVolume *= factor;
}
public ConstantVolumeJoint(World argWorld, ConstantVolumeJointDef def) {
super(argWorld.getPool(), def);
world = argWorld;
if (def.bodies.size() <= 2) {
throw new IllegalArgumentException(
"You cannot create a constant volume joint with less than three bodies.");
}
bodies = def.bodies.toArray(new Body[0]);
targetLengths = new float[bodies.length];
for (int i = 0; i < targetLengths.length; ++i) {
final int next = (i == targetLengths.length - 1) ? 0 : i + 1;
float dist = bodies[i].getWorldCenter().sub(bodies[next].getWorldCenter()).length();
targetLengths[i] = dist;
}
targetVolume = getBodyArea();
if (def.joints != null && def.joints.size() != def.bodies.size()) {
throw new IllegalArgumentException(
"Incorrect joint definition. Joints have to correspond to the bodies");
}
if (def.joints == null) {
final DistanceJointDef djd = new DistanceJointDef();
distanceJoints = new DistanceJoint[bodies.length];
for (int i = 0; i < targetLengths.length; ++i) {
final int next = (i == targetLengths.length - 1) ? 0 : i + 1;
djd.frequencyHz = def.frequencyHz;// 20.0f;
djd.dampingRatio = def.dampingRatio;// 50.0f;
djd.collideConnected = def.collideConnected;
djd.initialize(bodies[i], bodies[next], bodies[i].getWorldCenter(),
bodies[next].getWorldCenter());
distanceJoints[i] = (DistanceJoint) world.createJoint(djd);
}
} else {
distanceJoints = def.joints.toArray(new DistanceJoint[0]);
}
normals = new Vec2[bodies.length];
for (int i = 0; i < normals.length; ++i) {
normals[i] = new Vec2();
}
}
@Override
public void destructor() {
for (int i = 0; i < distanceJoints.length; ++i) {
world.destroyJoint(distanceJoints[i]);
}
}
private float getBodyArea() {
float area = 0.0f;
for (int i = 0; i < bodies.length; ++i) {
final int next = (i == bodies.length - 1) ? 0 : i + 1;
area +=
bodies[i].getWorldCenter().x * bodies[next].getWorldCenter().y
- bodies[next].getWorldCenter().x * bodies[i].getWorldCenter().y;
}
area *= .5f;
return area;
}
private float getSolverArea(Position[] positions) {
float area = 0.0f;
for (int i = 0; i < bodies.length; ++i) {
final int next = (i == bodies.length - 1) ? 0 : i + 1;
area +=
positions[bodies[i].m_islandIndex].c.x * positions[bodies[next].m_islandIndex].c.y
- positions[bodies[next].m_islandIndex].c.x * positions[bodies[i].m_islandIndex].c.y;
}
area *= .5f;
return area;
}
private boolean constrainEdges(Position[] positions) {
float perimeter = 0.0f;
for (int i = 0; i < bodies.length; ++i) {
final int next = (i == bodies.length - 1) ? 0 : i + 1;
float dx = positions[bodies[next].m_islandIndex].c.x - positions[bodies[i].m_islandIndex].c.x;
float dy = positions[bodies[next].m_islandIndex].c.y - positions[bodies[i].m_islandIndex].c.y;
float dist = MathUtils.sqrt(dx * dx + dy * dy);
if (dist < Settings.EPSILON) {
dist = 1.0f;
}
normals[i].x = dy / dist;
normals[i].y = -dx / dist;
perimeter += dist;
}
final Vec2 delta = pool.popVec2();
float deltaArea = targetVolume - getSolverArea(positions);
float toExtrude = 0.5f * deltaArea / perimeter; // *relaxationFactor
// float sumdeltax = 0.0f;
boolean done = true;
for (int i = 0; i < bodies.length; ++i) {
final int next = (i == bodies.length - 1) ? 0 : i + 1;
delta.set(toExtrude * (normals[i].x + normals[next].x), toExtrude
* (normals[i].y + normals[next].y));
// sumdeltax += dx;
float normSqrd = delta.lengthSquared();
if (normSqrd > Settings.maxLinearCorrection * Settings.maxLinearCorrection) {
delta.mulLocal(Settings.maxLinearCorrection / MathUtils.sqrt(normSqrd));
}
if (normSqrd > Settings.linearSlop * Settings.linearSlop) {
done = false;
}
positions[bodies[next].m_islandIndex].c.x += delta.x;
positions[bodies[next].m_islandIndex].c.y += delta.y;
// bodies[next].m_linearVelocity.x += delta.x * step.inv_dt;
// bodies[next].m_linearVelocity.y += delta.y * step.inv_dt;
}
pool.pushVec2(1);
// System.out.println(sumdeltax);
return done;
}
@Override
public void initVelocityConstraints(final SolverData step) {
Velocity[] velocities = step.velocities;
Position[] positions = step.positions;
final Vec2[] d = pool.getVec2Array(bodies.length);
for (int i = 0; i < bodies.length; ++i) {
final int prev = (i == 0) ? bodies.length - 1 : i - 1;
final int next = (i == bodies.length - 1) ? 0 : i + 1;
d[i].set(positions[bodies[next].m_islandIndex].c);
d[i].subLocal(positions[bodies[prev].m_islandIndex].c);
}
if (step.step.warmStarting) {
m_impulse *= step.step.dtRatio;
// float lambda = -2.0f * crossMassSum / dotMassSum;
// System.out.println(crossMassSum + " " +dotMassSum);
// lambda = MathUtils.clamp(lambda, -Settings.maxLinearCorrection,
// Settings.maxLinearCorrection);
// m_impulse = lambda;
for (int i = 0; i < bodies.length; ++i) {
velocities[bodies[i].m_islandIndex].v.x += bodies[i].m_invMass * d[i].y * .5f * m_impulse;
velocities[bodies[i].m_islandIndex].v.y += bodies[i].m_invMass * -d[i].x * .5f * m_impulse;
}
} else {
m_impulse = 0.0f;
}
}
@Override
public boolean solvePositionConstraints(SolverData step) {
return constrainEdges(step.positions);
}
@Override
public void solveVelocityConstraints(final SolverData step) {
float crossMassSum = 0.0f;
float dotMassSum = 0.0f;
Velocity[] velocities = step.velocities;
Position[] positions = step.positions;
final Vec2 d[] = pool.getVec2Array(bodies.length);
for (int i = 0; i < bodies.length; ++i) {
final int prev = (i == 0) ? bodies.length - 1 : i - 1;
final int next = (i == bodies.length - 1) ? 0 : i + 1;
d[i].set(positions[bodies[next].m_islandIndex].c);
d[i].subLocal(positions[bodies[prev].m_islandIndex].c);
dotMassSum += (d[i].lengthSquared()) / bodies[i].getMass();
crossMassSum += Vec2.cross(velocities[bodies[i].m_islandIndex].v, d[i]);
}
float lambda = -2.0f * crossMassSum / dotMassSum;
// System.out.println(crossMassSum + " " +dotMassSum);
// lambda = MathUtils.clamp(lambda, -Settings.maxLinearCorrection,
// Settings.maxLinearCorrection);
m_impulse += lambda;
// System.out.println(m_impulse);
for (int i = 0; i < bodies.length; ++i) {
velocities[bodies[i].m_islandIndex].v.x += bodies[i].m_invMass * d[i].y * .5f * lambda;
velocities[bodies[i].m_islandIndex].v.y += bodies[i].m_invMass * -d[i].x * .5f * lambda;
}
}
/** No-op */
@Override
public void getAnchorA(Vec2 argOut) {}
/** No-op */
@Override
public void getAnchorB(Vec2 argOut) {}
/** No-op */
@Override
public void getReactionForce(float inv_dt, Vec2 argOut) {}
/** No-op */
@Override
public float getReactionTorque(float inv_dt) {
return 0;
}
}