How Our Player Ship Will Move
Our player ship will change its angle of center axis rotation when the game player presses the left and right arrow keys. When the game player presses the up arrow key, the player ship will accelerate (thrust) in the angle it is facing. Because there is no friction applied to the ship, it will continue to float in the current accelerated angle until a different angle of acceleration is applied. This happens when the game player rotates to a new angle and presses the up (thrust) key once again.
The difference between facing and moving
Our player ship can rotate the direction it is facing while it
is moving in a different direction. For this reason, we cannot simply
use classic dx
and dy
values to represent the movement vector
on the x and y axes. We must keep both sets of values for the ship at
all times. When the player rotates the ship but does not thrust it, we
need to draw the ship in the new rotated angle. All missile
projectiles that the ship fires must also move in the direction the
ship is facing. On the x-axis, we will name this value facingX
; on the y-axis, it’s facingY
. movingX
and movingY
values will handle moving the ship
in the direction it was pointed in when the thrust was applied. All
four values are needed to thrust the ship in a new direction. Let’s
take a look at this next.
Thrusting in the rotated direction
After the ship is rotated to the desired direction, the player can thrust it forward by pressing the up arrow key. This thrust will ...
Get HTML5 Canvas, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.