It may seem strange but this example works.
package test;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.HeightfieldCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.terrain.Terrain;
import com.jme3.terrain.geomipmap.TerrainGrid;
import com.jme3.terrain.geomipmap.TerrainGridListener;
import com.jme3.terrain.geomipmap.TerrainGridLodControl;
import com.jme3.terrain.geomipmap.TerrainLodControl;
import com.jme3.terrain.geomipmap.TerrainQuad;
import com.jme3.terrain.geomipmap.grid.AssetTileLoader;
import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
public class Main extends SimpleApplication {
private TerrainGrid terrain;
private boolean usePhysics = true;
public static void main(final String[] args) {
Main app = new Main();
app.start();
}
private CharacterControl player3;
private Material matWire;
@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(100f);
AssetTileLoader grid = new AssetTileLoader(assetManager, "testgrid", "TerrainGrid");
terrain = new TerrainGrid("terrain", 65, 257, grid);
matWire = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
matWire.getAdditionalRenderState().setWireframe(true);
terrain.setMaterial(matWire);
rootNode.attachChild(terrain);
TerrainLodControl control = new TerrainGridLodControl(terrain, getCamera());
control.setLodCalculator( new DistanceLodCalculator(65, 2.7f) );
terrain.addControl(control);
final BulletAppState bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
getCamera().setLocation(new Vector3f(0, 256, 0));
viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
if (usePhysics) {
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(0.5f, 1.8f, 1);
player3 = new CharacterControl(capsuleShape, 0.5f);
player3.setJumpSpeed(20);
player3.setFallSpeed(10);
player3.setGravity(10);
player3.setPhysicsLocation(new Vector3f(cam.getLocation().x, 256, cam.getLocation().z));
bulletAppState.getPhysicsSpace().add(player3);
terrain.addListener(new TerrainGridListener() {
public void gridMoved(Vector3f newCenter) {
}
public void tileAttached(Vector3f cell, TerrainQuad quad) {
while(quad.getControl(RigidBodyControl.class)!=null){
quad.removeControl(RigidBodyControl.class);
}
quad.addControl(new RigidBodyControl(new HeightfieldCollisionShape(quad.getHeightMap(), terrain.getLocalScale()), 0));
bulletAppState.getPhysicsSpace().add(quad);
}
public void tileDetached(Vector3f cell, TerrainQuad quad) {
if (quad.getControl(RigidBodyControl.class) != null) {
bulletAppState.getPhysicsSpace().remove(quad);
quad.removeControl(RigidBodyControl.class);
}
}
});
}
this.initKeys();
}
private void initKeys() {
// You can map one or several inputs to one named action
inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("Jumps", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("pick", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, "Lefts");
inputManager.addListener(actionListener, "Rights");
inputManager.addListener(actionListener, "Ups");
inputManager.addListener(actionListener, "Downs");
inputManager.addListener(actionListener, "Jumps");
inputManager.addListener(actionListener, "pick");
}
private boolean left;
private boolean right;
private boolean up;
private boolean down;
private final ActionListener actionListener = new ActionListener() {
@Override
public void onAction(final String name, final boolean keyPressed, final float tpf) {
if (name.equals("Lefts")) {
if (keyPressed) {
Main.this.left = true;
} else {
Main.this.left = false;
}
} else if (name.equals("Rights")) {
if (keyPressed) {
Main.this.right = true;
} else {
Main.this.right = false;
}
} else if (name.equals("Ups")) {
if (keyPressed) {
Main.this.up = true;
} else {
Main.this.up = false;
}
} else if (name.equals("Downs")) {
if (keyPressed) {
Main.this.down = true;
} else {
Main.this.down = false;
}
} else if (name.equals("Jumps")) {
Main.this.player3.jump();
} else if (name.equals("pick") && keyPressed) {
//Terrain picked = terrain.getTerrainAt(player3.getPhysicsLocation());
Terrain picked = terrain.getTerrainAtCell(terrain.getCurrentCell());
System.out.println("** cell "+player3.getPhysicsLocation()+" picked terrain: "+picked);
}
}
};
private final Vector3f walkDirection = new Vector3f();
@Override
public void simpleUpdate(final float tpf) {
Vector3f camDir = this.cam.getDirection().clone().multLocal(0.6f);
Vector3f camLeft = this.cam.getLeft().clone().multLocal(0.4f);
this.walkDirection.set(0, 0, 0);
if (this.left) {
this.walkDirection.addLocal(camLeft);
}
if (this.right) {
this.walkDirection.addLocal(camLeft.negate());
}
if (this.up) {
this.walkDirection.addLocal(camDir);
}
if (this.down) {
this.walkDirection.addLocal(camDir.negate());
}
if (usePhysics) {
this.player3.setWalkDirection(this.walkDirection);
this.cam.setLocation(this.player3.getPhysicsLocation());
}
}
}
I could not install setWireframe, so j3o has its own stuff. But in this example, the LOD does the work.