Lab 2B: Rotate a 3D game object

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

First, you will learn how to rotate an entity To a particular orientation. Lastly, you will learn how to rotate an object By an angle amount.

Rotate To an Orientation

Our objective is to rotate the entity 45 degrees about the y-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, rotate the character using the rotateTo() method, as shown in the snippet below.

//Line 5. Rotate the character 45 degress about the y-axis
myAstronaut->rotateTo(0.0, 45.0, 0.0);

Complete code snippet

The complete code to rotate the character 45 degrees about the y-axis 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. Rotate the character 45 degress about the y-axis
myAstronaut->rotateTo(0.0, 45.0, 0.0);
}

Results

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

The character should rotate 45 degrees about the y-axis.

image

Rotate By an Amount

Now, let's rotate the astronaut by an amount of 1.0 degree about the y-axis. Again, since we want this rotation to be continuously applied, we are going to rotate the character in the update() method.

Scroll down to the update() method, and rotate the character using the rotateBy() method, as shown in the snippet below.

void Earth::update(double dt){
//Line 6. Rotate the character by 1.0 degree about the y-axis
myAstronaut->rotateBy(0.0, 1.0, 0.0);
}

Complete code snippet

The complete code to rotate 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. Rotate the character 45 degress about the y-axis
myAstronaut->rotateTo(0.0, 45.0, 0.0);
}
Code snippet: update() method
void Earth::update(double dt){
//Line 6. Rotate the character by 1.0 degree about the y-axis
myAstronaut->rotateBy(0.0, 1.0, 0.0);
}

Results

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

The character should continuously rotate as shown below:

image1

Source Code

Download the source game files from our Github Tutorials page.