Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Hands-On C++ Game Animation Programming
Hands-On C++ Game Animation Programming

Hands-On C++ Game Animation Programming: Learn modern animation techniques from theory to implementation with C++ and OpenGL

eBook
€20.98 €29.99
Paperback
€37.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Hands-On C++ Game Animation Programming

Chapter 2: Implementing Vectors

In this chapter, you will learn the basics of vector math. Much of what you will code throughout the rest of this book relies on having a strong understanding of vectors. Vectors will be used to represent displacement and direction.

By the end of this chapter, you will have implemented a robust vector library and will be able to perform a variety of vector operations, including component-wise and non-component-wise operations.

We will cover the following topics in this chapter:

  • Introducing vectors
  • Creating a vector
  • Understanding component-wise operations
  • Understanding non-component-wise operations
  • Interpolating vectors
  • Comparing vectors
  • Exploring more vectors

    Important information:

    In this chapter, you will learn how to implement vectors in an intuitive, visual way that relies on code more than math formulas. If you are interested in math formulas or want some interactive examples to try out, go to https://gabormakesgames.com/vectors.html.

Introducing vectors

What is a vector? A vector is an n-tuple of numbers. It represents a displacement measured as a magnitude and a direction. Each element of a vector is usually expressed as a subscript, such as (V0, V1, V2, … VN). In the context of games, vectors usually have two, three, or four components.

For example, a three-dimensional vector measures displacement on three unique axes: x, y, and z. Elements of vectors are often subscripted with the axis they represent, rather than an index. (VX, VY, VZ) and (V0, V1, V2) are used interchangeably.

When visualizing vectors, they are often drawn as arrows. The position of the base of an arrow does not matter because vectors measure displacement, not a position. The end of the arrow follows the displacement of the arrow on each axis.

For example, all of the arrows in the following figure represent the same vector:

Figure 2.1: Vector (2, 5) drawn in multiple locations

Figure 2.1: Vector (2, 5) drawn in multiple locations

Each arrow has the same length and points in the same direction, regardless of where it is positioned. In the next section, you will start to implement the vector structure that will be used throughout the rest of this book.

Creating a vector

Vectors will be implemented as structures, not classes. The vector struct will contain an anonymous union that allows the vector's components to be accessed as an array or as individual elements.

To declare the vec3 structure and the function headers, create a new file, vec3.h. Declare the new vec3 structure in this file. The vec3 struct needs three constructors—a default constructor, one that takes each component as an element, and one that takes a pointer to a float array:

#ifndef _H_VEC3_
#define _H_VEC3_
struct vec3 {
    union {
        struct  {
            float x;
            float y;
            float z;
        };
        float v[3];
    };
    inline vec3() : x(0.0f), y(0.0f), z(0.0f) { }
    inline vec3(float _x, float _y, float _z) :
        x(_x), y(_y), z(_z) { }
    inline vec3(float *fv) :
        x(fv[0]), y(fv[1]), z(fv[2]) { }
};
#endif 

The anonymous union in the vec3 struct allows data to be accessed using .x, .y, and .z notation, or as a contiguous array using .v. Before moving on to implementing functions that work on the vec3 struct, you need to consider comparing floating point numbers and whether or not to use an epsilon value.

Epsilon

Comparing floating point numbers is difficult. Instead of comparing two floating point numbers directly, you need to compare them using an epsilon. An epsilon is an arbitrarily small positive number that is the minimum difference two numbers need to have to be considered different numbers. Declare an epsilon constant in vec3.h:

#define VEC3_EPSILON 0.000001f

Important note:

You can learn more about floating point comparison at https://bitbashing.io/comparing-floats.html

With the vec3 structure created and the vec3 epsilon defined, you are ready to start implementing some common vector operations. In the next section, you're going to start by learning and implementing several component-wise operations.

Understanding component-wise operations

Several vector operations are just component-wise operations. A component-wise operation is one that you perform on each component of a vector or on like components of two vectors. Like components are components that have the same subscript. The component-wise operations that you will implement are as follows:

  • Vector addition
  • Vector subtraction
  • Vector scaling
  • Multiplying vectors
  • Dot product

Let's look at each of these in more detail.

Vector addition

Adding two vectors together yields a third vector, which has the combined displacement of both input vectors. Vector addition is a component-wise operation; to perform it, you need to add like components.

