Aviator on the BBC Micro

# Flight model: ApplyTurnAndThrust (Part 1 of 2)

```       Name: ApplyTurnAndThrust (Part 1 of 2)                        [Show more]
Type: Subroutine
Category: Flight model
Summary: Calculate the (dxTurn, dyTurn, dzTurn) vector
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* ApplyFlightModel (Part 4 of 7) calls ApplyTurnAndThrust

This part of the routine calculates the following:

[ dxTurn ]   [ xMomentsSc ]   [ xControlsSc ]
[ dyTurn ] = [ yMomentsSc ] + [ yControlsSc ]
[ dzTurn ]   [ zMomentsSc ]   [ zControlsSc ]

[  yGravity  ]   [      0      ]
+ [      0     ] - [      0      ]
[      0     ]   [ zSlipMoment ]

Arguments:

L                    The current value of onGround:

* 0 = we are not on the ground

* 1 = we are on the ground

.ApplyTurnAndThrust

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

.turn1

LDA xMomentsScLo,X     \ Set dxTurn = xMomentsSc + xControlsSc
CLC                    \
ADC xControlsScLo,X    \ starting with the low bytes
STA dxTurnLo,X

LDA xMomentsScHi,X     \ And then the high bytes
STA dxTurnHi,X

LDA xMomentsScTop,X    \ And then the top bytes
STA dxTurnTop,X

DEX                    \ Decrement the loop counter to move to the next axis

BPL turn1              \ Loop back until we have added all three axes, so we
\ now have the following, where all values are 24-bit
\ numbers:
\
\   dxTurn = xMomentsSc + xControlsSc
\   dyTurn = yMomentsSc + yControlsSc
\   dzTurn = zMomentsSc + zControlsSc

LDA #0                 \ Set S = 0, to use as the top byte of yGravity
STA S

LDA yGravityLo         \ Set dxTurn = dxTurn + yGravity
CLC                    \
ADC dxTurnLo           \ starting with the low bytes
STA dxTurnLo

\ We now add the high bytes, but because dxTurn is a
\ 24-bit number and yGravity is only a 16-bit number,
\ we have to add an extra top byte to yGravity, which we
\ do in S as follows:
\
\   (S yGravityHi yGravityLo)

LDA yGravityHi         \ Set A to the high byte of yGravity

BPL turn2              \ If yGravityHi is negative, decrement S to &FF so it
DEC S                  \ has the correct sign for (S yGravityHi yGravityLo)

.turn2

STA dxTurnHi

LDA dxTurnTop          \ And finally we add the top bytes, so we now have:
STA dxTurnTop          \   dxTurn = dxTurn + yGravity

SEC                    \ Set dzTurn = dzTurn - zSlipMomentSc
LDA dzTurnLo           \
SBC zSlipMomentScLo    \ starting with the low bytes
STA dzTurnLo

LDA dzTurnHi           \ And then the high bytes
SBC zSlipMomentScHi
STA dzTurnHi

LDA dzTurnTop          \ And then the top bytes
SBC zSlipMomentScTop
STA dzTurnTop
```