10-11-2024, 07:08 PM
Another fix for Asteroids easier than one above but East = 0 degrees
Code: (Select All)
_Title "** Asteroid demo using radians fix 2" ' bplus 2024-10-11
' use forumla
' Vx = cos(a)
' Vy = sin(a)
' and normal _Atan2 but we sacrifice North = 0
Const FALSE = 0, TRUE = Not FALSE ' truth detectors
Const PI = 3.1415926, PI2 = 2 * PI ' useful PI values
Const SPINRATE = PI2 / 360 ' asteroid spin rate
Const SWIDTH = 640, SHEIGHT = 480 ' screen dimensions
Const MAXASTEROIDS = 20 ' number of asteroids on screen
Type XYPAIR ' 2D point location definition
x As Single ' x coordinate
y As Single ' y coordinate
End Type
Type OBJECT ' object definition (points that make up an object)
Radian As Single ' direction of point
Radius As Single ' distance to point from center
End Type
Type ASTEROID ' asteroid definition
Loc As XYPAIR ' asteroid location
Dir As Single ' asteroid radian direction
Speed As Integer ' asteroid speed
Size As Integer ' asteroid size
End Type
Dim Object(10) As OBJECT ' object point data
Dim Asteroid(MAXASTEROIDS) As ASTEROID ' asteroids array
Dim Vector As XYPAIR ' vector calculations
Dim Obj As Integer ' object counter
Dim Ast As Integer ' asteroid counter
Dim P1 As XYPAIR ' first object point for PSET
Dim Np As XYPAIR ' next object point for LINE
Dim Spin As Integer ' TRUE to activate spin, FALSE otherwise
Randomize Timer ' seed RND generator
For Obj = 1 To 10 ' cycle through object points
Read Vector.x, Vector.y ' get object x,y vector point
Object(Obj).Radius = _Hypot(Vector.x, Vector.y) ' calculate radius from vector
' back to normal _Atan2
Object(Obj).Radian = _Atan2(Vector.y, Vector.x) ' calculate direction from vector
Next Obj
For Ast = 1 To MAXASTEROIDS ' cycle through asteroids
Asteroid(Ast).Loc.x = Int(Rnd * (SWIDTH - 40)) + 20 ' random location
Asteroid(Ast).Loc.y = Int(Rnd * (SHEIGHT - 40)) + 20
Asteroid(Ast).Dir = Rnd * PI2 ' random direction
Asteroid(Ast).Speed = Int(Rnd * 6) + 2 ' random speed
Asteroid(Ast).Size = 2 ^ Int(Rnd * 3) ' random size
Next Ast
Screen _NewImage(SWIDTH, SHEIGHT, 32) ' graphics screen
Spin = FALSE ' no asteroid spin
Do ' begin main loop
Cls ' clear screen
_Limit 60 ' 60 frames per second
Locate 2, 19: Print "Press the spacebar to activate asteroid spin" ' print directions
If _KeyHit = 32 Then Spin = Not Spin ' flip spin flag if spacebar pressed
If Spin Then ' spin asteroids?
For Obj = 1 To 10 ' yes, cycle through object points
Object(Obj).Radian = Object(Obj).Radian + SPINRATE ' move radian location
If Object(Obj).Radian > PI2 Then Object(Obj).Radian = Object(Obj).Radian - PI2 ' keep within limits
Next Obj
End If
For Ast = 1 To MAXASTEROIDS ' cycle through asteroids
Vector.x = Cos(Object(1).Radian) ' calculate vector from 1st object point
Vector.y = Sin(Object(1).Radian) ' <<< fix
P1.x = Asteroid(Ast).Loc.x + Vector.x * Object(1).Radius * Asteroid(Ast).Size ' plot location on screen
P1.y = Asteroid(Ast).Loc.y + Vector.y * Object(1).Radius * Asteroid(Ast).Size
PSet (P1.x, P1.y) ' draw a pixel
For Obj = 2 To 10 ' cycle through remaining points
' these assume North = 0 so Vectors are rotated 90 degrees
Vector.x = Cos(Object(Obj).Radian) ' calculate vector from object point
Vector.y = Sin(Object(Obj).Radian) ' <<<< this should be -Cos
Np.x = Asteroid(Ast).Loc.x + Vector.x * Object(Obj).Radius * Asteroid(Ast).Size ' plot location
Np.y = Asteroid(Ast).Loc.y + Vector.y * Object(Obj).Radius * Asteroid(Ast).Size ' on screen
Line -(Np.x, Np.y) ' draw line from previous point
Next Obj
Line -(P1.x, P1.y) ' draw final line back to start
Vector.x = Cos(Asteroid(Ast).Dir) ' get vector from asteroid radian
Vector.y = Sin(Asteroid(Ast).Dir) ' <<< This should be -Cos
Asteroid(Ast).Loc.x = Asteroid(Ast).Loc.x + Vector.x * Asteroid(Ast).Speed ' plot location on screen
Asteroid(Ast).Loc.y = Asteroid(Ast).Loc.y + Vector.y * Asteroid(Ast).Speed
If Asteroid(Ast).Loc.x < -19 Then Asteroid(Ast).Loc.x = SWIDTH + 19 ' keep asteroids on screen
If Asteroid(Ast).Loc.x > SWIDTH + 19 Then Asteroid(Ast).Loc.x = -19
If Asteroid(Ast).Loc.y < -19 Then Asteroid(Ast).Loc.y = SHEIGHT + 19
If Asteroid(Ast).Loc.y > SHEIGHT + 19 Then Asteroid(Ast).Loc.y = -19
Next Ast
_Display ' update screen with changes
Loop Until _KeyDown(27) ' leave when ESC pressed
System ' return to operating system
' Asteroid object data (10 coordinate vector points)
Data 0,-5,5,-10,10,-5,7,0,10,5,2,10,-5,10,-10,5,-10,-5,-5,-10
b = b + ...