2
What You’ll See in This Chapter This chapter introduces the idea of multiple coordinate systems. It is divided into five sections. Section 3.1 justifies the need for multiple coordinate systems. Section 3.2 introduces some common coordinate systems. Section 3.3 describes coordinate space transformations. Section 3.4 discusses nested coordinate spaces. Section 3.5 is a political campaign for more human readable code, and is not covered in these notes. Chapter 3 Notes3D Math Primer for Graphics & Game Dev2

5
Why Multiple Coordinate Spaces? Some things become easier in the correct coordinate space. There is some historical precedent for this observation (next slide). We can leave the details of transforming between coordinate spaces to the graphics hardware. Chapter 3 Notes3D Math Primer for Graphics & Game Dev5

8
Copernicus Nicholas Copernicus (1473- 1543) observed that the orbits of the planets can be explained more simply in a heliocentric universe. (Images from Wikimedia Commons) Chapter 3 Notes3D Math Primer for Graphics & Game Dev8

11
World Space World space is the global coordinate system. Use it to keep track of position and orientation of every object in the game. Just like in the movie Highlander, there can be only one. Chapter 3 Notes3D Math Primer for Graphics & Game Dev11

12
Object Space Every object in the game has: Its own origin (where it is), Its own concept of “up” and “right” and “forwards”, That is, its own coordinate space. Use it to keep track of relative positions and orientation (eg. Collision detection, AI) Chapter 3 Notes3D Math Primer for Graphics & Game Dev12

15
Upright Space Upright space is in a sense “half way” between world space and object space. Upright space has – Object space origin – World space axes It is nonstandard, we use it in our book because we like it. Chapter 3 Notes3D Math Primer for Graphics & Game Dev15

20
Why Upright Space? It separates translation and rotation – It is a handy visualization tool – It is inspired by both math and hardware implementation Translate between world and upright space. Rotate between upright and object space. Which brings us to coordinate space transformations… Chapter 3 Notes3D Math Primer for Graphics & Game Dev20

24
Example Let's say that we are working for a advertising agency that has just landed a big account with a food manufacturer. You are assigned to the project to make a slick computer-generated ad promoting one of their most popular items, Herring Packets, which are microwaveable herring sandwiches food products for robots. Of course, the client has a tendency to want changes made at the last minute, so we need to be able to get a model of the product at any possible position and orientation. Chapter 3 Notes3D Math Primer for Graphics & Game Dev24

25
Start with the Artist’s Model For now, because we have the model in its home position, object space and world space (and upright space) are all the same by definition. For all practical purposes, in the scene that the artist built containing only the model of the robot, world space is object space. Chapter 3 Notes3D Math Primer for Graphics & Game Dev25

26
Goal Transformation Our goal is to transform the vertices of the model from their home location to some new location (in our case, into a make-believe kitchen), according to the desired position and orientation of the robot based on the executive’s whims at that moment. See next slide… Chapter 3 Notes3D Math Primer for Graphics & Game Dev26

28
More Details Let's talk a bit about how to accomplish this. We won't get too far into the mathematical details - that's what the rest of this chapter is for. Conceptually, to move the robot into position we first rotate her clockwise 120° (or, as we'll learn in Chapter 8, by “heading left 120 °”). Then we translate 18ft east and 10ft north, which according to our conventions is a 3D displacement of [18, 0, 10]. Chapter 3 Notes3D Math Primer for Graphics & Game Dev28

29
Original Position Chapter 3 Notes3D Math Primer for Graphics & Game Dev29

32
Rotate Before Translate Why rotate before we translate? Rotation about the origin is easier – translating first would mean that we have to rotate around an arbitrary point. Rotation about the origin is a linear transform. Rotation about an arbitrary point is an affine transform. Affine transforms can be expressed as a sequence of primitive operations. Chapter 3 Notes3D Math Primer for Graphics & Game Dev32

33
Camera Space So we've managed to get the robot model into the right place in the world. But to render it, we need to transform the vertices of the model into camera space. In other words, we need to express the vertices' coordinates relative to the camera. For example, if a vertex is 9ft in front of the camera and 3ft to the right, then the z and x coordinates of that vertex in camera space would be 9 and 3, respectively. Chapter 3 Notes3D Math Primer for Graphics & Game Dev33

35
Camera Space It’s easier to reason about a camera at the origin, looking along a primary axis. So we move the whole world so that the camera is at the origin. First we translate, then we rotate (for the same reason as before, because rotation about the origin is easier than rotation about an arbitrary point). Chapter 3 Notes3D Math Primer for Graphics & Game Dev35

