TestChaseCameraAppState.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2009-2018 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. package jme3test.input;
  33. import com.jme3.app.ChaseCameraAppState;
  34. import com.jme3.app.FlyCamAppState;
  35. import com.jme3.app.SimpleApplication;
  36. import com.jme3.input.KeyInput;
  37. import com.jme3.input.controls.ActionListener;
  38. import com.jme3.input.controls.AnalogListener;
  39. import com.jme3.input.controls.KeyTrigger;
  40. import com.jme3.material.Material;
  41. import com.jme3.math.FastMath;
  42. import com.jme3.math.Quaternion;
  43. import com.jme3.math.Vector3f;
  44. import com.jme3.scene.Geometry;
  45. import com.jme3.scene.shape.Quad;
  46. /** A 3rd-person chase camera orbits a target (teapot).
  47. * Follow the teapot with WASD keys, rotate by dragging the mouse. */
  48. public class TestChaseCameraAppState extends SimpleApplication implements AnalogListener, ActionListener {
  49. private Geometry teaGeom;
  50. public static void main(String[] args) {
  51. TestChaseCameraAppState app = new TestChaseCameraAppState();
  52. app.start();
  53. }
  54. public void simpleInitApp() {
  55. // Load a teapot model
  56. teaGeom = (Geometry) assetManager.loadModel("Models/Teapot/Teapot.obj");
  57. Material mat_tea = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
  58. teaGeom.setMaterial(mat_tea);
  59. rootNode.attachChild(teaGeom);
  60. // Load a floor model
  61. Material mat_ground = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  62. mat_ground.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
  63. Geometry ground = new Geometry("ground", new Quad(50, 50));
  64. ground.setLocalRotation(new Quaternion().fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_X));
  65. ground.setLocalTranslation(-25, -1, 25);
  66. ground.setMaterial(mat_ground);
  67. rootNode.attachChild(ground);
  68. //disable the flyCam
  69. stateManager.detach(stateManager.getState(FlyCamAppState.class));
  70. // Enable a chase cam
  71. ChaseCameraAppState chaseCamAS = new ChaseCameraAppState();
  72. chaseCamAS.setTarget(teaGeom);
  73. stateManager.attach(chaseCamAS);
  74. //Uncomment this to invert the camera's vertical rotation Axis
  75. //chaseCamAS.setInvertVerticalAxis(true);
  76. //Uncomment this to invert the camera's horizontal rotation Axis
  77. //chaseCamAS.setInvertHorizontalAxis(true);
  78. //Uncomment this to enable rotation when the middle mouse button is pressed (like Blender)
  79. //WARNING : setting this trigger disable the rotation on right and left mouse button click
  80. //chaseCamAS.setToggleRotationTrigger(new MouseButtonTrigger(MouseInput.BUTTON_MIDDLE));
  81. //Uncomment this to set multiple triggers to enable rotation of the cam
  82. //Here space bar and middle mouse button
  83. //chaseCamAS.setToggleRotationTrigger(new MouseButtonTrigger(MouseInput.BUTTON_MIDDLE),new KeyTrigger(KeyInput.KEY_SPACE));
  84. //registering inputs for target's movement
  85. registerInput();
  86. }
  87. public void registerInput() {
  88. inputManager.addMapping("moveForward", new KeyTrigger(KeyInput.KEY_UP), new KeyTrigger(KeyInput.KEY_W));
  89. inputManager.addMapping("moveBackward", new KeyTrigger(KeyInput.KEY_DOWN), new KeyTrigger(KeyInput.KEY_S));
  90. inputManager.addMapping("moveRight", new KeyTrigger(KeyInput.KEY_RIGHT), new KeyTrigger(KeyInput.KEY_D));
  91. inputManager.addMapping("moveLeft", new KeyTrigger(KeyInput.KEY_LEFT), new KeyTrigger(KeyInput.KEY_A));
  92. inputManager.addMapping("displayPosition", new KeyTrigger(KeyInput.KEY_P));
  93. inputManager.addListener(this, "moveForward", "moveBackward", "moveRight", "moveLeft");
  94. inputManager.addListener(this, "displayPosition");
  95. }
  96. public void onAnalog(String name, float value, float tpf) {
  97. if (name.equals("moveForward")) {
  98. teaGeom.move(0, 0, -5 * tpf);
  99. }
  100. if (name.equals("moveBackward")) {
  101. teaGeom.move(0, 0, 5 * tpf);
  102. }
  103. if (name.equals("moveRight")) {
  104. teaGeom.move(5 * tpf, 0, 0);
  105. }
  106. if (name.equals("moveLeft")) {
  107. teaGeom.move(-5 * tpf, 0, 0);
  108. }
  109. }
  110. public void onAction(String name, boolean keyPressed, float tpf) {
  111. if (name.equals("displayPosition") && keyPressed) {
  112. teaGeom.move(10, 10, 10);
  113. }
  114. }
  115. @Override
  116. public void simpleUpdate(float tpf) {
  117. super.simpleUpdate(tpf);
  118. // teaGeom.move(new Vector3f(0.001f, 0, 0));
  119. // pivot.rotate(0, 0.00001f, 0);
  120. // rootNode.updateGeometricState();
  121. }
  122. // public void update() {
  123. // super.update();
  124. //// render the viewports
  125. // float tpf = timer.getTimePerFrame();
  126. // state.getRootNode().rotate(0, 0.000001f, 0);
  127. // stateManager.update(tpf);
  128. // stateManager.render(renderManager);
  129. // renderManager.render(tpf);
  130. // }
  131. }