To visualize the addition of two vectors, draw the base of the second vector at the tip of the first vector. Next, draw an arrow from the base of the first vector to the tip of the second vector. This arrow represents the vector that is the result of the addition:

Figure 2.2: Vector addition

To implement vector addition in code, add like components of the input vectors. Create a new file, vec3.cpp. This is where you will define functions related to the vec3 struct. Don't forget to include vec3.h. Overload the + operator to perform vector addition. Don't forget to add the function signature to vec3.h:

vec3 operator+(const vec3 &l, const vec3 &r) {
    return vec3(l.x + r.x, l.y + r.y, l.z + r.z);
}

When thinking about vector addition, remember that a vector represents a displacement. When adding two vectors, the result is the combined displacement of both input vectors.

Vector subtraction

As with adding vectors, subtracting vectors is also a component-wise operation. You can think of subtracting vectors as adding the negative of the second vector to the first vector. When visualized as an arrow, subtraction points from the tip of the second vector to the tip of the first one.

To visually subtract vectors, place both vectors so they share the same origin. Draw a vector from the tip of the second arrow to the tip of the first one. The resulting arrow is the subtraction result vector:

Figure 2.3: Vector subtraction

Figure 2.3: Vector subtraction

To implement vector subtraction, subtract like components. Implement the subtraction function by overloading the - operator in vec3.cpp. Don't forget to add the function declaration to vec3.h:

vec3 operator-(const vec3 &l, const vec3 &r) {
    return vec3(l.x - r.x, l.y - r.y, l.z - r.z);
}

The steps and logic are very similar to vector addition. It might help to think of vector subtraction as adding a negative vector.

Scaling vectors

When a vector is scaled, it only changes in magnitude, not direction. As with addition and subtraction, scaling is a component-wise operation. Unlike addition and subtraction, a vector is scaled by a scalar, not another vector.

Visually, a scaled vector points in the same direction as the original vector, but it has a different length. The following figure shows two vectors: (2, 1) and (2, 4). Both vectors share the same direction, but the magnitude of the second vector is longer:

Figure 2.4: Vector scaling

Figure 2.4: Vector scaling

To implement vector scaling, multiply every component of the vector by the given scalar value.

Implement the scale function by overloading the * operator in vec3.cpp. Don't forget to add the function declaration to vec3.h:

vec3 operator*(const vec3 &v, float f) {
    return vec3(v.x * f, v.y * f, v.z * f);
}

Negating a vector can be done by scaling the vector by -1. When negating a vector, the vector maintains its magnitude but changes its direction.

Multiplying vectors

Vector multiplication can be considered a non-uniform scale. Instead of scaling every component of a vector by a scalar, to multiply two vectors, you scale every component of a vector by the like component of another vector.

You can implement vector multiplication by overloading the * operator in vec3.cpp. Don't forget to add the function declaration to vec3.h:

vec3 operator*(const vec3 &l, const vec3 &r) {
    return vec3(l.x * r.x, l.y * r.y, l.z * r.z);
}

The result generated by multiplying two vectors will have a different direction and magnitude.

Dot product

The dot product is used to measure how similar two vectors are. Given two vectors, the dot product returns a scalar value. The result of the dot product has the following properties:

  • It is positive if the vectors point in the same direction.
  • It is negative if the vectors point in opposite directions.
  • It is 0 if the vectors are perpendicular.

If both input vectors have a unit length (you will learn about unit length vectors in the Normal vectors section of this chapter), the dot product will have a range of -1 to 1.

The dot product between two vectors, A and B, is equal to the length of A multiplied by the length of B multiplied by the cosine of the angle between the two vectors:

The easiest way to calculate the dot product is to sum the products of like components in the input vectors:

Implement the dot function in vec3.cpp. Don't forget to add the function definition to vec3.h:

float dot(const vec3 &l, const vec3 &r) {
    return l.x * r.x + l.y * r.y + l.z * r.z;
}

The dot product is one of the most used operations for video games. It's often used to check angles and in lighting calculations.

With the dot product, you have implemented the common component-wise operations of vectors. Next, you will learn about some of the non-component-wise operations that can be performed on vectors.

Understanding non-component-wise operations

Not all vector operations are component-wise; some operations require more math. In this section, you are going to learn how to implement common vector operations that are not component-based. These operations are as follows:

  • How to find the length of a vector
  • What a normal vector is
  • How to normalize a vector
  • How to find the angle between two vectors
  • How to project vectors and what rejection is
  • How to reflect vectors
  • What the cross product is and how to implement it

