Advertisement
Guest User

CollisionTest

a guest
Apr 18th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package at.limbo.blendertest;
  6.  
  7. import com.jme3.app.SimpleApplication;
  8. import com.jme3.collision.CollisionResults;
  9. import com.jme3.input.KeyInput;
  10. import com.jme3.input.MouseInput;
  11. import com.jme3.input.controls.ActionListener;
  12. import com.jme3.input.controls.AnalogListener;
  13. import com.jme3.input.controls.KeyTrigger;
  14. import com.jme3.input.controls.MouseButtonTrigger;
  15. import com.jme3.material.Material;
  16. import com.jme3.math.Vector3f;
  17. import com.jme3.scene.Geometry;
  18. import com.jme3.scene.shape.Box;
  19. import com.jme3.math.ColorRGBA;
  20. import com.jme3.scene.Node;
  21.  
  22. /** Sample 2 - How to use nodes as handles to manipulate objects in the scene.
  23. * You can rotate, translate, and scale objects by manipulating their parent nodes.
  24. * The Root Node is special: Only what is attached to the Root Node appears in the scene. */
  25. public class CollisionTest extends SimpleApplication {
  26.  
  27. Boolean isRunning = true;
  28. CollisionResults results;
  29. Node node1;
  30. Node node2;
  31.  
  32. public static void main(String[] args) {
  33. CollisionTest app = new CollisionTest();
  34. app.start();
  35. }
  36.  
  37. @Override
  38. public void simpleInitApp() {
  39.  
  40. /** create a blue box at coordinates (1,-1,1) */
  41. Box box1 = new Box(new Vector3f(1, -1, 1), 0.5f, 0.5f, 0.5f);
  42. Geometry blue = new Geometry("Box", box1);
  43. Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  44. mat1.setColor("Color", ColorRGBA.Blue);
  45. blue.setMaterial(mat1);
  46.  
  47. /** create a red box straight above the blue one at (1,3,1) */
  48. Box box2 = new Box(new Vector3f(1, 3, 1), 0.5f, 0.5f, 0.5f);
  49. Geometry red = new Geometry("Box", box2);
  50. Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  51. mat2.setColor("Color", ColorRGBA.Red);
  52. red.setMaterial(mat2);
  53.  
  54. /** Create a pivot node at (0,0,0) and attach it to the root node */
  55. node1 = new Node("blue");
  56. rootNode.attachChild(node1); // put this node in the scene
  57. node2 = new Node("red");
  58. rootNode.attachChild(node2); // put this node in the scene
  59. /** Attach the two boxes to the *pivot* node. */
  60. node1.attachChild(blue);
  61. node2.attachChild(red);
  62.  
  63.  
  64. results = new CollisionResults();
  65. node2.collideWith(node1, results);
  66.  
  67. initKeys(); // load my custom keybinding
  68. }
  69.  
  70. /** Custom Keybinding: Map named actions to inputs. */
  71. private void initKeys() {
  72. // You can map one or several inputs to one named action
  73. inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
  74. inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_H));
  75. inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_J));
  76. inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_U));
  77. inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_K));
  78. inputManager.addMapping("Rotate", new KeyTrigger(KeyInput.KEY_SPACE),
  79. new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
  80. // Add the names to the action listener.
  81. inputManager.addListener(actionListener, new String[]{"Pause"});
  82. inputManager.addListener(analogListener, new String[]{"Left", "Right", "Down", "Up", "Rotate"});
  83.  
  84. }
  85. private ActionListener actionListener = new ActionListener() {
  86.  
  87. public void onAction(String name, boolean keyPressed, float tpf) {
  88. if (name.equals("Pause") && !keyPressed) {
  89. isRunning = !isRunning;
  90. }
  91. }
  92. };
  93. private AnalogListener analogListener = new AnalogListener() {
  94.  
  95. public void onAnalog(String name, float value, float tpf) {
  96. if (isRunning) {
  97. if (name.equals("Rotate")) {
  98. node1.rotate(0, value * speed, 0);
  99. }
  100. if (name.equals("Right")) {
  101. node1.move(0.003f, 0.0f, 0.0f);
  102. }
  103. if (name.equals("Left")) {
  104. node1.move(-0.003f, 0.0f, 0.0f);
  105. }
  106. if (name.equals("Down")) {
  107. node1.move(0.0f, -0.003f, 0.0f);
  108. }
  109. if (name.equals("Up")) {
  110. node1.move(0.0f, 0.003f, 0.0f);
  111. }
  112. } else {
  113. System.out.println("Press P to unpause.");
  114. }
  115. }
  116. };
  117.  
  118. @Override
  119. public void simpleUpdate(float tpf) {
  120.  
  121. if (results.size() > 0) {
  122. node2.move(2.0f, 0.0f, 0.0f);
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement