-
Notifications
You must be signed in to change notification settings - Fork 22
/
TestSoftBodyControl.java
415 lines (371 loc) · 14.7 KB
/
TestSoftBodyControl.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
Copyright (c) 2019, Stephen Gold
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.
* Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 jme3utilities.minie.test;
import com.jme3.app.Application;
import com.jme3.bullet.PhysicsSoftSpace;
import com.jme3.bullet.SoftPhysicsAppState;
import com.jme3.bullet.collision.PhysicsCollisionObject;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.collision.shapes.infos.DebugMeshNormals;
import com.jme3.bullet.control.SoftBodyControl;
import com.jme3.bullet.debug.BulletDebugAppState;
import com.jme3.bullet.debug.DebugInitListener;
import com.jme3.bullet.joints.PhysicsJoint;
import com.jme3.bullet.objects.PhysicsRigidBody;
import com.jme3.bullet.objects.infos.Sbcp;
import com.jme3.bullet.objects.infos.SoftBodyConfig;
import com.jme3.export.Savable;
import com.jme3.input.CameraInput;
import com.jme3.input.KeyInput;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.shadow.DirectionalLightShadowRenderer;
import com.jme3.system.AppSettings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jme3utilities.Misc;
import jme3utilities.MyAsset;
import jme3utilities.MyCamera;
import jme3utilities.debug.AxesVisualizer;
import jme3utilities.minie.DumpFlags;
import jme3utilities.minie.PhysicsDumper;
import jme3utilities.ui.ActionApplication;
import jme3utilities.ui.CameraOrbitAppState;
import jme3utilities.ui.InputMode;
/**
* Test/demonstrate the SoftBodyControl class.
*
* @author Stephen Gold [email protected]
*/
public class TestSoftBodyControl
extends ActionApplication
implements BulletDebugAppState.DebugAppStateFilter, DebugInitListener {
// *************************************************************************
// constants and loggers
/**
* message logger for this class
*/
final public static Logger logger
= Logger.getLogger(TestSoftBodyControl.class.getName());
/**
* application name (for the title bar of the app's window)
*/
final private static String applicationName
= TestSoftBodyControl.class.getSimpleName();
// *************************************************************************
// fields
/**
* physics objects that are not to be visualized
*/
final private List<Savable> hiddenObjects = new ArrayList<>(9);
/**
* single-sided green material to visualize the platform
*/
private Material greenMaterial;
/**
* dump debugging information to System.out
*/
final private PhysicsDumper dumper = new PhysicsDumper();
/**
* space for physics simulation
*/
private PhysicsSoftSpace physicsSpace;
// *************************************************************************
// new methods exposed
/**
* Main entry point for the TestSoftBodyControl application.
*
* @param ignored array of command-line arguments (not null)
*/
public static void main(String[] ignored) {
/*
* Mute the chatty loggers in certain packages.
*/
Misc.setLoggingLevels(Level.WARNING);
Application application = new TestSoftBodyControl();
/*
* Customize the window's title bar.
*/
AppSettings settings = new AppSettings(true);
settings.setTitle(applicationName);
settings.setGammaCorrection(true);
settings.setSamples(4); // anti-aliasing
settings.setVSync(false);
application.setSettings(settings);
application.start();
}
// *************************************************************************
// ActionApplication methods
/**
* Initialize this application.
*/
@Override
public void actionInitializeApplication() {
configureCamera();
configureDumper();
configureMaterials();
configurePhysics();
ColorRGBA sky = new ColorRGBA(0.1f, 0.2f, 0.4f, 1f);
viewPort.setBackgroundColor(sky);
addLighting(rootNode, false);
addBox(0f);
addRubberDuck();
}
/**
* Add application-specific hotkey bindings and override existing ones.
*/
@Override
public void moreDefaultBindings() {
InputMode dim = getDefaultInputMode();
dim.bind("dump physicsSpace", KeyInput.KEY_O);
dim.bind("dump scenes", KeyInput.KEY_P);
dim.bind("signal " + CameraInput.FLYCAM_LOWER, KeyInput.KEY_DOWN);
dim.bind("signal " + CameraInput.FLYCAM_RISE, KeyInput.KEY_UP);
dim.bind("signal orbitLeft", KeyInput.KEY_LEFT);
dim.bind("signal orbitRight", KeyInput.KEY_RIGHT);
dim.bind("test rubberDuck", KeyInput.KEY_F1);
dim.bind("toggle pause", KeyInput.KEY_PERIOD);
}
/**
* Process an action that wasn't handled by the active input mode.
*
* @param actionString textual description of the action (not null)
* @param ongoing true if the action is ongoing, otherwise false
* @param tpf time interval between frames (in seconds, ≥0)
*/
@Override
public void onAction(String actionString, boolean ongoing, float tpf) {
if (ongoing) {
switch (actionString) {
case "dump physicsSpace":
dumper.dump(physicsSpace);
return;
case "dump scenes":
dumper.dump(renderManager);
return;
case "test rubberDuck":
cleanupAfterTest();
addBox(0f);
addRubberDuck();
return;
case "toggle pause":
togglePause();
return;
}
}
super.onAction(actionString, ongoing, tpf);
}
// *************************************************************************
// BulletDebugAppState.DebugAppStateFilter methods
/**
* Test whether the specified physics object should be rendered in the debug
* scene.
*
* @param physicsObject the joint or collision object to test (unaffected)
* @return return true if the object should be rendered, false if not
*/
@Override
public boolean displayObject(Savable physicsObject) {
return !hiddenObjects.contains(physicsObject);
}
// *************************************************************************
// DebugInitListener methods
/**
* Callback from BulletDebugAppState, invoked just before the debug scene is
* added to the debug viewports.
*
* @param physicsDebugRootNode the root node of the debug scene (not null)
*/
@Override
public void bulletDebugInit(Node physicsDebugRootNode) {
addLighting(physicsDebugRootNode, true);
}
// *************************************************************************
// private methods
/**
* Add a visualizer for the axes of the world coordinate system.
*/
private void addAxes() {
float axisLength = 0.4f;
AxesVisualizer axes = new AxesVisualizer(assetManager, axisLength);
axes.setLineWidth(0f);
rootNode.addControl(axes);
axes.setEnabled(true);
}
/**
* Add a large static box to the scene, to serve as a platform.
*
* @param topY the Y coordinate of the top surface (in physics-space
* coordinates)
*/
private void addBox(float topY) {
float halfExtent = 4f;
BoxCollisionShape shape = new BoxCollisionShape(halfExtent);
float boxMass = PhysicsRigidBody.massForStatic;
PhysicsRigidBody boxBody = new PhysicsRigidBody(shape, boxMass);
boxBody.setDebugMaterial(greenMaterial);
boxBody.setDebugMeshNormals(DebugMeshNormals.Facet);
boxBody.setPhysicsLocation(new Vector3f(0f, topY - halfExtent, 0f));
physicsSpace.add(boxBody);
}
/**
* Add a rubber duck to the scene.
*/
private void addRubberDuck() {
Spatial cgModel = assetManager.loadModel("Models/Duck/Duck.gltf");
rootNode.attachChild(cgModel);
boolean applyLocal = false;
boolean updateNormals = true;
boolean mergeVertices = true;
SoftBodyControl duckSbc = new SoftBodyControl(applyLocal, updateNormals,
mergeVertices);
cgModel.addControl(duckSbc);
float totalMass = 1f;
duckSbc.setMassByArea(totalMass);
SoftBodyConfig config = duckSbc.getSoftConfig();
config.set(Sbcp.KineticHardness, 1f);
config.set(Sbcp.PoseMatching, 0.03f);
//config.setPositionIterations(15);
boolean setVolumePose = false;
boolean setFramePose = true;
duckSbc.setPose(setVolumePose, setFramePose);
duckSbc.applyRotation(new Quaternion().fromAngles(0.4f, 0f, 1f));
duckSbc.applyTranslation(new Vector3f(0f, 1.2f, 0f));
physicsSpace.add(duckSbc);
hiddenObjects.add(duckSbc);
}
/**
* Add lighting to the specified scene.
*
* @param rootSpatial which scene (not null)
* @param shadowFlag if true, add a shadow renderer to the default viewport
*/
private void addLighting(Spatial rootSpatial, boolean shadowFlag) {
ColorRGBA ambientColor = new ColorRGBA(0.1f, 0.1f, 0.1f, 1f);
AmbientLight ambient = new AmbientLight(ambientColor);
rootSpatial.addLight(ambient);
ColorRGBA directColor = new ColorRGBA(0.7f, 0.7f, 0.7f, 1f);
Vector3f direction = new Vector3f(1f, -2f, -1f).normalizeLocal();
DirectionalLight sun = new DirectionalLight(direction, directColor);
rootSpatial.addLight(sun);
rootSpatial.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
if (shadowFlag) {
DirectionalLightShadowRenderer dlsr
= new DirectionalLightShadowRenderer(assetManager, 2048, 3);
dlsr.setLight(sun);
dlsr.setShadowIntensity(0.6f);
viewPort.addProcessor(dlsr);
}
}
/**
* Clean up after a test.
*/
private void cleanupAfterTest() {
/*
* Remove any scenery. Debug meshes are under a different root node.
*/
rootNode.detachAllChildren();
/*
* Remove physics objects, which also removes their debug meshes.
*/
Collection<PhysicsJoint> joints = physicsSpace.getJointList();
for (PhysicsJoint joint : joints) {
physicsSpace.remove(joint);
}
Collection<PhysicsCollisionObject> pcos = physicsSpace.getPcoList();
for (PhysicsCollisionObject pco : pcos) {
physicsSpace.remove(pco);
}
/*
* Clear the hidden-object list.
*/
hiddenObjects.clear();
}
/**
* Configure the camera during startup.
*/
private void configureCamera() {
float yDegrees = MyCamera.yDegrees(cam);
float aspectRatio = MyCamera.viewAspectRatio(cam);
float near = 0.02f;
float far = 20f;
cam.setFrustumPerspective(yDegrees, aspectRatio, near, far);
flyCam.setDragToRotate(true);
flyCam.setMoveSpeed(2f);
cam.setLocation(new Vector3f(0f, 2.6f, 4.6f));
cam.setRotation(new Quaternion(-0.014f, 0.9642f, -0.26f, -0.05f));
CameraOrbitAppState orbitState
= new CameraOrbitAppState(cam, "orbitLeft", "orbitRight");
stateManager.attach(orbitState);
}
/**
* Configure the PhysicsDumper during startup.
*/
private void configureDumper() {
dumper.setEnabled(DumpFlags.MatParams, true);
dumper.setEnabled(DumpFlags.ShadowModes, true);
dumper.setEnabled(DumpFlags.Transforms, true);
}
/**
* Configure materials during startup.
*/
private void configureMaterials() {
ColorRGBA green = new ColorRGBA(0f, 0.12f, 0f, 1f);
greenMaterial = MyAsset.createShadedMaterial(assetManager, green);
greenMaterial.setName("green");
}
/**
* Configure physics during startup.
*/
private void configurePhysics() {
CollisionShape.setDefaultMargin(0.005f);
SoftPhysicsAppState bulletAppState = new SoftPhysicsAppState();
bulletAppState.setDebugEnabled(true);
bulletAppState.setDebugFilter(this);
bulletAppState.setDebugInitListener(this);
stateManager.attach(bulletAppState);
physicsSpace = bulletAppState.getPhysicsSoftSpace();
physicsSpace.setGravity(new Vector3f(0f, -1f, 0f));
}
/**
* Toggle the animation and physics simulation: paused/running.
*/
private void togglePause() {
float newSpeed = (speed > 1e-12f) ? 1e-12f : 1f;
setSpeed(newSpeed);
}
}