Let's take a look at each one in more detail.

Vector length

Vectors represent a direction and a magnitude; the magnitude of a vector is its length. The formula for finding the length of a vector comes from trigonometry. In the following figure, a two-dimensional vector is broken down into parallel and perpendicular components. Notice how this forms a right triangle, with the vector being the hypotenuse:

Figure 2.5: A vector broken down into parallel and perpendicular components

Figure 2.5: A vector broken down into parallel and perpendicular components

The length of the hypotenuse of a right triangle can be found with the Pythagorean theorem, A2 + B2 = C2. This function extends to three dimensions by simply adding a Z component—X2 + Y2 + Z2 = length2.

You may have noticed a pattern here; the squared length of a vector equals the sum of its components. This could be expressed as a dot product—Length2(A) = dot(A, A):

Important note:

Finding the length of a vector involves a square root operation, which should be avoided when possible. When checking the length of a vector, the check can be done in squared space to avoid the square root. For example, if you wanted to check if the length of vector A is less than 5, that could be expressed as (dot(A, A) < 5 * 5).

  1. To implement the square length function, sum the result of squaring each component of the vector. Implement the lenSq function in vec3.cpp. Don't forget to add the function declaration to vec3.h:
    float lenSq(const vec3& v) {
        return v.x * v.x + v.y * v.y + v.z * v.z;
    }
  2. To implement the length function, take the square root of the result of the square length function. Take care not to call sqrtf with 0. Implement the lenSq function in vec3.cpp. Don't forget to add the function declaration to vec3.h:
    float len(const vec3 &v) {
        float lenSq = v.x * v.x + v.y * v.y + v.z * v.z;
        if (lenSq < VEC3_EPSILON) {
            return 0.0f;
        }
        return sqrtf(lenSq);
    }

    Important note:

    You can find the distance between two vectors by taking the length of the difference between them. For example, float distance = len(vec1 - vec2).

Normalizing vectors

A vector with a length of 1 is called a normal vector (or unit vector). Generally, unit vectors are used to represent a direction without a magnitude. The dot product of two unit vectors will always fall in the -1 to 1 range.

Aside from the 0 vector, any vector can be normalized by scaling the vector by the inverse of its length:

  1. Implement the normalize function in vec3.cpp. Don't forget to add the function declaration to vec3.h:
    void normalize(vec3 &v) {
        float lenSq = v.x * v.x + v.y * v.y + v.z * v.z;
        if (lenSq < VEC3_EPSILON) { return; }
        float invLen = 1.0f / sqrtf(lenSq);    
        v.x *= invLen;
        v.y *= invLen;
        v.z *= invLen;
    }
  2. Implement the normalized function in vec3.cpp. Don't forget to add the function declaration to vec3.h:
    vec3 normalized(const vec3 &v) {
        float lenSq = v.x * v.x + v.y * v.y + v.z * v.z;
        if (lenSq < VEC3_EPSILON) { return v; }
        float invLen = 1.0f / sqrtf(lenSq);
        return vec3(
            v.x * invLen,
            v.y * invLen,
            v.z * invLen
        );
    }

The normalize function takes a reference to a vector and normalizes it in place. The normalized function, on the other hand, takes a constant reference and does not modify the input vector. Instead, it returns a new vector.

The angle between vectors

If two vectors are of unit length, the angle between them is the cosine of their dot product:

If the two vectors are not normalized, the dot product needs to be divided by the product of the length of both vectors:

To find the actual angle, not just the cosine of it, we need to take the inverse of the cosine on both sides, which is the arccosine function:

Implement the angle function in vec3.cpp. Don't forget to add the function declaration to vec3.h:

float angle(const vec3 &l, const vec3 &r) {
    float sqMagL = l.x * l.x + l.y * l.y + l.z * l.z;
    float sqMagR = r.x * r.x + r.y * r.y + r.z * r.z;
    if (sqMagL<VEC3_EPSILON || sqMagR<VEC3_EPSILON) {
        return 0.0f;
    }
    float dot = l.x * r.x + l.y * r.y + l.z * r.z;
    float len = sqrtf(sqMagL) * sqrtf(sqMagR);
    return acosf(dot / len);
}

Important note:

The acosf function returns angles in radians. To convert radians to degrees, multiply by 57.2958f. To convert degrees to radians, multiply by 0.0174533f.

