通用简单 3D 矩阵旋转问题 [英] Generic Simple 3D Matrix Rotation Issue

查看:24
本文介绍了通用简单 3D 矩阵旋转问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 3D 对象旋转有问题(它在 Java 中,但并不重要)

I have a problem with my rotation on my 3D object (It's in java but it doesn't really matter)

背景:

我有一个简单的 3d 模型,你是它的第一人称玩家,你向上移动鼠标向上看(IE 沿 3D 的 x 轴旋转)并向下移动鼠标向下看(反向旋转方向)

I have a simple 3d model, and you are a first person player on it, you move your mouse up to look up (I.E Rotate by the 3D's x-axis) and move your mouse down to look down (Rotate in opposite direction)

但是:

我也有左右箭头键可以左右转(旋转 3D 的 y 轴)

I also have the left and right arrow keys to turn left/right (Which rotates the 3D's y-axis)

现在的问题是,当我转动时,当我沿 x 轴旋转时,它不再按预期转动,即如果您转动 180 度,向下移动鼠标实际上是向上看,如果向上移动鼠标你其实是在往下看.

Now the problem is, when I have turned, when I rotate by the x-axis it no longer turns as expected i.e if you turn 180 degrees, by moving your mouse down you actually look up and if you move your mouse up you actually look down.

我可以在 x/y 轴上执行什么旋转来解决这个问题?

What rotation can I perform on the x/y axis to fix this?

-所以无论我转了多少/多少,向上移动鼠标都会向上看,向下移动鼠标会向下看.

-So no matter how much/little I have turned, moving mouse up will look up and moving mouse down will look down.

抱歉,我无法解释得更好,但如果您需要更多信息,请发表评论.

Sorry I can't explain it better, but if you need any more info just make a comment.

非常感谢,

这可能是一个简单的转换,但我想不出来:(

It's probabaly a simple transformation, but i can't think :(

一些Java轮换代码:

Some of Java rotation code:

鼠标上/下:

  public void rotateXY(double radians, double Yradians)
  {
      vpTrans.getTransform(T3D); 
      Transform3D test = new Transform3D();
      Transform3D testX = new Transform3D();
      Transform3D testY = new Transform3D();
      translate.set(lastXcord, lastYcord, lastZcord);
      testX.rotX(radians);
      testY.rotY(Yradians);
      test.mul(testX, testY);
      test.setTranslation(translate);
      vpTrans.setTransform(test);
      //System.out.println(test);
  } // end of rotateXY()

左:(右相似,但在角度变化时用减号代替加号)

Left: (right is just similar but with minus instead of plus on angle changes)

 public void run() {
    Transform3D test = new Transform3D();
    Transform3D rotX = new Transform3D();
    Transform3D rotY = new Transform3D();

    vpTrans.getTransform(T3D);
    translate.set(lastXcord, lastYcord, lastZcord);
    angle += 0.05;
    trueangle += 0.05;
    rotX.rotX(angle);
    rotY.rotY(Yangle);

    test.mul(rotX, rotY);

    test.setTranslation(translate);
    vpTrans.setTransform(test);
}

我需要添加什么变换/旋转,以便无论我的 y 轴如何,当鼠标向下移动时,当鼠标向上和向下移动时,相机都会向上看?

What transformations/rotations I need to add to that so no matter my y-axis, camera will look up when mouse is moved up and down when mouse is moved down??

推荐答案

您的转换顺序似乎有问题.与数值乘法不同,矩阵乘法不是可交换的.3D 应用程序对转换顺序非常敏感.

It appears you have a problem with the order of transformations. Unlike numerical multiplication, matrix multiplication is not commutative. 3D applications are very sensitive to the order of transformations.

英文:

如果xy是数字,则x * y = y * x

if x and y are numbers, then x * y = y * x

然而,如果xy矩阵,那么x * y y * x!

However, if x and y are matrices, then x * y y * x!

考虑到这一点,让我们看看你在做什么.

With that in mind, let's look at what you are doing.

test.mul(rotX, rotY);

要想象乘法发生了什么,请想象您的脑海中有一条直线.我们称之为LineUp.现在低头看你的键盘,想象 LineUp 和你一起倾斜.现在在 LineUp 上转动你的头 90 度.这就是您的旋转矩阵发生的情况.疯了吧?

To visualize what is happening with this multiplication, imagine you have a straight line coming out of your head. We'll call this LineUp. Now look down at your keyboard, imagine LineUp tilting with you. Now turn your head 90 degrees on LineUp. That is what is happening to your rotation matrix. Crazy huh?

现在让我们像这样反转乘法:test.mul(rotY, rotX);

Now let's reverse the multiplication like so: test.mul(rotY, rotX);

所以坐直向前看,然后转动你的头/身体/椅子.现在稍微往下看.您首先在真正的 LineUp 轴上旋转,然后应用上/下变换.好玩吗?

So sit straight up and look forward, and turn your head/body/chair. Now look down a bit. You rotated on a true LineUp axis first, then applied the up/down transformation. Fun yeah?

考虑矩阵变换的一种好方法就像一个必须有序的指令集.

A great way to think about matrix transformations is like an instruction set that has to be in order.

  1. 向右转.(rotY)
  2. 抬起头.(rotX)
  3. 转到计算机(翻译)
  4. ...等

改变一个操作的顺序会以滑稽和离谱的方式改变所有后续指令.

Changing the order of one operation changes all the subsequent instructions in funny and outrageous ways.

你正在构建的是一个相机,所以我强烈推荐阅读如何构建一个.

What you are building is a Camera, so I highly recommend reading how to build one.

理解矩阵变换的一个很好的练习是构建一个太阳系,包括绕轨道运行的卫星和旋转的行星.这会让人大开眼界,而且信息量很大.

A great exercise to understand matrix transformations is building a solar system, complete with orbiting moons and spinning planets. It will be eye opening and very informative.

这篇关于通用简单 3D 矩阵旋转问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