Skip to content

Commit e6cc7ef

Browse files
committed
Document motion and line of sight calculations.
1 parent 8737ad9 commit e6cc7ef

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

main.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -419,17 +419,24 @@ def __init__(self, *args, **kwargs):
419419
# When flying gravity has no effect and speed is increased.
420420
self.flying = False
421421

422+
# Strafing is moving lateral to the direction you are facing,
423+
# e.g. moving to the left or right while continuing to face forward.
424+
#
422425
# First element is -1 when moving forward, 1 when moving back, and 0
423426
# otherwise. The second element is -1 when moving left, 1 when moving
424427
# right, and 0 otherwise.
425428
self.strafe = [0, 0]
426429

427-
# Current (x, y, z) position in the world, specified with floats.
430+
# Current (x, y, z) position in the world, specified with floats. Note
431+
# that, perhaps unlike in math class, the y-axis is the vertical axis.
428432
self.position = (0, 0, 0)
429433

430434
# First element is rotation of the player in the x-z plane (ground
431435
# plane) measured from the z-axis down. The second is the rotation
432-
# angle from the ground plane up.
436+
# angle from the ground plane up. Rotation is in degrees.
437+
#
438+
# The vertical plane rotation ranges from -90 (looking straight down) to
439+
# 90 (looking straight up). The horizontal rotation range is unbounded.
433440
self.rotation = (0, 0)
434441

435442
# Which sector the player is currently in.
@@ -478,7 +485,12 @@ def get_sight_vector(self):
478485
479486
"""
480487
x, y = self.rotation
488+
# y ranges from -90 to 90, or -pi/2 to pi/2, so m ranges from 0 to 1 and
489+
# is 1 when looking ahead parallel to the ground and 0 when looking
490+
# straight up or down.
481491
m = math.cos(math.radians(y))
492+
# dy ranges from -1 to 1 and is -1 when looking straight down and 1 when
493+
# looking straight up.
482494
dy = math.sin(math.radians(y))
483495
dx = math.cos(math.radians(x - 90)) * m
484496
dz = math.sin(math.radians(x - 90)) * m
@@ -497,20 +509,26 @@ def get_motion_vector(self):
497509
if any(self.strafe):
498510
x, y = self.rotation
499511
strafe = math.degrees(math.atan2(*self.strafe))
512+
y_angle = math.radians(y)
513+
x_angle = math.radians(x + strafe)
500514
if self.flying:
501-
m = math.cos(math.radians(y))
502-
dy = math.sin(math.radians(y))
515+
m = math.cos(y_angle)
516+
dy = math.sin(y_angle)
503517
if self.strafe[1]:
518+
# Moving left or right.
504519
dy = 0.0
505520
m = 1
506521
if self.strafe[0] > 0:
522+
# Moving backwards.
507523
dy *= -1
508-
dx = math.cos(math.radians(x + strafe)) * m
509-
dz = math.sin(math.radians(x + strafe)) * m
524+
# When you are flying up or down, you have less left and right
525+
# motion.
526+
dx = math.cos(x_angle) * m
527+
dz = math.sin(x_angle) * m
510528
else:
511529
dy = 0.0
512-
dx = math.cos(math.radians(x + strafe))
513-
dz = math.sin(math.radians(x + strafe))
530+
dx = math.cos(x_angle)
531+
dz = math.sin(x_angle)
514532
else:
515533
dy = 0.0
516534
dx = 0.0

0 commit comments

Comments
 (0)