Cos# ( degrees# )
Parameters
| degrees - an angle, measured in degrees |
Description
|
Returns the cosine of an angle given in degrees. Every angle in Blitz3D+ is in degrees, so you never convert to radians first. Cos(0) is 1, Cos(90) is 0, Cos(180) is -1. Picture a circle of radius 1 with the angle measured anticlockwise from the positive x axis: Cos gives you the x coordinate of the point on the rim, and Sin gives you the y. That pairing is the workhorse of 2D game code - orbiting a moon, spreading enemies evenly around a ring, walking a bullet forward along its heading, or driving a smooth wobble with Cos(MilliSecs()/10.0). To move something distance d along heading a, add d*Cos(a) to x and d*Sin(a) to y. Bear in mind screen y points downwards, so a positive Sin moves things down the screen but up in the 3D world. Cos repeats every 360 degrees, but accuracy drifts as the angle gets very large. If you accumulate an angle every frame, wrap it back with Mod 360 rather than letting it grow forever. See also: Sin, Tan, ACos, ATan2, Pi. |
Example
; Cos Example ; ----------- Graphics 640,480,0,2 SetBuffer BackBuffer() ; Blitz3D trig works in DEGREES. Cos(angle) is the horizontal part of a ; direction: a dot orbiting a centre point sits at ; (cx+r*Cos(angle),cy+r*Sin(angle)). cx=240 cy=240 r=120 angle#=0 While Not KeyDown(1) ; Left/Right arrows steer the angle; it also drifts on its own If KeyDown(203) Then angle=angle-2 If KeyDown(205) Then angle=angle+2 angle=angle+0.25 If angle>=360 Then angle=angle-360 If angle<0 Then angle=angle+360 ; Cos gives the horizontal offset, Sin the vertical offset x#=cx+r*Cos(angle) y#=cy+r*Sin(angle) Cls ; The circular path Color 70,70,70 Oval cx-r,cy-r,r*2,r*2,False ; Green bar: the Cos part of the dot's position (its sideways reach) Color 0,255,0 Line cx,y,x,y ; The rotating arm and the orbiting dot Color 255,255,0 Line cx,cy,x,y Oval x-4,y-4,8,8,True Color 255,255,255 Text 0,0,"Left/Right: change angle Esc: exit" Text 0,20,"Cos is +1 pointing right, 0 straight up or down, -1 pointing left" Text 400,220,"angle = "+angle Text 400,240,"Cos(angle) = "+Cos(angle) Text 400,260,"Sin(angle) = "+Sin(angle) Flip Wend End
Index