Skip to navigation

Maths (Geometry): GetSightsVector

Name: GetSightsVector [Show more] Type: Subroutine Category: Maths (Geometry) Summary: Calculate the angles of the vector from the player's eyes to the sights
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * ProcessActionKeys (Part 1 of 2) calls GetSightsVector

The vector from the player's eyes to the sights is calculated as follows: vectorYawAngle = (xSights * 32) + (objectYawAngle,X 0) - (10 0) vectorPitchAngle = (ySights - 5) * 16 + (objectPitchAngle,X 0) + (3 32) The (10 0) element in the yaw angle calculation represents half a screen width, as the screen is 20 yaw angles wide, so subtracting (10 0) from the object's yaw angle makes it to the left edge of the screen, and then we add the x-coordinate of the sights on-screen to get the vector's yaw angle. The -5 in the pitch angle calculation caters for the distance between the top of the sights and the centre of the sights, and the (3 32) element represents something I have yet to work out.
Arguments: X This is always set to the object number of the player (it is set in ProcessActionKeys before it calls this routine)
.GetSightsVector LDA xSights \ Set (U A) = xSights STA U LDA #0 LSR U \ Set (U A) = (U A) / 8 ROR A \ = xSights / 8 LSR U ROR A LSR U ROR A \ We now calculate the following: \ \ (U A) + (objectYawAngle,X 0) - (10 0) \ \ and store it in vectorYawAngle(Hi Lo) CLC \ Clear the C flag for the following STA vectorYawAngleLo \ Store the low byte of the calculation (which we know \ will be A) in vectorYawAngleLo LDA U \ Calculate the high byte of the calculation as ADC objectYawAngle,X \ follows: SEC \ SBC #10 \ U + objectYawAngle,X - 10 STA vectorYawAngleHi \ \ and store it in vectorYawAngleHi \ So vectorYawAngle(Hi Lo) is now equal to the \ following: \ \ (xSights / 8) + (objectYawAngle,X 0) - (10 0) LDA ySights \ Set (U A) = ySights - 5 SEC SBC #5 STA U LDA #0 LSR U \ Set (U A) = (U A) / 16 ROR A \ = (ySights - 5) / 16 LSR U ROR A LSR U ROR A LSR U ROR A \ We now calculate the following: \ \ (U A) + (objectPitchAngle,X 0) + (3 32) \ \ and store it in both (A T) and in \ vectorPitchAngle(Hi Lo) CLC \ Calculate the low byte and store it in both T and ADC #32 \ vectorPitchAngleLo STA vectorPitchAngleLo STA T LDA U \ Calculate the high byte, keep it in A and store it in ADC objectPitchAngle,X \ vectorPitchAngleHi CLC ADC #3 STA vectorPitchAngleHi \ So by this point we have the following: \ \ vectorYawAngle(Hi Lo) \ = (xSights / 8) + (objectYawAngle,X 0) - (10 0) \ \ vectorPitchAngle(Hi Lo) \ = (ySights-5) / 16 + (objectPitchAngle,X 0) + (3 32) \ \ We now fall through into GetVectorForAngles to convert \ these two angles into a cartesian vector: \ \ [ xVector(Lo Bot) ] \ [ yVector(Lo Bot) ] \ [ zVector(Lo Bot) ]