# Driving model: GetTyreForces

```       Name: GetTyreForces                                           [Show more]
Type: Subroutine
Category: Driving model
Summary: Calculate the tyre forces due to the throttle or brakes
Deep dive: The core driving model
Skidding
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* ApplySkidForces calls GetTyreForces
* ApplyTyreForces calls GetTyreForces

Calculate the following:

* If the throttle is not being applied:

* (NN MM) = scaled up |zVelocity|

* H = zVelocityHi with the sign flipped

* (A T) = throttleBrake * wingForce           (rear tyres)
= throttleBrake * wingForce * 3 / 4   (front tyres)

* C flag clear

* If the throttle is being applied:

* When we are processing the front tyres:

* C flag set

* When we are processing the rear tyres:

* H = gearNumber - 1

* (A T) = (throttleBrake * engineTorque) / 2

* C flag clear

The routine also sets G to 2 or 3 (for the front or rear tyres).

Arguments:

X                    The set of tyres to process:

* 0 = front tyres

* 1 = rear tyres

Returns:

C flag               Calculation status:

* Set if we are returning a set of calculated values

* Clear if we are not returning any values (which
happens if the throttle is being applied and we are
processing the front tyres)

G                    Set to the correct offset for calling ApplyLimitThrottle
or ApplyLimitAndSign:

* 2 = zTyreForceNose

* 3 = zTyreForceRear

H                    The sign of the force

(A T)                The force

(NN MM)              If the throttle is not being applied, a maximum for the
force

.GetTyreForces

TXA                    \ Set G = X + 2
CLC                    \
ADC #2                 \ So G = 2 for the front tyres and 3 for the rear tyres
STA G

LDY throttleBrakeState \ If throttleBrakeState = 1 then the throttle is being
BEQ tyfo1

\ If we get here then the throttle is not being applied

LDY #9                 \ Set Y = 8 so the call to Scale16Bit scales zVelocity

JSR Scale16Bit         \ Scale up |zVelocity| by 2^5, capping the result at the
\ maximum possible positive value of &7Fxx, and
\ returning the result in (NN MM)

LDA zVelocityHi        \ Set H = zVelocityHi with the sign flipped
EOR #%10000000
STA H

LDA wingForce,X        \ Set A to wingForce for tyre X

CPX #1                 \ If X = 1, then we are processing the rear tyres, so
BEQ tyfo2              \ jump to tyfo2 to use this value of A in the
\ calculation

LSR A                  \ Set A = (A / 2 + wingForce) / 2
CLC                    \       = (wingForce / 2 + wingForce) / 2
ADC wingForce,X        \       = wingForce * 3 / 4
LSR A

JMP tyfo2              \ Jump to tyfo2 to use this value of A in the
\ calculation

.tyfo1

\ If we get here then the throttle is being applied

CPX #1                 \ If X <> 1, then we are processing the front tyres, so
BNE tyfo4              \ jump to tyfo4 to return from the subroutine with the C
\ flag set

LDA gearNumber         \ Set H = gearNumber - 1
SEC
SBC #1
STA H

LDA engineTorque       \ Set A = engineTorque

.tyfo2

STA U                  \ Store the value of A in U to use in the multiplication
\ below

LDA throttleBrake      \ Set A to the amount of throttle or brake being applied

JSR Multiply8x8        \ Set (A T) = A * U
\           = throttleBrake * U

LDY throttleBrakeState \ If throttleBrakeState <> 1 then the throttle is not
BNE tyfo3

LSR A                  \ The throttle is being applied, so set:
ROR T                  \
\   (A T) = (A T) / 2

.tyfo3

CLC                    \ Clear the C flag to indicate that we are returning a
\ set of calculated values

RTS                    \ Return from the subroutine

.tyfo4

SEC                    \ Set the C flag to indicate that we are not returning
\ any calculated values

RTS                    \ Return from the subroutine
```