-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathTestSoftBody.java
275 lines (246 loc) · 9.46 KB
/
TestSoftBody.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
/*
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.bullet.PhysicsSoftSpace;
import com.jme3.bullet.SoftPhysicsAppState;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.objects.PhysicsRigidBody;
import com.jme3.bullet.objects.PhysicsSoftBody;
import com.jme3.bullet.objects.infos.Sbcp;
import com.jme3.bullet.util.NativeSoftBodyUtil;
import com.jme3.input.KeyInput;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;
import java.util.logging.Level;
import java.util.logging.Logger;
import jme3utilities.Misc;
import jme3utilities.MyAsset;
import jme3utilities.minie.DumpFlags;
import jme3utilities.minie.PhysicsDumper;
import jme3utilities.minie.test.mesh.Icosphere;
import jme3utilities.ui.ActionApplication;
import jme3utilities.ui.CameraOrbitAppState;
import jme3utilities.ui.InputMode;
/**
* Test soft-body physics.
*
* @author Stephen Gold sgold@sonic.net
*/
public class TestSoftBody extends ActionApplication {
// *************************************************************************
// constants and loggers
/**
* magnitude of the gravitational acceleration (≥0)
*/
final private float gravity = 1f;
/**
* mass of the soft body (>0)
*/
final private float mass = 1f;
/**
* pose-matching coefficient: how strongly the soft body will match its
* default pose (≥0, ≤1)
*/
final private float poseMatchingCoeff = 0.02f;
/**
* radius of the soft body (in world units, >0)
*/
final private float radius = 0.5f;
/**
* message logger for this class
*/
final public static Logger logger
= Logger.getLogger(TestSoftBody.class.getName());
/**
* application name (for the title bar of the app's window)
*/
final private static String applicationName
= TestSoftBody.class.getSimpleName();
// *************************************************************************
// fields
/**
* space for physics simulation
*/
private PhysicsSoftSpace physicsSpace;
// *************************************************************************
// new methods exposed
/**
* Main entry point for the TestSoftBody 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);
TestSoftBody application = new TestSoftBody();
/*
* Customize the window's title bar.
*/
AppSettings settings = new AppSettings(true);
settings.setTitle(applicationName);
settings.setGammaCorrection(true);
settings.setSamples(4); // anti-aliasing
settings.setVSync(true);
application.setSettings(settings);
application.start();
}
// *************************************************************************
// ActionApplication methods
/**
* Initialize this application.
*/
@Override
public void actionInitializeApplication() {
configureCamera();
configurePhysics();
addBox();
addFatBall();
}
/**
* 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 orbitLeft", KeyInput.KEY_LEFT);
dim.bind("signal orbitRight", KeyInput.KEY_RIGHT);
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":
dumpPhysicsSpace();
return;
case "dump scenes":
dumpScenes();
return;
case "toggle pause":
togglePause();
return;
}
}
super.onAction(actionString, ongoing, tpf);
}
// *************************************************************************
// private methods
/**
* Add a large static box to the scene, to serve as a platform.
*/
private void addBox() {
float halfExtent = 50f; // mesh units
Mesh mesh = new Box(halfExtent, halfExtent, halfExtent);
Geometry boxGeometry = new Geometry("box", mesh);
rootNode.attachChild(boxGeometry);
boxGeometry.move(0f, -halfExtent, 0f);
Material material = MyAsset.createDebugMaterial(assetManager);
boxGeometry.setMaterial(material);
BoxCollisionShape shape = new BoxCollisionShape(halfExtent);
float boxMass = PhysicsRigidBody.massForStatic;
RigidBodyControl boxBody = new RigidBodyControl(shape, boxMass);
boxGeometry.addControl(boxBody);
boxBody.setApplyScale(true);
boxBody.setPhysicsSpace(physicsSpace);
}
/**
* Add a squishy ball to the scene.
*/
private void addFatBall() {
int numSteps = 3;
Mesh mesh = new Icosphere(numSteps, radius);
PhysicsSoftBody softBody = new PhysicsSoftBody();
NativeSoftBodyUtil.appendFromTriMesh(mesh, softBody);
softBody.setMass(mass);
PhysicsSoftBody.Config config = softBody.getSoftConfig();
config.set(Sbcp.PoseMatching, poseMatchingCoeff);
boolean setVolumePose = false;
boolean setFramePose = true;
softBody.setPose(setVolumePose, setFramePose);
softBody.setPhysicsLocation(new Vector3f(0f, 3f, 0f));
physicsSpace.add(softBody);
softBody.setGravity(new Vector3f(0f, -gravity, 0f));
}
/**
* Configure the camera during startup.
*/
private void configureCamera() {
flyCam.setDragToRotate(true);
flyCam.setMoveSpeed(4f);
cam.setLocation(new Vector3f(0f, 1.2f, 5f));
CameraOrbitAppState orbitState
= new CameraOrbitAppState(cam, "orbitLeft", "orbitRight");
stateManager.attach(orbitState);
}
/**
* Configure physics during startup.
*/
private void configurePhysics() {
SoftPhysicsAppState bulletAppState = new SoftPhysicsAppState();
bulletAppState.setDebugEnabled(true);
stateManager.attach(bulletAppState);
physicsSpace = bulletAppState.getPhysicsSoftSpace();
}
/**
* Process a "dump physicsSpace" action.
*/
private void dumpPhysicsSpace() {
PhysicsDumper dumper = new PhysicsDumper();
dumper.setEnabled(DumpFlags.JointsInBodies, true);
dumper.setEnabled(DumpFlags.JointsInSpaces, true);
dumper.dump(physicsSpace);
}
/**
* Process a "dump scenes" action.
*/
private void dumpScenes() {
PhysicsDumper dumper = new PhysicsDumper();
dumper.setEnabled(DumpFlags.Transforms, true);
dumper.dump(renderManager);
}
/**
* Toggle the animation and physics simulation: paused/running.
*/
private void togglePause() {
float newSpeed = (speed > 1e-12f) ? 1e-12f : 1f;
setSpeed(newSpeed);
}
}