December 03, 2020, 08:07:48 PM

Author Topic: [bb] Sin() and Cos() demo by Rhodan [ 1+ years ago ]  (Read 718 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Sin() and Cos() demo by Rhodan [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Sin() and Cos() demo
Author : Rhodan
Posted : 1+ years ago

Description : Displays X (Sin) and Y(Cos) components and the resulting movement direction and speed as lines on the screen. Heavily commented.

Code :
Code: BlitzBasic
  1. [code]
  2. ;============================================
  3. ;     Demostration of Sin() and Cos()       =
  4. ;     (good for ship movement like in)      =
  5. ;               ASTEROOIDS!                 =
  6. ;============================================
  7.  
  8.  
  9.  
  10. Graphics 640,480
  11.  
  12. shipspeed#=0
  13. shipangle#=0
  14.  
  15. orig_x#=GraphicsWidth()/2 ; get center of screen for everything
  16. orig_y#=GraphicsHeight()/2
  17. ship_x#=orig_x#
  18. ship_y#=orig_y#
  19.  
  20. SetBuffer BackBuffer()
  21.  
  22. Repeat
  23.  
  24. Cls
  25.  
  26. If KeyDown(200) Then                            ; up arrow
  27.  
  28.         shipspeed#=shipspeed#+1
  29.         If shipspeed#>200 Then shipspeed#=200
  30.  
  31. ElseIf KeyDown(208) Then                        ; down arrow
  32.  
  33.         shipspeed#=shipspeed#-1
  34.         If shipspeed#<0 Then shipspeed#=0
  35.  
  36. EndIf
  37.  
  38. ;================================================
  39. ;                 Ship Angle                    =
  40. ; The first thing you have to do is give your   =
  41. ; ship a rotation. You rotate in a circle so    =
  42. ; you need a number between 0 and 359. Wrap the =
  43. ; value by adding or subtracting 360 from it    =
  44. ; when it goes out of range. The example below  =
  45. ; only steps by 1s so its would work if I just  =
  46. ; made it go to 359 if it was less than zero    =
  47. ; but, you'll probably be using floats so it    =
  48. ; wouldn't work quite right. Better this way    =
  49. ;================================================
  50.  
  51. If KeyDown(203) Then                            ; left arrow
  52.  
  53.         shipangle#=shipangle#-1
  54.         If shipangle#<0 Then shipangle#=shipangle#+360
  55.  
  56. ElseIf KeyDown(205) Then                        ; right arrow
  57.  
  58.         shipangle#=shipangle#+1
  59.         If shipangle#>359 Then shipangle#=shipangle#-360
  60.  
  61. EndIf
  62.  
  63. ; Now that we have a ship angle, we know where its
  64. ; pointing. Look at the chart below. If the ship is
  65. ; at 45 degrees, it would be pointing down and to
  66. ; the left. To move in that direction you need a
  67. ; negative change in its X coord and a positive
  68. ; change in its Y coord. This is where Sin() and
  69. ; Cos() come in. In your windows calculator (in
  70. ; start menu/programs/accessories), choose
  71. ; scientific mode. Near the lower left side are
  72. ; Sin and Cos buttons. Type in 45 then hit the
  73. ; Sin button. This will give you the change in
  74. ; X you'll need. Now do the same for Cos - which
  75. ; is the change in Y. You'll see its the same
  76. ; value. Both are about .707. Thats the amount
  77. ; you'd have to change each coordinate by to move
  78. ; 1 unit of distance at 45 degrees. Try some
  79. ; other angles. You'll notice that this chart and
  80. ; those numbers disagree about the sign of the
  81. ; Sin results. Where this chart shows negative,
  82. ; the calculator shows positive etc. Thats
  83. ; because computer graphics are the opposite
  84. ; in how they number horizontaly. Thats why the
  85. ; formula below subtracts Sin and adds Cos.
  86. ;
  87. ; Anyway, an angle of 45 degrees would need to
  88. ; move -.707 in the X sin(45) and +0.707 in the
  89. ; y Cos(45) planes. Imagine a little arrow
  90. ; from 0,0 to -0.707,0.707. That would be what
  91. ; people refer to as a "vector". If you measured
  92. ; its length, you'd see it was exactly the same
  93. ; length as it would be If it went +1 at 90
  94. ; degrees.
  95.  
  96. ; Here's the world according to 2D graphics
  97. ;                   180 degrees
  98. ;           -x,-y       | -y    +x,-y
  99. ;                       |
  100. ;                       |
  101. ;                       |
  102. ;           -x          |          +x
  103. ;90 degrees ------------+------------ 270 degrees
  104. ;                       |
  105. ;                       |
  106. ;                       |
  107. ;                       |
  108. ;           -x,+y       | +y    +x,+y
  109.  
  110. ;                  0/360 Degrees
  111.  
  112.  
  113.  
  114.  
  115. ; Sin() Returns the horizontal component of an angle
  116. ; Cos() Returns the vertical component of an angle
  117.  
  118. component_x#=Sin(shipangle#)
  119. component_y#=Cos(shipangle#)
  120.  
  121. ; Now that you know how much you need to move in each
  122. ; direction, multiply that by the speed of your ship.
  123. ; Lets assume you're going speed 10 at 120 degrees.
  124.  
  125. displaced_x#=component_x#*shipspeed# ; 0.866 * 10 = 8.66
  126. displaced_y#=component_y#*shipspeed# ; -0.5 * 10 = -5
  127.  
  128.  
  129. speeddir_x#=orig_x#-displaced_x# ; Now subtract from the ship's X position
  130. speeddir_y#=orig_y#+displaced_y# ; and add to the Y position
  131.  
  132. ; Now to show things on the screen.
  133.  
  134. Color 255,255,255
  135. Line orig_x#,orig_y#,speeddir_x#,speeddir_y# ; Shows direction and speed of ship
  136. Text speeddir_x#,speeddir_y#,"Ship speed: "+Int shipspeed#
  137.  
  138. ; Draw X component size
  139. If displaced_x#<0 Then Color 0,0,255 Else Color 255,0,0
  140. Line orig_x#,orig_y#,speeddir_x#,orig_y#
  141. Color 255,255,255
  142. Text speeddir_x#,orig_y#,"DX "+Int (0-displaced_x#) ; Int them, too many numbers flying around =)
  143.  
  144. ; Draw Y component size
  145. If displaced_y#>0 Then Color 0,0,255 Else Color 255,0,0
  146. Line orig_x#,orig_y#,orig_x#,speeddir_y#
  147. Color 255,255,255
  148. Text orig_x#,speeddir_y#,"YX "+ Int displaced_y#
  149.  
  150. Color 255,255,255
  151. Text 20,GraphicsHeight()-40,"Ship angle: "+shipangle#+" Sin: "+Sin(shipangle#)+" Cos: "+Cos(shipangle#)
  152.  
  153. Flip
  154.  
  155. Until KeyDown(1)
  156.  
  157. End
  158.  
  159.  
  160.  
[/code]

Comments :


Nicstt(Posted 1+ years ago)

 thx for this, finally beginning to understand sin and cos:)


itoleck(Posted 1+ years ago)

 Here is the updated code for BlitzMaxRem;============================================;     Demostration of Sin() And Cos()       =;     (good For ship movement like in)      =;               ASTEROOIDS!                 =;============================================EndRemConst ScreenX=800, ScreenY=600Graphics ScreenX,ScreenYshipspeed#=0shipangle#=0orig_x#=ScreenX/2 ' get center of screen For everythingorig_y#=ScreenY/2ship_x#=orig_x#ship_y#=orig_y#RepeatClsIf KeyDown(KEY_UP) Then            ' up arrow   shipspeed#=shipspeed#+1   If shipspeed#>200 Then shipspeed#=200ElseIf KeyDown(KEY_DOWN) Then         ' down arrow   shipspeed#=shipspeed#-1   If shipspeed#<0 Then shipspeed#=0EndIfRem;================================================;                 Ship Angle                    =; The first thing you have To do is give your   =; ship a rotation. You rotate in a circle so    =; you need a number between 0 And 359. Wrap the =; value by adding Or subtracting 360 from it    =; when it goes out of range. The example below  =; only steps by 1s so its would work If I just  =; made it go To 359 If it was less than zero    =; but, you'll probably be using floats so it    =; wouldn't work quite right. Better this way    =;================================================EndRemIf KeyDown(KEY_LEFT) Then            ' Left arrow   shipangle#=shipangle#-1   If shipangle#<0 Then shipangle#=shipangle#+360ElseIf KeyDown(KEY_RIGHT) Then         ' Right arrow   shipangle#=shipangle#+1   If shipangle#>359 Then shipangle#=shipangle#-360EndIfRem; Now that we have a ship angle, we know where its; pointing. Look at the chart below. If the ship is; at 45 degrees, it would be pointing down And To; the left. To move in that direction you need a; negative change in its X coord And a positive; change in its Y coord. This is where Sin() And; Cos() come in. In your windows calculator (in; start menu/programs/accessories), choose; scientific mode. Near the Lower Left side are; Sin And Cos buttons. Type in 45 Then hit the; Sin button. This will give you the change in; X you'll need. Now do the same for Cos - which; is the change in Y. You'll see its the same; value. Both are about .707. Thats the amount; you'd have to change each coordinate by to move; 1 unit of distance at 45 degrees. Try some; other angles. You'll notice that this chart and; those numbers disagree about the sign of the; Sin results. Where this chart shows negative,; the calculator shows positive etc. Thats; because computer Graphics are the opposite; in how they number horizontaly. Thats why the; formula below subtracts Sin And adds Cos.;; Anyway, an angle of 45 degrees would need To; move -.707 in the X Sin(45) And +0.707 in the; y Cos(45) planes. Imagine a little arrow; from 0,0 To -0.707,0.707. That would be what; people refer To as a "vector". If you measured; its length, you'd see it was exactly the same; length as it would be If it went +1 at 90; degrees.; Here's the world according to 2D graphics;                   180 degrees;           -x,-y       | -y    +x,-y;                       |;                       |;                       |;           -x          |          +x;90 degrees ------------+------------ 270 degrees;                       |;                       |;                       |;                       |;           -x,+y       | +y    +x,+y;                  0/360 Degrees; Sin() Returns the horizontal component of an angle; Cos() Returns the vertical component of an angleEndRemcomponent_x#=Sin(shipangle#)component_y#=Cos(shipangle#)Rem; Now that you know how much you need To move in each; direction, multiply that by the speed of your ship.; Lets assume you're going speed 10 at 120 degrees.EndRemdisplaced_x#=component_x#*shipspeed# ' 0.866 * 10 = 8.66 displaced_y#=component_y#*shipspeed# ' -0.5 * 10 = -5speeddir_x#=orig_x#-displaced_x# ' Now subtract from the ship's X positionspeeddir_y#=orig_y#+displaced_y# ' And add To the Y position' Now To show things on the screen.SetColor 255,255,255DrawLine(orig_x#,orig_y#,speeddir_x#,speeddir_y#) ' Shows direction And speed of shipDrawText("Ship speed: "+Int shipspeed#,speeddir_x#,speeddir_y#)' Draw X component sizeIf displaced_x#<0 Then SetColor 0,0,255 Else SetColor 255,0,0DrawLine(orig_x#,orig_y#,speeddir_x#,orig_y#)SetColor 255,255,255DrawText("DX "+Int (0-displaced_x#),speeddir_x#,orig_y#) ' Int them, too many numbers flying around =)' Draw Y component sizeIf displaced_y#>0 Then SetColor 0,0,255 Else SetColor 255,0,0DrawLine(orig_x#,orig_y#,orig_x#,speeddir_y#)SetColor 255,255,255DrawText("YX "+ Int displaced_y#,orig_x#,speeddir_y#)SetColor 255,255,255DrawText("Ship angle: "+shipangle#+" Sin: "+Sin(shipangle#)+" Cos: "+Cos(shipangle#),20,ScreenY-40)FlipUntil KeyDown(KEY_ESCAPE)End


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal