@@ -419,17 +419,24 @@ def __init__(self, *args, **kwargs):
419
419
# When flying gravity has no effect and speed is increased.
420
420
self .flying = False
421
421
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
+ #
422
425
# First element is -1 when moving forward, 1 when moving back, and 0
423
426
# otherwise. The second element is -1 when moving left, 1 when moving
424
427
# right, and 0 otherwise.
425
428
self .strafe = [0 , 0 ]
426
429
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.
428
432
self .position = (0 , 0 , 0 )
429
433
430
434
# First element is rotation of the player in the x-z plane (ground
431
435
# 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.
433
440
self .rotation = (0 , 0 )
434
441
435
442
# Which sector the player is currently in.
@@ -478,7 +485,12 @@ def get_sight_vector(self):
478
485
479
486
"""
480
487
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.
481
491
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.
482
494
dy = math .sin (math .radians (y ))
483
495
dx = math .cos (math .radians (x - 90 )) * m
484
496
dz = math .sin (math .radians (x - 90 )) * m
@@ -497,20 +509,26 @@ def get_motion_vector(self):
497
509
if any (self .strafe ):
498
510
x , y = self .rotation
499
511
strafe = math .degrees (math .atan2 (* self .strafe ))
512
+ y_angle = math .radians (y )
513
+ x_angle = math .radians (x + strafe )
500
514
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 )
503
517
if self .strafe [1 ]:
518
+ # Moving left or right.
504
519
dy = 0.0
505
520
m = 1
506
521
if self .strafe [0 ] > 0 :
522
+ # Moving backwards.
507
523
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
510
528
else :
511
529
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 )
514
532
else :
515
533
dy = 0.0
516
534
dx = 0.0
0 commit comments