Aviator on the BBC Micro

```       Name: AdjustVelocity                                          [Show more]
Type: Subroutine
Category: Flight model
Summary: Adjust the plane's velocity vector
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* ApplyFlightModel (Part 6 of 7) calls AdjustVelocity

This routine adjusts the plane's velocity vector by adding a 16-bit signed
vector to each axis of the plane's velocity vector. Specifically, it does the
following for each of the three axes (x, y and z):

(xVelocityTop xVelocityHi xVelocityLo) += (dxVelocityHi dxVelocityLo) * 2

so that's:

[ xVelocity ]   [ xVelocity ]   [ dxVelocity ]
[ yVelocity ] = [ yVelocity ] + [ dyVelocity ] * 2
[ zVelocity ]   [ zVelocity ]   [ dzVelocity ]

where (dxVelocity dyVelocity dzVelocity) is a vector made up of signed 16-bit
numbers and (xVelocity zVelocity zVelocity) is a vector made up of signed
24-bit numbers.

LDX #2                 \ Set a counter in X to work through the three axes (the
\ comments below cover the iteration for the x-axis)

.avel1

LDA #0                 \ Set R = 0, to use as the top byte in (R A V)
STA R

LDA dxVelocityLo,X     \ Set (A V) = (dxVelocityHi dxVelocityLo)
STA V                  \           = dxVelocity
LDA dxVelocityHi,X

BPL avel2              \ If (A V) is negative, decrement R to &FF so it has the
DEC R                  \ correct sign for (R A V)

.avel2

ASL V                  \ Set (R A V) = (R A V) << 1
ROL A                  \             = dxVelocity * 2
ROL R

PHA                    \ Store the high byte on the stack, so we can retrieve
\ it below

LDA xVelocityLo,X      \ Set xVelocity = xVelocity + (R A V)
CLC                    \               = xVelocity + dxVelocity * 2
STA xVelocityLo,X      \ starting with the low bytes

PLA                    \ And then the high bytes, retrieving the high byte
ADC xVelocityHi,X      \ that we stored on the stack above
STA xVelocityHi,X

LDA xVelocityTop,X     \ And then the top bytes