Skip to content

Commit

Permalink
MyControl: add 4 methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Sep 12, 2017
1 parent 1c0957c commit 0f6fc7d
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions heart/src/main/java/jme3utilities/MyControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
import com.jme3.animation.Animation;
import com.jme3.animation.SkeletonControl;
import com.jme3.app.StatsView;
import com.jme3.bullet.collision.PhysicsCollisionObject;
import com.jme3.bullet.control.AbstractPhysicsControl;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.GhostControl;
import com.jme3.bullet.control.PhysicsControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.control.VehicleControl;
import com.jme3.bullet.objects.PhysicsCharacter;
import com.jme3.bullet.objects.PhysicsGhostObject;
import com.jme3.bullet.objects.PhysicsRigidBody;
import com.jme3.bullet.objects.PhysicsVehicle;
import com.jme3.cinematic.events.MotionEvent;
import com.jme3.effect.ParticleEmitter;
import com.jme3.input.ChaseCamera;
Expand Down Expand Up @@ -67,6 +76,22 @@ private MyControl() {
// *************************************************************************
// new methods exposed

/**
* Check whether a scene-graph control implements applyPhysicsLocal().
*
* @param sgc control to test (may be null, unaffected)
* @return true if it's implemented, otherwise false
*/
public static boolean canApplyPhysicsLocal(Control sgc) {
boolean result = sgc instanceof AbstractPhysicsControl
|| sgc instanceof CharacterControl
|| sgc instanceof GhostControl
|| sgc instanceof RigidBodyControl
|| sgc instanceof VehicleControl;

return result;
}

/**
* Check whether a scene-graph control implements isEnabled() and
* setEnabled().
Expand Down Expand Up @@ -154,6 +179,44 @@ public static int findIndex(Control sgc, Spatial spatial) {
return result;
}

/**
* Test whether the specified SGC applies physics coordinates to its
* spatial's local translation.
*
* @param sgc which scene-graph control (may be null, unaffected)
* @return true if applied to local translation, otherwise false
*/
public static boolean isApplyPhysicsLocal(Control sgc) {
Validate.nonNull(sgc, "control");

boolean result;
if (sgc instanceof AbstractPhysicsControl) {
AbstractPhysicsControl apc = (AbstractPhysicsControl) sgc;
result = apc.isApplyPhysicsLocal();

} else if (sgc instanceof CharacterControl) {
CharacterControl cc = (CharacterControl) sgc;
result = cc.isApplyPhysicsLocal();

} else if (sgc instanceof GhostControl) {
GhostControl gc = (GhostControl) sgc;
result = gc.isApplyPhysicsLocal();

} else if (sgc instanceof RigidBodyControl) {
RigidBodyControl rbc = (RigidBodyControl) sgc;
result = rbc.isApplyPhysicsLocal();

} else if (sgc instanceof VehicleControl) {
VehicleControl vc = (VehicleControl) sgc;
result = vc.isApplyPhysicsLocal();

} else {
throw new IllegalArgumentException();
}

return result;
}

/**
* Test whether a scene-graph control is enabled.
*
Expand Down Expand Up @@ -197,6 +260,65 @@ public static boolean isEnabled(Control sgc) {
return result;
}

/**
* Generate a name for the specified physics object.
*
* @param pco object to name (not null, unaffected)
* @return name (not null, not empty)
*/
public static String objectName(PhysicsCollisionObject pco) {
Validate.nonNull(pco, "physics object");

long id = pco.getObjectId();
String name;
if (pco instanceof PhysicsCharacter) {
name = String.format("chara%d", id);
} else if (pco instanceof PhysicsGhostObject) {
name = String.format("ghost%d", id);
} else if (pco instanceof PhysicsRigidBody) {
name = String.format("rigid%d", id);
} else if (pco instanceof PhysicsVehicle) {
name = String.format("vehic%d", id);
} else {
throw new IllegalArgumentException();
}

return name;
}

/**
* Alter whether the specified SGC applies physics coordinates to its
* spatial's local translation.
*
* @param sgc control to alter (not null)
* @param newSetting true means enable the control, false means disable it
*/
public static void setApplyPhysicsLocal(Control sgc, boolean newSetting) {
if (sgc instanceof AbstractPhysicsControl) {
AbstractPhysicsControl apc = (AbstractPhysicsControl) sgc;
apc.setApplyPhysicsLocal(newSetting);

} else if (sgc instanceof CharacterControl) {
CharacterControl cc = (CharacterControl) sgc;
cc.setApplyPhysicsLocal(newSetting);

} else if (sgc instanceof GhostControl) {
GhostControl gc = (GhostControl) sgc;
gc.setApplyPhysicsLocal(newSetting);

} else if (sgc instanceof RigidBodyControl) {
RigidBodyControl rbc = (RigidBodyControl) sgc;
rbc.setApplyPhysicsLocal(newSetting);

} else if (sgc instanceof VehicleControl) {
VehicleControl vc = (VehicleControl) sgc;
vc.setApplyPhysicsLocal(newSetting);

} else {
throw new IllegalArgumentException();
}
}

/**
* Alter the enabled state of a scene-graph control.
*
Expand Down

0 comments on commit 0f6fc7d

Please sign in to comment.