基本对象运动
After adding an object to UNIGINE, you can control its transformations with your control devices. This article shows how to control basic object movements and combine different transformations.将对象添加到UNIGINE后,您可以使用控制设备控制其转换。 本文展示了如何控制基本的对象运动并组合不同的变换。
See Also请参阅#
Direction Vector方向向量#
The direction vector is an important concept of mesh transformation. To move the node forward, you should know where the forward direction of the mesh is. When the mesh is exported from a 3D editor, it saves the information about the forward direction. And when you add the mesh to UNIGINE, it has the same orientation it had in a 3D editor. 方向向量是网格变换的一个重要概念。 要向前移动节点,您应该知道网格的前进方向在哪里。 当网格从3D编辑器导出时,它会保存有关前进方向的信息。 当您将网格添加到UNIGINE时,它具有与3D编辑器中相同的方向。
![]() |
![]() |
A mesh in MayaMaya中的网格
|
The same mesh in UNIGINEUNIGINE中的相同网格
|
In the images above, the direction vector has the positive Y direction. To move this mesh forward, you should get the direction of the mesh by using the Y component (the second column) of the world transformation matrix of the mesh.在上面的图像中,方向矢量具有正Y方向。 要向前移动此网格,您应该通过使用网格的世界变换矩阵的Y组件(第二列)来获取网格的方向。
The point is that content creators and programmers should make an arrangement for the direction vector.重点是内容创建者和程序员应该为方向向量做出安排。
Basic Movements基本动作#
Prearrangement先期准备#
We are going to make a component that allows moving and rotating an object it is assigned to. Follow these steps to prearrange the environment for using the component:我们将制作一个组件,允许移动和旋转分配给它的对象。 按照以下步骤预先设置使用该组件的环境:
- Prepare a C#-based project.准备一个基于C#的项目。
- Open the project in the Editor.在编辑器中打开项目。
- create a C# component.创建一个C#组件。
-
Create the object that you are going to move, or select from the existing ones, and assign the component to it.创建要移动的对象,或从现有对象中选择,并将组件分配给它。
注意Make sure you assign the component to the node itself, not to a Node Reference.确保将组件分配给节点本身,而不是分配给节点引用。If the node you want assign this component to is a Node Reference, click Edit in the Parameters tab, and assign the component to the node directly.如果要将此组件分配给的节点是节点引用,请单击Parameters选项卡中的Edit,并直接将组件分配给节点。
- Open the component in IDE.在IDE中打开组件。
Moving Forward向前移动#
This section demonstrates how to set the forward movement of the mesh.本节演示如何设置网格的向前移动。
In this example, we use the "p" key pressing to move the mesh forward. The direction vector is visualized for clarity.在这个例子中,我们使用"p"键来向前移动网格。 为了清晰起见,方向矢量被可视化。
In the C# component file add the following (don't change the Component PropertyGuid, it is assigned by the system):在C#组件文件中添加以下内容(不要更改组件PropertyGuid,它是由系统分配的):
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;
#if UNIGINE_DOUBLE
using Vec3 = Unigine.dvec3;
using Vec4 = Unigine.dvec4;
using Mat4 = Unigine.dmat4;
#else
using Vec3 = Unigine.vec3;
using Vec4 = Unigine.vec4;
using Mat4 = Unigine.mat4;
#endif
[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class MovementControls : Component
{
// define the movement speed
public float movement_speed = 5.0f;
private void Init()
{
// check if the key is pressed and update the state of the specified control
ControlsApp.SetStateKey(Controls.STATE_AUX_0, Input.KEY.O);
}
private void Update()
{
// get the frame duration
float ifps = Game.IFps;
// enable visualizer
Visualizer.Enabled = true;
// get the current world transform matrix of the node
Mat4 transform = node.WorldTransform;
// get the direction vector of the mesh from the second column of the transformation matrix
Vec3 direction = transform.GetColumn3(1);
// render the direction vector for visual clarity
Visualizer.RenderDirection(node.WorldPosition, new vec3(direction), new vec4(1.0f, 0.0f, 0.0f, 1.0f), 0.1f, false);
// check if the control key is pressed
if (ControlsApp.GetState(Controls.STATE_AUX_0) == 1) {
// calculate the delta of movement
Vec3 delta_movement = direction * movement_speed * ifps;
// set a new position to the node
node.WorldPosition = node.WorldPosition + delta_movement;
}
}
}
设置网格位置的另一种方法
The new position can be also set by setting the WorldTransform variable. The following examples contain the code from the Update() function of the AppWorldLogic class. The part of controls initialization is the same for this method, the difference is in the Update() function only.新位置也可以通过设置WorldTransform变量来设置。 以下示例包含来自AppWorldLogic类的Update()函数的代码。 控件初始化的部分与此方法相同,区别仅在Update()函数中。
// check if the control key is pressed
if (ControlsApp.GetState(Controls.STATE_AUX_0) == 1) {
// calculate the delta of movement
Vec3 delta_movement = direction * movement_speed * ifps;
// set a new position to the node
node.WorldTransform = MathLib.Translate(delta_movement) * transform;
}
Or you can change the translation column of the world transformation matrix (see the Matrix Transformations article) to move the node:或者您可以更改世界变换矩阵的translation列(请参阅Matrix Transformations文章)以移动节点:
// check if the control key is pressed
if (ControlsApp.GetState(Controls.STATE_AUX_0) == 1) {
// calculate the delta of movement
Vec3 delta_movement = direction * movement_speed * ifps;
// set a new position
// here, you can also use transform.setColumn3(3, transform.getColumn3(3) + delta_movement);
transform.SetColumn(3, transform.GetColumn(3) + new Vec4(delta_movement, 1.0f));
// set a new world transform matrix to the node
node.WorldTransform = transform;
}
Rotation旋转#
This section contains implementation of the mesh rotation.本节包含网格旋转的实现。
You can rotate the mesh in two ways, by changing the transformation matrix represented by the WorldTransform variable (recommended way) or via the SetWorldRotation() function. The following example uses the second one:您可以通过两种方式旋转网格,通过更改由WorldTransform变量表示的变换矩阵(推荐方式)或通过SetWorldRotation()函数。 下面的示例使用第二个:
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;
#if UNIGINE_DOUBLE
using Vec3 = Unigine.dvec3;
using Vec4 = Unigine.dvec4;
using Mat4 = Unigine.dmat4;
#else
using Vec3 = Unigine.vec3;
using Vec4 = Unigine.vec4;
using Mat4 = Unigine.mat4;
#endif
[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class MovementControls : Component
{
// define the rotation speed
public float rotation_speed = 30.0f;
private void Init()
{
// check if the key is pressed and update the state of the specified control
ControlsApp.SetStateKey(Controls.STATE_AUX_1, Input.KEY.I);
}
private void Update()
{
// get the frame duration
float ifps = Game.IFps;
// enable visualizer
Visualizer.Enabled = true;
// check if the control key is pressed
if (ControlsApp.GetState(Controls.STATE_AUX_1) == 1) {
// set the node rotation along the Z axis assuming node's scale equal to 1
node.SetWorldRotation(node.GetWorldRotation() * new quat(MathLib.RotateZ(rotation_speed * ifps)), true);
}
}
}
In the example above, the node is rotated to the left by pressing the "o" keyboard key.在上面的示例中,节点通过按下"o"键盘键向左旋转。
- It is recommended to set the second argument of the SetWorldRotation() function to 1 for all non-scaled nodes to improve performance and accuracy.建议将所有非缩放节点的SetWorldRotation()函数的第二个参数设置为1,以提高性能和准确性。
- Scaling of nodes should be avoided whenever possible, as it requires addidional calculations and may lead to error accumulation.应尽可能避免节点的缩放,因为它需要额外的计算,并可能导致错误累积。
To rotate the object by via the WorldTransform variable, you should replace the line containing the SetWorldRotation() function in the example above with the following one:要通过WorldTransform变量旋转对象,您应该将上面示例中包含SetWorldRotation()函数的行替换为以下行:
node.WorldTransform = node.WorldTransform * new Mat4(MathLib.RotateZ(rotation_speed * ifps));
This way is preferred, especially in case of complex transformations, as it allows composing the transformation matrix and setting it only once.这种方式是优选的,特别是在复杂变换的情况下,因为它允许组合变换矩阵并只设置一次。
Combining Movements结合运动#
Combining different movement controls is not more difficult than adding only one movement control.结合不同的移动控件并不比仅添加一个移动控件更困难。
The following code is an example that adds a mesh to the world and assigns a component on it that allows controlling its movements. You can rotate the mesh by using the "o", "[" keyboard keys and move forward by using the "p" key.下面的代码是一个示例,它向世界添加了一个网格,并在其上分配了一个允许控制其运动的组件。 您可以使用"o", "["键盘键旋转网格体,并使用"p"键向前移动。
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;
#if UNIGINE_DOUBLE
using Vec3 = Unigine.dvec3;
using Vec4 = Unigine.dvec4;
using Mat4 = Unigine.dmat4;
#else
using Vec3 = Unigine.vec3;
using Vec4 = Unigine.vec4;
using Mat4 = Unigine.mat4;
#endif
[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class MovementControls : Component
{
// define the movement speed
public float movement_speed = 5.0f;
// define the rotation speed
public float rotation_speed = 30.0f;
private void Init()
{
// check if the key is pressed and update the state of the specified control
// you can use 'I', 'O', 'P' keys
ControlsApp.SetStateKey(Controls.STATE_AUX_0, Input.KEY.O);
ControlsApp.SetStateKey(Controls.STATE_AUX_1, Input.KEY.I);
ControlsApp.SetStateKey(Controls.STATE_AUX_2, Input.KEY.P);
}
private void Update()
{
// get the frame duration
float ifps = Game.IFps;
// enable visualizer
Visualizer.Enabled = true;
// get the current world transform matrix of the mesh
Mat4 transform = node.WorldTransform;
// get the direction vector of the mesh from the second column of the transformation matrix
Vec3 direction = transform.GetColumn3(1);
// initialize rotation and movement and update flag
Mat4 rotation = Mat4.IDENTITY;
Vec3 delta_movement = new Vec3(0.0f);
bool update_transform = false;
// render the direction vector for visual clarity
Visualizer.RenderDirection(node.WorldPosition, new vec3(direction), new vec4(1.0f, 0.0f, 0.0f, 1.0f), 0.1f, false);
// check if the control key for movement is pressed
if (ControlsApp.GetState(Controls.STATE_AUX_0) == 1)
{
// calculate the delta of movement
delta_movement = direction * movement_speed * ifps;
update_transform = true;
}
// check if the control key for left rotation is pressed
if (ControlsApp.GetState(Controls.STATE_AUX_1) == 1)
{
// set the node's left rotation along the Z axis
rotation.SetRotateZ(rotation_speed * ifps);
update_transform = true;
}
// check if the control key for right rotation is pressed
else if (ControlsApp.GetState(Controls.STATE_AUX_2) == 1)
{
// set the node's right rotation along the Z axis
rotation.SetRotateZ(-rotation_speed * ifps);
update_transform = true;
}
// update transformation if necessary
if (update_transform)
{
// combine transformations: movement + rotation
transform = transform * rotation;
transform.SetColumn3(3, transform.GetColumn3(3) + delta_movement);
// set the resulting transformation
node.WorldTransform = transform;
}
}
}
本页面上的信息适用于 UNIGINE 2.19.1 SDK.