π导航  


【首页】

BGE Python: 读写旋转


【2020-04-12】 Blender学堂】


Introduction

This one’s for the Python dabblers that haven’t been able to figure out localOrientation. Using the function localPosition is easy enough because it returns a list of x, y, and z values. Using localOrientation for rotation values, however, returns a 3×3 matrix. So unless you’re brave enough to handle matrices what we’re going to do in this quick tutorial is convert our object’s matrix to degrees for easier reading and changing. For an intro to python scripting in the BGE, check out my beginner’s python tutorial.

这一个是为那些还没有搞清楚本地定位的Python爱好者准备的。使用函数localPosition非常简单,因为它返回x、y和z值的列表。但是,对旋转值使用localOrientation会返回一个3×3的矩阵。所以,除非你有足够的勇气来处理矩阵,否则我们在本快速教程中要做的就是将对象的矩阵转换为度,以便于阅读和更改。有关BGE中python脚本的介绍,请查看我的python初学者教程。

Reading Your Object’s Rotation

Open the text editor in Blender and add a new file. Name this “rotation”. Paste in the following code:

import bge

import math

cont = bge.logic.getCurrentController()

own = cont.owner

xyz = own.localOrientation.to_euler()

rotz = math.degrees(xyz[2])

print (rotz)

Now select your object and in the Logic editor add an Always sensor(set to true pulse) and connect it to a Python controller(set to script “rotation”). Add logic bricks for turning the object left and right. Press P over the 3D View and turn the object around a bit so you can see the rotation values change. Then check the system console. It’ll print out the object’s Z rotation value in degrees.

现在选择您的对象并在逻辑编辑器中添加一个Always传感器(设置为true pulse)并将其连接到Python控制器(设置为脚本“旋转”)。添加用于左右转动对象的逻辑块。在三维视图上按P键,并稍微旋转对象,以便可以看到旋转值的变化。然后检查系统控制台。它将以度为单位打印出对象的Z旋转值。

Logic setup:

Z rotation values printed in the console:

This is a simple script. In the first line we import the math module because we’ll need special functions for angle conversions. Then the cont and own lines are standard lines for getting information on the object running the python script(the owner).

这是一个简单的脚本。在第一行中,我们导入数学模块,因为我们需要用于角度转换的特殊函数。然后cont和own行是获取运行python脚本的对象(所有者)信息的标准行。

The xyz variable is the object’s orientation matrix, except we added to_euler() at the end of it to convert the matrix to radians. Radians are a different angle measurement that ranges from 0 to 2π. So now instead of being a matrix, localOrientation is converted to a list with x, y, and z values in radians. Now we can take each one of these values and convert them into degrees. So the last variable, rotz, takes the third item in the xyz list and converts it to degrees.

xyz变量是对象的方向矩阵,但我们在其末尾添加了_euler(),以将矩阵转换为弧度。弧度是一种不同的角度测量,范围从0到2π。因此,现在localOrientation不再是矩阵,而是转换为以弧度表示的x、y和z值的列表。现在我们可以将这些值中的每一个值转换为度。最后一个变量rotz接受xyz列表中的第三项并将其转换为度。

Then we print rotz. Now you can see what direction an object is facing. Make a note that the degree values will range from -180 to 180, not 0 to 360(unless you want to add a little extra math to make that happen). Also make a note that to get the x value, it would be xyz[0], and the y value would be xyz[1]. The first item in python lists is 0 in case you were confused.

然后我们打印rotz。现在您可以看到对象所面对的方向。请注意,度值的范围是-180到180,而不是0到360(除非您想添加一点额外的数学来实现这一点)。还要注意,要获得x值,它将是xyz[0],y值将是xyz[1]。python列表中的第一项是0,以防混淆。

Setting Your Object’s Rotation

Now reading the rotation value is one thing, but how about changing it? It’s pretty much the same process, except in reverse. Erase your script and paste in the following script:

import bge

import math

cont = bge.logic.getCurrentController()

own = cont.owner

xyz = own.localOrientation.to_euler()

xyz[2] = math.radians(45)

own.localOrientation = xyz.to_matrix()

In this script we still convert localOrientation to euler first. Then we change xyz[2], which is the Z value, to the radian equivalent of 45 degrees(you can put any number or variable you want in there). Then we convert it back to a matrix in the last line and assign it to own.localOrientation. When you press P in the 3D View, the cube will instantly rotate to 45 degrees.

在这个脚本中,我们仍然首先将localOrientation转换为euler。然后我们将xyz[2]更改为45度的弧度(您可以在其中输入任何数字或变量)。然后我们将其转换回最后一行中的矩阵,并将其指定给own.localOrientation。在三维视图中按P键时,立方体将立即旋转到45度。

Easy as pi.

Check out a little demo I made of aiming a catapult at a castle. You can even type in your own angles to set the catapult to for precise aiming. There are a couple different short scripts with notes in each one telling you what they do.

Download the catapult demo blend



                      

copyright©2018-2024 gotopie.com