Gimble Lock I assume

I have a problem I’m tring to change the rotation of an odejava body by using a joystick. The problem is I think I’m getting Gimble Lock.

Here is my code:



private Matrix3f turn;
private Matrix3f pitch;
private Matrix3f roll;

/* 
turn, pitch, roll are call in order 
rot = the rotation being passed from the jostick to body
*/
       void turn(float rot){
         turn.setZero();
         turn.rotY(rot);
      }
   
       void pitch(float rot){
         pitch.setZero();
         pitch.rotX(rot);
      
      }
   
       void roll(float rot){
         roll.setZero();
         roll.rotZ(rot);
         rotate(); // turn, pitch, roll done now call rotate() to apply them
      }
      
       private void rotate(){
      
         Matrix3f m = new Matrix3f();
         m.setZero();
         m.mul(turn, pitch);   
         m.mul(m, roll);
         m.mul(m, shipBody.getRotation());
         shipBody.setRotation(m); 

      }

This code works if I only use 1 axis but as soon as I add in the 2 other axis it gets totaly confused and rotates all wrong. I’ve been doing a bit of reading into gimble lock but havnt found a solution yet. Does somone have a method that will fix this problem.

Read up on 3d rotation, Quaternions and rotation matrices. You cannot simply set those values in the matrix. You have to create rotation matrices (e.g. from axis and angle) and multiply them. Additionally the order of multiplication matters - you would have to decide what you really want to do, as simply multiplying absolute angles won’t achieve something intuitive, either. Or do you already use deltas?

I have done a lot of reading into quaternion and matrix rotation but I cant work it out. I’ve tried 100 different things but they all come back to the fact that: I have a euler vector, created from the joystick and I dont know of a way that I can convert that into either quaternions or matrix rotatrion without creating gimble lock.

This is what I do to get a euler angle to a quaternion:



       private Quat4f createQuaternionFromEuler(float angleX, float angleY, float angleZ )
      {
      //simply call createQuaternionFromAxisAndAngle for each axis
      //and multiply the results
         Quat4f qx = createQuaternionFromAxisAndAngle(
            1f,0f,0f, angleX );
         Quat4f qy = createQuaternionFromAxisAndAngle(
            0f,1f,0f, angleY );
         Quat4f qz = createQuaternionFromAxisAndAngle(
            0f,0f,1f, angleZ );
      
      //qx = qx * qy
         qx.mul( qy );
      
      //qx = qx * qz
         qx.mul( qz );
      
         return qx;
      }
      
       public final Quat4f createQuaternionFromAxisAndAngle(
       float x, float y, float z, float angle )
      {
         float sin_a = (float) Math.sin( angle * 0.5f );
         float cos_a = (float) Math.cos( angle * 0.5f );
      
         x = (float) (x * sin_a);
         y = (float) (y * sin_a);
         z = (float) (z * sin_a);
         float w = (float) cos_a;
         
         return new Quat4f(x,y,z,w);
      }

I then multiply the 3 quaternion created from eluer angles x, y and z and I just get gimble lock.