Vector projection and rejection

Projecting vector A onto vector B yields a new vector that has the length of A in the direction of B. A good way to visualize vector projection is to imagine that vector A is casting a shadow onto vector B, as shown:

Figure 2.6: Vector A casting a shadow onto vector B

Figure 2.6: Vector A casting a shadow onto vector B

To calculate the projection of A onto B (projB A), vector A must be broken down into parallel and perpendicular components with respect to vector B. The parallel component is the length of A in the direction of B—this is the projection. The perpendicular component is the parallel component subtracted from A—this is the rejection:

Figure 2.7: Vector projection and rejection showing parallel and perpendicular vectors

Figure 2.7: Vector projection and rejection showing parallel and perpendicular vectors

If the vector that is being projected onto (in this example, vector B) is a normal vector, then finding the length of A in the direction of B is a simple dot product between A and B. However, if neither input vector is normalized, the dot product needs to be divided by the length of vector B (the vector being projected onto).

Now that the parallel component of A with respect to B is known, vector B can be scaled by this component. Again, if B wasn't of unit length, the result will need to be divided by the length of vector B.

Rejection is the opposite of projection. To find the rejection of A onto B, subtract the projection of A onto B from vector A:

  1. Implement the project function in vec3.cpp. Don't forget to add the function declaration to vec3.h:
    vec3 project(const vec3 &a, const vec3 &b) {
        float magBSq = len(b);
        if (magBSq < VEC3_EPSILON) {
            return vec3();
        }
        float scale = dot(a, b) / magBSq;
        return b * scale;
    }
  2. Implement the reject function in vec3.cpp. Don't forget to declare this function in vec3.h:
    vec3 reject(const vec3 &a, const vec3 &b) {
        vec3 projection = project(a, b);
        return a - projection;
    }

Vector projection and rejection are generally used for gameplay programming. It is important that they are implemented in a robust vector library.

Vector reflection

Vector reflection can mean one of two things: a mirror-like reflection or a bounce-like reflection. The following figure shows the different types of reflections:

Figure 2.8: A comparison of the mirror and bounce reflections

Figure 2.8: A comparison of the mirror and bounce reflections

The bounce reflection is more useful and intuitive than the mirror reflection. To make a bounce projection work, project vector A onto vector B. This will yield a vector that points in the opposite direction to the reflection. Negate this projection and subtract it twice from vector A. The following figure demonstrates this:

Figure 2.9: Visualizing a bounce reflection

Figure 2.9: Visualizing a bounce reflection

Implement the reflect function in vec3.cpp. Don't forget to add the function declaration to vec3.h:

vec3 reflect(const vec3 &a, const vec3 &b) {
    float magBSq = len(b);
    if (magBSq < VEC3_EPSILON) {
        return vec3();
    }
    float scale = dot(a, b) / magBSq;
    vec3 proj2 = b * (scale * 2);
    return a - proj2;
}

Vector reflection is useful for physics and AI. We won't need to use reflection for animation, but it's good to have the function implemented in case it is needed.

Cross product

When given two input vectors, the cross product returns a third vector that is perpendicular to both input vectors. The length of the cross product equals the area of the parallelogram formed by the two vectors.

The following figure demonstrates what the cross product looks like visually. The input vectors don't have to be 90 degrees apart, but it's easier to visualize them this way:

Figure 2.10: Visualizing the cross product

Figure 2.10: Visualizing the cross product

Finding the cross product involves some matrix math, which will be covered in more depth in the next chapter. For now, you need to create a 3x3 matrix, with the top row being the result vector. The second and third rows should be filled in with the input vectors. The value of each component of the result vector is the minor of that element in the matrix.

What exactly is the minor of an element in a 3x3 matrix? It's the determinant of a smaller, 2x2 sub-matrix. Assuming you want to find the value of the first component, ignore the first row and column, which yields a smaller 2x2 sub-matrix. The following figure shows the smaller sub-matrix for each component:

Figure 2.11: The submatrix for each component

Figure 2.11: The submatrix for each component

To find the determinant of a 2x2 matrix, you need to cross multiply. Multiply the top-left and bottom-right elements, then subtract the product of the top-right and bottom-left elements. The following figure shows this for each element of the resulting vector:

Figure 2.12: The determinant of each component in the result vector

Figure 2.12: The determinant of each component in the result vector

