I have this orbit camera test class, but when I move the mouse up, the camera goes down, when I mvoe the mosue to left, the camera goes to right, and vice-versa. How can I change that, in a way that, when you move the mouse to, for example, left, the camera goes to left?
My code:
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingSphere;
import com.jme.input.MouseInput;
import com.jme.input.MouseInputListener;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;
import com.jme.scene.CameraNode;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
public class TestOrbitingCamera extends SimpleGame {
boolean isOrbiting = false;
float camX = 0;
float camY = 0;
protected int camDistance = 10;
private Node pivotNode;
@Override
protected void simpleInitGame() {
// remove existing SimpleGame input handler
input.removeAllFromAttachedHandlers();
Sphere center = new Sphere("center", 15, 15, 3);
center.setModelBound(new BoundingSphere());
center.updateModelBound();
rootNode.attachChild(center);
Sphere s1 = new Sphere("center", 15, 15, 1);
s1.setLocalTranslation(-5, 4, 0);
s1.setModelBound(new BoundingSphere());
s1.updateModelBound();
rootNode.attachChild(s1);
Sphere s2 = new Sphere("center", 15, 15, 1);
s2.setLocalTranslation(7, 5, 5);
s2.setModelBound(new BoundingSphere());
s2.updateModelBound();
rootNode.attachChild(s2);
// to have a reference
Box b = new Box("floor", new Vector3f(-5, -10, -5), 10, 0.3f, 10);
b.setModelBound(new BoundingBox());
b.updateModelBound();
rootNode.attachChild(b);
MouseInput.get().addListener(new MouseInputListener() {
@Override
public void onWheel(int wheelDelta, int x, int y) {
}
@Override
public void onMove(int xDelta, int yDelta, int newX, int newY) {
if (isOrbiting) {
float speed = 100f;
camX += xDelta*tpf*speed;
camY += yDelta*tpf*speed;
// rotate the pivot node
pivotNode.getLocalRotation().fromAngles(camY*FastMath.DEG_TO_RAD, camX*FastMath.DEG_TO_RAD, 0);
}
}
@Override
public void onButton(int button, boolean pressed, int x, int y) {
if (button == 0 && pressed) {
isOrbiting = true;
}
if (button == 0 && pressed == false) {
isOrbiting = false;
}
}
});
// create a pivot Node which will be rotated by the mouse input
pivotNode = new Node("pivot");
rootNode.attachChild(pivotNode);
// create a camera node which is attached to the pivot node and translated by the viewing distance
CameraNode camNode = new CameraNode("camNode", cam);
camNode.setLocalTranslation(0, 0, -30);
pivotNode.attachChild(camNode);
}
public static void main(String[] args) {
SimpleGame game = new TestOrbitingCamera();
game.setConfigShowMode(ConfigShowMode.AlwaysShow);
game.start();
}
}