Relatively new here, so please be patient…
I have trouble with the BetterCharacterControl. I have checked out the latest version from SVN:
[java]
$ svn info
Path: .
URL: http://jmonkeyengine.googlecode.com/svn/trunk
Repository Root: http://jmonkeyengine.googlecode.com/svn
Repository UUID: 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
Revision: 10587
Node Kind: directory
Schedule: normal
Last Changed Author: Kaelthas_Spellsinger@o2.pl
Last Changed Rev: 10587
Last Changed Date: 2013-05-01 09:44:05 +1200 (Wed, 01 May 2013)[/java]
This is my code:
[java]
package com.gravitygame;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.BetterCharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
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.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
/**
-
test
-
@author geekdenz
*/
public class Main extends SimpleApplication implements ActionListener {private BulletAppState bulletAppState;
private Spatial gameLevel;
private RigidBodyControl landscape;
private BetterCharacterControl player;
private CapsuleCollisionShape smallerCapsule;
private CapsuleCollisionShape capsuleShape;
private RigidBodyControl otherForce;
private Vector3f grav;private Node playerNode;
private CameraNode camNode;private SphereCollisionShape playerSphere;
private boolean left = false, right = false, up = false, down = false;
private Vector3f walkDirection = new Vector3f();
private Vector3f gravity = new Vector3f();
private Node physicsNode;
private CollisionShape sceneShape;
private Quaternion rot = new Quaternion();
private Vector3f dir = new Vector3f(-9.81f,0f,0f);
private Vector3f upVector = new Vector3f(0, 1, 0);
private Node wholeScene;
public static void main(String[] args) {
Main app = new Main();
app.start();
}private void setUpLight() {
// We add light so we see the scene
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(1.3f));
rootNode.addLight(al);DirectionalLight dl = new DirectionalLight(); dl.setColor(ColorRGBA.White); //dl.setDirection(new Vector3f(-2.8f, -2.8f, 0f).normalizeLocal()); dl.setDirection(new Vector3f(-1f, -1f, -2f).normalizeLocal()); rootNode.addLight(dl); DirectionalLight dl1 = new DirectionalLight(); dl1.setColor(ColorRGBA.White); //dl.setDirection(new Vector3f(-2.8f, -2.8f, 0f).normalizeLocal()); dl1.setDirection(new Vector3f(-1f, -1f, 2f).normalizeLocal()); rootNode.addLight(dl1); DirectionalLight dl2 = new DirectionalLight(); dl2.setColor(ColorRGBA.White); //dl.setDirection(new Vector3f(-2.8f, -2.8f, 0f).normalizeLocal()); dl2.setDirection(new Vector3f(1f, 0f, 2f).normalizeLocal()); rootNode.addLight(dl2);
}
private void setUpKeys() {
mouseInput.setCursorVisible(false);
inputManager.setCursorVisible(true);
inputManager.addMapping(“Left”, new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping(“Up”, new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping(“Down”, new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping(“Jump”, new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping(“j”, new KeyTrigger(KeyInput.KEY_J));
inputManager.addMapping(“h”, new KeyTrigger(KeyInput.KEY_H));
inputManager.addMapping(“l”, new KeyTrigger(KeyInput.KEY_L));
inputManager.addMapping(“k”, new KeyTrigger(KeyInput.KEY_K));
inputManager.addListener(this, “Left”);
inputManager.addListener(this, “Right”);
inputManager.addListener(this, “Up”);
inputManager.addListener(this, “Down”);
inputManager.addListener(this, “Jump”);
inputManager.addListener(this, “gLeft”);
inputManager.addListener(this, “gRight”);
inputManager.addListener(this, “gUp”);
inputManager.addListener(this, “gDown”);
inputManager.addListener(this, “j”);
inputManager.addListener(this, “h”);
inputManager.addListener(this, “l”);
inputManager.addListener(this, “k”);
}/**
- These are our custom actions triggered by key presses. We do not walk
- yet, we just keep track of the direction the user pressed.
*/
public void onAction(String binding, boolean value, float tpf) {
if (binding.equals(“Left”)) {
left = value;
} else if (binding.equals(“Right”)) {
right = value;
} else if (binding.equals(“Up”)) {
up = value;
} else if (binding.equals(“Down”)) {
down = value;
} else if (binding.equals(“Jump”)) {
player.jump();
} else if (binding.equals(“j”)) {
if (value) {
} else {
}
} else if (binding.equals(“h”) && value) {
} else if (binding.equals(“l”) && value) {
} else if (binding.equals(“k”) && value) {
}
}
@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);setUpKeys(); setUpLight(); initScene(); initPlayer(); initPhysics();
}
@Override
public void simpleUpdate(float tpf) {
Vector3f camDir = cam.getDirection().clone();
Vector3f camLeft = cam.getLeft().clone();
walkDirection.set(0, 0, 0);
if (left) {
walkDirection.addLocal(camLeft);
}
if (right) {
walkDirection.addLocal(camLeft.negate());
}
if (up) {
walkDirection.addLocal(camDir);
}
if (down) {
walkDirection.addLocal(camDir.negate());
}
walkDirection.multLocal(tpf*1000);
walkDirection.setY(0);
player.setWalkDirection(walkDirection);
player.setViewDirection(camDir);
}@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}private void initScene() {
gameLevel = assetManager.loadModel(“Models/box3d/box3d.j3o”);
gameLevel.setLocalTranslation(0, -5.2f, 0);
gameLevel.setLocalScale(2);
rootNode.attachChild(gameLevel);
sceneShape = CollisionShapeFactory.createMeshShape((Node) gameLevel);
landscape = new RigidBodyControl(sceneShape, 0);
}private void initPlayer() {
player = new BetterCharacterControl(0.5f, 1.9f, 80f);
bulletAppState.getPhysicsSpace().add(player);
camNode = new CameraNode(“camera”, cam);
camNode.addControl(player);
rootNode.attachChild(camNode);
}private void initPhysics() {
bulletAppState.getPhysicsSpace().add(landscape);
bulletAppState.getPhysicsSpace().enableDebug(assetManager);
//bulletAppState.getPhysicsSpace().setGravity(new Vector3f(0f,0f,0f));
}
}
[/java]
The problem is that player.setViewDirection(camDir) seems to screw up the whole thing, but without it, when I move the mouse, nothing happens.
Please help!
I know the latest version might still have bugs, but that is also why I’m posting here.