Implement the cross product in vec3.cpp. Don't forget to add the function declaration to vec3.h:

vec3 cross(const vec3 &l, const vec3 &r) {
    return vec3(
        l.y * r.z - l.z * r.y,
        l.z * r.x - l.x * r.z,
        l.x * r.y - l.y * r.x
    );
}

The dot product has a relationship to the cosine of the angle between two vectors and the cross product has a relationship to the sine of the angle between the two vectors. The length of the cross product between the two vectors is the product of both vectors, lengths, scaled by the sine of the angle between them:

In the next section, you will learn how to interpolate between vectors using three different techniques.

Interpolating vectors

Two vectors can be interpolated linearly by scaling the difference between the two vectors and adding the result back to the original vector. This linear interpolation is often abbreviated to lerp. The amount to lerp by is a normalized value between 0 and 1; this normalized value is often represented by the letter t. The following figure shows lerp between two vectors with several values for t:

Figure 2.13: Linear interpolation

Figure 2.13: Linear interpolation

When t = 0, the interpolated vector is the same as the starting vector. When t = 1, the interpolated vector is the same as the end vector.

Implement the lerp function in vec3.cpp. Don't forget to add the function declaration to vec3.h:

vec3 lerp(const vec3 &s, const vec3 &e, float t) {
    return vec3(
        s.x + (e.x - s.x) * t,
        s.y + (e.y - s.y) * t,
        s.z + (e.z - s.z) * t
    );
}

Linearly interpolating between two vectors will always take the shortest path from one vector to another. Sometimes, the shortest path isn't the best path; you may need to interpolate between two vectors along the shortest arc, instead. Interpolating on the shortest arc is called a spherical linear interpolation (slerp). The following figure shows the difference between the slerp and lerp processes for several values of t:

Figure 2.14: Comparing slerp and lerp

Figure 2.14: Comparing slerp and lerp

To implement slerp, find the angle between the two input vectors. Assuming the angle is known, the formula for slerp is as follows

Implement the slerp function in vec3.cpp. Don't forget to add the function declaration to vec3.h. Take care of when the value of t is close to 0, as slerp will yield unexpected results. When the value of t is close to 0, fall back on lerp or normalized lerp (nlerp) (which will be covered next):

vec3 slerp(const vec3 &s, const vec3 &e, float t) {
    if (t < 0.01f) {
        return lerp(s, e, t);
    }
    vec3 from = normalized(s);
    vec3 to = normalized(e);
    float theta = angle(from, to);
    float sin_theta = sinf(theta);
    float a = sinf((1.0f - t) * theta) / sin_theta;
    float b = sinf(t * theta) / sin_theta;
    return from * a + to * b;
}

The last interpolation method to cover is nlerp. nlerp approximates slerp. Unlike slerp, nlerp is not constant in velocity. nlerp is much faster than slerp and easier to implement; just normalize the result of lerp. The following figure compares lerp, slerp, and nlerp, where t = 0.25:

Figure 2.15: Comparing lerp, slerp, and nlerp

Figure 2.15: Comparing lerp, slerp, and nlerp

Implement the nlerp function in vec3.cpp. Don't forget to add the function declaration to vec3.h:

vec3 nlerp(const vec3 &s, const vec3 &e, float t) {
    vec3 linear(
        s.x + (e.x - s.x) * t,
        s.y + (e.y - s.y) * t,
        s.z + (e.z - s.z) * t
    );
    return normalized(linear);
}

Generally, nlerp is a better choice than slerp. It's a very close approximation and much cheaper to calculate. The only time it makes sense to use slerp instead is if constant interpolation velocity is required. Throughout this book, you will be using lerp and nlerp to interpolate between vectors.

In the next section, you will learn how to use an epsilon value to compare vectors for equality and inequality.

Comparing vectors

The last operation that needs to be implemented is vector comparison. Comparison is a component-wise operation; each element must be compared using an epsilon. Another way to measure whether two vectors are the same is to subtract them. If they were equal, subtracting them would yield a vector with no length.

Overload the == and != operators in vec3.cpp. Don't forget to add the function declarations to vec3.h:

bool operator==(const vec3 &l, const vec3 &r) {
    vec3 diff(l - r);
    return lenSq(diff) < VEC3_EPSILON;
}
bool operator!=(const vec3 &l, const vec3 &r) {
    return !(l == r);
}

Important note:

