• Home
  • Textbooks
  • Mathematics for Game Programming and Computer Graphics: Explore the essential mathematics for creating, rendering, and manipulating 3D virtual environments
  • Getting Acquainted with Lines, Rays, and Normals

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

Penny de Byl

Chapter 10

Getting Acquainted with Lines, Rays, and Normals - all with Video Answers

Educators


Section 1

Defining lines, segments, and rays

Problem 1

Make a new folder in PyCharm called Chapter 10. Make a copy of all the files from the Chapter 9 folder and place them in this new folder. This includes Vectors. py.

Check back soon!
02:39

Problem 2

Rename the copy of Vectors . py to Animate.py.

Kamalesh Kumar
Kamalesh Kumar
Numerade Educator

Problem 3

Modify the content of Ani mate.py, like this:
.
glViewport(0, 0, screen.get_width(),
screen.get_height())
glEnable (GL_DEPTH_TEST)
trans: Transform = cube.get_component (Transform)
start_position $=$ pygame. $\operatorname{Vector} 3(-3,0,-5)$
end_position $=$ pygame. $\operatorname{Vector} 3(3,0,-5)$
$v$ = end_position - start_position
$t=0$
trans.set_position(start_position)
$\mathrm{dt}=0$
trans2: Transform = cube2.get_component (Transform)
while not done:
events $=$ pygame. event $\cdot$ get $($ )
for event in events:
if event. type == pygame.QUIT:
done $=$ True
if $t<=1$ :
trans.set_position(start_position + $t * v$ )
$t+=0.0001 * d t$
glPushMatrix ()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
.
glPopMatrix()
pygame.display.flip()
dt = clock.tick(fps)
pygame.quit()

Check back soon!
01:29

Problem 4

Press play and watch the cube move across the screen, stopping when $t=1$, as shown in Figure 10.5:
(Figure can't copy)

Yujie Wang
Yujie Wang
College of San Mateo

Problem 5

Now, modify the ending position of the line segment to $(0,0,-5)$, like this:
$$
\text { end_position }=\text { pygame. } \operatorname{Vector} 3(0,0,-5)
$$

Check back soon!

Problem 6

Run the program again. The cube will stop in the center of the screen.

Check back soon!
View

Problem 7

To see how values of $t$ greater than 1 will keep the cube moving along the same line, comment out the if statement, as follows:
\#if $\mathrm{t}<=1$ :

JH
J Hardin
Numerade Educator
00:57

Problem 8

Run the program one more time. The cube will move across the screen and out of view on the right-hand side.
The code you've created in this exercise will move the cube between any two points, but make sure you put the if statement we took out previously back in.

Melissa Stefan
Melissa Stefan
Numerade Educator