Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package at.limbo.blendertest;
- import com.jme3.app.SimpleApplication;
- import com.jme3.collision.CollisionResults;
- import com.jme3.input.KeyInput;
- import com.jme3.input.MouseInput;
- import com.jme3.input.controls.ActionListener;
- import com.jme3.input.controls.AnalogListener;
- import com.jme3.input.controls.KeyTrigger;
- import com.jme3.input.controls.MouseButtonTrigger;
- import com.jme3.material.Material;
- import com.jme3.math.Vector3f;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.shape.Box;
- import com.jme3.math.ColorRGBA;
- import com.jme3.scene.Node;
- /** Sample 2 - How to use nodes as handles to manipulate objects in the scene.
- * You can rotate, translate, and scale objects by manipulating their parent nodes.
- * The Root Node is special: Only what is attached to the Root Node appears in the scene. */
- public class CollisionTest extends SimpleApplication {
- Boolean isRunning = true;
- CollisionResults results;
- Node node1;
- Node node2;
- public static void main(String[] args) {
- CollisionTest app = new CollisionTest();
- app.start();
- }
- @Override
- public void simpleInitApp() {
- /** create a blue box at coordinates (1,-1,1) */
- Box box1 = new Box(new Vector3f(1, -1, 1), 0.5f, 0.5f, 0.5f);
- Geometry blue = new Geometry("Box", box1);
- Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- mat1.setColor("Color", ColorRGBA.Blue);
- blue.setMaterial(mat1);
- /** create a red box straight above the blue one at (1,3,1) */
- Box box2 = new Box(new Vector3f(1, 3, 1), 0.5f, 0.5f, 0.5f);
- Geometry red = new Geometry("Box", box2);
- Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- mat2.setColor("Color", ColorRGBA.Red);
- red.setMaterial(mat2);
- /** Create a pivot node at (0,0,0) and attach it to the root node */
- node1 = new Node("blue");
- rootNode.attachChild(node1); // put this node in the scene
- node2 = new Node("red");
- rootNode.attachChild(node2); // put this node in the scene
- /** Attach the two boxes to the *pivot* node. */
- node1.attachChild(blue);
- node2.attachChild(red);
- results = new CollisionResults();
- node2.collideWith(node1, results);
- initKeys(); // load my custom keybinding
- }
- /** Custom Keybinding: Map named actions to inputs. */
- private void initKeys() {
- // You can map one or several inputs to one named action
- inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
- inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_H));
- inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_J));
- inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_U));
- inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_K));
- inputManager.addMapping("Rotate", new KeyTrigger(KeyInput.KEY_SPACE),
- new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
- // Add the names to the action listener.
- inputManager.addListener(actionListener, new String[]{"Pause"});
- inputManager.addListener(analogListener, new String[]{"Left", "Right", "Down", "Up", "Rotate"});
- }
- private ActionListener actionListener = new ActionListener() {
- public void onAction(String name, boolean keyPressed, float tpf) {
- if (name.equals("Pause") && !keyPressed) {
- isRunning = !isRunning;
- }
- }
- };
- private AnalogListener analogListener = new AnalogListener() {
- public void onAnalog(String name, float value, float tpf) {
- if (isRunning) {
- if (name.equals("Rotate")) {
- node1.rotate(0, value * speed, 0);
- }
- if (name.equals("Right")) {
- node1.move(0.003f, 0.0f, 0.0f);
- }
- if (name.equals("Left")) {
- node1.move(-0.003f, 0.0f, 0.0f);
- }
- if (name.equals("Down")) {
- node1.move(0.0f, -0.003f, 0.0f);
- }
- if (name.equals("Up")) {
- node1.move(0.0f, 0.003f, 0.0f);
- }
- } else {
- System.out.println("Press P to unpause.");
- }
- }
- };
- @Override
- public void simpleUpdate(float tpf) {
- if (results.size() > 0) {
- node2.move(2.0f, 0.0f, 0.0f);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement