Lab 2: Translate a 3D game object

Our goal is to translate a 3D game object across the screen using the two methods explained in this article.

First, you will learn how to translate an entity To a specific location. Lastly, you will learn how to translate an object By an amount.

1. Translate To Location

Our first objective is to translate the entity to location (-2.0,1.0,2.0). That is, we are going to move the character two units along the negative x-axis, one unit along the y-axis and two units along the z-axis.

In the engine, open the file Earth.mm.

Before we can move an object, we need to create it. Therefore, create a character as specified in Rendering Lab 1

Once you have created the character, then translate the character to (-2.0,1.0,2.0) using the translateTo() method, as shown in the snippet below (line 5).

//Line 5. Translate the game object to location (-2.0, 1.0, 2.0)
myAstronaut->translateTo(-2.0, 1.0, 2.0);

Complete code snippet

The complete code to translate the character to a particular location should look as follows. Pay particular attention to line 5:

Code Snippet: init() method
void Earth::init(){
/*----DO NOT REMOVE. THIS IS REQUIRED-----*/
//Configures the perspective view, shadows, lights and camera.
setupConfiguration();
//Renders the background models: skybox and island models. Remove if you want to.
setupBackgroundModels();
/*----CREATE ASTRONAUT HERE-----*/
//Line 1. Create an instance of U4DGameObject type
myAstronaut=new U4DEngine::U4DGameObject();
//Line 2. Load attribute (rendering information) into the game entity
if (myAstronaut->loadModel("astronaut","astronautAttributes.u4d")) {
//Line 3. Load rendering information into the GPU
myAstronaut->loadRenderingInformation();
//Line 4. Add astronaut to the scenegraph
addChild(myAstronaut);
}
//Line 5. Translate the game object to location (-2.0, 1.0, 2.0)
myAstronaut->translateTo(-2.0, 1.0, 2.0);
}

Results

In Xcode, select either the iOS or Mac device and then click on Run.

Run the project.

The character should have translated to a new position as shown.

image

2. Translate By an amount

Next, let's translate the character by 0.01 units along the +x-axis.

This time, however, we want the character to translate the specified amount during every game-tick. To accomplish this task, we will translate the entity in the update() method. Doing so will create the illusion that the astronaut continuously moves to the right.

Scroll down to the update() method and translate the entity using the translateBy() method, as shown in the snippet below (line 6).

void Earth::update(double dt){
//Line 6. Translate the game object BY an amount of 0.01 units along the x-axis
myAstronaut->translateBy(0.01, 0.0, 0.0);
}

Complete code snippet

The complete code to translate the character by an amount should look as follows. Pay particular attention to line 6:

Code Snippet: init() method
void Earth::init(){
/*----DO NOT REMOVE. THIS IS REQUIRED-----*/
//Configures the perspective view, shadows, lights and camera.
setupConfiguration();
//Renders the background models: skybox and island models. Remove if you want to.
setupBackgroundModels();
/*----CREATE ASTRONAUT HERE-----*/
//Line 1. Create an instance of U4DGameObject type
myAstronaut=new U4DEngine::U4DGameObject();
//Line 2. Load attribute (rendering information) into the game entity
if (myAstronaut->loadModel("astronaut","astronautAttributes.u4d")) {
//Line 3. Load rendering information into the GPU
myAstronaut->loadRenderingInformation();
//Line 4. Add astronaut to the scenegraph
addChild(myAstronaut);
}
//Line 5. Translate the game object to location (-2.0, 1.0, 2.0)
myAstronaut->translateTo(-2.0, 1.0, 2.0);
}
Code snippet: update() method
void Earth::update(double dt){
//Line 6. Translate the game object BY an amount of 0.01 units along the x-axis
myAstronaut->translateBy(0.01, 0.0, 0.0);
}

Results

In Xcode, select either the iOS or Mac device and then click on Run.

You should see the astronaut moving to the right as shown below:

image1

Source Code

Download the source game files from our Github Tutorials page.