36
Original Position Chapter 3 Notes3D Math Primer for Graphics & Game Dev36

39
Transform to Camera Space We use the opposite translation and rotation amounts, compared to the camera's position and orientation. For example, in Figure 3.9 the camera is at (13.5, 4, 2). So to move the camera to the origin, we will translate by [-13.5, -4, -2]. The camera is facing roughly northeast and thus has a clockwise heading compared to north; a counter-clockwise rotation is required to align camera space axes with the world space axes. Chapter 3 Notes3D Math Primer for Graphics & Game Dev39

40
Notes The world space → camera space transform is usually done inside the rendering system, often on a dedicated graphics processor. Camera space isn't the finish line" as far as the graphics pipeline is concerned. From camera space, vertices are transformed into clip space and finally projected to screen space. Chapter 3 Notes3D Math Primer for Graphics & Game Dev40

41
Basis Vectors: Example Suppose we need the world space coordinates of the light on the robot's right shoulder. Start with its object-space coordinates, (-1, 5). How do we get the world-space coordinates? – Start at her origin. – Move to the right 1 foot. – Move up 5 feet. The terms “to the right” and “up” are in object space. We know that the robot's “to the left” vector is [0.87, 0.5], and the robot's “up” vector is [-0.5, 0.87] Chapter 3 Notes3D Math Primer for Graphics & Game Dev41

42
Doing the Arithmetic Start at the origin. No problem, we know her origin is (4.5, 1.5). Move to the right 1 foot. We know that the vector “the robot's left” is [0.87, 0.50], and so we scale this direction by the distance of -1 unit, and add on the displacement to our position, to get (4.5, 1.5) + (-1) [0.87, 0.5] = (3.63, 1). Move up 5 feet. Once again, we know that “the robot's up” direction is [-0.50, 0.87], so we just scale this by 5 units and add it to the result, yielding (4.5, 1.5) - [0.87, 0:5] + 5 [-0.5, 0.87] = (1.13, 5.35) Chapter 3 Notes3D Math Primer for Graphics & Game Dev42

43
Check the Result If you look again at the Figure, you'll see that the world-space coordinates of the light are, indeed (1.13, 5.35). Chapter 3 Notes3D Math Primer for Graphics & Game Dev43

44
Abstract the Process Let b be a point with object-space coordinates b = (b x, b y ). Let w = (w x, w y ) be the world-space coordinates of b. We know the world space coordinates for the origin o and the right and up directions, which we will denote as p and q, respectively. Now w can be computed by w = o + b x p + b y q Chapter 3 Notes3D Math Primer for Graphics & Game Dev44

45
Basis Vectors Now let's be even more general. It will help greatly to remove translation from consideration. Geometrically, any vector may be decomposed into a sequence of axially-aligned displacements. Thus an arbitrary vector v can be written as v = xp + yq + zr Here, p, q and r are basis vectors for 3D space. v is a linear combination of the basis vectors. Chapter 3 Notes3D Math Primer for Graphics & Game Dev45

46
Notes Mostly, p = [1, 0, 0], q = [0, 1, 0], and r = [0, 0, 1]. This is always true when expressed in the coordinate space for which they are the basis, but relative to some other basis they can have arbitrary coordinates. Basis vectors are usually mutually perpendicular, for example, p x q = r, but they don’t have to be. Basis vectors are usually unit length, but they don’t have to be. They may not even have the same length. Chapter 3 Notes3D Math Primer for Graphics & Game Dev46

47
They Don’t Have to be Parallel Chapter 3 Notes3D Math Primer for Graphics & Game Dev47

48
The Span The set of vectors that can be expressed as a linear combination of the basis vectors is called the span of the basis. The span of a pair of vectors in a 3D space is usually a plane. Consider the vector c, which lies behind the plane in this figure. Chapter 3 Notes3D Math Primer for Graphics & Game Dev48

49
Rank The vector c is not in the span of p and q, which means we cannot express it as a linear combination of the basis vectors. In other words, there are no such coordinates [c x, c y ] such that c = c x p + c y q. The term used to describe the number of dimensions in the space spanned by the basis is the rank of the basis. In the examples so far, we have two basis vectors that span a two dimensional space. Clearly, if we have n basis vectors, the best we can hope for is full rank, meaning the span is an n-dimensional space. But it can be less. For example, if the basis vectors are linearly dependent. Chapter 3 Notes3D Math Primer for Graphics & Game Dev49

