This page has been translated automatically.
Getting Started
Migrating to UNIGINE 2.0
C++ API Migration
UnigineScript
The Language
Core Library
Engine Library
Node-Related Classes
GUI-Related Classes
Plugins Library
High-Level Systems
Samples
C++ API
API Reference
Integration Samples
Usage Examples
C++ Plugins
Внимание! Эта версия документация УСТАРЕЛА, поскольку относится к более ранней версии SDK! Пожалуйста, переключитесь на самую актуальную документацию для последней версии SDK.
Внимание! Эта версия документации описывает устаревшую версию SDK, которая больше не поддерживается! Пожалуйста, обновитесь до последней версии SDK.

Basic Object Movements

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.

Notice
In examples below, we use the statue.mesh mesh from the <UNIGINE SDK>/data/samples/common/meshes/ folder.

See Also

Direction Vector

A direction vector is the important concept of mesh transformation. To move the node forward, you should know where is the forward direction of the mesh. When the mesh is exported from a 3D editor, it saves the information about the forward direction. And when you add the mesh to the Unigine, it will have the same orientation as it had in a 3D editor.

A mesh in Maya
The same mesh in Unigine

On pictures given above, the direction vector has 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.

For example, in the City Traffic project the direction vector has positive Y-direction. To add a new car to City Traffic (to apply all the logic to the added car), it should be exported from a 3D editor with the specific direction vector.

The point is that content creators and programmers should make an arrangement about the direction vector.

Basic Movements

Moving Forward

This section contains different ways of setting the forward movement of the mesh.

In this example, we used the "p" key pressing to move the mesh forward.

Source code (UnigineScript)
// define the ObjectMeshStatic mesh and movement speed
ObjectMeshStatic mesh;
float movement_speed = 5.0f;

// initialize custom controls
void init_controls() {

	// check if the key is pressed and update the state of the specified control
	// you can use both 'p' or ASCII code (112)
	engine.controls.setStateKey(CONTROLS_STATE_AUX_0,'p');
}

// add a mesh as a node to the editor
Node add_editor(Node node) {
	engine.editor.addNode(node);
	return node_remove(node);
}

/* the world script init() function
 */
int init() {
	/* create a camera and add it to the world
	 */
	Player player = new PlayerSpectator();
	player.setDirection(Vec3(0.755f,-1.0f,0.25f));
	engine.game.setPlayer(player);
	
	// add the mesh to the world and set the material
	mesh = add_editor(new ObjectMeshStatic("unigine_project/meshes/statue.mesh"));
	mesh.setMaterial("mesh_base", "*");

	// call the init_controls() function
	init_controls();

	return 1;
}

/* the world script update() function
*/
int update() {

	// get the frame duration
	float ifps = engine.game.getIFps();

	// check if the control key is pressed
	if(engine.controls.getState(CONTROLS_STATE_AUX_0)) {

		// get the current world transform matrix of the mesh
		Mat4 transform = mesh.getWorldTransform();

		// get the direction vector of the mesh
		Vec3 direction = transform.m00m10m20;

		// calculate the delta of movement
		Vec3 delta_movement = direction * movement_speed * ifps;

		// set a new position to the mesh
		mesh.setWorldPosition(mesh.getWorldPosition() + delta_movement);
	}

	return 1;
}
					

Another way of getting the direction vector is by using this command:

Source code (UnigineScript)
Vec3 direction = Vec3(transform.col0);
					

This line is more readable, but it requires an additional casting to Vec3.

Another Way of Setting Mesh Position

The new position can be also set by using the setWorldTransform() function. The following examples contain the code from the update function of the world script. The part of controls initialization is the same for this method, the difference is in the world script's update() function.

Source code (UnigineScript)
// check if the control key is pressed
if(engine.controls.getState(CONTROLS_STATE_AUX_0)) {
	
	// get the current world transform matrix of the mesh
	Mat4 transform = mesh.getWorldTransform();

	// get the direction vector of the mesh
	Vec3 direction = transform.m00m10m20;

	// calculate the delta of movement
	Vec3 delta_movement = direction * movement_speed * ifps;

	// set a new position to the mesh
	mesh.setWorldTransform(translate(delta_movement) * transform);
}
					

Or you can change the translation column of the world transformation matrix (see the Matrix Transformations article) to move the mesh:

Source code (UnigineScript)
// check if the control key is pressed.
if(engine.controls.getState(CONTROLS_STATE_AUX_0)) {
	
	// get the current world transform matrix of the mesh
	Mat4 transform = mesh.getWorldTransform();

	// get the direction vector of the mesh
	Vec3 direction = transform.m00m10m20;

	// calculate the delta of movement
	Vec3 delta_movement = direction * movement_speed * ifps;

	// set a new position
	// here, you can also use transform.m03m13m23 += delta_movement;
	transform.col3 += Vec4(delta_movement,1.0f);
	
	// set a new world transform matrix to the mesh
	mesh.setWorldTransform(transform);
}
					

