Hey guys,
I’m new to 3D programming and the JMonkeyEngine, so please do not judge me for the mistakes I surely made
I’m just trying to create a Terrain where a first person character can walk. This is my approach:
[java]package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.BetterCharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
/**
-
test
-
@author normenhansen
*/
public class Main extends SimpleApplication implements ActionListener {private BulletAppState bulletAppState;
private RigidBodyControl terrainControl;
private Node player;
private BetterCharacterControl playerControl;private Vector3f walkDirection = new Vector3f(0.0f, 0.0f, 0.0f);
private boolean left,right,front,back;public static void main(String[] args) {
Main app = new Main();
app.start();
}@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);initTerrain(); initLight(); initPlayer(); initKeys();
}
private void initTerrain() {
Spatial terrain = assetManager.loadModel(“Scenes/Terrain.j3o”);
terrainControl = new RigidBodyControl(0.0f);
terrain.addControl(terrainControl);
bulletAppState.getPhysicsSpace().add(terrainControl);
rootNode.attachChild(terrain);
}private void initPlayer() {
player = new Node(“player”);
player.setLocalTranslation(new Vector3f(50f, 30f, 0.0f));
playerControl = new BetterCharacterControl(1f, -1.8f, 1f);
playerControl.setApplyPhysicsLocal(true);
playerControl.setSpatial(player);
player.addControl(playerControl);
bulletAppState.getPhysicsSpace().add(playerControl);
rootNode.attachChild(player);
}private void initLight() {
AmbientLight ambient = new AmbientLight();
ambient.setColor(ColorRGBA.White.mult(5f));
rootNode.addLight(ambient);
}public void initKeys() {
inputManager.addMapping(“Left”, new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping(“Front”, new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping(“Back”, new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping(“Jump”, new KeyTrigger(KeyInput.KEY_SPACE));inputManager.addListener(this, "Left"); inputManager.addListener(this, "Right"); inputManager.addListener(this, "Front"); inputManager.addListener(this, "Back"); inputManager.addListener(this, "Jump");
}
@Override
public void simpleUpdate(float tpf) {
Vector3f camDir = cam.getDirection();
Vector3f camLeft = cam.getLeft();walkDirection.set(0.0f, 0.0f, 0.0f); if(left) { walkDirection.addLocal(camLeft); } if(right) { walkDirection.addLocal(camLeft.negate()); } if(front) { walkDirection.addLocal(camDir); } if(back) { walkDirection.addLocal(camDir.negate()); } walkDirection.setY(0.0f); playerControl.setWalkDirection(walkDirection); cam.setLocation(player.getLocalTranslation());
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}public void onAction(String name, boolean isPressed, float tpf) {
if(name.equals(“Left”)) {
left = isPressed;
} else if(name.equals(“Right”)) {
right = isPressed;
} else if(name.equals(“Front”)) {
front = isPressed;
} else if(name.equals(“Back”)) {
back = isPressed;
} else if(name.equals(“Jump”)) {
playerControl.jump();
}
}
}
[/java]
It works fine so far, but I’ve got a few problems. First of all, I need to set the speed for the BetterCharacterControl. It’s moving incredebly slow. I could change it by changing this
[java]Vector3f camDir = cam.getDirection();
Vector3f camLeft = cam.getLeft();[/java]
to this:
[java]Vector3f camDir = cam.getDirection().mult(20f);
Vector3f camLeft = cam.getLeft().mult(10f);
[/java]
But after this the physics when walking onto a hill are completely messed up. When moving over a hill, the character flies over the terrain because he’s so fast. Any idea how I could solve this?
Then I’d also like to disable the possibility to look to the feet of the character (I’m simply to lazy to make a model for them :)). How can I tell the camera not to move further when a certain point is reached?
It’d also be great if I could disable moving in the air. How can I find out if the character does not touch the ground?
And at last I’d like to disable the zooming. Is this just done with the stateManager?
As you see, I tried to add jumping (space) but BetterCharacterControl.jump() simply does nothing. Any ideas?
Thanks!
mathiasj