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

Getting Acquainted with Lines, Rays, and Normals

Since we covered vectors in the previous chapter, it’s time to jump into exploring the mathematics of lines, rays, and normals. All three can be defined by vectors and on the surface, all appear to be the same and yet they have very specific and differing uses in graphics. Fortunately, their similarities concerning geometric structure allow them to be defined and manipulated by the same equations, as you will discover in this chapter.

The one thing lines, rays, and normals have in common is that they are all straight. This makes them very useful in graphics for defining space, direction, the edges of meshes, distance, collisions, reflections, and much more. The line construct is one of the fundamental drawing operations in graphics, as we covered in Chapter 1, Hello Graphics Window: You're On Your Way, and Chapter 2, Let’s Start Drawing.

In this chapter, we will cover the essential knowledge you need to define...

Technical requirements

In this chapter, we will continue working on the project that we’ve constructed using Python, PyCharm, Pygame, and PyOpenGL.

The solution files containing this chapter’s code can be found on GitHub at https://github.com/PacktPublishing/Mathematics-for-Game-Programming-and-Computer-Graphics/tree/main/Chapter10 in the Chapter10 folder.

Defining lines, segments, and rays

What most people call a line is a line segment. A line segment is just a piece of a line. By true mathematical definition, a line continues infinitely. In Chapter 2, Let’s Start Drawing, we used the equation for a line to draw segments between mouse clicks in our project window. Recall that the following equation was used:

y = mx + c

Here, m is the gradient (or slope) and c is the y-intercept, as shown in Figure 10.1:

Figure 10.1: The gradient and y-intercept

The value of y can be calculated for infinite values of x and vice versa, making a line continuous. However, a line segment has a start and end, as illustrated in Figure 10.2:

Figure 10.2: Differing straight geometrics

To clarify the difference between these straight geometrics, let’s examine their properties:

  • A vector has a magnitude and direction, as we discovered in Chapter 9, Practicing Vector Essentials. It doesn...

Using the parametric form of lines

While the line equation given in the previous section is something most people are familiar with from high school mathematics, it’s not particularly useful in graphics when you want to manipulate objects or work out intersections, animations, and collisions. Therefore, we tend to use the parametric form. The parametric form of an equation, rather than using x and y to calculate positions, uses time, represented by t. This might sound confusing at first but bear with me while I explain.

Consider Figure 10.3 (a). Notice how a line segment can be represented by two points and a vector going between them:

Figure 10.3: A line segment with a vector between the start and end points

The calculation for v is as follows:

v = b – a

We can also express it like so:

b = a + v

This tells us that if we start at point a and travel along the whole length of v, we will end up at b. Where would you be if you only...

Calculating and displaying normals

Unlike rays, which have a starting position and direction and are infinite in length, normals are special vectors assigned to mesh surfaces and vertices. A normal is a vector that usually sits at 90 degrees to a surface. I say usually because they can be manipulated for special surface texturing effects. Normals are used to calculate how light falls on a surface, as well as define the side of a polygon to which a texture is applied. There are two places normals are used; on surfaces and vertices, as shown in Figure 10.6, though usually, they are defined with vertices in mesh files:

Figure 10.6: Normals

As shown in Figure 10.6, mathematically, a plane has two normals, which can be found using the cross product of two vectors on the surface. Recall from Chapter 9, Practicing Vector Essentials, that multiplying two vectors together results in a third vector that sits at right angles to the initial vectors. In Figure 10.6, one...

Summary

By now, I am sure you are gaining an appreciation of the importance of vectors, and just how important it is for graphics and game developers to have a firm grasp of them both conceptually and practically. Trust me, this chapter won’t be the last time you see them. The mathematical concepts we explored in this chapter might appear to cover the same ground over and over concerning calculating points and vectors, but this should only convince you further how important these methods are.

Lines are far more complex than they first seem. Although most straight geometrical elements are called lines in a general, collective sense, the differences between vectors, lines, line segments, and rays are clear. Each has its place in graphics development in data structures and drawing, though we can apply many of the same mathematical calculations to any of them. Is it any wonder that many of these are stored in the same data structure of pygame.Vector3?

We started this chapter...

Answers

Exercise A:

end = start + v

end = (3, 6, 7) + (1, 5, 10)

end = (4, 11, 17)

Exercise B:

v = (10, 18, 3) – (1, 9, 5)

v = (9, 9, -2)

t = 0.5

point = (1, 9, 5) + 0.5 * (9, 9, -2)

point = (1, 9, 5) + (4.5, 4.5, -1)

point = (5.5, 13.5, 4)

Exercise C:

start position = (2, 3, 4) – (5, 5, 3) = (-3, -2, 1)

t = 0.75

point = (-3, -2, 1) + 0.75 * (5, 5, 3)

point = (-3, -2, 1) + (3.75, 3.75, 2.25)

point = (0.75, 1.75, 3.25)

Exercise D:

(8, 2, 3) x (1, 2, 3) = (2*3 – 3*2, 3*1 – 8*3, 8*2-2*1)

= (0, -21, 14)

(1, 2, 3) x (8, 2, 3) = (2*3 – 3*2, 3*8 – 1*3, 1*2- 2*8)

= (0, 21, -14)

These two vectors are direct opposites. They are parallel but pointing in opposite directions, which is what you expect from the two normals. You can tell this by looking at the x, y, and z values, each of which has the same values but is signed differently.

Exercise E:

To begin solving this, you need two...

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