Skip to content

Creating an Animation

Coela Can't! edited this page Aug 20, 2021 · 6 revisions

Animations

What is an Animation? The Animation parent object contains a scene, and at least one Object3D instance. The Animation object is meant to allow easy switching between multiple animations. Animations can be faded in, faded out, and require updating every frame to calculate the internal changes that are either mapped to sensors, KeyframeTracks, or FunctionGenerators.

Creating an Animation Object

To create an Animation child object, you must initialize the parent constructor, and override the FadeIn, FadeOut, and Update functions. A minimal Animation object would look as follows:

#pragma once

// Include necessary files
#include "Animation.h"
#include "..\Math\FunctionGenerator.h"
#include "..\Objects\Spyro.h"

// Define class as child of animation object
class SpyroAnimation : public Animation{
private:
    // Create .FBX or .OBJ object instance
    Spyro spyro;

    // Create FunctionGenerator or KeyFrameTrack objects to control parameters
    FunctionGenerator fGenRotation = FunctionGenerator(FunctionGenerator::Sine, -30.0f, 30.0f, 2.6f);
    FunctionGenerator fGenScale = FunctionGenerator(FunctionGenerator::Sine, 5.0f, 10.0f, 4.2f);

public:
    // Create default constructor and initialize Animation parent with max number of objects in animation
    SpyroAnimation() : Animation(1) {
        // Add object instance to parent scene
        scene->AddObject(spyro.GetObject());
    }
    
    // Override fade in function if necessary
    void FadeIn(float stepRatio) override {}

    // Override fade out function if necessary
    void FadeOut(float stepRatio) override {}

    // Override update function, pass in the current ratio of completion for the animation 0.0f -> 1.0f linear ramp
    void Update(float ratio) override {
        // Get current value of function generator objects
        float x = fGenRotation.Update();
        float sx = fGenScale.Update();
        
        // Create or modify object in some manner
        // This creates a quaternion rotation with an oscillating X axis and a constantly rotating Y axis
        Quaternion rotation = Rotation(EulerAngles(Vector3D(x, ratio * 5760.0f, 0), EulerConstants::EulerOrderXZYS)).GetQuaternion();

        // Reset the Object3D instance to reset the previously modified vertices to the original value in flash
        spyro.GetObject()->ResetVertices();

        // Update the transform with the new/desired parameters to rotate, scale, or set the position of the object
        spyro.GetObject()->GetTransform()->SetRotation(rotation);
        spyro.GetObject()->GetTransform()->SetScale(Vector3D(sx, sx, sx));
        spyro.GetObject()->GetTransform()->SetPosition(Vector3D(0.0f, 0.0f, 600.0f));

        // Updates the object to calculate the transformation set by the previous modifications to the Transform object
        spyro.GetObject()->UpdateTransform();
    }
};

Additional Functionality