• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.bounding.BoundingVolume;
37 import com.jme3.collision.CollisionResults;
38 import com.jme3.input.KeyInput;
39 import com.jme3.input.controls.AnalogListener;
40 import com.jme3.input.controls.KeyTrigger;
41 import com.jme3.light.DirectionalLight;
42 import com.jme3.material.Material;
43 import com.jme3.math.ColorRGBA;
44 import com.jme3.math.Vector3f;
45 import com.jme3.scene.Geometry;
46 import com.jme3.scene.Mesh;
47 import com.jme3.scene.Spatial;
48 import com.jme3.scene.shape.Box;
49 
50 public class TestTriangleCollision extends SimpleApplication {
51 
52     Geometry geom1;
53 
54     Spatial golem;
55 
main(String[] args)56     public static void main(String[] args) {
57         TestTriangleCollision app = new TestTriangleCollision();
58         app.start();
59     }
60 
61     @Override
simpleInitApp()62     public void simpleInitApp() {
63         // Create two boxes
64         Mesh mesh1 = new Box(0.5f, 0.5f, 0.5f);
65         geom1 = new Geometry("Box", mesh1);
66         geom1.move(2, 2, -.5f);
67         Material m1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
68         m1.setColor("Color", ColorRGBA.Blue);
69         geom1.setMaterial(m1);
70         rootNode.attachChild(geom1);
71 
72         // load a character from jme3test-test-data
73         golem = assetManager.loadModel("Models/Oto/Oto.mesh.xml");
74         golem.scale(0.5f);
75         golem.setLocalTranslation(-1.0f, -1.5f, -0.6f);
76 
77         // We must add a light to make the model visible
78         DirectionalLight sun = new DirectionalLight();
79         sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f).normalizeLocal());
80         golem.addLight(sun);
81         rootNode.attachChild(golem);
82 
83         // Create input
84         inputManager.addMapping("MoveRight", new KeyTrigger(KeyInput.KEY_L));
85         inputManager.addMapping("MoveLeft", new KeyTrigger(KeyInput.KEY_J));
86         inputManager.addMapping("MoveUp", new KeyTrigger(KeyInput.KEY_I));
87         inputManager.addMapping("MoveDown", new KeyTrigger(KeyInput.KEY_K));
88 
89         inputManager.addListener(analogListener, new String[]{
90                     "MoveRight", "MoveLeft", "MoveUp", "MoveDown"
91                 });
92     }
93     private AnalogListener analogListener = new AnalogListener() {
94 
95         public void onAnalog(String name, float value, float tpf) {
96             if (name.equals("MoveRight")) {
97                 geom1.move(2 * tpf, 0, 0);
98             }
99 
100             if (name.equals("MoveLeft")) {
101                 geom1.move(-2 * tpf, 0, 0);
102             }
103 
104             if (name.equals("MoveUp")) {
105                 geom1.move(0, 2 * tpf, 0);
106             }
107 
108             if (name.equals("MoveDown")) {
109                 geom1.move(0, -2 * tpf, 0);
110             }
111         }
112     };
113 
114     @Override
simpleUpdate(float tpf)115     public void simpleUpdate(float tpf) {
116         CollisionResults results = new CollisionResults();
117         BoundingVolume bv = geom1.getWorldBound();
118         golem.collideWith(bv, results);
119 
120         if (results.size() > 0) {
121             geom1.getMaterial().setColor("Color", ColorRGBA.Red);
122         }else{
123             geom1.getMaterial().setColor("Color", ColorRGBA.Blue);
124         }
125     }
126 }
127