Sphere mouse drag rotation

Hi all

i’m newbie with jme3 i’d like to create a simple sphere that rotates over center sphere point.

I tried to use pivot and camera but no result.

Anyone can give me a detailed example with a mouse listener implemented?




1 Like

My purpose is to drag mouse pointer over an object and let it rotate so i’d like to add a mouse input listener or sort of to my simpleapplication but samples I found show me how to do it with jme previous version.

I 've already done hello node sample and it works.I need a j,e3 mouse listener for rotating sphere on a pivot poit


so you want to click with your mouse on an object and let it be your pivot point? Is that right?

If so, you need to do collision checking with ray casting etc. to find the corresponding object and determine its center point. Just register your inputListener with a mouse click and if the user clicks, you send a ray for the collision.

To specify such a rotating camera you could look into the test.bullet.TestHoveringTank example which I remember has such a “sphere drag rotation”.

Good luck!

not exactly.The matter is more easy.

I’d like to rotate on a pivot poit centered to sphere center ,dragging mouse.


you could use the chase camera which supplies that, I suppose.

[java]ChaseCamera chaseCam = new ChaseCamera(cam, inputManager);



I got a compile error

cannot find symbol symbol: constructor ChaseCamera(com.jme3.renderer.Camera,com.jme3.input.InputManager)

You are using alpha 3, aren’t you?

2 options for you :

ok now with nightly builds.

(I’ve done mostly the same implementing RawInputListener).

Anyway using your solution ,dragging down the mouse the sphere goes up. And it does not look to follow the mouse pointer direction rotation.

What should I do?


nehon said:
You are using alpha 3, aren't you?
2 options for you :
- update to nightly builds doc here -> https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:jmonkeyplatform:update_center
- use the old constructor
ChaseCamera chaseCam = new ChaseCamera(cam, sphere, inputManager);

My purpose is to create a sphere rotation in roll PITCH YAW direction using mouse gragging.Your solution works just on y axis , on x axis rotate in opposite way og mouse drag.

you can invert the rotation :



nehon said:
you can invert the rotation :

it is not what i need.
I 'd like to rotate sphere on axis normal to mouse direction variation.
nehon said:
you can invert the rotation :

i d like to do something like this applet shows: select the combo 'rotate sphere' and drag mouse on sphere

looks at http://torus.math.uiuc.edu/jms/java/dragsphere/

Can you please show your own effort until now? From your behavior it seems you try to repeat your request until someone posts the code for you. Did you do the tutorials?

I show you what i have done till now,The sphere does not follows properly draggin mouse movement;I modified HelloInput adding

ChaseCamera chaseCam = new ChaseCamera(cam, inputManager);



