• Home
  • Textbooks
  • Mathematics for Game Programming and Computer Graphics: Explore the essential mathematics for creating, rendering, and manipulating 3D virtual environments
  • Practicing Vector Essentials

Mathematics for Game Programming and Computer Graphics: Explore the essential mathematics for creating, rendering, and manipulating 3D virtual environments

Penny de Byl

Chapter 9

Practicing Vector Essentials - all with Video Answers

Educators


Section 1

Understanding the difference between points and vectors

Problem 1

Create a new folder in PyCharm called Chapter 9 and make copies of the final versions for it based on the files from Chapter 8; include Button. py, Cube.py, Mesh3D.py, object. py, Settings . py, Transform.py, and Utils.py.

Check back soon!

Problem 2

Make a new Python script called Vectors. py and copy into it the final code from the original AddingBut tons. py file from Chapter 7, but remove the code that creates the button, as highlighted in the following snippet:
def button_click() :
print ("Hello Button")
white $=$ pygame. $\operatorname{Color}(255,255,255)$
green $=$ pygame. $\operatorname{Color}(0,255,0)$
blue $=$ pygame. $\operatorname{Color}(0,0,255)$
button1 = object ("Button")
button1.add_component (Button (screen, $(0,0), 100,50$,
green, white, blue,
button_click))
objects_2d.append(button1)
When this code is run, you will see the regular window and cube in the middle. The cube will still move with the arrow keys.

Check back soon!

Problem 3

When we start using vectors, we want to be able to measure the results. To do this, we will add a visible grid to the environment. Create a new Python script called Grid. py and add the following code to it:
from OpenGL.GL import *
class Grid():
def_init_(self, interval, halfsize, colour):
self. interval $=$ interval
self.halfsize $=$ halfsize
self. colour $=$ colour
def draw(self) :
glColor3fv(self.colour)
glBegin (GL_LINES)
for $x$ in range(-self.halfsize, self.halfsize):
for $y$ in range(-self.halfsize,
self.halfsize):
glVertex3fv((x * self.interval,
y * self.interval - 10,
0))
glVertex3fv((x * self.interval,
y * self.interval + 500,
0))
glVertex3fv((y * self.interval - 10,
x * self.interval, 0))
glVertex3fv((y * self.interval + 500,
x * self.interval, 0))
glEnd()

Check back soon!

Problem 4

The Grid class can now be used in Vectors.py to draw a grid on the screen:
from Grid import *
from Object import *
cube.add_component(Cube(GL_POLYGON,
“Chapter_Four/images/wall.tif”))
objects_3d.append(cube)
grid = Object(“Grid”)
grid.add_component(Transform((0, 0, -5)))
grid.add_component(Grid(0.5, 8, (0, 0, 255)))
objects_3d.append(grid)
clock = pygame.time.Clock()

Check back soon!
03:35

Problem 5

Before running this, we need to tweak Object. py to ensure it calls the draw () method of the grid. As the call will be the same as for Mesh3D, rearrange the update () function as follows:
def update (self, events $=$ None) :
glPushMatrix ()
for $\mathrm{c}$ in self.components:
if isinstance (c, Transform) :
pos $=c \cdot g e t \_$position ()
glTranslatef (pos.x, pos.y, pos.z)
elif isinstance (c, Mesh3D) :
c.draw ()
elif isinstance(c, Grid) :
c.draw ()
elif isinstance(c, Button) :
c. draw (events)
glPopMatrix()

Jordan Gassaway
Jordan Gassaway
Numerade Educator

Problem 6

We will now program the functionality to move the cube using a vector. To the Transform class, add a new method called move:
def move(self, amount: pygame.math.Vector3):
self.position $=$ pygame $\cdot$ math.Vector3 (
self.position. $x+$ amount. $x$,
self.position.y + amount.y,
self.position. $z$ + amount. $z$ )
This method will accept a 3D vector as the parameter and then use each part of the vector to update the corresponding position coordinate.

Check back soon!

Problem 7

Let's try using it. In Vectors . py, add a key press for the spacebar and move the cube by 0.5 in the $\mathrm{x}$ direction, like this:
while not done:
events $=$ pygame. event $\cdot$ get ()
for event in events:
if event.type $==$ pygame.QUIT:
done $=$ True
if event. type $==$ KEYDOWN:
if event.key == K_SPACE:
trans.move(pygame.Vector3(0.5, 0, 0))
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:

Check back soon!
02:15

Problem 8

Run the code, take note of where the cube is, and then press the spacebar. The grid lines are 0.5 apart and we are moving the cube by 0.5 in the $\mathrm{x}$ direction. The cube will move 0.5 to the right.
What we have done here is taken the cube's position of $(0,0,-5)$ and added a vector of $(0.5,0,0)$ to it. This places the cube at a new position of $(0.5,0,-5)$. Each time you press the spacebar, the vector $(0.5,0,0)$ will be added to the cube's position, resulting in placing the cube at $(1,0,-5)$.
The position of the cube is a point, a location measured from the origin of the world. The movement instructions for the cube of $(0.5,0,0)$ is a vector. This same vector value is added to the position of the cube each time the spacebar is pressed. The vector is not a fixed location in space like the cube's positions. It merely provides instructions for how the cube should move in the $\mathrm{x}, \mathrm{y}$ and $\mathrm{z}$ directions.

Derek Walkama
Derek Walkama
Numerade Educator