对象旋转问题

时间:2017-03-22 21:22:02

标签: java rotation

以下是问题的视频: https://youtu.be/YlAwGUADP7c

以下是我的轮换问题:

  1. 偏航 - 当upVector.z / lengthUpVector == -1(面向前方)时,不绕z旋转但绕y旋转
  2. 偏航 - 当upVector.x / lengthUpVector == 1(向上朝上)时,不绕x旋转但绕y旋转
  3. 音高 - 当upVector.x / lengthUpVector == 1时,不绕y旋转但绕x旋转
  4. roll - works
  5. 在类似的帖子中:OpenGL - Object local rotation ..其他用户发现这是一个问题,“滚动操作就像它应该工作,但如果我把它转向面对例如(1,0,0)它表现得像音调”,而我希望我的旋转改变像这样。

    其他信息:

    1. 看着背部加入偏航从左向右旋转
    2. 看着后面加上球场从上到下旋转
    3. 查看右侧添加滚动从下向上旋转
    4. 我试图手动使旋转相对于物体所朝向的方向发生变化,但是当只是试图用下面的方法旋转偏航时,它只会使物体在所有三个轴上旋转失控(偏航是围绕y旋转,俯仰是围绕x旋转,滚动围绕z旋转:

          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();
          }
      

      但我实际上无法修复我之前列出的问题,因为我认为这是一个涉及转换矩阵影响我想要旋转的对象的问题;换句话说,上面没有问题,它应该工作我期望,但它不是因为转换矩阵计算有问题。

      我的旋转矩阵完全基于以下内容:https://www.youtube.com/watch?v=50Y9u7K0PZo&index=8&list=PLRIWtICgwaX0u7Rf9zkZhLoLuZVfUksDP

      我创建了一个转换矩阵,如下所示:

          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;
          }
      

      以下是旋转矩阵的代码(这就是问题所在):

          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;
          }
      

      我找到了一些旋转矩阵的例子,我只是不确定如何实现它们(仍然在寻找更多):

      1. https://www.youtube.com/watch?v=fIHgznIttvk

      2. Java transformation matrix operations

      3. https://en.wikipedia.org/wiki/Transformation_matrix

      4. https://en.wikipedia.org/wiki/Rotation_matrix

0 个答案:

没有答案