2013-02-05 04:21:57 +00:00
|
|
|
/*
|
2018-02-19 13:47:47 -08:00
|
|
|
* Copyright (c) 2009-2018 jMonkeyEngine
|
2013-02-05 04:21:57 +00:00
|
|
|
* 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.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;
|
2018-02-19 13:47:47 -08:00
|
|
|
import com.jme3.scene.control.Control;
|
2016-03-20 02:47:16 -04:00
|
|
|
import com.jme3.util.clone.Cloner;
|
|
|
|
import com.jme3.util.clone.JmeCloneable;
|
2013-02-05 04:21:57 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
/**
|
2018-09-16 19:59:45 -07:00
|
|
|
* Manage the life cycle of a physics object linked to a spatial in a scene
|
|
|
|
* graph.
|
|
|
|
* <p>
|
|
|
|
* This class is shared between JBullet and Native Bullet.
|
2013-02-05 04:21:57 +00:00
|
|
|
*
|
|
|
|
* @author normenhansen
|
|
|
|
*/
|
2016-03-20 02:47:16 -04:00
|
|
|
public abstract class AbstractPhysicsControl implements PhysicsControl, JmeCloneable {
|
2013-02-05 04:21:57 +00:00
|
|
|
|
2018-09-16 19:59:45 -07:00
|
|
|
/**
|
|
|
|
* temporary storage during calculations
|
|
|
|
*/
|
2013-02-05 04:38:30 +00:00
|
|
|
private final Quaternion tmp_inverseWorldRotation = new Quaternion();
|
2018-09-16 19:59:45 -07:00
|
|
|
/**
|
|
|
|
* spatial to which this control is added, or null if none
|
|
|
|
*/
|
2013-02-05 04:21:57 +00:00
|
|
|
protected Spatial spatial;
|
2018-09-16 19:59:45 -07:00
|
|
|
/**
|
|
|
|
* true→control is enabled, false→control is disabled
|
|
|
|
*/
|
2013-02-05 04:21:57 +00:00
|
|
|
protected boolean enabled = true;
|
2018-09-16 19:59:45 -07:00
|
|
|
/**
|
|
|
|
* true→body is added to the physics space, false→not added
|
|
|
|
*/
|
2013-02-05 04:21:57 +00:00
|
|
|
protected boolean added = false;
|
2018-09-16 19:59:45 -07:00
|
|
|
/**
|
|
|
|
* space to which the physics object is (or would be) added
|
|
|
|
*/
|
2013-02-05 04:21:57 +00:00
|
|
|
protected PhysicsSpace space = null;
|
2018-09-16 19:59:45 -07:00
|
|
|
/**
|
|
|
|
* true → physics coordinates match local transform, false →
|
|
|
|
* physics coordinates match world transform
|
|
|
|
*/
|
2013-02-05 04:21:57 +00:00
|
|
|
protected boolean applyLocal = false;
|
|
|
|
|
2013-02-09 09:24:04 +00:00
|
|
|
/**
|
2018-09-16 19:59:45 -07:00
|
|
|
* Create spatial-dependent data. Invoked when this control is added to a
|
|
|
|
* spatial.
|
2013-02-09 09:24:04 +00:00
|
|
|
*
|
2018-09-16 19:59:45 -07:00
|
|
|
* @param spat the controlled spatial (not null)
|
2013-02-09 09:24:04 +00:00
|
|
|
*/
|
|
|
|
protected abstract void createSpatialData(Spatial spat);
|
|
|
|
|
|
|
|
/**
|
2018-09-16 19:59:45 -07:00
|
|
|
* Destroy spatial-dependent data. Invoked when this control is removed from
|
|
|
|
* a spatial.
|
2013-02-09 09:24:04 +00:00
|
|
|
*
|
2018-09-16 19:59:45 -07:00
|
|
|
* @param spat the previously controlled spatial (not null)
|
2013-02-09 09:24:04 +00:00
|
|
|
*/
|
|
|
|
protected abstract void removeSpatialData(Spatial spat);
|
|
|
|
|
2013-02-05 04:21:57 +00:00
|
|
|
/**
|
2018-09-16 19:59:45 -07:00
|
|
|
* Translate the physics object to the specified location.
|
2013-02-05 04:21:57 +00:00
|
|
|
*
|
2018-09-16 19:59:45 -07:00
|
|
|
* @param vec desired location (not null, unaffected)
|
2013-02-05 04:21:57 +00:00
|
|
|
*/
|
|
|
|
protected abstract void setPhysicsLocation(Vector3f vec);
|
|
|
|
|
|
|
|
/**
|
2018-09-16 19:59:45 -07:00
|
|
|
* Rotate the physics object to the specified orientation.
|
2013-02-05 04:21:57 +00:00
|
|
|
*
|
2018-09-16 19:59:45 -07:00
|
|
|
* @param quat desired orientation (not null, unaffected)
|
2013-02-05 04:21:57 +00:00
|
|
|
*/
|
|
|
|
protected abstract void setPhysicsRotation(Quaternion quat);
|
|
|
|
|
|
|
|
/**
|
2018-09-16 19:59:45 -07:00
|
|
|
* Add all managed physics objects to the specified space.
|
2013-02-05 04:21:57 +00:00
|
|
|
*
|
2018-09-16 19:59:45 -07:00
|
|
|
* @param space which physics space to add to (not null)
|
2013-02-05 04:21:57 +00:00
|
|
|
*/
|
|
|
|
protected abstract void addPhysics(PhysicsSpace space);
|
|
|
|
|
|
|
|
/**
|
2018-09-16 19:59:45 -07:00
|
|
|
* Remove all managed physics objects from the specified space.
|
2013-02-05 04:21:57 +00:00
|
|
|
*
|
2018-09-16 19:59:45 -07:00
|
|
|
* @param space which physics space to remove from (not null)
|
2013-02-05 04:21:57 +00:00
|
|
|
*/
|
|
|
|
protected abstract void removePhysics(PhysicsSpace space);
|
|
|
|
|
2018-09-16 19:59:45 -07:00
|
|
|
/**
|
|
|
|
* Test whether physics-space coordinates should match the spatial's local
|
|
|
|
* coordinates.
|
|
|
|
*
|
|
|
|
* @return true if matching local coordinates, false if matching world
|
|
|
|
* coordinates
|
|
|
|
*/
|
2013-02-05 04:21:57 +00:00
|
|
|
public boolean isApplyPhysicsLocal() {
|
|
|
|
return applyLocal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-16 19:59:45 -07:00
|
|
|
* Alter whether physics-space coordinates should match the spatial's local
|
|
|
|
* coordinates.
|
2013-02-05 04:21:57 +00:00
|
|
|
*
|
2018-09-16 19:59:45 -07:00
|
|
|
* @param applyPhysicsLocal true→match local coordinates,
|
|
|
|
* false→match world coordinates (default=false)
|
2013-02-05 04:21:57 +00:00
|
|
|
*/
|
|
|
|
public void setApplyPhysicsLocal(boolean applyPhysicsLocal) {
|
|
|
|
applyLocal = applyPhysicsLocal;
|
|
|
|
}
|
|
|
|
|
2018-09-16 19:59:45 -07:00
|
|
|
/**
|
|
|
|
* Access whichever spatial translation corresponds to the physics location.
|
|
|
|
*
|
|
|
|
* @return the pre-existing location vector (in physics-space coordinates,
|
|
|
|
* not null)
|
|
|
|
*/
|
2013-02-05 04:38:30 +00:00
|
|
|
protected Vector3f getSpatialTranslation() {
|
2013-02-05 04:21:57 +00:00
|
|
|
if (applyLocal) {
|
|
|
|
return spatial.getLocalTranslation();
|
|
|
|
}
|
|
|
|
return spatial.getWorldTranslation();
|
|
|
|
}
|
|
|
|
|
2018-09-16 19:59:45 -07:00
|
|
|
/**
|
|
|
|
* Access whichever spatial rotation corresponds to the physics rotation.
|
|
|
|
*
|
|
|
|
* @return the pre-existing quaternion (in physics-space coordinates, not
|
|
|
|
* null)
|
|
|
|
*/
|
2013-02-05 04:38:30 +00:00
|
|
|
protected Quaternion getSpatialRotation() {
|
2013-02-05 04:21:57 +00:00
|
|
|
if (applyLocal) {
|
|
|
|
return spatial.getLocalRotation();
|
|
|
|
}
|
|
|
|
return spatial.getWorldRotation();
|
|
|
|
}
|
|
|
|
|
2013-02-05 04:38:30 +00:00
|
|
|
/**
|
2018-09-16 19:59:45 -07:00
|
|
|
* Apply a physics transform to the spatial.
|
2013-02-05 04:38:30 +00:00
|
|
|
*
|
2018-09-16 19:59:45 -07:00
|
|
|
* @param worldLocation location vector (in physics-space coordinates, not
|
|
|
|
* null, unaffected)
|
|
|
|
* @param worldRotation orientation (in physics-space coordinates, not null,
|
|
|
|
* unaffected)
|
2013-02-05 04:38:30 +00:00
|
|
|
*/
|
2013-02-05 17:30:04 +00:00
|
|
|
protected void applyPhysicsTransform(Vector3f worldLocation, Quaternion worldRotation) {
|
2013-02-05 04:38:30 +00:00
|
|
|
if (enabled && spatial != null) {
|
|
|
|
Vector3f localLocation = spatial.getLocalTranslation();
|
|
|
|
Quaternion localRotationQuat = spatial.getLocalRotation();
|
|
|
|
if (!applyLocal && spatial.getParent() != null) {
|
|
|
|
localLocation.set(worldLocation).subtractLocal(spatial.getParent().getWorldTranslation());
|
|
|
|
localLocation.divideLocal(spatial.getParent().getWorldScale());
|
|
|
|
tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);
|
2013-02-05 17:30:04 +00:00
|
|
|
localRotationQuat.set(worldRotation);
|
2013-02-05 04:38:30 +00:00
|
|
|
tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().mult(localRotationQuat, localRotationQuat);
|
|
|
|
|
|
|
|
spatial.setLocalTranslation(localLocation);
|
|
|
|
spatial.setLocalRotation(localRotationQuat);
|
|
|
|
} else {
|
|
|
|
spatial.setLocalTranslation(worldLocation);
|
|
|
|
spatial.setLocalRotation(worldRotation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-02-19 13:47:47 -08:00
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
@Override
|
|
|
|
public Control cloneForSpatial(Spatial spatial) {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
2018-09-16 19:59:45 -07:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2018-09-23 21:28:51 -07:00
|
|
|
* @param cloner the cloner that's cloning this control (not null)
|
2018-09-16 19:59:45 -07:00
|
|
|
* @param original the control from which this control was shallow-cloned
|
|
|
|
* (unused)
|
|
|
|
*/
|
2018-02-19 13:47:47 -08:00
|
|
|
@Override
|
2016-03-20 02:47:16 -04:00
|
|
|
public void cloneFields( Cloner cloner, Object original ) {
|
|
|
|
this.spatial = cloner.clone(spatial);
|
|
|
|
createSpatialData(this.spatial);
|
|
|
|
}
|
|
|
|
|
2018-09-16 19:59:45 -07:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*/
|
2013-02-05 04:21:57 +00:00
|
|
|
public void setSpatial(Spatial spatial) {
|
2013-02-09 09:24:04 +00:00
|
|
|
if (this.spatial != null && this.spatial != spatial) {
|
|
|
|
removeSpatialData(this.spatial);
|
2013-04-12 13:06:45 +00:00
|
|
|
} else if (this.spatial == spatial) {
|
2013-02-09 09:24:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-02-05 04:21:57 +00:00
|
|
|
this.spatial = spatial;
|
|
|
|
if (spatial == null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-02-09 09:24:04 +00:00
|
|
|
createSpatialData(this.spatial);
|
2013-02-05 04:21:57 +00:00
|
|
|
setPhysicsLocation(getSpatialTranslation());
|
|
|
|
setPhysicsRotation(getSpatialRotation());
|
|
|
|
}
|
|
|
|
|
2018-09-16 19:59:45 -07:00
|
|
|
/**
|
|
|
|
* Enable or disable this control.
|
|
|
|
* <p>
|
|
|
|
* When the control is disabled, the physics object is removed from physics
|
|
|
|
* space. When the control is enabled again, the physics object is moved to
|
|
|
|
* the spatial's location and then added to the physics space.
|
|
|
|
*
|
|
|
|
* @param enabled true→enable the control, false→disable it
|
|
|
|
*/
|
2013-02-05 04:21:57 +00:00
|
|
|
public void setEnabled(boolean enabled) {
|
|
|
|
this.enabled = enabled;
|
|
|
|
if (space != null) {
|
|
|
|
if (enabled && !added) {
|
|
|
|
if (spatial != null) {
|
|
|
|
setPhysicsLocation(getSpatialTranslation());
|
|
|
|
setPhysicsRotation(getSpatialRotation());
|
|
|
|
}
|
|
|
|
addPhysics(space);
|
|
|
|
added = true;
|
|
|
|
} else if (!enabled && added) {
|
|
|
|
removePhysics(space);
|
|
|
|
added = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-16 19:59:45 -07:00
|
|
|
/**
|
|
|
|
* Test whether this control is enabled.
|
|
|
|
*
|
|
|
|
* @return true if enabled, otherwise false
|
|
|
|
*/
|
2013-02-05 04:21:57 +00:00
|
|
|
public boolean isEnabled() {
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void update(float tpf) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public void render(RenderManager rm, ViewPort vp) {
|
|
|
|
}
|
|
|
|
|
2018-09-06 10:30:51 -07:00
|
|
|
/**
|
|
|
|
* 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
|
2018-09-16 19:59:45 -07:00
|
|
|
* is removed from any other space it's in.
|
2018-09-06 10:30:51 -07:00
|
|
|
*
|
|
|
|
* @param newSpace where to add, or null to simply remove
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void setPhysicsSpace(PhysicsSpace newSpace) {
|
|
|
|
if (space == newSpace) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (added) {
|
|
|
|
removePhysics(space);
|
|
|
|
added = false;
|
|
|
|
}
|
|
|
|
if (newSpace != null && isEnabled()) {
|
|
|
|
addPhysics(newSpace);
|
2013-02-05 04:21:57 +00:00
|
|
|
added = true;
|
|
|
|
}
|
2018-09-06 10:30:51 -07:00
|
|
|
/*
|
|
|
|
* If this control isn't enabled, its physics object will be
|
|
|
|
* added to the new space when the control becomes enabled.
|
|
|
|
*/
|
|
|
|
space = newSpace;
|
2013-02-05 04:21:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-16 19:59:45 -07:00
|
|
|
/**
|
|
|
|
* Access the physics space to which the object is (or would be) added.
|
|
|
|
*
|
|
|
|
* @return the pre-existing space, or null for none
|
|
|
|
*/
|
2013-02-05 04:21:57 +00:00
|
|
|
public PhysicsSpace getPhysicsSpace() {
|
|
|
|
return space;
|
|
|
|
}
|
|
|
|
|
2018-09-16 19:59:45 -07:00
|
|
|
/**
|
|
|
|
* Serialize this object, for example when saving to a J3O file.
|
|
|
|
*
|
|
|
|
* @param ex exporter (not null)
|
|
|
|
* @throws IOException from exporter
|
|
|
|
*/
|
2013-02-05 04:21:57 +00:00
|
|
|
@Override
|
|
|
|
public void write(JmeExporter ex) throws IOException {
|
|
|
|
OutputCapsule oc = ex.getCapsule(this);
|
|
|
|
oc.write(enabled, "enabled", true);
|
|
|
|
oc.write(applyLocal, "applyLocalPhysics", false);
|
|
|
|
oc.write(spatial, "spatial", null);
|
|
|
|
}
|
|
|
|
|
2018-09-16 19:59:45 -07:00
|
|
|
/**
|
|
|
|
* De-serialize this control from the specified importer, for example when
|
|
|
|
* loading from a J3O file.
|
|
|
|
*
|
|
|
|
* @param im importer (not null)
|
|
|
|
* @throws IOException from importer
|
|
|
|
*/
|
2013-02-05 04:21:57 +00:00
|
|
|
@Override
|
|
|
|
public void read(JmeImporter im) throws IOException {
|
|
|
|
InputCapsule ic = im.getCapsule(this);
|
|
|
|
enabled = ic.readBoolean("enabled", true);
|
|
|
|
spatial = (Spatial) ic.readSavable("spatial", null);
|
|
|
|
applyLocal = ic.readBoolean("applyLocalPhysics", false);
|
|
|
|
}
|
|
|
|
}
|