Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 9

Vector Operations: Add, Subtract, and Scalar Multiply

Recommended resources

Hello! Welcome to the first lesson in our second module, 3D Math Fundamentals.

In the previous module, we built a solid foundation in C programming, culminating in an understanding of how our source code is compiled and linked into an executable program. We've focused on the tools and the language. Now, we pivot to the concepts that bring a 3D world to life. This module is all about the mathematics that underpins 3D graphics.

You've likely encountered vectors in your physics and calculus courses to describe quantities like force, displacement, or fluid flow. In game development, we use them in a very similar but highly visual and interactive way. They are the fundamental atoms of our 3D universe.

Today, we'll start with the absolute basics. This lesson will cover what 2D and 3D vectors are in the context of game development and how to perform the most fundamental operations on them: addition, subtraction, and scalar multiplication. Mastering these simple operations is the first and most critical step toward creating movement, calculating trajectories, and building a camera system.

What is a Vector?

At its core, a vector is a quantity that has both magnitude (length or size) and direction. Think about giving someone directions: "walk 100 meters north." You've just described a vector.

In game development, we use vectors to represent several key properties:

  • Position: Where an object is in the world (e.g., (x, y, z) coordinates).
  • Velocity: How an object's position is changing over time (e.g., moving 2 meters per second along the x-axis).
  • Direction: Which way an object is facing or moving, independent of its speed.

To get a practical sense of this, let's start with a short article that frames vectors in the context of games.

Linear algebra for game developers ~ part 1

The article 'Linear algebra for game developers ~ part 1' from the Wolfire Games Blog gives excellent, straightforward examples of how vectors are used in games.

Read the first two sections, 'Why do we care about linear algebra?' and 'What is a vector?'. Pay attention to how a simple list of numbers can represent a position, a velocity, or a direction depending on the context.

As the article explains, a vector is mathematically just a list of numbers, which we call components. For 2D graphics, we use two components [x, y], and for 3D, we use three: [x, y, z]. Each component represents a signed displacement along its corresponding axis.

A crucial concept is that a vector itself does not have a position. It only describes a displacement. Imagine an arrow floating in space. Its length and the direction it points are what matter, not where its tail is located. This is why the same vector, say [3, 5, 2], can represent the velocity of a spaceship or the displacement from your character to a treasure chest.

To dig a bit deeper into this distinction, the following reading offers a more formal definition.

Vectors - 3D Math Primer for Graphics and Game Development

The book '3D Math Primer for Graphics and Game Development' provides a thorough breakdown of vector concepts. These sections will clarify the mathematical and geometric definitions.

Read the following sections: Section 2.1: 'Mathematical Definition of Vector...'. This formally defines vectors and scalars. Section 2.2: 'Geometric Definition of Vector'. This section emphasizes the critical idea that vectors have magnitude and direction, but not position. Section 2.3: 'Specifying Vectors with Cartesian Coordinates'. This connects the mathematical and geometric ideas. Focus on the difference between the abstract idea of a vector (an arrow) and how we represent it with numbers (coordinates).


Core Vector Operations

Now that we have a solid idea of what vectors are, let's learn how to manipulate them. These operations are simple component-wise calculations, but their geometric interpretations are what make them so powerful.

The following video gives a fantastic, quick overview of these operations with game-centric examples.

Game Math is easier than you think.

The video 'Game Math is easier than you think' by Bitlytic provides a very intuitive visual explanation of vector operations.

Watch the segments covering addition, subtraction, and multiplication. Addition (01:43 - 02:29): Notice how adding a velocity vector to a position vector creates movement. Subtraction (02:29 - 03:02): Focus on how subtraction gives you a vector pointing from one point to another. Scalar Multiplication (04:22 - 04:50): See how this scales the length of a vector.

Let's break down each operation with its rule, geometric meaning, and a key application.

1. Vector Addition

  • The Rule: To add two vectors, add their corresponding components.

  • Geometric Interpretation: Geometrically, this is the "head-to-tail" rule. If you place the tail of vector at the head of vector , the resulting vector goes from the tail of to the head of .

  • Key Application: Movement. This is the foundation of all movement in a game. Each frame, you update an object's position by adding its velocity vector (scaled by the time elapsed).
    new_position = current_position + velocity

The Wolfire Games blog post has a perfect illustration of this.

Linear algebra for game developers ~ part 1

Let's revisit the Wolfire Games blog post to see a great example of vector addition in action.

Read the section 'Vector addition'. The Mario jumping example clearly shows how repeatedly adding velocity to position and acceleration to velocity creates a parabolic arc—basic physics!

2. Vector Subtraction

  • The Rule: To subtract two vectors, subtract their corresponding components. This is equivalent to adding the negation of a vector: . A negated vector has the same magnitude but points in the opposite direction.
