From 9292ac98980122dc414742c1bfd5ee42a002a1ab Mon Sep 17 00:00:00 2001 From: "nor..67" Date: Mon, 28 May 2012 16:52:56 +0000 Subject: [PATCH] - add Tests for physics raytest and sweeptest (thanks to @wezrule) git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9435 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../jme3test/bullet/TestPhysicsRayCast.java | 60 ++++++++++++++++ .../test/jme3test/bullet/TestSweepTest.java | 71 +++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 engine/src/test/jme3test/bullet/TestPhysicsRayCast.java create mode 100644 engine/src/test/jme3test/bullet/TestSweepTest.java diff --git a/engine/src/test/jme3test/bullet/TestPhysicsRayCast.java b/engine/src/test/jme3test/bullet/TestPhysicsRayCast.java new file mode 100644 index 000000000..c51afd3cc --- /dev/null +++ b/engine/src/test/jme3test/bullet/TestPhysicsRayCast.java @@ -0,0 +1,60 @@ +package jme3test.bullet; + +import com.jme3.app.SimpleApplication; +import com.jme3.bullet.BulletAppState; +import com.jme3.bullet.collision.PhysicsCollisionObject; +import com.jme3.bullet.collision.PhysicsRayTestResult; +import com.jme3.bullet.collision.shapes.CollisionShape; +import com.jme3.bullet.control.RigidBodyControl; +import com.jme3.bullet.util.CollisionShapeFactory; +import com.jme3.font.BitmapText; +import com.jme3.scene.Node; +import com.jme3.scene.Spatial; +import java.util.List; +/** + * + * @author @wezrule + */ +public class TestPhysicsRayCast extends SimpleApplication { + + private BulletAppState bulletAppState = new BulletAppState(); + + public static void main(String[] args) { + new TestPhysicsRayCast().start(); + } + + @Override + public void simpleInitApp() { + stateManager.attach(bulletAppState); + initCrossHair(); + + Spatial s = assetManager.loadModel("Models/Elephant/Elephant.mesh.xml"); + s.setLocalScale(0.1f); + + CollisionShape collisionShape = CollisionShapeFactory.createMeshShape(s); + Node n = new Node("elephant"); + n.addControl(new RigidBodyControl(collisionShape, 1)); + n.getControl(RigidBodyControl.class).setKinematic(true); + bulletAppState.getPhysicsSpace().add(n); + rootNode.attachChild(n); + bulletAppState.getPhysicsSpace().enableDebug(assetManager); + } + + @Override + public void simpleUpdate(float tpf) { + List rayTest = bulletAppState.getPhysicsSpace().rayTest(cam.getLocation(), cam.getLocation().add(cam.getDirection())); + if (rayTest.size() > 0) { + PhysicsRayTestResult get = rayTest.get(0); + PhysicsCollisionObject collisionObject = get.getCollisionObject(); + //do stuff + fpsText.setText(collisionObject.getUserObject().toString()); + } + } + + private void initCrossHair() { + BitmapText bitmapText = new BitmapText(guiFont); + bitmapText.setText("+"); + bitmapText.setLocalTranslation((settings.getWidth() - bitmapText.getLineWidth())*0.5f, (settings.getHeight() + bitmapText.getLineHeight())*0.5f, 0); + guiNode.attachChild(bitmapText); + } +} diff --git a/engine/src/test/jme3test/bullet/TestSweepTest.java b/engine/src/test/jme3test/bullet/TestSweepTest.java new file mode 100644 index 000000000..b459be331 --- /dev/null +++ b/engine/src/test/jme3test/bullet/TestSweepTest.java @@ -0,0 +1,71 @@ +package jme3test.bullet; + +import com.jme3.app.SimpleApplication; +import com.jme3.bullet.BulletAppState; +import com.jme3.bullet.collision.PhysicsCollisionObject; +import com.jme3.bullet.collision.PhysicsSweepTestResult; +import com.jme3.bullet.collision.shapes.CapsuleCollisionShape; +import com.jme3.bullet.control.RigidBodyControl; +import com.jme3.math.Transform; +import com.jme3.scene.Node; +import java.util.List; + +/** + * + * A spatial moves and sweeps its next movement for obstacles before moving + * there Run this example with Vsync enabled + * + * @author + * @wezrule + */ +public class TestSweepTest extends SimpleApplication { + + private BulletAppState bulletAppState = new BulletAppState(); + private CapsuleCollisionShape obstacleCollisionShape = new CapsuleCollisionShape(0.3f, 0.5f); + private CapsuleCollisionShape capsuleCollisionShape = new CapsuleCollisionShape(1f, 1f); + private Node capsule; + private Node obstacle; + private float dist = .5f; + + public static void main(String[] args) { + new TestSweepTest().start(); + } + + @Override + public void simpleInitApp() { + stateManager.attach(bulletAppState); + + capsule = new Node("capsule"); + capsule.addControl(new RigidBodyControl(capsuleCollisionShape, 1)); + capsule.getControl(RigidBodyControl.class).setKinematic(true); + capsule.move(-2, 0, 0); + bulletAppState.getPhysicsSpace().add(capsule); + rootNode.attachChild(capsule); + + obstacle = new Node("obstacle"); + RigidBodyControl bodyControl = new RigidBodyControl(obstacleCollisionShape, 0); + obstacle.move(2, 0, 0); + obstacle.addControl(bodyControl); + bulletAppState.getPhysicsSpace().add(obstacle); + rootNode.attachChild(obstacle); + + bulletAppState.getPhysicsSpace().enableDebug(assetManager); + } + + @Override + public void simpleUpdate(float tpf) { + + float move = tpf * 1; + + List sweepTest = bulletAppState.getPhysicsSpace().sweepTest(capsuleCollisionShape, new Transform(capsule.getWorldTranslation()), new Transform(capsule.getWorldTranslation().add(dist, 0, 0))); + + if (sweepTest.size() > 0) { + PhysicsSweepTestResult get = sweepTest.get(0); + PhysicsCollisionObject collisionObject = get.getCollisionObject(); + fpsText.setText("Almost colliding with " + collisionObject.getUserObject().toString()); + } else { + // if the sweep is clear then move the spatial + capsule.move(move, 0, 0); + } + } +}