为了理解blender python游戏脚本,我目前尝试使用this link中的FPSController结构构建一个可以围绕球体行走的场景。对于重力和FPSController方向,我尝试构建一个python控制器,目前看起来像这样:
def main():
print("Started")
controller = bge.logic.getCurrentController()
me = controller.owner
distance, loc, glob = me.getVectTo((0,0,0))
grav = controller.actuators['Gravity']
strength = me['Gravity']
force = strength*(distance*distance)*glob
grav.force = force
try:
rot = Vector((0,0,-1)).rotation_difference(glob).to_matrix()
except Exception as E:
print(E)
rot = (0,0,0)
rotZ = me.orientation
me.orientation = rot*rotZ
controller.activate(grav)
main()
大致有效,直到任何角度超过180度,然后看起来不连续。我假设这来自于rotation_difference是不连续的 - blender documentation on Math Types & Utilities没有说什么,我还没有想到四元数表示还没有看到连续的地图是否可行 - 我想有更优雅的方法来实现局部Z方向是连续的鼠标依赖,而局部X和Y方向连续依赖于某个给定的向量,但是如何?
答案 0 :(得分:4)
共识似乎是你应该使用四元数来完成这种旋转。
请参阅api:http://www.blender.org/documentation/249PythonDoc/Mathutils.Quaternion-class.html
有关数学的介绍,请参阅此内容:http://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions#Quaternions
答案 1 :(得分:0)
有一个allign功能。如果游戏对象被称为自己,它应该是own.alignAxisToVect(vector, 2, 1)
,其中2是Z轴的索引(x = 0,y = 1,z = 2),1是对齐的速度(在0和1)