Skip to navigation

Revs on the BBC Micro

Maths (Geometry): GetAngleInRadians

Name: GetAngleInRadians [Show more] Type: Subroutine Category: Maths (Geometry) Summary: Convert a 16-bit angle into radians, restricted to a quarter circle Deep dive: Trigonometry
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * GetRotationMatrix (Part 1 of 5) calls GetAngleInRadians

Arguments: (A T) A yaw angle in Revs format (-128 to +127)
Returns: (U A) The angle, reduced to a quarter circle, converted to radians, and halved
.GetAngleInRadians ASL T \ Set (V T) = (A T) << 2 ROL A \ ASL T \ This shift multiplies (A T) by four, removing bits 6 ROL A \ and 7 in the process STA V \ \ The degree system in Revs looks like this: \ \ 0 \ -32 | +32 Overhead view of car \ \ | / \ \ | / 0 = looking straight ahead \ \|/ +64 = looking sharp right \ -64 -----+----- +64 -64 = looking sharp left \ /|\ \ / | \ \ / | \ \ -96 | +96 \ 128 \ \ The top byte of (A T) is in this range, so shifting to \ the left by two places drops bits 6 and 7 and scales \ the angle into the range 0 to 252, as follows: \ \ * 0 to 63 (%00000000 to %00111111) \ -> 0 to 252 (%00000000 to %11111100) \ \ * 64 to 127 (%01000000 to %01111111) \ -> 0 to 252 (%00000000 to %11111100) \ \ * -1 to -64 (%11111111 to %11000000) \ -> 252 to 0 (%11111100 to %00000000) \ \ * -65 to -128 (%10111111 to %10000000) \ -> 252 to 0 (%11111100 to %00000000) \ We now convert this number from a Revs angle into \ radians \ \ The value of (V T) represents a quarter-circle, which \ is PI/2 radians, but we actually multiply by PI/4 to \ return the angle in radians divided by 2, to prevent \ overflow in the GetRotationMatrix routine LDA #201 \ Set U = 201 STA U \ Fall through into Multiply8x16 to calculate: \ \ (U A) = U * (V T) / 256 \ = 201 * (V T) / 256 \ = (201 / 256) * (V T) \ = (3.14 / 4) * (V T) \ \ So we return (U A) = PI/4 * (V T) \ \ which is the original angle, reduced to a quarter \ circle, converted to radians, and halved