Finding the right epsilon value to use for comparison operations is difficult. In this chapter, you declared 0.000001f as the epsilon. This value is the result of some trial and error. To learn more about comparing floating point values, check out https://bitbashing.io/comparing-floats.html.

In the next section, you will implement vectors with two and four components. These vectors will only be used as a convenient way to store data; they won't actually need any math operations implemented on them.

Exploring more vectors

At some point later on in this book, you will need to utilize two- and four-component vectors as well. The two- and four-component vectors don't need any mathematical functions defined as they will be used exclusively as containers used to pass data to the GPU.

Unlike the three-component vector you have implemented, the two- and four-component vectors need to exist as both integer and floating point vectors. To avoid duplicating code, both structures will be implemented using a template:

  1. Create a new file, vec2.h, and add the definition of the vec2 struct. All the vec2 constructors are inline; there is no need for a cpp file. The TVec2 struct is templated and typedef is used to declare vec2 and ivec2:
    template<typename T>
    struct TVec2 {
        union {
            struct {
                T x;
                T y;
            };
            T v[2];
        };
        inline TVec2() : x(T(0)), y(T(0)) { }
        inline TVec2(T _x, T _y) :
            x(_x), y(_y) { }
        inline TVec2(T* fv) :
            x(fv[0]), y(fv[1]) { }
    };
    typedef TVec2<float> vec2;
    typedef TVec2<int> ivec2;
  2. Similarly, create a vec4.h file, which will hold the vec4 structure:
    template<typename T>
    struct TVec4 {
        union {
            struct {
                T x;
                T y;
                T z;
                T w;
            };
            T v[4];
        };
        inline TVec4<T>(): x((T)0),y((T)0),z((T)0),w((T)0){}
        inline TVec4<T>(T _x, T _y, T _z, T _w) :
            x(_x), y(_y), z(_z), w(_w) { }
        inline TVec4<T>(T* fv) :
            x(fv[0]), y(fv[ ]), z(fv[2]), w(fv[3]) { }
    };
    typedef TVec4<float> vec4;
    typedef TVec4<int> ivec4;
    typedef TVec4<unsigned int> uivec4;

The declaration of the vec2, ivec2, vec4, and ivec4 structs are all very similar to the declaration of the vec3 struct. All these structures can be accessed using component subscripts or as a pointer to a linear array of memory. They all have similar constructors, as well.

Summary

In this chapter, you have learned the vector math required to create a robust animation system. Animation is a math-heavy topic; the skills you have learned in this chapter are required to complete the rest of this book. You implemented all the common vector operations for three-component vectors. The vec2 and vec4 structures don't have a full implementation like vec3, but they are only used to send data to the GPU.

In the next chapter, you will continue to learn more about game-related math by learning about matrices.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Build a functional and production-ready modern animation system with complete features using C++
  • Learn basic, advanced, and skinned animation programming with this step-by-step guide
  • Discover the math required to implement cutting edge animation techniques such as inverse kinematics and dual quaternions

Description

Animation is one of the most important parts of any game. Modern animation systems work directly with track-driven animation and provide support for advanced techniques such as inverse kinematics (IK), blend trees, and dual quaternion skinning. This book will walk you through everything you need to get an optimized, production-ready animation system up and running, and contains all the code required to build the animation system. You’ll start by learning the basic principles, and then delve into the core topics of animation programming by building a curve-based skinned animation system. You’ll implement different skinning techniques and explore advanced animation topics such as IK, animation blending, dual quaternion skinning, and crowd rendering. The animation system you will build following this book can be easily integrated into your next game development project. The book is intended to be read from start to finish, although each chapter is self-contained and can be read independently as well. By the end of this book, you’ll have implemented a modern animation system and got to grips with optimization concepts and advanced animation techniques.

Who is this book for?

This book is for professional, independent, and hobbyist developers interested in building a robust animation system from the ground up. Some knowledge of the C++ programming language will be helpful.

What you will learn

  • Get the hang of 3D vectors, matrices, and transforms, and their use in game development
  • Discover various techniques to smoothly blend animations
  • Get to grips with GLTF file format and its design decisions and data structures
  • Design an animation system by using animation tracks and implementing skinning
  • Optimize various aspects of animation systems such as skinned meshes, clip sampling, and pose palettes
  • Implement the IK technique for your game characters using CCD and FABRIK solvers
  • Understand dual quaternion skinning and how to render large instanced crowds