Visualizing Vector Negation
Vector negation flips the direction of a vector while keeping its magnitude the same. For example, the vector `[2, 2]` becomes `[-2, -2]`.
  • Geometric Interpretation: The vector is the vector that points from the tip of to the tip of .
Graphical Representation of 2D Vector Addition and Subtraction
This diagram shows the geometric interpretation of vector addition and subtraction. `a + b` is the diagonal of the parallelogram formed by `a` and `b`. `a - b` is found by adding `a` and `-b`.
  • Key Application: Finding Direction. This is extremely useful. If you have the position of the player and an enemy, how do you make the enemy face the player? You subtract the positions!
    direction_to_player = player_position - enemy_position

Linear algebra for game developers ~ part 1

The Wolfire blog has another concise example for this.

Read the section 'Vector subtraction'. The laser rifle example is a classic use case for this operation.

3. Scalar-Vector Multiplication

A scalar is just a regular number (like 5, -0.5, or ).

  • The Rule: To multiply a vector by a scalar, multiply each component of the vector by that scalar.

  • Geometric Interpretation: Multiplying by a scalar s scales the vector's magnitude (length) by a factor of |s|. If s is negative, it also reverses the vector's direction.

  • Key Application: Controlling Speed and Scale. If you have a direction vector (which we'll see later is a vector of length 1), you can multiply it by a scalar speed to get a velocity vector. You can also use it to scale an object or apply forces like friction.
    velocity = direction * speed

Linear algebra for game developers ~ part 1

Let's check the final operation in the Wolfire blog post.

Read the section 'Scalar-vector multiplication'. The air resistance example shows how you can use scalar multiplication to gradually reduce a vector's magnitude.


Implementing Vectors in C

This is all great in theory, but how do we use this in our C code? In Module 1, you learned about structs for creating custom data types. A struct is the perfect tool for representing a vector.

For a 3D vector, we could define it like this:

typedef struct {
    float x;
    float y;
    float z;
} Vec3;

We typically use float for vector components because graphics hardware is highly optimized for floating-point math.

To perform our operations, we would write simple functions that take Vec3 structs as arguments and return a new Vec3 struct.

// Adds two vectors and returns the result
Vec3 vec3_add(Vec3 a, Vec3 b) {
    Vec3 result = { a.x + b.x, a.y + b.y, a.z + b.z };
    return result;
}

// Subtracts vector b from vector a and returns the result
Vec3 vec3_subtract(Vec3 a, Vec3 b) {
    Vec3 result = { a.x - b.x, a.y - b.y, a.z - b.z };
    return result;
}

// Multiplies a vector by a scalar and returns the result
Vec3 vec3_scale(Vec3 v, float scalar) {
    Vec3 result = { v.x * scalar, v.y * scalar, v.z * scalar };
    return result;
}

This is a practical application of the C skills you've just learned. We are creating a simple math library that we will build upon throughout this course.

In other languages like C++ (which you may have seen in some online tutorials), these functions are often implemented using "operator overloading," allowing for a more natural syntax like Vec3 c = a + b;. While C doesn't support this, our function-based approach is just as powerful and clear.

To see one more perspective on these concepts, you can watch the start of the following video.

Linear Algebra for Game Development 1: Adding Subtracting and Multiplying Vectors

The video 'Linear Algebra for Game Development 1' offers another clear, component-wise explanation of these operations.

Watch from 00:30 to 03:57. This section reinforces the component-wise rules for addition, subtraction, and scalar multiplication. The second half of the video implements this in C++, so you can stop after the conceptual explanation, as our C approach will be different.


Conclusion

In this lesson, we took our first step into the world of 3D mathematics. We've established a firm understanding of what vectors are and how to manipulate them with the most fundamental operations.

Key Takeaways:

  • A vector is a quantity with magnitude and direction, represented as a list of components (e.g., [x, y, z]).
  • In games, vectors define position, velocity, and direction.
  • Vector addition (a + b) combines displacements and is used for movement.
  • Vector subtraction (a - b) finds the vector that points from the tip of b to the tip of a, essential for calculating directions between objects.
  • Scalar multiplication (s * v) changes a vector's length (and potentially reverses its direction), used for adjusting speed, force, and scale.
  • In C, we can represent vectors with structs and implement these operations as simple functions.

These three operations are the bedrock upon which we will build everything else. Make sure you are comfortable with them, as we will use them in almost every future lesson.

In our next lesson, we will explore two new, and incredibly powerful, types of vector multiplication: the dot product and the cross product. These will allow us to answer more complex geometric questions and are essential for lighting, collision detection, and much more.