3D旋转与轴和放大器;角度 [英] 3D rotation with Axis & Angle

查看:318
本文介绍了3D旋转与轴和放大器;角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道3D旋转是有据可查的SO和许多其他网站,但尽管阅人无数的解释我还没有想通了,我要去哪里错了。我的背景是在艺术和设计,而不是数学和编程,我从来没有真正肯定,如果我的攻击的角度(没有双关语意)是正确的。而不是贴我的惨淡code东拼西凑,我包括图像描述了我的问题。我真正喜欢的是如何解决它一步一步的措辞击穿。伪code是有用的,但我会学到更多,如果有人只是瞄准我朝着正确的方向或指出常见的陷阱。

I know 3D rotation is well documented on SO and many other sites, but despite reading countless explanations I still haven't figured out where I'm going wrong. My background is in art and design, not math and programming, and I'm never really certain if my angle of attack (no pun intended) is the right one. Rather than paste a patchwork of my dismal code, I'm including an image describing my problem. What I'd really like is a step-by-step worded breakdown of how to solve it. Pseudo code is useful, but I will learn more if someone will just aim me in the right direction or point out common pitfalls.

红= X轴,绿= Y轴,蓝色= Z轴

Red = X-Axis, Green = Y-Axis, Blue = Z-Axis

品红载体=产地 - >一些X,Y,Z点

Magenta vectors = origin --> some X,Y,Z point

品红立方=平均两个品红向量的端点(是有这更好的名字吗?)

Magenta cube = average of the endpoints of the two magenta vectors (is there a better name for this?)

白色矢量=两个品红向量的积(扩展显示,实际的载体是标准化)

White vector = cross product of the two magenta vectors (extended for display, actual vector is normalized)

青色立方体对象=旋转失败

Cyan cube object = rotation fail

我有previously使用Away3D中和Papervision;在这些库,应用欧拉角到一个对象的的rotationX,的rotationY,或的rotationZ属性将旋转对象局部,就好像它是在原点而不管其实际位置。与Three.js,不是这种情况。修改对象的rotation.x和rotation.y性质产生奇异的效果,其中对象显然倾斜一点上的Z轴。更令人困惑的是,这发生在物体停留在原点。我认为,用四元数可能 - >矩阵或轴/角 - >矩阵功能,可以解决我的问题,但没有骰子。它似乎有一个核心理念,我没有变。

I've previously used Away3D and Papervision; in these libraries, applying Euler angles to an object's rotationX, rotationY, or rotationZ properties will rotate the object locally, as if it's at the origin regardless of its actual position. With Three.js, this is not the case. Modifying an object's rotation.x and rotation.y properties produces a bizarre effect where the object apparently tilts a bit on the Z axis. Even more confusing is that this happens when the object rests at the origin. I thought that maybe using Quaternion-->Matrix or Axis/Angle-->Matrix functions would solve my problem, but no dice. It seems there's a core concept I'm not getting.

不管怎样,我希望做的是东方的多维数据集的叉积向量(白色),从而使立方体顶正面临着矢量的方向。然后我想沿旋转轴相同的立方体。我已附加的图像显示的更加小时,结果比我想承认努力实现这一结果。我的code松散看起来是这样的:

Anyway, what I'd like to do is orient the cube to the cross product vector (white), so that the top of the cube is facing the direction of that vector. Then I'd like to rotate the cube along the same axis. The image I've attached shows the result of more hours than I'd like to admit trying to achieve this result. My code loosely looks like this:

axis = Vector3.cross(a, b)
axis.normalize()
angle = 45 * TO_RADIANS;
quat = AxisAngle2Quaternion(axis, angle)
rot = Quaternion2Matrix(quat)
cube.matrix = rot

在此先感谢,

Thanks in advance,

凯西

编辑:启动赏金

也许我误解这是如何工作的。下面是另一个形象:

Maybe I am misunderstanding how this is supposed to work. Here's another image:

我是不正确的以为这品红矢量是轴,和橙色箭头表示关于此轴线基于角度旋转?这种或那种方式,我想定位基于一些方向性的矢量青色立方体旋转它。我在做什么错了!?

Am I incorrect in thinking that this magenta vector is the axis, and the orange arrows indicate rotation about this axis based on the angle? One way or another, I want to orient the cyan cube based on some directional vector and spin it. What am I doing wrong!?

推荐答案

您的做法听起来很正确的,但你不显示什么A,B向量和恒角度,我猜,只是为了测试。我这样做之前,所以我挖了我的code,这是我发现数学...

Your approach sounds correct but you don't show what the a, b vectors are and the constant angle is, I'm guessing, just for testing. I did this before so I dug up my code and this is the math I found...

给出:
originalVec =单位矢量指向了Y轴(立方顶部/垂直方向)
targetVec =白矢量

given:
originalVec = unit vector pointing up Y axis (direction of cube top/normal)
targetVec = white vector

现在你希望轴和角度,将旋转originalVec,以配合targetVec。旋转的最直接轴垂直于两个输入向量,所以采取它们的横产物。该轴是不是一个单位向量所以也归一化。旋转(弧度)的角度是反余弦点积的

Now you want the axis and angle that will rotate originalVec to align with targetVec. The most direct axis of rotation is perpendicular to both input vectors, so take their cross product. That axis is not a unit vector so also normalise it. The angle to rotate (in radians) is the inverse-cosine of the dot-product.

axis = Vector3.cross(originalVec, targetVec)
axis.normalise
angle = inversecos(Vector3.dot(originalVec, targetVec))

quat = AxisAngle2Quaternion(axis, angle)
rot = Quaternion2Matrix(quat)
cube.matrix = rot

而不是替换多维数据集的矩阵我想你想用它组成新的变换...

Instead of replacing the cube's matrix I think you want to compose it with the new transform...

cube.matrix.multiplyBy(rot)

...但我不是100%肯定。另外,我已经看到了实现中AxisAngle2Quaternion接受度的角度。当输入矢量平行或轴线相反为< 0,0,0>使应进行测试。如果立方体旋转走错了路,然后在跨产品的向量参数在错误的顺序,我从来不记得这也试着他们两个。心连心。照片

...but I'm not 100% sure about that. Also, I've seen implementations where AxisAngle2Quaternion takes an angle in degrees. When the input vectors are parallel or opposite the axis is <0,0,0> so that should be tested for. If the cube rotates the wrong way then the cross-product vector parameters are in the wrong order, I never remember which and just try them both. hth.

修改
我有机会与Three.js发挥和砍死的定向立方体的例子之一。评论显示在那里我加的东西,所有的定向数学发生在alignCube()。
对齐和自旋例如
滑鼠向上/向下移动目标线。鼠标移动向左/向右旋转就行了。

Edit
I've had a chance to play with Three.js and hacked one of the examples to orient a cube. Comments show where I added stuff and all the orienting math happens in alignCube().
Align and Spin example
Mousing up/down moves the target line. Mousing left/right spins on the line.

场景中Three.js对象似乎都是从Object3D具有autoUpdateMatrix属性设置为true默认继承。这需要设置假否则updateMatrix函数被调用哪个重新计算从对象位置,尺度和旋转特性的矩阵。或者你可以分配不同的updateMatrix功能。

Scene objects in Three.js seem to all inherit from Object3D which has a autoUpdateMatrix property set true by default. This needs to be set false otherwise the updateMatrix function gets called which recalculates the matrix from the objects position, scale and rotation properties. Alternately you could assign a different updateMatrix function.

这将会是很好,如果Three.js有一些文件:)

It'd be nice if Three.js had some documentation :)

这篇关于3D旋转与轴和放大器;角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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