50
Poor Bases So a set of linearly dependent vectors is certainly a poor choice of basis. But there are other more stringent properties we might desire of a basis. Suppose that we have an object body whose basis vectors are p, q, r, and we know the coordinates of these vectors in world space. Let b = [b x, b y, b z ] be the coordinates of some arbitrary vector in body space. Let u = [u x, u y, u z ] be the coordinates of that same vector in upright space. Chapter 3 Notes3D Math Primer for Graphics & Game Dev50

51
From the Robot Example From our robot example, we already know the relationship between u and b. Chapter 3 Notes3D Math Primer for Graphics & Game Dev51 What if u is known and b is the vector we’re trying to determine?

53
Solving Systems of Equations The system of equations on the left is not really much of a “system” at all, it's just a list: each equation is independent, and each unknown quantity can be immediately computed from a single equation. On the right, however, we have three interrelated equations, and none of the unknown quantities can be determined without all three equations. In fact, if the basis vectors are linearly dependent, then the system on the right may have zero solutions (if u is not in the span), or it might have an infinite number of solutions (if u is in the span but the coordinates are not uniquely determined). Chapter 3 Notes3D Math Primer for Graphics & Game Dev53

54
Linear Algebra to the Rescue? Linear algebra provides a number of general- purpose tools for solving systems of linear equations like this, but we don't need to delve into these topics, as the solution to this system is not our primary aim. For now, we're interested in understanding a special situation where the solution is easy. In Chapter 6 we'll learn how to use the matrix inverse to solve the general case. Chapter 3 Notes3D Math Primer for Graphics & Game Dev54

55
Dot Product to the Rescue The dot product is the key. Remember from Chapter 2 that the dot product can be used to measure distance in a particular direction. When using the standard basis p = [1, 0, 0], q = [0, 1, 0], and r = [0, 0, 1] (corresponding to the object axes being parallel with the world axes in our robot example), we can dot the vector with a basis vector to “sift out” the corresponding coordinate… Chapter 3 Notes3D Math Primer for Graphics & Game Dev55

56
Simple Sifting But does this “sifting” action work for any arbitrary basis? No. In fact we can see that it doesn't work for the example we have been using. The figure on the next slide compares the correct coordinates a x, a y with the dot products a∙p and a∙q. Chapter 3 Notes3D Math Primer for Graphics & Game Dev56

57
Failure to Sift The illustration is only completely correct if p and q are unit vectors. The dot product doesn't “sift out" the coordinate in this case. Notice that in each case, the result produced by the dot product is larger than the correct coordinate value. Why is this? Chapter 3 Notes3D Math Primer for Graphics & Game Dev57

58
Here’s Why it Fails Dot product measures displacement in a given direction. But a coordinate value does not simply measure the amount of displacement from the origin along a given direction! A coordinate is a coefficient in the expansion a = xp + yq. The reason the dot product doesn't work in this case is because we are ignoring the fact that yq will cause some displacement parallel to p. To visualize this, imagine in the preceding figure that we increased a x while holding a y constant. As a moves to the right and slightly upwards, its projection onto q, which is measured by the dot product, increases. Chapter 3 Notes3D Math Primer for Graphics & Game Dev58

59
Synopsis The problem is that the basis vectors are not perpendicular. A set of basis vectors that are mutually perpendicular is called an orthogonal basis. When the basis are orthogonal, the coordinates are uncoupled. Any given coordinate of a vector v can be determined solely from v and the corresponding basis vector. For example, we can compute the v x coordinate knowing only p, provided q and r are perpendicular to p. Chapter 3 Notes3D Math Primer for Graphics & Game Dev59

60
Orthonormal Bases It's the best when the basis vectors all have unit length too. Such a set of vectors are known as an orthonormal basis. Why is the unit length helpful? Remember the geometric definition of the dot product: a∙p is equal to the signed length of a projected onto p, times the length of p. If the basis vector doesn't have unit length, but it is perpendicular to all the others, we can still determine the corresponding coordinate using the dot product, we just need to divide by the square of the length of the basis vector. Chapter 3 Notes3D Math Primer for Graphics & Game Dev60

61
Wrapping it Up Thus, in the special circumstance of an orthonormal basis, we have a simple way to determine the object-space coordinates, knowing only the world-coordinates of the body axes. b x = u ∙ p b y = u ∙ q b z = u ∙ r Chapter 3 Notes3D Math Primer for Graphics & Game Dev61