如何结合X,Y和Z旋转模型中的局部空间(不是全球性)? [英] How do you combine X, Y, and Z rotations in a model's local space (not global)?

查看:286
本文介绍了如何结合X,Y和Z旋转模型中的局部空间(不是全球性)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始了对Molehill的和我有很多抓的Matrix3D问题。 我的第一个问题是如何管理的Matrix3D描述一个模型的方向。

I am starting out on Molehill and am having a lot of problems grasping Matrix3D. My first problem is how to manage a Matrix3D that describes a single model's orientation.

我用Matrix3D.appendRotation()来做到这一点。但根据我追加的顺序上,我得到不同的结果(很明显,因为我在全球空间旋转一次)。 我知道这是一个共同的问题,我遇到了它与三维软件建模。但是这一次,我一定要programtically修复它。这就是我需要帮助。

I use Matrix3D.appendRotation() to do this. But depending on the order I append in I get a different result (obviously, since I am rotating one at a time in the global space). I know this is a common problem, I have run into it with 3d software modeling. But this time, I have to fix it programtically. This is where I need help.

首先,万一有任何混淆,这里是图片的问题:

First, in case there is any confusion, here is the problem in pictures:

步骤1:我渲染模型

步骤2a中:我将其旋转的X轴。 看起来是正确的!

Step 2a: I rotate it on the X-axis. Looks correct!

步骤2b中:我将其旋转的Z轴。 看起来是正确的!

Step 2b: I rotate it on the Z-axis. Looks correct!

所以,如果我在旋转模型的X轴和Z轴我的需要的这样的:

So... if I rotate on the model's X AND Z axis I want this:

不过,sadface,我得到这样的:

But, sadface, I get this:

我的的问题。我追加旋转逐个在全局空间中的时间。我需要在对象的局部空间旋转。我不知道如何做到这一点还是我应该甚至可以搜索(术语等等)。在code是简单的(错误):

I can see the problem. I am appending the rotations one at a time in the global space. I need to rotate in the object's local space. I have NO idea how to do this or what I should even be searching for (terminology, etc.). The code is simple (and wrong):

modelLocalMatrix.identity();
modelLocalMatrix.appendRotation(modelZRot, Vector3D.Z_AXIS);
modelLocalMatrix.appendRotation(modelXRot, Vector3D.X_AXIS);
//eventually y-axis rotation as well...
renderMatrix.append(modelLocalMatrix);

我猜测使用的Vector3D轴常量的替代,我想用一些标准化的载体,一些......呃,东西...到位modelZRot,modelXRot,并最终modelYRot的。谁能告诉我什么是最佳实践的解决方案是采用旋转所需的类型上面?

I am guessing that instead of using the Vector3D axis constants, I want to use some normalized vector and some... uh, thing... in place of modelZRot, modelXRot, and eventually modelYRot. Can anyone tell me what the best-practice solution is for applying the desired type of rotation above?

更新:在花了一天打了书(又名KhanAcademy在YouTube上),并开始在此presentation此处的 3D旋转矩阵我在一种都迷迷糊糊的数学看的方法,这是非常接近我想要的解决方案。不幸的是,我只有50%的理解。我还是希望有人能阐明这一话题一些轻!

UPDATE: Having spent the day hitting the "books" (aka KhanAcademy on YouTube) and starting on this presentation here: The Mathematics of the 3d Rotation Matrix I have stumbled upon a sort of "look at" method which is very close to my desired solution. Unfortunately, I only 50% understand it. I still hope someone can shed some light on this topic!

var angleOfRotation:Number = Math.PI / 2; //90 degrees...
var axisOfRotation:Vector3D = new Vector3D(0, 1, 0); //some point to look at... 
//normalize the vector!
axisOfRotation.normalize();
var x:Number = axisOfRotation.x;
var y:Number = axisOfRotation.y;
var z:Number = axisOfRotation.z;
var c:Number = Math.cos(angleOfRotation);
var s:Number = Math.sin(angleOfRotation);
var t:Number = 1 - c;

