【发布时间】:2015-11-14 17:46:10
【问题描述】:
我正在使用搅拌机游戏引擎和 python 我制作了一个脚本,在 3D 空间中跟随我的光标。 (我现在使用键盘作为高度)。 现在我想使用 python 为一般对象而不是相机实现 LookAt 函数。我希望对象准确地看到我在屏幕上悬停的点(空位置)。现在我正在使用一个立方体,所以基本上立方体的一个面应该总是面向空的。 所以,我想到了使用矩阵或四元数,但问题是我所拥有的只是一个方向向量,我选择了 x 轴作为局部外观方向。所以无论哪种方式,我都需要计算欧拉角并将它们转换为轴旋转角。 (theta*[轴^])。
我在 Blender 游戏引擎中拥有的资源是:mathutils(提供四元数、基于欧拉的旋转(通过轴角)、矩阵)——尽管它没有任何更新的文档,这简直太可怕了!我必须打印帮助才能获得某种信息! 现在,当我仅旋转 Z 轴时,我已经能够使对象看起来是空的。我使用了一个小技巧,使用简单的三角函数为我处理角符号,所以符号被处理了,我不需要任何矩阵技巧或四元数。当我再次尝试旋转时,问题就开始了 - 我想旋转 Y 轴以获得上下外观(在 3D 中我们需要两种旋转来面对某人,第三种只是用于倒置旋转视图- “滚动相机”),因为这个旋转轴是观察方向向量。 这是我的脚本:
import bge
from mathutils import Vector, Matrix
import math
# Basic stuff
cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()
c = scene.objects["Cube"]
e = scene.objects["Empty"]
# axises (we're using localOrientation)
x = Vector((1.0,0.0,0.0))
y = Vector((0.0,1.0,0.0))
z = Vector((0.0,0.0,1.0))
vec = Vector(e.worldPosition - c.worldPosition) # direction vector
# Converting direction vector into euler angles
# Using trigonometry we get: tan(psi) = cos(phi2)/cos(phi1)
# Where phi1 is the angle between x axises (euler angle)
# and phi2 is the euler of the y axises.
# psi is the z rotation angle.
# get cos(euler_angle)
phi1 = vec.dot(x)/vec.length # = cos p1
phi2 = vec.dot(y)/vec.length # = cos p2
phi3 = vec.dot(z)/vec.length # = cos p3
# get the rotation/steer angles
zAngle = math.atan(phi2/phi1)
yAngle = math.atan2(phi3,phi1)
xAngle = math.atan(phi2/phi3)
# use only 2 as the third must adapt (also: view concept - x is the looking direction, rotating it would make rolling)
r = c.localOrientation.to_euler()
r.z = zAngle
r.y = -yAngle
#r.x = xAngle
c.localOrientation = r
单独每个轴都可以完美地工作,但是当我通过全局 Y 轴时,当组合起来时,几乎没有跳跃故障。 此外,搅拌机中的“本地”方向似乎与“世界方向”相同,这也很烦人,因为我不再确定我正在工作的参考框架是什么。如果有人知道,请帮忙!
编辑 1: 显然有一个内置的逻辑块可以为我处理这个问题,当我按下“3D”时,它会跟踪并成功旋转两个轴。不过,我仍然想知道我的脚本有什么问题! 3D 按钮做了哪些我没有做的事情?
编辑 2: 我试着停止制作三角技巧,发现当我使用局部方向时,我总是会在一个轴上锁定万向节。这大概就是幕后发生的事情。感谢任何有兴趣的人,如果您有任何好技巧,我仍然很高兴听到=]!
【问题讨论】:
标签: rotation blender euler-angles