The Sin() function returns the sine of an angle.
FACTS:
* Angle# should range between 0 and 360 degrees. You can use wrapangle to bring your angle into range if required.
TUTORIAL INDEX
Mini Tutorial #1 - Cos / Sin / Tan:
; Display the Cosine, Sine and Tangent of different angles Print Cos(180) Print Sin(90) Print Tan(45) ; Display the Arc Cosine, Arc Sine and Arc Tangent of different values Print ACos(-1.0) Print ASin(1.0) Print ATan(1.0) ; Display the Screen and wait for the user to press a key Sync WaitKey |
This example would output. -1.0 1.0 1.0 180.0 90.0 45.0 |
Top
Mini Tutorial #2 - Polar coordinates:
This example is little more interesting and demostrates the a common useage of COS,SIN (Polar Coordinates) in programming. Polar coordinates let us think in terms of a direction (angle that something is moving in), allowing us to easily plot the path that our game character might be traveling in.
In this example, we'll move the circle in a random direction (starting at the screen center) using the COS() and SIN() functions, to move along.
// Limit the programs speed, so we can see whats happening SetFPS 30 // Randomly pick a direction for this character to move in Angle#= Rnd(360) // Init the X position variable to hold the X# coordinate // on the Screen Xposition#= GetScreenWidth()/2 // Init the Y position variable to hold the Y# coordinate // on the Screen Yposition#= GetScreenHeight()/2 // USe a for/next loop to run the program a limited // number of times For NumberOfMoves =0 To 100 // Clear the screen to Black (RGB(0,0,0) ='s black) Cls RGB(0,0,0) // Display the angle# (in degrees) we're moving // our character in Print "Movement Angle="+Str$(Angle#)+" Degrees" // Calculate the New X position by ADDing the COS(Angle#) // to the old Xpos Xposition# = Xposition# + Cos(Angle#) // Calculate the New Y position by ADDing the SIN(Angle#) // to the old Ypos Yposition# = Yposition# + Sin(Angle#) // DRaw circle to represent the character Circle Xposition#,Yposition#,10,true // Show the updated screen to the user Sync Next Print "Print Example Complete. Press space to end" Sync WaitKey |
Top
Mini Tutorial #3 - Character Control:
This is the same the previous example, except this time we're speed control as the character is moving in this direction.
// Limit the programs speed, so we can see whats happening SetFPS 30 Do // Randomly pick a direction for this character to move in Angle#= Rnd(360) // Randomly pick a speed for this character to be moving at Speed#= Rnd(10) // Init the X position variable to hold the X# coordinate // on the Screen Xposition#= GetScreenWidth()/2 // Init the Y position variable to hold the Y# coordinate // on the Screen Yposition#= GetScreenHeight()/2 // USe a for/next loop to run the program a // limited number of times For NumberOfMoves =0 To 100 // Clear the screen to Black (RGB(0,0,0) ='s black) Cls RGB(0,0,0) // Display the angle# (in degrees) we're moving our character in Print "Movement Angle="+Str$(Angle#)+" Degrees" Print "Movement Speed="+Str$(Speed#) // Calculate the New X position by ADDing the COS(Angle#)*Speed# // to the old Xpos Xposition# = Xposition# + Cos(Angle#)*Speed# // Calculate the New Y position by ADDing the SIN(Angle#)*Speed# // to the old Ypos Yposition# = Yposition# + Sin(Angle#)*Speed# // DRaw circle to represent the character Circle Xposition#,Yposition#,10,true // Show the updated screen to the user Sync Next Print "Print Example Complete. Press space to end" Sync WaitKey Loop |
Top
Mini Tutorial #4 - Orbit:
This example demonstrates how Cos() & Sin() can be used to make objects orbit around a central point.
// Limit the programs speed, so we can see whats happening SetFPS 60 Do // Randomly pick a starting angle for the orbiting object Angle#= Rnd(360) // Randomly pick the orbiting radius of this object Radius= RndRange(10,100) // Init the X position variable to hold the X coordinate // on the Screen Xposition#= GetScreenWidth()/2 // Init the Y position variable to hold the Y# coordinate // on the Screen Yposition#= GetScreenHeight()/2 // USe a for/next loop to run the program a // limited number of times For NumberOfMoves =0 To 180 // Clear the screen to Black (RGB(0,0,0) ='s black) Cls RGB(0,0,0) // Display the angle# (in degrees) we're moving our character in Print "Angle="+Str$(Angle#)+" Degrees" Print "Radius="+Str$(Radius) // Calculate the Screen X position of orbiting objects ScreenX# = Xposition# + Cos(Angle#)*Radius // Calculate the Screen Y position of orbiting objects ScreenY# = Yposition# + Sin(Angle#)*Radius // Bump the Angle# by 2 degrees each loop, so the we see the object // orbit the center of the screen Angle#=WrapAngle(Angle#,2) // DRaw circle to represent the orbiting character Circle SCreenX#,ScreenY#,10,true // Draw a line from the objects obiting center to the objects // current screen position Line Xposition#,Yposition#,ScreenX#,ScreenY# // Show the updated screen to the user Sync Next Print "Print Example Complete. Press space to end" Sync WaitKey Loop |
Top
|