Blitz3D+ Command Reference

Sin# ( degrees# )

Parameters

degrees - an angle, measured in degrees

Description

Returns the sine of an angle given in degrees.

Angles are always degrees here, never radians. Sin(0) is 0, Sin(90) is 1, Sin(270) is -1.

On a circle of radius 1, with the angle measured anticlockwise from the positive x axis, Sin gives the y coordinate of the point on the rim and Cos gives the x. Used together they turn an angle plus a distance into a movement: x = x + speed*Cos(angle), y = y + speed*Sin(angle).

Sin on its own is the cheapest way to get a smooth back-and-forth. Sin(MilliSecs()/5.0) bobs a pickup, pulses a HUD element or sways a tree, and multiplying by an amplitude sets how far it travels.

Screen y points downwards while world y points upwards, so the same Sin term dips a sprite and lifts a 3D entity. Sin also repeats every 360 degrees but loses precision at very large angles, so wrap accumulated angles with Mod 360.

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

Example

; Sin Example
; -----------

Graphics 640,480,0,2
SetBuffer BackBuffer()

; Blitz3D trig works in DEGREES. Sin(angle) is the vertical 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

    ; Sin gives the vertical offset, Cos the horizontal 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

    ; Red bar: the Sin part of the dot's position (its height)
    Color 255,0,0
    Line x,cy,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,"Note: screen y runs downward, so +Sin draws below centre"
    Text 400,220,"angle = "+angle
    Text 400,240,"Sin(angle) = "+Sin(angle)
    Text 400,260,"Cos(angle) = "+Cos(angle)

    Flip

Wend

End

Index