Lab 4: Create a 3D animation object

Our goal is to apply a walking animation to the 3D model using the steps mentioned in this article.

Along the way, you will learn how to create an Animation object, how to import animation data and play the animation. The Untold Engine comes with a default animation which we will use for this lab.

image

Step 1. Create a 3D animation object

In the engine, open the file Earth.mm.

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

Once you have created the character, create an Animation object and link it to the 3D model as shown in line 3.

//Line 3. Create an Animation object and link it to the 3D model
walkAnimation=new U4DEngine::U4DAnimation(myAstronaut);

Step 2. Load animation data

Next, we need to load animation data into the animation object. This operation is done using the loadAnimationToModel() method, as shown below:

//Line 4. Load animation data into the animation object
if(myAstronaut->loadAnimationToModel(walkAnimation, "walking", "astronautWalkAnim.u4d")){
//If animation data was successfully loaded, you can set other parameters here. For now, we won't do this.
}

The loadAnimationToModel() method loads the animation data into the animation object (line 4).

The method requires the animation object, the name of the animation (specified in Blender) and the file that contains the animation data.

Note that the loadAnimationToModel() method returns a boolean value. The method will return false if the file was not found, or if the animation data is corrupted.

If the loadAnimationToModel() method returns true, you can set additional properties to the animation object. However, for the sake of simplicity, we won't set any other properties.

Step 3. Play the animation

Before we play the animation, we want to verify that the Animation object does exist. If it does, then we play the animation as shown in line 7.

//Line 7. Check if the animation object exist and play the animation
if (walkAnimation!=nullptr) {
walkAnimation->play();
}

Complete code snippet

The complete code to animate a 3D character should look as follows. Pay particular attention to lines 3-4, 7:

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. Create an Animation object and link it to the 3D model
walkAnimation=new U4DEngine::U4DAnimation(myAstronaut);
//Line 4. Load animation data into the animation object
if(myAstronaut->loadAnimationToModel(walkAnimation, "walking", "astronautWalkAnim.u4d")){
//If animation data was successfully loaded, you can set other parameters here. For now, we won't do this.
}
//Line 5. Load rendering information into the GPU
myAstronaut->loadRenderingInformation();
//Line 6. Add astronaut to the scenegraph
addChild(myAstronaut);
}
//Line 7. Check if the animation object exist and play the animation
if (walkAnimation!=nullptr) {
walkAnimation->play();
}
}

Results

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

The game engine should play the animation shown below:

image1

Source Code

Download the source game files from our Github Tutorials page.