ADD R14, R11, #xPlayer \ Set R0 to R5 as follows: LDMIA R14, {R0-R5} \ \ R0 = xPlayer \ R1 = yPlayer \ R2 = zPlayer \ \ R3 = xVelocity \ R4 = yVelocity \ R5 = zVelocity \ \ So (R0, R1, R2) is the player's coordinate \ and (R3, R4, R5) is the player's velocity \ vector \ \ We now update the velocity by adding in \ various factors such as friction, thrust \ and gravity CMN R1, #HIGHEST_ALTITUDE \ If R1 is higher than the altitude where LDRLTB R9, [R11, #fuelBurnRate] \ the engines cut out, clear bits 2 and 3 of BICLT R9, R9, #%00000110 \ the fuel burn rate to cut the engines STRLTB R9, [R11, #fuelBurnRate] LDR R6, [R11, #xRoofV] \ Set (R6, R7, R8) to the roof vector from LDR R7, [R11, #yRoofV] \ the rotation matrix, which is the vector LDR R8, [R11, #zRoofV] \ that points directly down through the \ ship's floor as the y-axis is inverted \ (so it's in the direction of thrust, as \ the thrusters are on the bottom of the \ ship) \ \ Let's refer to this thrust vector as \ follows: \ \ R6 = xExhaust \ R7 = yExhaust \ R8 = zExhaust LDRB R9, [R11, #fuelBurnRate] \ Set R9 to the fuel burn rate TST R9, #%00000100 \ Set the flags according to bit 2 of the \ fuel burn rate, which is set if the left \ button is being pressed (i.e. full thrust) \ We start by updating xVelocity in R3 SUB R3, R3, R3, ASR #6 \ Set R3 = R3 - R3 >> 6 \ = xVelocity - (xVelocity / 64) \ \ This reduces the velocity along the x-axis \ by 1/64, so this applies a deceleration in \ this direction (due to friction) SUBNE R3, R3, R6, ASR #11 \ Increase R3 by a further xExhaust / 2048 \ if full thrust is being pressed, so this \ applies the engine thrust \ \ We subtract the exhaust vector to apply \ thrust because the direction of the thrust \ applied by the plume is in the opposite \ direction to the plume (in other words, a \ rocket's plume pushes downwards so the \ rocket can rise up) ADD R0, R0, R3 \ Set R0 = R0 + R3 \ = xPlayer + R3 \ \ This applies the newly calculated velocity \ to the player's x-coordinate, which moves \ the player in 3D space \ Next we update yVelocity in R4 SUB R4, R4, R4, ASR #6 \ Set R4 = R4 - R4 >> 6 \ = yVelocity - (yVelocity / 64) \ \ This reduces the velocity along the y-axis \ by 1/64, so this applies a deceleration in \ this direction (due to friction) SUBNE R4, R4, R7, ASR #11 \ Increase R3 by a further yExhaust / 2048 \ if full thrust is being pressed, so this \ applies the engine thrust \ \ We subtract the exhaust vector to apply \ thrust because the direction of the thrust \ applied by the plume is in the opposite \ direction to the plume (in other words, a \ rocket's plume pushes downwards so the \ rocket can rise up) ADD R1, R1, R4 \ Set R1 = R1 + R4 \ = yPlayer + R4 \ \ This applies the newly calculated velocity \ to the player's y-coordinate, which moves \ the player in 3D space \ And finally we update zVelocity in R4 SUB R5, R5, R5, ASR #6 \ Set R5 = R5 - R5 >> 6 \ = zVelocity - (zVelocity / 64) \ \ This reduces the velocity along the z-axis \ by 1/64, so this applies a deceleration in \ this direction (due to friction) SUBNE R5, R5, R8, ASR #11 \ Increase R3 by a further zExhaust / 2048 \ if full thrust is being pressed, so this \ applies the engine thrust \ \ We subtract the exhaust vector to apply \ thrust because the direction of the thrust \ applied by the plume is in the opposite \ direction to the plume (in other words, a \ rocket's plume pushes downwards so the \ rocket can rise up) ADD R2, R2, R5 \ Set R2 = R2 + R5 \ = zPlayer + R5 \ \ This applies the newly calculated velocity \ to the player's z-coordinate, which moves \ the player in 3D space \ By this point we have updated the player's \ coordinates in (R0, R1, R2) by the new \ velocity in (R3, R4, R5) TST R9, #%00000010 \ Set the flags according to bit 1 of the \ fuel burn rate, which is set if the middle \ button is being pressed (i.e. hover) SUBNE R3, R3, R6, ASR #13 \ If the middle button is being pressed, for SUBNE R4, R4, R7, ASR #13 \ hovering mode, then apply a quarter of the SUBNE R5, R5, R8, ASR #13 \ full thrust vector to the velocity, rather \ than the full thrust vector we apply for \ the left button \ \ Note that the hover thrust is applied \ after we applied the velocity vector to \ the player's coordinates, so hovering has \ a slightly delayed impact on the ship, to \ simulate the effects of inertia LDR R9, [R11, #gravity] \ Add the effect of gravity to yVelocity in ADD R4, R4, R9 \ R4, adding it to R4 as it is a downwards \ force along the z-axis STMIA R14, {R0-R8} \ Store the updated player coordinates, ship \ velocity and thrust vector as follows: \ \ xPlayer = R0 \ yPlayer = R1 \ zPlayer = R2 \ \ xVelocity = R3 \ yVelocity = R4 \ zVelocity = R5 \ \ xExhaust = R6 \ yExhaust = R7 \ zExhaust = R8 \ \ We use these values in part 4Name: MoveAndDrawPlayer (Part 2 of 5) [Show more] Type: Subroutine Category: Player Summary: Update the player's velocity and coordinates Deep dive: Flying by mouseContext: See this subroutine in context in the source code References: No direct references to this subroutine in this source file
[X]
Configuration variable HIGHEST_ALTITUDE = TILE_SIZE * 52
Highest altitude for the engines to work (52 tile sizes, but as a positive number; the altitude is actually -TILE_SIZE * 52)
[X]
Configuration variable fuelBurnRate = &15C
The current fuel burn rate (bit 0 is ignored)
[X]
Configuration variable gravity = &12C
The current setting of gravity (which changes on higher levels)
[X]
Configuration variable xPlayer = &94
The x-coordinate of the player's ship
[X]
Configuration variable xRoofV = &34
The x-coordinate of the roof vector
[X]
Configuration variable yRoofV = &40
The y-coordinate of the roof vector
[X]
Configuration variable zRoofV = &4C
The z-coordinate of the roof vector