159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/* 259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 jMonkeyEngine 359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved. 459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without 659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are 759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met: 859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright 1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * notice, this list of conditions and the following disclaimer. 1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright 1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * notice, this list of conditions and the following disclaimer in the 1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * documentation and/or other materials provided with the distribution. 1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * may be used to endorse or promote products derived from this software 1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * without specific prior written permission. 1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage jme3test.helloworld; 3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.app.SimpleApplication; 3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.collision.CollisionResult; 3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.collision.CollisionResults; 3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.font.BitmapText; 3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.input.KeyInput; 4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.input.MouseInput; 4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.input.controls.ActionListener; 4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.input.controls.KeyTrigger; 4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.input.controls.MouseButtonTrigger; 4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.light.DirectionalLight; 4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.material.Material; 4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.ColorRGBA; 4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Ray; 4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Vector3f; 4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Geometry; 5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Node; 5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Spatial; 5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.shape.Box; 5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.shape.Sphere; 5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/** Sample 8 - how to let the user pick (select) objects in the scene 5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * using the mouse or key presses. Can be used for shooting, opening doors, etc. */ 5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class HelloPicking extends SimpleApplication { 5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public static void main(String[] args) { 6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta HelloPicking app = new HelloPicking(); 6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta app.start(); 6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Node shootables; 6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Geometry mark; 6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta @Override 6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void simpleInitApp() { 6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta initCrossHairs(); // a "+" in the middle of the screen to help aiming 6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta initKeys(); // load custom key mappings 7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta initMark(); // a red sphere to mark the hit 7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** create four colored boxes and a floor to shoot at: */ 7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta shootables = new Node("Shootables"); 7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta rootNode.attachChild(shootables); 7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta shootables.attachChild(makeCube("a Dragon", -2f, 0f, 1f)); 7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta shootables.attachChild(makeCube("a tin can", 1f, -2f, 0f)); 7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta shootables.attachChild(makeCube("the Sheriff", 0f, 1f, -2f)); 7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta shootables.attachChild(makeCube("the Deputy", 1f, 0f, -4f)); 7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta shootables.attachChild(makeFloor()); 8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta shootables.attachChild(makeCharacter()); 8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** Declaring the "Shoot" action and mapping to its triggers. */ 8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private void initKeys() { 8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta inputManager.addMapping("Shoot", 8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta new KeyTrigger(KeyInput.KEY_SPACE), // trigger 1: spacebar 8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta new MouseButtonTrigger(MouseInput.BUTTON_LEFT)); // trigger 2: left-button click 8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta inputManager.addListener(actionListener, "Shoot"); 8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** Defining the "Shoot" action: Determine what was hit and how to respond. */ 9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private ActionListener actionListener = new ActionListener() { 9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void onAction(String name, boolean keyPressed, float tpf) { 9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (name.equals("Shoot") && !keyPressed) { 9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // 1. Reset results list. 9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta CollisionResults results = new CollisionResults(); 9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // 2. Aim the ray from cam loc to cam direction. 9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Ray ray = new Ray(cam.getLocation(), cam.getDirection()); 9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // 3. Collect intersections between Ray and Shootables in results list. 10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta shootables.collideWith(ray, results); 10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // 4. Print the results 10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta System.out.println("----- Collisions? " + results.size() + "-----"); 10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (int i = 0; i < results.size(); i++) { 10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // For each hit, we know distance, impact point, name of geometry. 10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float dist = results.getCollision(i).getDistance(); 10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Vector3f pt = results.getCollision(i).getContactPoint(); 10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta String hit = results.getCollision(i).getGeometry().getName(); 10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta System.out.println("* Collision #" + i); 10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta System.out.println(" You shot " + hit + " at " + pt + ", " + dist + " wu away."); 11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // 5. Use the results (we mark the hit object) 11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (results.size() > 0) { 11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // The closest collision point is what was truly hit: 11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta CollisionResult closest = results.getClosestCollision(); 11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // Let's interact - we mark the hit with a red dot. 11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta mark.setLocalTranslation(closest.getContactPoint()); 11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta rootNode.attachChild(mark); 11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } else { 11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // No hits? Then remove the red mark. 12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta rootNode.detachChild(mark); 12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta }; 12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** A cube object for target practice */ 12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected Geometry makeCube(String name, float x, float y, float z) { 12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Box box = new Box(new Vector3f(x, y, z), 1, 1, 1); 12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Geometry cube = new Geometry(name, box); 13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta mat1.setColor("Color", ColorRGBA.randomColor()); 13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta cube.setMaterial(mat1); 13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return cube; 13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** A floor to show that the "shot" can go through several objects. */ 13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected Geometry makeFloor() { 13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Box box = new Box(new Vector3f(0, -4, -5), 15, .2f, 15); 13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Geometry floor = new Geometry("the Floor", box); 14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta mat1.setColor("Color", ColorRGBA.Gray); 14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta floor.setMaterial(mat1); 14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return floor; 14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** A red ball that marks the last spot that was "hit" by the "shot". */ 14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected void initMark() { 14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Sphere sphere = new Sphere(30, 30, 0.2f); 14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta mark = new Geometry("BOOM!", sphere); 15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Material mark_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta mark_mat.setColor("Color", ColorRGBA.Red); 15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta mark.setMaterial(mark_mat); 15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** A centred plus sign to help the player aim. */ 15659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected void initCrossHairs() { 15759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta guiNode.detachAllChildren(); 15859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt"); 15959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta BitmapText ch = new BitmapText(guiFont, false); 16059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta ch.setSize(guiFont.getCharSet().getRenderedSize() * 2); 16159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta ch.setText("+"); // crosshairs 16259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta ch.setLocalTranslation( // center 16359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2, 16459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta settings.getHeight() / 2 + ch.getLineHeight() / 2, 0); 16559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta guiNode.attachChild(ch); 16659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 16759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 16859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected Spatial makeCharacter() { 16959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // load a character from jme3test-test-data 17059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Spatial golem = assetManager.loadModel("Models/Oto/Oto.mesh.xml"); 17159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta golem.scale(0.5f); 17259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta golem.setLocalTranslation(-1.0f, -1.5f, -0.6f); 17359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 17459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // We must add a light to make the model visible 17559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta DirectionalLight sun = new DirectionalLight(); 17659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f)); 17759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta golem.addLight(sun); 17859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return golem; 17959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 18059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta} 181