From 0f6fc7d5b12b52a31ffa5d6eb57f9237e6e355c8 Mon Sep 17 00:00:00 2001 From: Stephen Gold Date: Mon, 11 Sep 2017 20:30:22 -0700 Subject: [PATCH] MyControl: add 4 methods --- .../main/java/jme3utilities/MyControl.java | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/heart/src/main/java/jme3utilities/MyControl.java b/heart/src/main/java/jme3utilities/MyControl.java index 0f88477cc..d13e46aac 100644 --- a/heart/src/main/java/jme3utilities/MyControl.java +++ b/heart/src/main/java/jme3utilities/MyControl.java @@ -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; @@ -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(). @@ -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. * @@ -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. *