Dragging mouse for camera orbit? (with exampel)


I'm working on a school project and I'm new to JME.

I'm trying to achieve this type of camera handling using the mouse:


(click on the picture to see the example in runtime)

I've tried to port the code from actionscript to java but it did not work and the camera went bananas :stuck_out_tongue:

Would appreciate if anyone could help me out. Also tried chase camera but i dont really know how to set it up properly and size my objects i want to rotate around is static, its not what i need.

1 Like

I'm using a pivot and CameraNode to rotate the camera.

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;
   protected void simpleInitGame() {
      // remove existing SimpleGame input handler
      Sphere center = new Sphere("center", 15, 15, 3);
      center.setModelBound(new BoundingSphere());
      Sphere s1 = new Sphere("center", 15, 15, 1);
      s1.setLocalTranslation(-5, 4, 0);
      s1.setModelBound(new BoundingSphere());
      Sphere s2 = new Sphere("center", 15, 15, 1);
      s2.setLocalTranslation(7, 5, 5);
      s2.setModelBound(new BoundingSphere());
      // to have a reference
      Box b = new Box("floor", new Vector3f(-5, -10, -5), 10, 0.3f, 10);
      b.setModelBound(new BoundingBox());
      MouseInput.get().addListener(new MouseInputListener() {
         public void onWheel(int wheelDelta, int x, int y) {
         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);
         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");
      // 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);
   public static void main(String[] args) {
      SimpleGame game = new TestOrbitingCamera();

1 Like

Just as a small improvement, you can implement the onWheel method with something like:

           public void onWheel(int wheelDelta, int x, int y) {
                if(wheelDelta > 0) {
                else {

You can use aproxFactor = 0.9f and moveAwayFactor = 1.1f to try it out :D
1 Like

Thanks alot you guys!

Exactly what i was looking for?  :mrgreen:

YOU ROCK! :stuck_out_tongue:


is it possible to zoom with the mousewheel and to move the whole scene with the right mouse button?


Dca said:

is it possible to zoom with the mousewheel and to move the whole scene with the right mouse button?

Isn't that what the code by TaviO allready does?  :)