you suggest me.


  • To change this template, choose Tools | Templates
  • and open the template in the editor.


    package it.game;

    import com.jme3.app.SimpleApplication;

    import com.jme3.bounding.BoundingSphere;

    import com.jme3.input.ChaseCamera;

    import com.jme3.input.RawInputListener;

    import com.jme3.input.event.JoyAxisEvent;

    import com.jme3.input.event.JoyButtonEvent;

    import com.jme3.input.event.KeyInputEvent;

    import com.jme3.input.event.MouseButtonEvent;

    import com.jme3.input.event.MouseMotionEvent;

    import com.jme3.material.Material;

    import com.jme3.math.Vector3f;

    import com.jme3.scene.Geometry;

    import com.jme3.math.ColorRGBA;

    import com.jme3.input.KeyInput;

    import com.jme3.input.MouseInput;

    import com.jme3.input.controls.KeyTrigger;

    import com.jme3.input.controls.MouseButtonTrigger;

    import com.jme3.light.DirectionalLight;

    import com.jme3.math.FastMath;

    import com.jme3.math.Quaternion;

    import com.jme3.scene.shape.Sphere;

    import com.jme3.system.AppSettings;

    import com.jme3.util.TangentBinormalGenerator;

    * Sample 5 - how to map keys and mousebuttons to actions /

    public class HelloInput extends SimpleApplication {

    public static void main(String[] args) {

    HelloInput app = new HelloInput();



    protected Geometry shiny_rock;

    Boolean buttonPressed=false;

    public static final Quaternion PITCH090 = new Quaternion().fromAngleAxis(FastMath.PI/2, new Vector3f(1,0,0));


    public void simpleInitApp() {


    AppSettings settings = new AppSettings(true);




    Sphere rock = new Sphere(32,32, 2f);


    rock.setBound(new BoundingSphere());

    shiny_rock = new Geometry(“Shiny rock”, rock);

    rock.setTextureMode(Sphere.TextureMode.Original); // better quality on spheres

    TangentBinormalGenerator.generate(rock); // for lighting effect

    Material mat_lit = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);

    mat_lit.setTexture(“m_DiffuseMap”, assetManager.loadTexture(“Textures/Terrain/Pond/Pond.png”));

    mat_lit.setTexture(“m_NormalMap”, assetManager.loadTexture(“Textures/Terrain/Pond/Pond_normal.png”));

    mat_lit.setFloat(“m_Shininess”, 1f); // [0,128]


    DirectionalLight sun = new DirectionalLight();

    sun.setDirection(new Vector3f(1,1,-1));




    ChaseCamera chaseCam = new ChaseCamera(cam, inputManager);



    // initKeys(); // load my custom keybinding


    * Custom Keybinding: Map named actions to inputs. /

    private void initKeys() {

    // You can map one or several inputs to one named action

    inputManager.addMapping(“Pause”, new KeyTrigger(KeyInput.KEY_P));

    inputManager.addMapping(“Left”, new KeyTrigger(KeyInput.KEY_J));

    inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_K));

    inputManager.addMapping(“Rotate”, new KeyTrigger(KeyInput.KEY_SPACE),

    new MouseButtonTrigger(MouseInput.BUTTON_LEFT));



    private RawInputListener rawInputListener=new RawInputListener() {

    public void onJoyAxisEvent(JoyAxisEvent evt) {


    public void onJoyButtonEvent(JoyButtonEvent evt) {


    public void onMouseMotionEvent(MouseMotionEvent evt) {

    if (buttonPressed)





    public void onMouseButtonEvent(MouseButtonEvent evt) {



    public void onKeyEvent(KeyInputEvent evt) {



    public void beginInput() {


    public void endInput() {




Ahhh, so you want to rotate the sphere on its own and not the camera at all…

Just check for x and y changes of the mouse movement in your InputListener and apply a bit of rotation to the sphere along the corresponding axes. Where’s the problem?

looks at Spherical Geometry

it’s an applet that does what i need.There is a combo box where to select ‘Rotate sphere’.That’s the purpose of my previous code.

But it does not work properly,After a while i drag mouse the sphere hangs rotation.

Please help

Well, ok. Let’s try some pseudo-code

[java]int dx, int dy;

dx,dy = getRelativeMouseMovement();

// rotate a “dx”-bit around Y-axis

sphere.rotate(dx/PI, Vector3_UNIT_Y);

// rotate a “dy”-bit around X-axis

sphere.rotate(dy/PI, Vector3_UNIT_X);


You take the updated mouse movement and rotate along the desired axises…

Of course this code doesn’t compile. Could be that you need a quaternion for the rotation. Don’t remember.

Similar to what I have done:


where evt is com.jme3.input.MouseMotionEvent.

In this way x-y-z axis move with sphere

I want to rotate sphere on world scene axis.

The solution is pretty close but I don’t know how to define code for it.

Erm, still don’t get your problem exactly. Maybe you want a local rotation but get a global one?

Your x-y-z-axis won’t move (at least not of the world). Maybe you just have to translate the sphere to sit in the center of your world (means global (0,0,0)) and then do the rotation?