Rotation

This section contains implementation of the mesh rotation.

You can rotate the mesh in two ways, by using the setWorldTransform() function or the setWorldRotation() function. The following example uses the setWorldRotation() function:

Source code (UnigineScript)
// define the ObjectMeshStatic mesh and movement speed
ObjectMeshStatic mesh;
float rotation_speed = 30.0f;

// initialize custom controls
void init_controls() {

	// check if the key is pressed and update the state of the specified control
	// you can use both 'o' or ASCII code (111)
	engine.controls.setStateKey(CONTROLS_STATE_AUX_1,'o');
}

// add a mesh as a node to the editor
Node add_editor(Node node) {
	engine.editor.addNode(node);
	return node_remove(node);
}

/* the world script init() function
 */
int init() {
	/* create a camera and add it to the world
	 */
	Player player = new PlayerSpectator();
	player.setDirection(Vec3(0.755f,-1.0f,0.25f));
	engine.game.setPlayer(player);
	
	// add the mesh to the world and set the material
	mesh = add_editor(new ObjectMeshStatic("unigine_project/meshes/statue.mesh"));
	mesh.setMaterial("mesh_base", "*");

	// call the init_controls() function
	init_controls();

	return 1;
}

/* the world script update() function
*/
int update() {

	// get the frame duration
	float ifps = engine.game.getIFps();

	/* this block rotates the mesh
	*/
	// check if the control key is pressed.
	if(engine.controls.getState(CONTROLS_STATE_AUX_1)) {
		// set the node rotation along the Z axis
		mesh.setWorldRotation(mesh.getWorldRotation() * quat(rotateZ(rotation_speed * ifps)));
	}

	return 1;
}
					

In the example above, the node is rotated to the left by pressing the "o" keyboard key.

To use the setWorldTransform() function, you should replace the setWorldRotation() function with the following line:

Source code (UnigineScript)
		mesh.setWorldTransform(mesh.getWorldTransform() * rotateZ(rotation_speed * ifps));
					

Combining Movements

Combining different movement controls is not more difficult than adding only one movement control.

The following example adds a mesh to the world and allows you to control the mesh. You can rotate it by using the "o", "[" keyboard keys and move forward by using the "p" keyboard key.

Source code (UnigineScript)
// define the ObjectMeshStatic mesh and movement speed
ObjectMeshStatic mesh;
float movement_speed = 5.0f;
float rotation_speed_left = 30.0f;
float rotation_speed_right = -30.0f;

// initialize custom controls
void init_controls() {
	// check if the key is pressed and update the state of the specified control
	// you can use both 'p' or ASCII code (112)
	engine.controls.setStateKey(CONTROLS_STATE_AUX_0,'p');
	engine.controls.setStateKey(CONTROLS_STATE_AUX_1,'o');
	engine.controls.setStateKey(CONTROLS_STATE_AUX_2,'[');
}
// add a mesh as a node to the editor
Node add_editor(Node node) {
	engine.editor.addNode(node);
	return node_remove(node);
}

/* the world script init() function
 */
int init() {
	/* create a camera and add it to the world
	 */
	Player player = new PlayerSpectator();
	player.setDirection(Vec3(0.755f,-1.0f,0.25f));
	engine.game.setPlayer(player);
	
	// add the mesh to the world and set the material
	mesh = add_editor(new ObjectMeshStatic("unigine_project/meshes/statue.mesh"));
	mesh.setMaterial("mesh_base", "*");

	// call the init_controls() function
	init_controls();

	return 1;
}

/* the world script update() function
*/
int update() {
	// get the frame duration
	float ifps = engine.game.getIFps();

	// check if the control key for moving forward is pressed
	if(engine.controls.getState(CONTROLS_STATE_AUX_0)) {
	
		// get the current world transform matrix of the mesh
		Mat4 transform = mesh.getWorldTransform();

		// get the direction vector of the mesh
		Vec3 direction = transform.m00m10m20;
		
		// calculate the delta of movement
		Vec3 delta_movement = direction * movement_speed * ifps;

		// set a new position to the mesh
		mesh.setWorldPosition(mesh.getWorldPosition() + delta_movement);
	}

	// check if the control key for left rotation is pressed
	if(engine.controls.getState(CONTROLS_STATE_AUX_1)) {
		// set the node left rotation
		mesh.setWorldRotation(mesh.getWorldRotation() * quat(rotateZ(rotation_speed_left * ifps)));
	}

	// check if the control key for right rotation is pressed
	if(engine.controls.getState(CONTROLS_STATE_AUX_2)) {
		// set the node right rotation
		mesh.setWorldRotation(mesh.getWorldRotation() * quat(rotateZ(rotation_speed_right * ifps)));
	}

	return 1;
}

			
Last update: 03.07.2017
Build: ()