diff options
Diffstat (limited to 'engine/src/test/jme3test/collision')
-rw-r--r-- | engine/src/test/jme3test/collision/RayTrace.java | 102 | ||||
-rw-r--r-- | engine/src/test/jme3test/collision/TestMousePick.java | 153 | ||||
-rw-r--r-- | engine/src/test/jme3test/collision/TestRayCasting.java | 95 | ||||
-rw-r--r-- | engine/src/test/jme3test/collision/TestTriangleCollision.java | 126 |
4 files changed, 476 insertions, 0 deletions
diff --git a/engine/src/test/jme3test/collision/RayTrace.java b/engine/src/test/jme3test/collision/RayTrace.java new file mode 100644 index 0000000..7e02ad1 --- /dev/null +++ b/engine/src/test/jme3test/collision/RayTrace.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2009-2010 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 jme3test.collision; + +import com.jme3.collision.CollisionResults; +import com.jme3.math.Ray; +import com.jme3.math.Vector2f; +import com.jme3.math.Vector3f; +import com.jme3.renderer.Camera; +import com.jme3.scene.Spatial; +import java.awt.FlowLayout; +import java.awt.image.BufferedImage; +import javax.swing.ImageIcon; +import javax.swing.JFrame; +import javax.swing.JLabel; + +public class RayTrace { + + private BufferedImage image; + private Camera cam; + private Spatial scene; + private CollisionResults results = new CollisionResults(); + private JFrame frame; + private JLabel label; + + public RayTrace(Spatial scene, Camera cam, int width, int height){ + image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + this.scene = scene; + this.cam = cam; + } + + public void show(){ + frame = new JFrame("HDR View"); + label = new JLabel(new ImageIcon(image)); + frame.getContentPane().add(label); + frame.setLayout(new FlowLayout()); + frame.pack(); + frame.setVisible(true); + } + + public void update(){ + int w = image.getWidth(); + int h = image.getHeight(); + + float wr = (float) cam.getWidth() / image.getWidth(); + float hr = (float) cam.getHeight() / image.getHeight(); + + scene.updateGeometricState(); + + for (int y = 0; y < h; y++){ + for (int x = 0; x < w; x++){ + Vector2f v = new Vector2f(x * wr,y * hr); + Vector3f pos = cam.getWorldCoordinates(v, 0.0f); + Vector3f dir = cam.getWorldCoordinates(v, 0.3f); + dir.subtractLocal(pos).normalizeLocal(); + + Ray r = new Ray(pos, dir); + + results.clear(); + scene.collideWith(r, results); + if (results.size() > 0){ + image.setRGB(x, h - y - 1, 0xFFFFFFFF); + }else{ + image.setRGB(x, h - y - 1, 0xFF000000); + } + } + } + + label.repaint(); + } + +} diff --git a/engine/src/test/jme3test/collision/TestMousePick.java b/engine/src/test/jme3test/collision/TestMousePick.java new file mode 100644 index 0000000..835b2c6 --- /dev/null +++ b/engine/src/test/jme3test/collision/TestMousePick.java @@ -0,0 +1,153 @@ +/* + * Copyright (c) 2009-2010 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 jme3test.collision; + +import com.jme3.app.SimpleApplication; +import com.jme3.collision.CollisionResult; +import com.jme3.collision.CollisionResults; +import com.jme3.light.DirectionalLight; +import com.jme3.material.Material; +import com.jme3.math.ColorRGBA; +import com.jme3.math.Quaternion; +import com.jme3.math.Ray; +import com.jme3.math.Vector3f; +import com.jme3.scene.Geometry; +import com.jme3.scene.Node; +import com.jme3.scene.Spatial; +import com.jme3.scene.debug.Arrow; +import com.jme3.scene.shape.Box; + +public class TestMousePick extends SimpleApplication { + + public static void main(String[] args) { + TestMousePick app = new TestMousePick(); + app.start(); + } + + Node shootables; + Geometry mark; + + @Override + public void simpleInitApp() { + flyCam.setEnabled(false); + initMark(); // a red sphere to mark the hit + + /** create four colored boxes and a floor to shoot at: */ + shootables = new Node("Shootables"); + rootNode.attachChild(shootables); + shootables.attachChild(makeCube("a Dragon", -2f, 0f, 1f)); + shootables.attachChild(makeCube("a tin can", 1f, -2f, 0f)); + shootables.attachChild(makeCube("the Sheriff", 0f, 1f, -2f)); + shootables.attachChild(makeCube("the Deputy", 1f, 0f, -4f)); + shootables.attachChild(makeFloor()); + shootables.attachChild(makeCharacter()); + } + + @Override + public void simpleUpdate(float tpf){ + Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f); + Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f); + direction.subtractLocal(origin).normalizeLocal(); + + Ray ray = new Ray(origin, direction); + CollisionResults results = new CollisionResults(); + shootables.collideWith(ray, results); +// System.out.println("----- Collisions? " + results.size() + "-----"); +// for (int i = 0; i < results.size(); i++) { +// // For each hit, we know distance, impact point, name of geometry. +// float dist = results.getCollision(i).getDistance(); +// Vector3f pt = results.getCollision(i).getWorldContactPoint(); +// String hit = results.getCollision(i).getGeometry().getName(); +// System.out.println("* Collision #" + i); +// System.out.println(" You shot " + hit + " at " + pt + ", " + dist + " wu away."); +// } + if (results.size() > 0) { + CollisionResult closest = results.getClosestCollision(); + mark.setLocalTranslation(closest.getContactPoint()); + + Quaternion q = new Quaternion(); + q.lookAt(closest.getContactNormal(), Vector3f.UNIT_Y); + mark.setLocalRotation(q); + + rootNode.attachChild(mark); + } else { + rootNode.detachChild(mark); + } + } + + /** A cube object for target practice */ + protected Geometry makeCube(String name, float x, float y, float z) { + Box box = new Box(new Vector3f(x, y, z), 1, 1, 1); + Geometry cube = new Geometry(name, box); + Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); + mat1.setColor("Color", ColorRGBA.randomColor()); + cube.setMaterial(mat1); + return cube; + } + + /** A floor to show that the "shot" can go through several objects. */ + protected Geometry makeFloor() { + Box box = new Box(new Vector3f(0, -4, -5), 15, .2f, 15); + Geometry floor = new Geometry("the Floor", box); + Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); + mat1.setColor("Color", ColorRGBA.Gray); + floor.setMaterial(mat1); + return floor; + } + + /** A red ball that marks the last spot that was "hit" by the "shot". */ + protected void initMark() { + Arrow arrow = new Arrow(Vector3f.UNIT_Z.mult(2f)); + arrow.setLineWidth(3); + + //Sphere sphere = new Sphere(30, 30, 0.2f); + mark = new Geometry("BOOM!", arrow); + //mark = new Geometry("BOOM!", sphere); + Material mark_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); + mark_mat.setColor("Color", ColorRGBA.Red); + mark.setMaterial(mark_mat); + } + + protected Spatial makeCharacter() { + // load a character from jme3test-test-data + Spatial golem = assetManager.loadModel("Models/Oto/Oto.mesh.xml"); + golem.scale(0.5f); + golem.setLocalTranslation(-1.0f, -1.5f, -0.6f); + + // We must add a light to make the model visible + DirectionalLight sun = new DirectionalLight(); + sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f).normalizeLocal()); + golem.addLight(sun); + return golem; + } +} diff --git a/engine/src/test/jme3test/collision/TestRayCasting.java b/engine/src/test/jme3test/collision/TestRayCasting.java new file mode 100644 index 0000000..b2ef0de --- /dev/null +++ b/engine/src/test/jme3test/collision/TestRayCasting.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2009-2010 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 jme3test.collision; + +import com.jme3.app.SimpleApplication; +import com.jme3.bounding.BoundingSphere; +import com.jme3.material.Material; +import com.jme3.math.FastMath; +import com.jme3.scene.Mesh; +import com.jme3.scene.Spatial; +import com.jme3.scene.VertexBuffer.Type; + +public class TestRayCasting extends SimpleApplication { + + private RayTrace tracer; + private Spatial teapot; + + public static void main(String[] args){ + TestRayCasting app = new TestRayCasting(); + app.setPauseOnLostFocus(false); + app.start(); + } + + @Override + public void simpleInitApp() { +// flyCam.setEnabled(false); + + // load material + Material mat = (Material) assetManager.loadMaterial("Interface/Logo/Logo.j3m"); + + Mesh q = new Mesh(); + q.setBuffer(Type.Position, 3, new float[] + { + 1, 0, 0, + 0, 1.5f, 0, + -1, 0, 0 + } + ); + q.setBuffer(Type.Index, 3, new int[]{ 0, 1, 2 }); + q.setBound(new BoundingSphere()); + q.updateBound(); +// Geometry teapot = new Geometry("MyGeom", q); + + teapot = assetManager.loadModel("Models/Teapot/Teapot.mesh.xml"); +// teapot.scale(2f, 2f, 2f); +// teapot.move(2f, 2f, -.5f); + teapot.rotate(FastMath.HALF_PI, FastMath.HALF_PI, FastMath.HALF_PI); + teapot.setMaterial(mat); + rootNode.attachChild(teapot); + +// cam.setLocation(cam.getLocation().add(0,1,0)); +// cam.lookAt(teapot.getWorldBound().getCenter(), Vector3f.UNIT_Y); + + tracer = new RayTrace(rootNode, cam, 160, 128); + tracer.show(); + tracer.update(); + } + + @Override + public void simpleUpdate(float tpf){ + teapot.rotate(0,tpf,0); + tracer.update(); + } + +}
\ No newline at end of file diff --git a/engine/src/test/jme3test/collision/TestTriangleCollision.java b/engine/src/test/jme3test/collision/TestTriangleCollision.java new file mode 100644 index 0000000..29ed81f --- /dev/null +++ b/engine/src/test/jme3test/collision/TestTriangleCollision.java @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2009-2010 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 jme3test.collision; + +import com.jme3.app.SimpleApplication; +import com.jme3.bounding.BoundingVolume; +import com.jme3.collision.CollisionResults; +import com.jme3.input.KeyInput; +import com.jme3.input.controls.AnalogListener; +import com.jme3.input.controls.KeyTrigger; +import com.jme3.light.DirectionalLight; +import com.jme3.material.Material; +import com.jme3.math.ColorRGBA; +import com.jme3.math.Vector3f; +import com.jme3.scene.Geometry; +import com.jme3.scene.Mesh; +import com.jme3.scene.Spatial; +import com.jme3.scene.shape.Box; + +public class TestTriangleCollision extends SimpleApplication { + + Geometry geom1; + + Spatial golem; + + public static void main(String[] args) { + TestTriangleCollision app = new TestTriangleCollision(); + app.start(); + } + + @Override + public void simpleInitApp() { + // Create two boxes + Mesh mesh1 = new Box(0.5f, 0.5f, 0.5f); + geom1 = new Geometry("Box", mesh1); + geom1.move(2, 2, -.5f); + Material m1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); + m1.setColor("Color", ColorRGBA.Blue); + geom1.setMaterial(m1); + rootNode.attachChild(geom1); + + // load a character from jme3test-test-data + golem = assetManager.loadModel("Models/Oto/Oto.mesh.xml"); + golem.scale(0.5f); + golem.setLocalTranslation(-1.0f, -1.5f, -0.6f); + + // We must add a light to make the model visible + DirectionalLight sun = new DirectionalLight(); + sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f).normalizeLocal()); + golem.addLight(sun); + rootNode.attachChild(golem); + + // Create input + inputManager.addMapping("MoveRight", new KeyTrigger(KeyInput.KEY_L)); + inputManager.addMapping("MoveLeft", new KeyTrigger(KeyInput.KEY_J)); + inputManager.addMapping("MoveUp", new KeyTrigger(KeyInput.KEY_I)); + inputManager.addMapping("MoveDown", new KeyTrigger(KeyInput.KEY_K)); + + inputManager.addListener(analogListener, new String[]{ + "MoveRight", "MoveLeft", "MoveUp", "MoveDown" + }); + } + private AnalogListener analogListener = new AnalogListener() { + + public void onAnalog(String name, float value, float tpf) { + if (name.equals("MoveRight")) { + geom1.move(2 * tpf, 0, 0); + } + + if (name.equals("MoveLeft")) { + geom1.move(-2 * tpf, 0, 0); + } + + if (name.equals("MoveUp")) { + geom1.move(0, 2 * tpf, 0); + } + + if (name.equals("MoveDown")) { + geom1.move(0, -2 * tpf, 0); + } + } + }; + + @Override + public void simpleUpdate(float tpf) { + CollisionResults results = new CollisionResults(); + BoundingVolume bv = geom1.getWorldBound(); + golem.collideWith(bv, results); + + if (results.size() > 0) { + geom1.getMaterial().setColor("Color", ColorRGBA.Red); + }else{ + geom1.getMaterial().setColor("Color", ColorRGBA.Blue); + } + } +} |