TerrainGridSerializationTest.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package jme3test.terrain;
  2. import com.jme3.app.SimpleApplication;
  3. import com.jme3.app.state.ScreenshotAppState;
  4. import com.jme3.asset.plugins.HttpZipLocator;
  5. import com.jme3.asset.plugins.ZipLocator;
  6. import com.jme3.bullet.BulletAppState;
  7. import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
  8. import com.jme3.bullet.collision.shapes.HeightfieldCollisionShape;
  9. import com.jme3.bullet.control.CharacterControl;
  10. import com.jme3.bullet.control.RigidBodyControl;
  11. import com.jme3.input.KeyInput;
  12. import com.jme3.input.controls.ActionListener;
  13. import com.jme3.input.controls.KeyTrigger;
  14. import com.jme3.material.Material;
  15. import com.jme3.math.ColorRGBA;
  16. import com.jme3.math.Vector3f;
  17. import com.jme3.terrain.geomipmap.TerrainGrid;
  18. import com.jme3.terrain.geomipmap.TerrainGridListener;
  19. import com.jme3.terrain.geomipmap.TerrainLodControl;
  20. import com.jme3.terrain.geomipmap.TerrainQuad;
  21. import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
  22. import java.io.File;
  23. public class TerrainGridSerializationTest extends SimpleApplication {
  24. private TerrainGrid terrain;
  25. private boolean usePhysics = true;
  26. public static void main(final String[] args) {
  27. TerrainGridSerializationTest app = new TerrainGridSerializationTest();
  28. app.start();
  29. }
  30. private CharacterControl player3;
  31. @Override
  32. public void simpleInitApp() {
  33. File file = new File("TerrainGridTestData.zip");
  34. if (!file.exists()) {
  35. assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/TerrainGridTestData.zip", HttpZipLocator.class);
  36. } else {
  37. assetManager.registerLocator("TerrainGridTestData.zip", ZipLocator.class);
  38. }
  39. this.flyCam.setMoveSpeed(100f);
  40. ScreenshotAppState state = new ScreenshotAppState();
  41. this.stateManager.attach(state);
  42. this.terrain= (TerrainGrid) assetManager.loadModel("TerrainGrid/TerrainGrid.j3o");
  43. this.rootNode.attachChild(this.terrain);
  44. TerrainLodControl control = new TerrainLodControl(this.terrain, getCamera());
  45. control.setLodCalculator( new DistanceLodCalculator(65, 2.7f) ); // patch size, and a multiplier
  46. this.terrain.addControl(control);
  47. final BulletAppState bulletAppState = new BulletAppState();
  48. stateManager.attach(bulletAppState);
  49. this.getCamera().setLocation(new Vector3f(0, 256, 0));
  50. this.viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
  51. if (usePhysics) {
  52. CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(0.5f, 1.8f, 1);
  53. player3 = new CharacterControl(capsuleShape, 0.5f);
  54. player3.setJumpSpeed(20);
  55. player3.setFallSpeed(10);
  56. player3.setGravity(10);
  57. player3.setPhysicsLocation(new Vector3f(cam.getLocation().x, 256, cam.getLocation().z));
  58. bulletAppState.getPhysicsSpace().add(player3);
  59. terrain.addListener(new TerrainGridListener() {
  60. public void gridMoved(Vector3f newCenter) {
  61. }
  62. public Material tileLoaded(Material material, Vector3f cell) {
  63. return material;
  64. }
  65. public void tileAttached(Vector3f cell, TerrainQuad quad) {
  66. //workaround for bugged test j3o's
  67. while(quad.getControl(RigidBodyControl.class)!=null){
  68. quad.removeControl(RigidBodyControl.class);
  69. }
  70. quad.addControl(new RigidBodyControl(new HeightfieldCollisionShape(quad.getHeightMap(), terrain.getLocalScale()), 0));
  71. bulletAppState.getPhysicsSpace().add(quad);
  72. }
  73. public void tileDetached(Vector3f cell, TerrainQuad quad) {
  74. bulletAppState.getPhysicsSpace().remove(quad);
  75. quad.removeControl(RigidBodyControl.class);
  76. }
  77. });
  78. }
  79. this.initKeys();
  80. }
  81. private void initKeys() {
  82. // You can map one or several inputs to one named action
  83. this.inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_A));
  84. this.inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_D));
  85. this.inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_W));
  86. this.inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_S));
  87. this.inputManager.addMapping("Jumps", new KeyTrigger(KeyInput.KEY_SPACE));
  88. this.inputManager.addListener(this.actionListener, "Lefts");
  89. this.inputManager.addListener(this.actionListener, "Rights");
  90. this.inputManager.addListener(this.actionListener, "Ups");
  91. this.inputManager.addListener(this.actionListener, "Downs");
  92. this.inputManager.addListener(this.actionListener, "Jumps");
  93. }
  94. private boolean left;
  95. private boolean right;
  96. private boolean up;
  97. private boolean down;
  98. private final ActionListener actionListener = new ActionListener() {
  99. @Override
  100. public void onAction(final String name, final boolean keyPressed, final float tpf) {
  101. if (name.equals("Lefts")) {
  102. if (keyPressed) {
  103. TerrainGridSerializationTest.this.left = true;
  104. } else {
  105. TerrainGridSerializationTest.this.left = false;
  106. }
  107. } else if (name.equals("Rights")) {
  108. if (keyPressed) {
  109. TerrainGridSerializationTest.this.right = true;
  110. } else {
  111. TerrainGridSerializationTest.this.right = false;
  112. }
  113. } else if (name.equals("Ups")) {
  114. if (keyPressed) {
  115. TerrainGridSerializationTest.this.up = true;
  116. } else {
  117. TerrainGridSerializationTest.this.up = false;
  118. }
  119. } else if (name.equals("Downs")) {
  120. if (keyPressed) {
  121. TerrainGridSerializationTest.this.down = true;
  122. } else {
  123. TerrainGridSerializationTest.this.down = false;
  124. }
  125. } else if (name.equals("Jumps")) {
  126. TerrainGridSerializationTest.this.player3.jump();
  127. }
  128. }
  129. };
  130. private final Vector3f walkDirection = new Vector3f();
  131. @Override
  132. public void simpleUpdate(final float tpf) {
  133. Vector3f camDir = this.cam.getDirection().clone().multLocal(0.6f);
  134. Vector3f camLeft = this.cam.getLeft().clone().multLocal(0.4f);
  135. this.walkDirection.set(0, 0, 0);
  136. if (this.left) {
  137. this.walkDirection.addLocal(camLeft);
  138. }
  139. if (this.right) {
  140. this.walkDirection.addLocal(camLeft.negate());
  141. }
  142. if (this.up) {
  143. this.walkDirection.addLocal(camDir);
  144. }
  145. if (this.down) {
  146. this.walkDirection.addLocal(camDir.negate());
  147. }
  148. if (usePhysics) {
  149. this.player3.setWalkDirection(this.walkDirection);
  150. this.cam.setLocation(this.player3.getPhysicsLocation());
  151. }
  152. }
  153. }