# Maths (Geometry): RotateCarToCoord

```       Name: RotateCarToCoord                                        [Show more]
Type: Subroutine
Category: Maths (Geometry)
Summary: Rotate a vector from the frame of reference of the player's car
into the 3D world coordinate system
Deep dive: The core driving model
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* ApplyDrivingModel calls RotateCarToCoord

Calculate the following:

[ xAcceleration ]   [  cosYawAngle   0   sinYawAngle ]   [ xPlayerAccel ]
[       -       ] = [       0        1        0      ] . [       -      ]
[ zAcceleration ]   [ -sinYawAngle   0   cosYawAngle ]   [ zPlayerAccel ]

This rotates the xPlayerAccel vector from the frame of reference of the
player's car into the 3D world coordinate system.

The rotation matrix is the transpose of the matrix from RotateCoordToCar,
which is the inverse, so the RotateCarToCoord routine reverses the rotation in
the RotateCoordToCar routine.

The individual calculations are as follows:

xAcceleration = xPlayerAccel * cosYawAngle + zPlayerAccel * sinYawAngle

zAcceleration = zPlayerAccel * cosYawAngle - xPlayerAccel * sinYawAngle

.RotateCarToCoord

LDY #6                 \ Set Y = 6, so in the call to RotateVector, variableY
\ is xPlayerAccel and variableY+1 is zPlayerAccel

LDA #3                 \ Set A = 3, so in the call to RotateVector, we store
\ the result in xAcceleration and zAcceleration

LDX #%01000000         \ Set bit 6 and clear bit 7 of X, to set the polarity in
\ the call to RotateVector

\ Fall through into RotateVector to calculate the
\ following:
\
\   xAcceleration =   xPlayerAccel * cosYawAngle
\                   + zPlayerAccel * sinYawAngle
\
\   zAcceleration =   zPlayerAccel * cosYawAngle
\                   - xPlayerAccel * sinYawAngle
```