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