Blitz3D+ Command Reference

ACos# ( float# )

Parameters

float - a cosine value; must be in the range -1 to 1

Description

Returns the angle, in degrees, whose cosine is the given value.

ACos is the inverse of Cos. Cos turns an angle into a ratio; ACos turns that ratio back into an angle. The answer always comes back between 0 and 180 degrees, because every cosine value between -1 and 1 has exactly one angle in that half-turn.

Angles in Blitz3D+ are always degrees, never radians, so the result drops straight into RotateEntity, TurnEntity or another Sin/Cos pair with no conversion.

The usual game use is recovering an angle between two directions: normalise both vectors, take their dot product, and ACos the result to get the angle between them in degrees. That is how you build a "can the guard see the player" cone test.

Watch out for values outside -1 to 1: they have no matching angle and give you NaN (not a number), which then infects every calculation it touches. Dot products of nearly parallel vectors love to come back as 1.0000001 thanks to floating point rounding, so clamp before you call ACos.

See also: Cos, ASin, ATan, ATan2, Pi.

Example

; ACos Example
; ------------

; ACos is the inverse of Cos: given a cosine value between -1.0 and
; 1.0, it returns the matching angle in DEGREES, from 180 down to 0.

Print "     value  ACos(value)"
Print "-----------------------"

For n=-4 To 4
    v#=n/4.0
    Print RSet(v,10)+RSet(ACos(v),12)
Next

Print ""

; Feeding a cosine back through ACos recovers the original angle
Print "Cos(60)       = "+Cos(60)
Print "ACos(Cos(60)) = "+ACos(Cos(60))

Print ""
Print "Press any key to close the example"
WaitKey

End

Index