© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2021
M. Cywiak, D. CywiakMulti-Platform Graphics Programming with Kivyhttps://doi.org/10.1007/978-1-4842-7113-1_4

4. Three-Dimensional Projections and Rotations

Moisés Cywiak1   and David Cywiak2
Leon, Guanajuato, Mexico
Queretaro, Mexico

In this chapter, we present two basic concepts required for constructing and rotating three-dimensional polygons. The first concept refers to the analytical equations to project points from a three-dimensional space onto a two-dimensional screen. The second concept refers to the equations to calculate the rotation of points in a three-dimensional space.

4.1 Projecting a Three-Dimensional Point Onto a Plane

Figure 4-1 depicts two three-dimensional points, P and P, with coordinates (x, y, z) and (x, y, z), respectively. Point P, the projected point, is obtained by projecting P on the computer’s screen by drawing a straight line from a fixed point V with coordinates (VX, VY, VZ) up to P and passing through P. Point V is referred to as the point of projection. Vectors P and P correspond to the vectors that go from the origin with coordinates (0, 0, 0) to (x, y, z) and to (x, y, z), respectively.

The unit direction vectors of the three-dimensional space are given by i, j, and k, as depicted in Figure 4-1. For simplicity, the screen is placed parallel to the x − y plane and the normal distance between planes is D.
Figure 4-1

Projection on a screen of a point P with coordinates (x, y, z) to a point Pwith coordinates (x, y, z). The point of projection has coordinates (VX, VY, VZ). Their corresponding vectors are P, P, and V, respectively

Let A denote the vector from point V to P and let B denote the vector from point V to P. As both vectors are parallel to each other, we can write:
$$ B= tA $$

In Equation (4.1), t is a scalar.

It can be seen from Figure 4-1 that:
$$ P=V+A $$
$$ {P}^{prime }=V+B $$
Using Equations (4.1)-(4.3) gives the following:
$$ {P}^{prime }-V=tleft(P-V
ight) $$
On the two-dimensional screen, as depicted in Figure 4-2, the bottom-left corner has coordinates (0, 0), while its center has coordinates (XC, YC). Vector Q corresponds to the vector that goes from (0, 0) to (XC, YC). Vector T goes from (XC, YC) to P, while vector R goes from (0, 0) to P. As these three vectors are on the screen, each one of them is normal to k.
Figure 4-2

Vectors Q, T, and R on the screen

From Figure 4-2, we can write:
$$ R=Q+T $$
Noticing in Figure 4-1 that the vector Dk goes from (0 ,0, 0) to the center of the screen, we can write:
$$ {P}^{prime }= Dk+T $$
Using Equations (4.6) and (4.4) gives the following:
$$ Dk+T=tleft(P-V
ight)+V $$
Now, as vector T is normal to k, the dot product vanishes. Therefore, the dot product of Equation (4.7) with k gives the following:
$$ Dkcdotp k=tleft(P-V
ight)cdotp k+Vcdotp k $$
Performing the dot products in Equation (4.8) allows us to obtain the following:
$$ t=frac{D-{V}_Z}{z-{V}_Z} $$

To obtain Equation (4.9), we used P = Dk.

Note from Figure 4-1 that z in Equation (4.9) is the distance from the origin (0, 0, 0) to point P, along the unitary k vector. For programming purposes, it is more convenient to measure this distance starting from the screen. This distance is denoted as ZP. Therefore, z = D − ZP. This value will be substituted in Equation (4.9).

Now, we use the value of t given by Equation (4.9) and the value of T obtained from Equation (4.5) to rewrite Equation (4.7) as follows:
$$ R=Q+frac{D-{V}_Z}{D-{Z}_P-{V}_Z}left(P-V
ight)+V- Dk $$

In Equation (4.10), we substituted z with D − ZP.

Equation (4.10) allows us to calculate the coordinates of point P on the screen. For this, according to Figure 4-2, we will express vectors R and Q on the screen as R = XPi + YPj and Q = XCi + YCj. Therefore, Equation (4.10) gives the position of point P in terms of the vector pointing to the center of the screen (Q), the normal distance to the screen (D), the vector of projection (V), and the position of the three-dimensional point (P).

