Rotating helicopter Spatial

Hi guys, I’m trying to rotate a Spatial Node using the mouse, meaning when the mouse moves left the spatial rotates left if mouse moves right the Spatial will rotate right etc . To accomplish this i tried using the analog listener by mapping the mouse x and y axis.



Here is the code for analog listener, When i move the mouse left, the spatial does rotate but then it goes back to it original place.

I’m trying to achieve rotation like this. http://www.youtube.com/watch?v=FVM8yC5D-uE i’m using a helicopter as well and could not find any test helicopter examples anywhere with source code :frowning:

Code:
private AnalogListener analogList = new AnalogListener() {
    public void onAnalog(String name, float value, float tpf) {
        Quaternion q2 = heliNode.getLocalRotation();
        if (name.equals("left")) {
           
            q2.fromAngleAxis(30 * value,Vector3f.UNIT_X);
            heli.setLocalRotation(q2);
         
        }else if(name.equals("right")){
           
            q2.fromAngleAxis(-30 * value,Vector3f.UNIT_X);
            heli.setLocalRotation(q2);
        
        }else if(name.equals("up")){
           
            q2.fromAngleAxis(30 * value,Vector3f.UNIT_Y);
            heli.setLocalRotation(q2);
         
        }else if(name.equals("down")){
        
            q2.fromAngleAxis(-30 * value,Vector3f.UNIT_Y);
            heli.setLocalRotation(q2);
        }
      
        System.out.println("Name: " + name + " Value: " + value + " tpf: " + tpf);
    }
};</div>

You take the current rotation quaternion from the heli and then you overwrite it… just have a global float variable “rot”, adjust it in your analoglistener and then do this q.fromAngleAxis(…)



[java]

private AnalogListener analogList = new AnalogListener() {



public void onAnalog(String name, float value, float tpf) {

if (name.equals(“left”)) {

rot += tpf;

heli.setLocalRotation(new Quaternion (…));

}else if(name.equals(“right”)){

rot -= tpf;

heli.setLocalRotation(new Quaternion (…));



}else if(name.equals(“up”)){





}

};

[/java]



OR you could get the current rotation, create the new quaternion and then heli.setLocalRotation(q1.multLocal(q2)). You still ned t figure out if q1.mult(q2) or q2.mult(q1). Quaternion multiplication is not commutative so just check both.

Hi Dodikles, thanks for the reply, Question is there any mouse listener that returns the x,y coordinates of the cursor when its moved? I’m guessing that would of been easier, the Spatial should only move when the mouse moves.

Maybe there is method inside the inputManager, look through it. But I still don’t see the problem of tracking 2 variables for x and y, updating them in the analogListener and applying as rotation.

Thanks Dodikles your method works :slight_smile: i’m keeping track of two fields :slight_smile: