Hello
I need help figuring out how to make the camera rotate around a quad. The code works but the camera rotates it's own axis (Having problems with the up vector I think).
I want to do it so that when you press left (or right) it circles around the quad keeping it's Z value and without tilting/twisting.
I think the problem lies with the camera lookat operation but im not quite sure. I thought I might have to compute left,up,dir vectors too to get that result but I have no idea how to do that.
This is my code so far:
Left key action
import com.jme.input.action.InputActionEvent;
import com.jme.input.action.KeyInputAction;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
public class kleftaction extends KeyInputAction {
private float campos; //campos is the camera position around the quad, using 2*pi as a complete turn so campos is from 0 to 6.28
private float cercania; //cercania is how close to the board the camera should be
private Camera cam;
private Vector3f gpos; //this is the quad position, used 0,0,0 for testing
private keyboardcam kc; //InputHandler, holds all the actions
public kleftaction(Camera cam, Vector3f gpos, keyboardcam kc) {
this.cam = cam;
this.gpos = gpos;
this.kc = kc;
}
public void performAction(InputActionEvent evt) {
Vector3f cpos = cam.getLocation(); //placeholder for the new camera position
campos = kc.getCampos() + .001f; //moves around the circle
cercania = kc.getCercania();
if( campos > 6.28f ) campos = 0; //restart to 0 to avoid computing big numbers
float cx = (float) (cercania*Math.cos(campos)); //calculates Camera X within the new capos range
float cy = (float) (cercania*Math.sin(campos)); //calculates Camera Y within the new capos range
cpos.setX(cx);
cpos.setY(cy);
// Z value of the camera should stay the same.
kc.setCampos(campos); //returns the new value of campos to the InputHandler so that other actions can move from the same position.
cam.setLocation(cpos);
cam.lookAt(gpos, new Vector3f(0, 1, 0)); //This is where I think it doesn't work
cam.update();
}
}
Input handler in case you need it for testing
import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
public class keyboardcam extends InputHandler {
private float campos = 3.14f;
private float cercania = 50f;
private Vector3f gpos;
private Camera cam;
private kleftaction left;
public keyboardcam(Vector3f gpos, Camera cam) {
this.gpos = gpos;
this.cam = cam;
KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();
keyboard.set("cleft", KeyInput.KEY_LEFT);
left = new kleftaction(cam, gpos, this);
addAction(left, "cleft", true);
}
public float getCampos() {
return campos;
}
public void setCampos(float campos) {
this.campos = campos;
}
public float getCercania() {
return cercania;
}
public void setCercania(float cercania) {
this.cercania = cercania;
}
}
