title: Problems with Object Rotation
body: <p>Here is the video of the problems: 
  <a href="https://youtu.be/YlAwGUADP7c" rel="nofollow noreferrer">https://youtu.be/YlAwGUADP7c</a></p>
  
  <p>Here are the problems with my rotation: </p>
  
  <ol>
  <li>yaw   - when upVector.z/lengthUpVector == -1 (which is top facing forward), not rotation around z but rotation around y occurs </li>
  <li>yaw   - when upVector.x/lengthUpVector == 1 (which is top facing rightward), not rotation around x but rotation around y occurs </li>
  <li>pitch - when upVector.x/lengthUpVector == 1, not rotation around y but rotation around x occurs </li>
  <li>roll  - works</li>
  </ol>
  
  <p>In a similar post: <a href="https://stackoverflow.com/questions/15159037/opengl-object-local-rotation">OpenGL - Object local rotation</a>
  ..an other user found it to be a problem that "roll operation works like it should but if i turn it to face for example (1,0,0) it behaves like pitch", whereas I want my rotations to change like this. </p>
  
  <p>Additional Information: </p>
  
  <ol>
  <li>looking at the back adding to yaw rotates from left to right </li>
  <li>looking at the back adding to pitch rotates from up to down </li>
  <li>looking at the right adding to roll rotates from down to up </li>
  </ol>
  
  <p>I tried manually to make the rotation change relative to the direction the object was facing, but it only made the object rotate out of control in all three axis when just the yaw was attempted to be rotated with the below method (yaw is rotation around y, pitch is rotation around x, roll is rotation around z):  </p>
  
  <pre><code>    public void increaseRelativeRot(float dYaw, float dPitch, float dRoll) {
      //
      float changeInYaw = 0; 
      float changeInPitch = 0; 
      float changeInRoll = 0;
      Vector3f up = getUpVector(); 
      Vector3f front = getFrontVector();
      float lengthUp = Maths.getLength(up); 
      float lengthFront = Maths.getLength(front);
      changeInYaw     += dYaw     * up.y/lengthUp;
      //changeInYaw     += dPitch   * up.x/lengthUp; 
      //changeInYaw     += dRoll    * front.y/lengthFront; 
      changeInPitch   += dYaw     * up.x/lengthUp; 
      //changeInPitch   += dPitch   * up.x/lengthUp;
      //changeInPitch   += -dRoll   * front.x/lengthFront;
      changeInRoll    += dYaw     * up.z/lengthUp;
      //changeInRoll    += -dPitch  * front.x/lengthFront; 
      changeInRoll    += dRoll; 
      float yaw = this.getYaw();
      float pitch = this.getPitch(); 
      float roll = this.getRoll(); 
      yaw += changeInYaw;
      pitch += changeInPitch;
      roll += changeInRoll;
      //System.out.println("yaw: " + yaw); 
      //System.out.println("pitch: " + pitch); 
      //System.out.println("roll: " + roll); 
      //System.out.println();
      this.setYaw(yaw);
      this.setPitch(pitch);
      this.setRoll(roll);
      this.adjustRelToMaximumAngles();
      }
  </code></pre>
  
  <p>But I can't actually fix my problems I listed by doing the previous because I believe this is a problem that involves the way the transformation matrix affects the object I want to rotate; in other words, there is not a problem with the above, it should work I expect, but it does not because there is a problem with the transformation matrix calculations. </p>
  
  <p>My rotation matrix is exactly based off of the following: <a href="https://www.youtube.com/watch?v=50Y9u7K0PZo&amp;index=8&amp;list=PLRIWtICgwaX0u7Rf9zkZhLoLuZVfUksDP" rel="nofollow noreferrer">https://www.youtube.com/watch?v=50Y9u7K0PZo&amp;index=8&amp;list=PLRIWtICgwaX0u7Rf9zkZhLoLuZVfUksDP</a> </p>
  
  <p>I create a transformation matrix like so: </p>
  
  <pre><code>    public static Matrix4f createTransformationMatrix(Vector3f translation, float yaw, float pitch,
                  float roll, float scale) {
          Matrix4f matrix = new Matrix4f();
          matrix.setIdentity();
          Matrix4f.translate(translation, matrix, matrix);
          matrix = rotate(matrix, (float) Math.toRadians(yaw), new Vector3f(0,1,0)); //Matrix4f.rotate((float) Math.toRadians(yaw), new Vector3f(0,1,0), matrix, matrix); 
          matrix = rotate(matrix, (float) Math.toRadians(pitch), new Vector3f(1,0,0)); //Matrix4f.rotate((float) Math.toRadians(pitch), new Vector3f(1,0,0), matrix, matrix);
          matrix = rotate(matrix, (float) Math.toRadians(roll), new Vector3f(0,0,1)); //Matrix4f.rotate((float) Math.toRadians(roll), new Vector3f(0,0,1), matrix, matrix);
          Matrix4f.scale(new Vector3f(scale,scale,scale), matrix, matrix);
          return matrix;
      }
  </code></pre>
  
  <p>Here is the code for rotating a matrix (this is where the problem is): </p>
  
  <pre><code>    public static Matrix4f rotate(Matrix4f m, float angle, Vector3f axis) {
         return rotate(m, angle, axis, m);
      }
  
      public static Matrix4f rotate(Matrix4f m, float angle, Vector3f axis, Matrix4f dest) {
         return rotate(angle, axis, m, dest);
      }
  
      public static Matrix4f rotate(float angle, Vector3f axis, Matrix4f src, Matrix4f dest) {
      //Source at: https://github.com/LWJGL/lwjgl/blob/master/src/java/org/lwjgl/util/vector/Matrix4f.java
      if (dest == null) {dest = new Matrix4f();} 
      float c = (float) Math.cos(angle);
      float s = (float) Math.sin(angle);
      float oneminusc = 1.0f - c;
      float xy = axis.x*axis.y;
      float yz = axis.y*axis.z;
      float xz = axis.x*axis.z;
      float xs = axis.x*s;
      float ys = axis.y*s;
      float zs = axis.z*s;
  
      float f00 = axis.x*axis.x*oneminusc+c;
      float f01 = xy*oneminusc+zs;
      float f02 = xz*oneminusc-ys;
      // n[3] not used
      float f10 = xy*oneminusc-zs;
      float f11 = axis.y*axis.y*oneminusc+c;
      float f12 = yz*oneminusc+xs;
      // n[7] not used
      float f20 = xz*oneminusc+ys;
      float f21 = yz*oneminusc-xs;
      float f22 = axis.z*axis.z*oneminusc+c;
  
      float t00 = src.m00 * f00 + src.m10 * f01 + src.m20 * f02;
      float t01 = src.m01 * f00 + src.m11 * f01 + src.m21 * f02;
      float t02 = src.m02 * f00 + src.m12 * f01 + src.m22 * f02;
      float t03 = src.m03 * f00 + src.m13 * f01 + src.m23 * f02;
      float t10 = src.m00 * f10 + src.m10 * f11 + src.m20 * f12;
      float t11 = src.m01 * f10 + src.m11 * f11 + src.m21 * f12;
      float t12 = src.m02 * f10 + src.m12 * f11 + src.m22 * f12;
      float t13 = src.m03 * f10 + src.m13 * f11 + src.m23 * f12;
      dest.m20 = src.m00 * f20 + src.m10 * f21 + src.m20 * f22;
      dest.m21 = src.m01 * f20 + src.m11 * f21 + src.m21 * f22;
      dest.m22 = src.m02 * f20 + src.m12 * f21 + src.m22 * f22;
      dest.m23 = src.m03 * f20 + src.m13 * f21 + src.m23 * f22;
      dest.m00 = t00;
      dest.m01 = t01;
      dest.m02 = t02;
      dest.m03 = t03;
      dest.m10 = t10;
      dest.m11 = t11;
      dest.m12 = t12;
      dest.m13 = t13;
      return dest;
      }
  </code></pre>
  
  <p>I found some examples of rotation matrices, I'm just not sure of how to implement them (still searching for more): </p>
  
  <ol>
  <li><p><a href="https://www.youtube.com/watch?v=fIHgznIttvk" rel="nofollow noreferrer">https://www.youtube.com/watch?v=fIHgznIttvk</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/9010546/java-transformation-matrix-operations">Java transformation matrix operations</a></p></li>
  <li><p><a href="https://en.wikipedia.org/wiki/Transformation_matrix" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Transformation_matrix</a></p></li>
  <li><p><a href="https://en.wikipedia.org/wiki/Rotation_matrix" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Rotation_matrix</a></p></li>
  </ol>