Skip to navigation


Driving model: UpdateVelocity

Name: UpdateVelocity [Show more] Type: Subroutine Category: Driving model Summary: Update the player's velocity and spin yaw angle 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 UpdateVelocity

Calculate the following: xPlayerSpeed = xPlayerSpeed + xAcceleration << 5 zPlayerSpeed = zPlayerSpeed + zAcceleration << 3 spinYawAngle = spinYawAngle + spinYawDelta << 3
.UpdateVelocity LDX #2 \ Set X = 2, to act as an axis counter in the following \ loop, working through three axes from 2 to 0 .delf1 LDA #0 \ Set V = 0, to use as the sign bit for (V A T) STA V LDA xAccelerationLo,X \ Set (A T) = xAcceleration for axis X STA T LDA xAccelerationHi,X BPL delf2 \ If A is positive, jump to delf2 to skip the following \ instruction DEC V \ Set V = &FF, so it contains the correct sign bit for \ (V A T) .delf2 LDY #3 \ Set Y = 3, to act as a shift counter in the loop \ below, so we left-shift by three places CPX #2 \ If X <> 2, jump to delf3 to skip the following BNE delf3 \ instruction \ If we get here then X = 2, so (A T) contains \ spinYawDelta LDY #5 \ Set Y = 5, so for the third axis, we left-shift by \ five places .delf3 ASL T \ Set (V A T) = (V A T) << 1 ROL A ROL V DEY \ Decrement the shift counter in Y BNE delf3 \ Loop back until we have left-shifted Y times STA U \ Set (V U T) = (V A T) \ In the following, we update: \ \ * xPlayerSpeed when X = 0 \ \ * zPlayerSpeed when X = 1 \ \ * spinYawAngle when X = 2 \ \ The following comments are for when X = 0 LDA xPlayerSpeedLo,X \ Set xPlayerSpeed = xPlayerSpeed + (V U T) CLC \ ADC T \ starting with the low bytes STA xPlayerSpeedLo,X LDA xPlayerSpeedHi,X \ Then the high bytes ADC U STA xPlayerSpeedHi,X LDA xPlayerSpeedTop,X \ And then the top bytes ADC V STA xPlayerSpeedTop,X DEX \ Decrement the axis counter in X to point to the next \ axis BPL delf1 \ Loop back until we have processed all three axes RTS \ Return from the subroutine