So, can you help me making my camera to be ok ? Here is my code (The game main class) :
[java]
package fr.jme3mmo.client;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.ZipLocator;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
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.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.system.AppSettings;
public class JME3MMO extends SimpleApplication
implements ActionListener {
private Spatial sceneModel;
private BulletAppState bulletAppState;
private RigidBodyControl landscape;
private CharacterControl player;
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false, sneak = false, sprint = false;
private Vector3f camDir = new Vector3f();
private Vector3f camLeft = new Vector3f();
public static void main(String[] args) {
JME3MMO app = new JME3MMO();
app.setShowSettings(false);
app.setDisplayFps(true);
app.setDisplayStatView(false);
AppSettings settings = new AppSettings(true);
settings.put(“Width”, 1600);
settings.put(“Height”, 900);
settings.put(“BitsPerPixel”, 24);
settings.put(“Fullscreen”, false);
settings.put(“Title”, “JME3MMORPG - FPS by Yuri6037 (Early Alpha 0.0.1)”);
settings.put(“DisableJoysticks”, true);
settings.put(“VSync”, false);
app.setSettings(settings);
app.start();
}
public void simpleInitApp() {
/** Set up Physics */
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
//flyby camera for rotation, while positioning is handled by physics
viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
flyCam.setMoveSpeed(100);
setUpKeys();
setUpLight();
// load the scene from the zip file and adjust its size.
//assetManager.registerLocator("town.zip", ZipLocator.class);
sceneModel = assetManager.loadModel("Scenes/TestScene.j3o");
sceneModel.setLocalScale(2f);
// We set up collision detection for the scene by creating a compound collision shape and a static RigidBodyControl with mass zero.
CollisionShape sceneShape = CollisionShapeFactory.createMeshShape((Node) sceneModel);
landscape = new RigidBodyControl(sceneShape, 0);
sceneModel.addControl(landscape);
// We set up collision detection for the player by creating a capsule collision shape and a CharacterControl. The CharacterControl offers extra settings for size, stepheight, jumping, falling, and gravity. We also put the player in its starting position.
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setJumpSpeed(32);
player.setFallSpeed(256);
player.setGravity(128);
player.setPhysicsLocation(new Vector3f(0, 10, 0));
// We attach the scene and the player to the rootnode and the physics space, to make them appear in the game world.
rootNode.attachChild(sceneModel);
bulletAppState.getPhysicsSpace().add(landscape);
bulletAppState.getPhysicsSpace().add(player);
}
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, -2.8f).normalizeLocal());
rootNode.addLight(dl);
}
// The key mapping init. Later this init part will cal a BrickBrokenDataFile to load saved player (user) settings.
private void setUpKeys() {
inputManager.addMapping(“Left”, new KeyTrigger(KeyInput.KEY_Q));
inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping(“Up”, new KeyTrigger(KeyInput.KEY_Z));
inputManager.addMapping(“Down”, new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping(“Jump”, new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping(“Sneak”, new KeyTrigger(KeyInput.KEY_LCONTROL));
inputManager.addMapping(“Sprint”, new KeyTrigger(KeyInput.KEY_LSHIFT));
inputManager.addListener(this, “Left”);
inputManager.addListener(this, “Right”);
inputManager.addListener(this, “Up”);
inputManager.addListener(this, “Down”);
inputManager.addListener(this, “Jump”);
inputManager.addListener(this, “Sneak”);
inputManager.addListener(this, “Sprint”);
}
// Handling key pressed
public void onAction(String binding, boolean isPressed, float tpf) {
if (binding.equals(“Left”)) {
left = isPressed;
} else if (binding.equals(“Right”)) {
right= isPressed;
} else if (binding.equals(“Up”)) {
up = isPressed;
} else if (binding.equals(“Down”)) {
down = isPressed;
} else if (binding.equals(“Jump”)) {
if (isPressed) { player.jump(); }
} else if (binding.equals(“Sneak”)){
sneak = isPressed;
} else if (binding.equals(“Sprint”) && up){
sprint = isPressed;
}
}
public void simpleUpdate(float tpf) {
camDir.set(cam.getDirection()).multLocal(0.2f);
camLeft.set(cam.getLeft()).multLocal(0.15f);
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());
}
if (sneak){
Vector3f var = player.getPhysicsLocation();
var.y /= 2f;
cam.setLocation(var);
Vector3f vec = walkDirection;
vec.setX(vec.getX() / 3.5F);
vec.setY(vec.getY() / 3.5F);
vec.setZ(vec.getZ() / 3.5F);
player.setWalkDirection(vec);
} else {
player.setWalkDirection(walkDirection);
cam.setLocation(player.getPhysicsLocation());
}
if (sprint){
Vector3f vec = walkDirection;
vec.setX(vec.getX() * 3.5F);
vec.setY(vec.getY() * 3.5F);
vec.setZ(vec.getZ() * 3.5F);
player.setWalkDirection(vec);
}
float y = player.getPhysicsLocation().y;
if (y <= -128F){
player.setPhysicsLocation(new Vector3f(0, 10, 0));
}
}
}
[/java]
Thanks by advance,
Yuri6037