Hi, it’s always me! I finally managed to develop several AppStates for my game (I have 3 one for the menu, one during the game, and one for the game paused), when I pause the game and then I resume everything fine, but if I switch from I play the menu and then back to the game, the camera gets buggy … I don’t know how to explain it, so I’m attaching a video on youtube, (jmonkeyBug camera - YouTube).
I think it’s the fault of the fact that I manually control the rotation of the camera by creating a class for it.
Camera class:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Object;
import com.jme3.input.CameraInput;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseAxisTrigger;
import com.jme3.math.Matrix3f;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
/**
*
* @author gattolfo
*/
public class FpsCam implements AnalogListener, ActionListener{
final private static String[] mappings = new String[]{
CameraInput.FLYCAM_LEFT,
CameraInput.FLYCAM_RIGHT,
CameraInput.FLYCAM_UP,
CameraInput.FLYCAM_DOWN
};
//telecamera che controllo con questa classe
protected Camera cam;
//vettore che inizializza la cam
protected Vector3f initialUpVec;
//velocità di rotazione
protected float rotationSpeed = 1f;
protected boolean invertY = false;
InputManager inputManager;
public boolean canUpdate;
public FpsCam(Camera cam, InputManager inputManager){
this.cam = cam;
initialUpVec = cam.getUp();
System.out.print(cam.getUp());
this.inputManager = inputManager;
canUpdate = true;
registerInput();
}
@Override
public void onAnalog(String name, float value, float tpf) {
if (name.equals(CameraInput.FLYCAM_LEFT)) {
rotateCamera(value, initialUpVec);
} else if (name.equals(CameraInput.FLYCAM_RIGHT)) {
rotateCamera(-value, initialUpVec);
} else if (name.equals(CameraInput.FLYCAM_UP)) {
rotateCamera(-value * (invertY ? -1 : 1), cam.getLeft());
} else if (name.equals(CameraInput.FLYCAM_DOWN)) {
rotateCamera(value * (invertY ? -1 : 1), cam.getLeft());
}
}
@Override
public void onAction(String action, boolean arg1, float arg2) {
}
//INPUT MANAGER
private void registerInput(){
inputManager.addMapping(CameraInput.FLYCAM_LEFT, new MouseAxisTrigger(MouseInput.AXIS_X, true),
new KeyTrigger(KeyInput.KEY_LEFT));
inputManager.addMapping(CameraInput.FLYCAM_RIGHT, new MouseAxisTrigger(MouseInput.AXIS_X, false),
new KeyTrigger(KeyInput.KEY_RIGHT));
inputManager.addMapping(CameraInput.FLYCAM_UP, new MouseAxisTrigger(MouseInput.AXIS_Y, false),
new KeyTrigger(KeyInput.KEY_UP));
inputManager.addMapping(CameraInput.FLYCAM_DOWN, new MouseAxisTrigger(MouseInput.AXIS_Y, true),
new KeyTrigger(KeyInput.KEY_DOWN));
inputManager.addListener(this, mappings);
}
protected void rotateCamera(float value, Vector3f axis) {
if(!canUpdate)
return;
Matrix3f mat = new Matrix3f();
mat.fromAngleNormalAxis(rotationSpeed * value, axis);
Vector3f up = cam.getUp();
Vector3f left = cam.getLeft();
Vector3f dir = cam.getDirection();
mat.mult(up, up);
mat.mult(left, left);
mat.mult(dir, dir);
Quaternion q = new Quaternion();
q.fromAxes(left, up, dir);
q.normalizeLocal();
if(up.getY()<0.3f || up.getY()<0f)
return;
cam.setAxes(q);
//System.out.println(cam.getDirection());
}
//setter e getter
public float getRotationSpeed() {
return rotationSpeed;
}
public void setRotationSpeed(float rotationSpeed) {
this.rotationSpeed = rotationSpeed;
}
public void setLocalPosition(Vector3f pos){
cam.setLocation(pos);
}
public Camera getCam() {
return cam;
}
public Vector3f getDirection() {
return cam.getDirection();
}
Vector3f getLeft() {
return cam.getLeft();
}
}
I wonder if there is a way to reset the camera, I tried to pass camera.clone () so as not to change the main camera, but I don’t think I understand how the cameras work because I still see the main camera