Reader small image

You're reading from  Mathematics for Game Programming and Computer Graphics

Product typeBook
Published inNov 2022
PublisherPackt
ISBN-139781801077330
Edition1st Edition
Tools
Right arrow
Author (1)
Penny de Byl
Penny de Byl
author image
Penny de Byl

Penny de Byl is a full stack developer with an honors in graphics and Ph.D. in artificial intelligence for games. She has a passion for teaching, teaching games development and computer graphics for over 25 years in universities in Australia and Europe. Her best-selling textbooks, including Holistic Game Development with Unity, are used in over 100 institutions. She has won numerous awards for teaching, including an Australian Government Excellence in Teaching Award and the Unity Mobile Game Curriculum Competition. Her approach to teaching computer science and related fields is project-based giving you hands-on workshops you can immediately get your teeth into. The full range of her teaching interests can be found at H3D Learn.
Read more about Penny de Byl

Right arrow

Practicing Vector Essentials

If there’s one thing you must learn to conquer in the domain of programming graphics (besides triangles), it is vector mathematics. I may have mentioned this before, but that’s how important it is! As you will start to see in this chapter and then throughout the rest of this book, you can’t do anything in this domain without a solid understanding of them.

This understanding began in Chapter 8, Reviewing Our Knowledge of Triangles, with the introduction of trigonometry. Vectors are used in everything from defining meshes as the positions of vertices, edges that run between vertices, UV values, and more to moving objects to rendering pixels on the screen. They are extremely versatile, as are their mathematical principles.

This chapter will begin by examining the similarities and differences between the concepts of points and vectors to help you distinguish between the two since, mathematically, they are very similar. We will then...

Technical requirements

In this chapter, we will be continuing to build on the project that has been developed throughout this book using Python in PyCharm with Pygame and PyOpenGL. The solution files containing the necessary code can be found on GitHub at https://github.com/PacktPublishing/Mathematics-for-Game-Programming-and-Computer-Graphics/tree/main/Chapter09 in the Chapter09 folder.

Understanding the difference between points and vectors

We can make sense of vectors by examining them in Cartesian coordinates. A vector in 2D is expressed as (x, y), in 3D as (x, y, z), and in 4D as (x, y, z, w).

Yes, I said four dimensions! At this stage, you are most likely looking at that “w” at the end of the expression and wondering where it came from. Don’t worry about it too much as its purpose will become clearer when we examine matrix multiplication.

In theory, a vector can be defined in any number of dimensions extending to infinity. They are used for complex mathematical calculations that can be found in applications relating to machine learning, astrophysics, financial analysis, and inverse kinematics, to name a few. However, in graphics, 2D, 3D, and 4D vectors are used.

Figure 9.1 illustrates a point and a vector in both 2D and 3D space. If you were to just look at the expressions for a vector, shown previously, you could be forgiven for...

Working out a vector’s magnitude

By drawing and working with vectors, it’s obvious to see they have a length. This is something that also distinguishes them from a point. The magnitude or length of a vector is useful for calculating the distance between where it starts and where it ends. For example, in Figure 9.3, we calculated the vector the pirate had to travel to get from a starting location to the treasure. From this vector, we can see the direction of travel, but we can also calculate how far the pirate is from the treasure. A very common operation to perform in graphics when it comes to moving objects is determining how far objects are apart, as well as working with collisions and a multitude of other functions. Therefore, it’s useful to understand how the magnitude is calculated.

To perform this operation, we must go back to Pythagoras theorem and triangles. Essentially, every vector can be made into a right-angled triangle. From there, the vector...

Exploring the relationship between angles and vectors

The angles that are made from intersecting two vectors can best be explored by considering how a game character facing one vector should turn to be facing another. In this section, we will explore how such a game character might turn toward the desired object.

At the end of Chapter 8, Reviewing Our Knowledge of Triangles, you added a mesh loading class to your project and used it to display a teapot in the graphics window. How much did you want to turn this teapot around to get a better look at it? Besides translation, rotation is another primary transformation used in graphics. Rotations require angles; these angles are measured between vectors using the principles we explored in Chapter 8.

For example, let’s consider the case shown in Figure 9.7, in which the game character, Chomper, would like to turn and face the jar of Vegemite. A vector is used to represent the direction Chomper is facing, as well as the direction...

Summary

There’s a lot to learn when it comes to vector mathematics, and this chapter has only scratched at the surface and plucked out the essential parts relevant to graphics and games. If you get a chance, I would strongly suggest that you review the extra links given throughout. Vector mathematics is a university-level topic. I’ve attempted to reveal the simplicity of the topic and reduce it to simple addition and multiplication operations, but if you feel you haven’t grasped the topic, I’d encourage you to investigate what’s available on Khan Academy (khanacademy.org). In this book, I’m addressing the most relevant aspects of all mathematical topics concerning computer graphics and games, but unfortunately, due to page count, I can’t cover everything.

We started this chapter by looking at the differences between points and vectors, followed by several key vector operations required to manipulate them to move objects in space....

Answers

Exercise A:

trans.move(pygame.Vector3(0.5, 0.5, 0))

The cube will move diagonally to the top right.

Exercise B:

trans.move(pygame.Vector3(0, -1, 0))

The cube will move into the screen, away from the viewer.

Exercise C:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mathematics for Game Programming and Computer Graphics
Published in: Nov 2022Publisher: PacktISBN-13: 9781801077330
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Penny de Byl

Penny de Byl is a full stack developer with an honors in graphics and Ph.D. in artificial intelligence for games. She has a passion for teaching, teaching games development and computer graphics for over 25 years in universities in Australia and Europe. Her best-selling textbooks, including Holistic Game Development with Unity, are used in over 100 institutions. She has won numerous awards for teaching, including an Australian Government Excellence in Teaching Award and the Unity Mobile Game Curriculum Competition. Her approach to teaching computer science and related fields is project-based giving you hands-on workshops you can immediately get your teeth into. The full range of her teaching interests can be found at H3D Learn.
Read more about Penny de Byl