By expressing R = XPi + YPj and Q = XCi + YCj in Equation (4.10), the vector components give the following three equations:
$$ {X}_p={X}_C+frac{D-{V}_Z}{D-{Z}_P-{V}_Z}left(x-{V}_X
ight)+{V}_X $$
$$ {Y}_p={Y}_C+frac{D-{V}_Z}{D-{Z}_P-{V}_Z}left(y-{V}_Y
ight)+{V}_Y $$
$$ 0=frac{D-{V}_Z}{D-{Z}_P-{V}_Z}left(D-{Z}_P-{V}_Z
ight)+{V}_Z-D $$

Equations (4.11) and (4.12) represent the main tool for determining the projection of the three-dimensional point on the screen. Equation (4.13), although algebraically correct, will not be used, as it does not provide further information.

In addition to Equations (4.11) and (4.12), we will also need equations to rotate the polygons. In the following section, we obtain these equations.

4.2 Rotating a Point on a Plane

Rotation equations for a three-dimensional point projected on a screen can be calculated by successive rotations performed in two-dimensional planes. Therefore, our description here is presented in a two-dimensional space.

In Figure 4-3, the center of the screen, with coordinates (x, y), is given by the position vector C on a coordinate plane (x, y).
Figure 4-3

Screen with coordinates (x, y) on a coordinate plane (x, y)

In Figure 4-3, the black dot represents an arbitrary point on the two-dimensional (x, y) space. As depicted, P and C represent vectors going from the origin of the (x, y) system to the point and to the center of the screen, respectively. Vector R goes from the center of the screen to the point. It can be observed that:
$$ P=C+{R}^{prime } $$
In terms of their components, vectors in Equation (4.14) will be written as follows:
$$ P= xi+ yj $$
$$ C={X}_Ci+{Y}_Cj $$
$$ {R}^{prime }={x}^{prime }{i}^{prime }+{y}^{prime }{j}^{prime } $$

It will also be noticed that:

i = i and j = j (4.18)

We assume that the black dot depicted in Figure 4-3 is fixed on the screen. Therefore, if the screen rotates an angle θ, as illustrated in Figure 4-4, the coordinates of the rotated point remain fixed on the screen.
Figure 4-4

Rotated screen with coordinates (x′′, y′′). The coordinate system (x, y) remains static. The point on the screen represented by the black dot rotates with the screen, remaining fixed on it

From Figure 4-4, we obtain the following:
$$ {P}^{prime prime }=C+{R}^{prime prime } $$
$$ {i}^{prime prime }=mathit{cos}left(	heta 
ight)i+mathit{sin}left(	heta 
ight)j $$
$$ {j}^{prime prime }=-mathit{sin}left(	heta 
ight)i+mathit{cos}left(	heta 
ight)j $$

In Equations (4.20) and (4.21), we substituted i and j with their corresponding values given in Equation (4.18).

As mentioned, the rotated point maintains fixed coordinates on the screen; therefore, we can write:
$$ {R}^{prime prime }={x}^{prime prime }{i}^{prime prime }+{y}^{prime prime }{j}^{prime prime }={x}^{prime }{i}^{prime prime }+{y}^{prime }{j}^{prime prime } $$
Substituting Equations (4.20) and (4.21) in Equation (4.22) gives the following:
$$ {R}^{prime prime }={x}^{prime}left[mathit{cos}left(	heta 
ight)i+mathit{sin}left(	heta 
ight]+{y}^{prime}left[-mathit{sin}left(	heta 
ight)i+mathit{cos}left(	heta 
ight] $$
Now, x and y can be calculated using the component vectors of Equation (4.14) as:
$$ {x}^{prime }=x-{X}_C $$
$$ {y}^{prime }=y-{Y}_C $$
By explicitly writing the components of R′′ in Equation (4.23) and using Equations (4.24) and (4.25), we obtain the following:
$$ {x}^{prime prime }={X}_C+left(x-{X}_C
ight)mathit{cos}left(	heta 
ight)mathit{sin}left(	heta 
ight) $$
$$ {y}^{prime prime }={Y}_C+left(x-{X}_C
ight)mathit{sin}left(	heta 
ight)mathit{cos}left(	heta 
ight) $$

Equations (4.26) and (4.27) give the coordinates of the rotated point. These equations are used in Chapter 5 to program the rotation of the polygons.

4.3 Summary

In this chapter, we derived analytical equations for projecting three-dimensional points onto a two-dimensional screen. Additionally, we obtained equations to calculate the rotation of points in a three-dimensional space. These equations are used in Chapter 5 to position and rotate polygons on the screen.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.