-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathGhostControl.java
More file actions
351 lines (329 loc) · 11.3 KB
/
GhostControl.java
File metadata and controls
351 lines (329 loc) · 11.3 KB
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
/*
* Copyright (c) 2009-2020 jMonkeyEngine
* 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 'jMonkeyEngine' 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 OWNER 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 com.jme3.bullet.control;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.objects.PhysicsGhostObject;
import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import com.jme3.util.clone.Cloner;
import com.jme3.util.clone.JmeCloneable;
import java.io.IOException;
/**
* A physics control to link a PhysicsGhostObject to a spatial.
* <p>
* The ghost object moves with the spatial it is attached to and can be used to
* detect overlaps with other physics objects (e.g. aggro radius).
* <p>
* This class is shared between JBullet and Native Bullet.
*
* @author normenhansen
*/
@Deprecated
public class GhostControl extends PhysicsGhostObject implements PhysicsControl, JmeCloneable {
/**
* spatial to which this control is added, or null if none
*/
protected Spatial spatial;
/**
* true→control is enabled, false→control is disabled
*/
protected boolean enabled = true;
/**
* true→body is added to the physics space, false→not added
*/
protected boolean added = false;
/**
* space to which the ghost object is (or would be) added
*/
protected PhysicsSpace space = null;
/**
* true → physics coordinates match local transform, false →
* physics coordinates match world transform
*/
protected boolean applyLocal = false;
/**
* No-argument constructor needed by SavableClassUtil. Do not invoke
* directly!
*/
protected GhostControl() {
}
/**
* Instantiate an enabled control with the specified shape.
*
* @param shape (not null)
*/
public GhostControl(CollisionShape shape) {
super(shape);
}
/**
* Test whether physics-space coordinates should match the spatial's local
* coordinates.
*
* @return true if matching local coordinates, false if matching world
* coordinates
*/
public boolean isApplyPhysicsLocal() {
return applyLocal;
}
/**
* Alter whether physics-space coordinates should match the spatial's local
* coordinates.
*
* @param applyPhysicsLocal true→match local coordinates,
* false→match world coordinates (default=false)
*/
public void setApplyPhysicsLocal(boolean applyPhysicsLocal) {
applyLocal = applyPhysicsLocal;
}
/**
* Access whichever spatial translation corresponds to the physics location.
*
* @return the pre-existing vector (not null)
*/
private Vector3f getSpatialTranslation() {
if (applyLocal) {
return spatial.getLocalTranslation();
}
return spatial.getWorldTranslation();
}
/**
* Access whichever spatial rotation corresponds to the physics rotation.
*
* @return the pre-existing quaternion (not null)
*/
private Quaternion getSpatialRotation() {
if (applyLocal) {
return spatial.getLocalRotation();
}
return spatial.getWorldRotation();
}
/**
* Clone this control for a different spatial. No longer used as of JME 3.1.
*
* @param spatial the spatial for the clone to control (or null)
* @return a new control (not null)
*/
@Deprecated
@Override
public Control cloneForSpatial(Spatial spatial) {
throw new UnsupportedOperationException();
}
/**
* Create a shallow clone for the JME cloner.
*
* @return a new control (not null)
*/
@Override
public Object jmeClone() {
GhostControl control = new GhostControl(collisionShape);
control.setCcdMotionThreshold(getCcdMotionThreshold());
control.setCcdSweptSphereRadius(getCcdSweptSphereRadius());
control.setCollideWithGroups(getCollideWithGroups());
control.setCollisionGroup(getCollisionGroup());
control.setPhysicsLocation(getPhysicsLocation());
control.setPhysicsRotation(getPhysicsRotationMatrix());
control.setApplyPhysicsLocal(isApplyPhysicsLocal());
control.spatial = this.spatial;
return control;
}
/**
* Callback from {@link com.jme3.util.clone.Cloner} to convert this
* shallow-cloned control into a deep-cloned one, using the specified cloner
* and original to resolve copied fields.
*
* @param cloner the cloner that's cloning this control (not null)
* @param original the control from which this control was shallow-cloned
* (unused)
*/
@Override
public void cloneFields( Cloner cloner, Object original ) {
this.spatial = cloner.clone(spatial);
}
/**
* Alter which spatial is controlled. Invoked when the control is added to
* or removed from a spatial. Should be invoked only by a subclass or from
* Spatial. Do not invoke directly from user code.
*
* @param spatial the spatial to control (or null)
*/
@Override
public void setSpatial(Spatial spatial) {
this.spatial = spatial;
setUserObject(spatial);
if (spatial == null) {
return;
}
setPhysicsLocation(getSpatialTranslation());
setPhysicsRotation(getSpatialRotation());
}
/**
* @return returns the spatial the control is added to, or null if the control is not attached to a spatial yet.
*/
public Spatial getSpatial(){
return this.spatial;
}
/**
* Enable or disable this control.
* <p>
* When the control is disabled, the ghost object is removed from physics
* space. When the control is enabled again, the object is moved to the
* current location of the spatial and then added to the physics space.
*
* @param enabled true→enable the control, false→disable it
*/
@Override
public void setEnabled(boolean enabled) {
this.enabled = enabled;
if (space != null) {
if (enabled && !added) {
if (spatial != null) {
setPhysicsLocation(getSpatialTranslation());
setPhysicsRotation(getSpatialRotation());
}
space.addCollisionObject(this);
added = true;
} else if (!enabled && added) {
space.removeCollisionObject(this);
added = false;
}
}
}
/**
* Test whether this control is enabled.
*
* @return true if enabled, otherwise false
*/
@Override
public boolean isEnabled() {
return enabled;
}
/**
* Update this control. Invoked once per frame during the logical-state
* update, provided the control is added to a scene. Do not invoke directly
* from user code.
*
* @param tpf the time interval between frames (in seconds, ≥0)
*/
@Override
public void update(float tpf) {
if (!enabled) {
return;
}
setPhysicsLocation(getSpatialTranslation());
setPhysicsRotation(getSpatialRotation());
}
/**
* Render this control. Invoked once per view port per frame, provided the
* control is added to a scene. Should be invoked only by a subclass or by
* the RenderManager.
*
* @param rm the render manager (not null)
* @param vp the view port to render (not null)
*/
@Override
public void render(RenderManager rm, ViewPort vp) {
}
/**
* If enabled, add this control's physics object to the specified physics
* space. If not enabled, alter where the object would be added. The object
* is removed from any other space it's currently in.
*
* @param newSpace where to add, or null to simply remove
*/
@Override
public void setPhysicsSpace(PhysicsSpace newSpace) {
if (space == newSpace) {
return;
}
if (added) {
space.removeCollisionObject(this);
added = false;
}
if (newSpace != null && isEnabled()) {
newSpace.addCollisionObject(this);
added = true;
}
/*
* If this control isn't enabled, its physics object will be
* added to the new space when the control becomes enabled.
*/
space = newSpace;
}
/**
* Access the physics space to which the ghost object is (or would be)
* added.
*
* @return the pre-existing space, or null for none
*/
@Override
public PhysicsSpace getPhysicsSpace() {
return space;
}
/**
* Serialize this control, for example when saving to a J3O file.
*
* @param ex exporter (not null)
* @throws IOException from exporter
*/
@Override
public void write(JmeExporter ex) throws IOException {
super.write(ex);
OutputCapsule oc = ex.getCapsule(this);
oc.write(enabled, "enabled", true);
oc.write(applyLocal, "applyLocalPhysics", false);
oc.write(spatial, "spatial", null);
}
/**
* De-serialize this control, for example when loading from a J3O file.
*
* @param im importer (not null)
* @throws IOException from importer
*/
@Override
public void read(JmeImporter im) throws IOException {
super.read(im);
InputCapsule ic = im.getCapsule(this);
enabled = ic.readBoolean("enabled", true);
spatial = (Spatial) ic.readSavable("spatial", null);
applyLocal = ic.readBoolean("applyLocalPhysics", false);
setUserObject(spatial);
}
}