Link

Animating a 3D character

Steps to create a 3D animation

The steps to create a 3D animation object are summarized below:

  1. Create a 3D Animation object
  2. Link the animation object to the 3D model
  3. Load keyframes and bone data into the animation object
  4. Play the animation

image1

Step 1. Create an Animation Object

The first step is to represent the animation programmatically by creating an object of type U4DEngine::U4DAnimation and link the animation object to the 3D model.

image

This is shown in the snippet below:

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

Step 2. Load animation data

Next, we load the animation keyframes, bone space and bone weights into the animation object using the method U4DEngine::U4DGameObject::loadAnimationToModel().

//Line 4. Load animation data into the animation object
if(myAstronaut->loadAnimationToModel(walkAnimation, "walking", "walkinganimation.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. For example, you can set the animation to only play once or play continuously using the method U4DEngine::U4DAnimation::setPlayContinuousLoop().

Step 3. Play the animation

At this point, the animation object is linked to the 3D model, and we can play the animation using the method U4DEngine::U4DAnimation::play().

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

    walkAnimation->play();

}

To stop the animation, the U4DEngine::U4DAnimation::stop() method is called, as shown below:

//Check if the animation object exist and stop the animation
if (walkAnimation!=nullptr) {

    walkAnimation->stop();

}