1 /* 2 * Copyright (c) 2009-2010 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 package jme3test.collision; 34 35 import com.jme3.app.SimpleApplication; 36 import com.jme3.collision.CollisionResult; 37 import com.jme3.collision.CollisionResults; 38 import com.jme3.light.DirectionalLight; 39 import com.jme3.material.Material; 40 import com.jme3.math.ColorRGBA; 41 import com.jme3.math.Quaternion; 42 import com.jme3.math.Ray; 43 import com.jme3.math.Vector3f; 44 import com.jme3.scene.Geometry; 45 import com.jme3.scene.Node; 46 import com.jme3.scene.Spatial; 47 import com.jme3.scene.debug.Arrow; 48 import com.jme3.scene.shape.Box; 49 50 public class TestMousePick extends SimpleApplication { 51 main(String[] args)52 public static void main(String[] args) { 53 TestMousePick app = new TestMousePick(); 54 app.start(); 55 } 56 57 Node shootables; 58 Geometry mark; 59 60 @Override simpleInitApp()61 public void simpleInitApp() { 62 flyCam.setEnabled(false); 63 initMark(); // a red sphere to mark the hit 64 65 /** create four colored boxes and a floor to shoot at: */ 66 shootables = new Node("Shootables"); 67 rootNode.attachChild(shootables); 68 shootables.attachChild(makeCube("a Dragon", -2f, 0f, 1f)); 69 shootables.attachChild(makeCube("a tin can", 1f, -2f, 0f)); 70 shootables.attachChild(makeCube("the Sheriff", 0f, 1f, -2f)); 71 shootables.attachChild(makeCube("the Deputy", 1f, 0f, -4f)); 72 shootables.attachChild(makeFloor()); 73 shootables.attachChild(makeCharacter()); 74 } 75 76 @Override simpleUpdate(float tpf)77 public void simpleUpdate(float tpf){ 78 Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f); 79 Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f); 80 direction.subtractLocal(origin).normalizeLocal(); 81 82 Ray ray = new Ray(origin, direction); 83 CollisionResults results = new CollisionResults(); 84 shootables.collideWith(ray, results); 85 // System.out.println("----- Collisions? " + results.size() + "-----"); 86 // for (int i = 0; i < results.size(); i++) { 87 // // For each hit, we know distance, impact point, name of geometry. 88 // float dist = results.getCollision(i).getDistance(); 89 // Vector3f pt = results.getCollision(i).getWorldContactPoint(); 90 // String hit = results.getCollision(i).getGeometry().getName(); 91 // System.out.println("* Collision #" + i); 92 // System.out.println(" You shot " + hit + " at " + pt + ", " + dist + " wu away."); 93 // } 94 if (results.size() > 0) { 95 CollisionResult closest = results.getClosestCollision(); 96 mark.setLocalTranslation(closest.getContactPoint()); 97 98 Quaternion q = new Quaternion(); 99 q.lookAt(closest.getContactNormal(), Vector3f.UNIT_Y); 100 mark.setLocalRotation(q); 101 102 rootNode.attachChild(mark); 103 } else { 104 rootNode.detachChild(mark); 105 } 106 } 107 108 /** A cube object for target practice */ makeCube(String name, float x, float y, float z)109 protected Geometry makeCube(String name, float x, float y, float z) { 110 Box box = new Box(new Vector3f(x, y, z), 1, 1, 1); 111 Geometry cube = new Geometry(name, box); 112 Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 113 mat1.setColor("Color", ColorRGBA.randomColor()); 114 cube.setMaterial(mat1); 115 return cube; 116 } 117 118 /** A floor to show that the "shot" can go through several objects. */ makeFloor()119 protected Geometry makeFloor() { 120 Box box = new Box(new Vector3f(0, -4, -5), 15, .2f, 15); 121 Geometry floor = new Geometry("the Floor", box); 122 Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 123 mat1.setColor("Color", ColorRGBA.Gray); 124 floor.setMaterial(mat1); 125 return floor; 126 } 127 128 /** A red ball that marks the last spot that was "hit" by the "shot". */ initMark()129 protected void initMark() { 130 Arrow arrow = new Arrow(Vector3f.UNIT_Z.mult(2f)); 131 arrow.setLineWidth(3); 132 133 //Sphere sphere = new Sphere(30, 30, 0.2f); 134 mark = new Geometry("BOOM!", arrow); 135 //mark = new Geometry("BOOM!", sphere); 136 Material mark_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 137 mark_mat.setColor("Color", ColorRGBA.Red); 138 mark.setMaterial(mark_mat); 139 } 140 makeCharacter()141 protected Spatial makeCharacter() { 142 // load a character from jme3test-test-data 143 Spatial golem = assetManager.loadModel("Models/Oto/Oto.mesh.xml"); 144 golem.scale(0.5f); 145 golem.setLocalTranslation(-1.0f, -1.5f, -0.6f); 146 147 // We must add a light to make the model visible 148 DirectionalLight sun = new DirectionalLight(); 149 sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f).normalizeLocal()); 150 golem.addLight(sun); 151 return golem; 152 } 153 } 154