Estimated delivery fee Deliver to Austria

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 12, 2020
Length: 368 pages
Edition : 1st
Language : English
ISBN-13 : 9781800208087
Languages :
Concepts :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Austria

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Publication date : Jun 12, 2020
Length: 368 pages
Edition : 1st
Language : English
ISBN-13 : 9781800208087
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 104.97
Beginning C++ Game Programming
€33.99
Game Development Projects with Unreal Engine
€32.99
Hands-On C++ Game Animation Programming
€37.99
Total 104.97 Stars icon
Banner background image

Table of Contents

16 Chapters
Chapter 1: Creating a Game Window Chevron down icon Chevron up icon
Chapter 2: Implementing Vectors Chevron down icon Chevron up icon
Chapter 3: Implementing Matrices Chevron down icon Chevron up icon
Chapter 4: Implementing Quaternions Chevron down icon Chevron up icon
Chapter 5: Implementing Transforms Chevron down icon Chevron up icon
Chapter 6: Building an Abstract Renderer Chevron down icon Chevron up icon
Chapter 7: Exploring the glTF File Format Chevron down icon Chevron up icon
Chapter 8: Creating Curves, Frames, and Tracks Chevron down icon Chevron up icon
Chapter 9: Implementing Animation Clips Chevron down icon Chevron up icon
Chapter 10: Mesh Skinning Chevron down icon Chevron up icon
Chapter 11: Optimizing the Animation Pipeline Chevron down icon Chevron up icon
Chapter 12: Blending between Animations Chevron down icon Chevron up icon
Chapter 13: Implementing Inverse Kinematics Chevron down icon Chevron up icon
Chapter 14: Using Dual Quaternions for Skinning Chevron down icon Chevron up icon
Chapter 15: Rendering Instanced Crowds Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(4 Ratings)
5 star 75%
4 star 0%
3 star 0%
2 star 0%
1 star 25%
Walt Georgio Apr 23, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I read the kindle edition. This book provides excellent and fairly in depth knowledge if you want to learn animation using C++ and openGL with insights on topics such file formats to store animation data (GLTF in this case), inverse kinematic techniques (CCD and Fabrik) and mesh skinning, to mention a few. Each chapter has working code examples that illustrate the topics involved in animating a character. The book ends with a chapter on baking animations to textures.An interesting and useful read if you're interested in "under the hood" computer animation techniques.
Amazon Verified review Amazon
Aitor Jul 10, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is the book if you want to learn how to properly program animations from scratch. I have been searching for this knowledge on the internet ages and it was always really hard to find a step by step guide that I could understand.
Amazon Verified review Amazon
Jadan Bliss Jul 23, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I absolutely recommend this book. It is a great introduction to core game/technical animation concepts, uses a free open and modern file format (glTF from the Khronos Group), and provides a solid foundation on which to expand. It takes advanced concepts and--between illustration, explanation, and example code--makes them clear enough that the reader can modify them for their own needs with little to no further assistance. This book is a great expansion and companion to the author's blog. The clarity of his explanations on his blog were what convinced me to buy the book, and I'm glad I did.I spent about a week to build everything in my spare time, and went from having zero familiarity with the programming side of modern game animation, dual quaternions, or IK, to understanding and knowing enough to consider the needs of my upcoming game project in modifying the codebase built with this book.My advice to fellow readers: The book moves at a good pace, and it's easy to get caught up in just typing in what's in the book. Take time to look at, and ideally type in yourself, the sample application code (Sample.h and Sample.cpp in each sample application's Code folder) in the download. It's a great way to cement understanding and apply the concepts and techniques presented in the book. When in doubt about your code's behavior, the download also contains compiled versions of all the examples in one convenient program.
Amazon Verified review Amazon
mathman54 Oct 16, 2020
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I have gotten to chapter 1. The author leaves a lot to be desired. No use of bold lettering for important concepts. He acknowledges that there are going to be beginning users of the book. He does not write for these beginning users. I can troubleshoot basic c++ programs but his text is more for advanced users. He does not give specific instructions for the beginner.In chapter 1 he has you write a .h file. But does not give you what you need to write the file properly. His writing is muddy. I am not impressed with Packt books.I have another book from packt. When I compile the code I get all kinds of errors.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact [email protected] with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at [email protected] using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on [email protected] with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on [email protected] within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on [email protected] who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on [email protected] within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela