.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: \ \ (A T) = (U A) + (objectPitchAngle,X 0) + (3 32) \ \ and store it in both vectorPitchAngle(Hi Lo) and (A T) \ \ In this context, (3 32) is a pitch angle of 3.125 \ expressed with the low byte as a fraction 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) \ \ And (A T) contains the same as vectorPitchAngle(Hi Lo) \ so we can pass it to GetVectorForAngles \ \ We now fall through into GetVectorForAngles to convert \ these two angles into a Cartesian vector: \ \ [ xVector(Lo Bot) ] \ [ yVector(Lo Bot) ] \ [ zVector(Lo Bot) ]Name: GetSightsVector [Show more] Type: Subroutine Category: Maths (Geometry) Summary: Calculate the angles of the vector from the player's eyes to the sights Deep dive: Converting angles to coordinates The crosshair sightsContext: 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 / 8) - (10 0) + (objectYawAngle,X 0) vectorPitchAngle = ((ySights - 5) / 16) + (3 32) + (objectPitchAngle,X 0) The division by 8 converts pixel x-coordinates into yaw angles, and the division by 16 converts pixel y-coordinates into pitch angles. 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 sights yaw angle makes it relative to the centre of the screen rather than the left edge, 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. The (3 32) element is not fully understood, but it might be composed of -(6 0) to subtract half a screen height to make the pitch angle relative to the centre of the screen, and then adding (9 32) to capture the fact that the player's eyes are 0.875 tiles above the tile surface.
Arguments: X This is always set to the object number of the player (it is set in ProcessActionKeys before it calls this routine)
[X]
Variable objectPitchAngle in workspace Stack variables
The pitch angle for each object (i.e. the vertical direction in which they are facing)
[X]
Variable objectYawAngle (category: 3D objects)
The yaw angle for each 3D object (i.e. the horizontal direction in which they are facing)
[X]
Variable vectorPitchAngleHi in workspace Zero page
The pitch angle of a vector (high byte)
[X]
Variable vectorPitchAngleLo in workspace Zero page
The pitch angle of a vector (low byte)
[X]
Variable vectorYawAngleHi in workspace Zero page
The yaw angle of a vector (high byte)
[X]
Variable vectorYawAngleLo in workspace Zero page
The yaw angle of a vector (low byte)
[X]
Variable xSights in workspace Main variable workspace
The pixel x-coordinate of the sights on-screen (i.e. of the top pixel in the sights)
[X]
Variable ySights in workspace Main variable workspace
The pixel y-coordinate of the sights on-screen (i.e. of the top pixel in the sights)