//Graphics Gems (Glassner, Academic Press, 1990). 
modelLocalMatrix = new Matrix3D(new <Number>[
  t * (x * x) + c,   t * x * y - s * z,   t * x * z + s * y,   0, 
  t * x * y + s * z,   t * (y * y) + c,   t * y * z - s * x,   0, 
  t * x * z - s * y,   t * y * z + s * x,   t * (z * z) + c,   0, 
  0,           0,           0,           1
]);

是,这似乎很笨重。特别是因为旋转对将Vector3D角度应该是第四(w)的值。更重要的是,已经在Matrix3D似乎有一个注视()方法。我不明白这一点尚未...请人把我从试验和错误的时间!

Thing is, this seems rather clunky. Especially since the angle of rotation for a Vector3D should be the fourth (w) value. More importantly, Matrix3D already appears to have a lookAt() method. I don't get it yet... please someone save me from hours of trial and error!

推荐答案

好吧,我已经基本想通了如何做到这一点。这是非常简单的比我做的。这也有助于审查的Matrix3D API(它已得到一些真棒功能!)。

Ok, I have basically figured out how to do this. It is MUCH simpler than I was making it. It also helped to review the Matrix3D API (it has got some awesome functions!).

首先,源$ C ​​$ C我用我的旋转模型,其局部轴:

First, the source code I used to rotate my model on its local axes:

var raw:Vector.<Number> = modelLocalMatrix.rawData;
var right:Vector3D = new Vector3D(raw[0], raw[1], raw[2]);
var up:Vector3D = new Vector3D(raw[4], raw[5], raw[6]);
var out:Vector3D = new Vector3D(raw[8], raw[9], raw[10]);

modelLocalMatrix.appendRotation(modelXRot, right);
modelLocalMatrix.appendRotation(modelYRot, up);
modelLocalMatrix.appendRotation(modelZRot, out);

向上,右和出归一化向量中的链接在原来的职位讨论。它们基本上是模型的俯仰,偏转和滚动轴。我只想说,当你初始化的对象,这些载体是相同的Vector3D.X_AXIS,Y_AXIS和Z_AXIS。我已经使用Matrix3D.rawData属性,虽然有可能是一个更好的方式提取它们。

The "up", "right", and "out" normalized vectors are discussed in the link in my original post. They are basically the model's pitch, yaw, and roll axes. Suffice to say that when you initialize your object, these vectors are identical to Vector3D.X_AXIS, Y_AXIS, and Z_AXIS. I have extracted them using Matrix3D.rawData property although there may be a better way.

现在,你可以沿着任何你选择这些轴的转动,但你的瞬间,另两轴的方向立刻改变。这是有道理的。你的模型旋转和右,上和前进现在是不同的!所以,如果你通过旋转型号的权利轴启动(请记住,这开始时一样的全球X_AXIS),然后要滚你的模型在其走出去轴,则需要从模型的地方把它矩阵。显然,它不再是相同的全球Z_AXIS。请注意,我不叫Matrix3D.identity()。如果我这样做,那么我的模型的轴将被重置!这是确定我的渲染循环,虽然我不得不改变我的code(和思维方式)一点,因为我一直都CH375复位我的矩阵应用转换和转动之前。需要注意的是modelXRot,modelYRot和modelZRot存储度的旋转值。

Now, you can rotate along any of these axes as you choose, but the instant you do, the other two axes' orientation changes immediately. This makes sense. Your model has rotated and its "right", "up", and "forward" are now different! So if you start by rotating on the model's "right" axis (remember, this starts out the same as the global X_AXIS) and then you want to roll your model on its "out" axis, you need to get it from the model's local matrix. Obviously, it is no longer the same as the global Z_AXIS. Note that I do not call Matrix3D.identity(). If I did that, then my model's axes would be reset! This is ok for my render loop, although I had to change my code (and way of thinking) a bit since I had been reseting all my matrices before applying transformations and rotations. Note that modelXRot, modelYRot, and modelZRot store a rotation value in degrees.

我当然希望这可以帮助别人。我简直不敢相信这样一个共同的问题,这样一个简单的解决办法是无处可寻。无论如何,它肯定似乎是在做什么,我想它。

I sure hope this helps someone. I can't believe such a common problem with such a simple solution was nowhere to be found. Anyway, it sure seems to be doing what I want it to.

这篇关于如何结合X,Y和Z旋转模型中的局部空间(不是全球性)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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