Ooops
November 28, 2020, 10:29:12 AM

Author Topic: [bmx] Worm Screen Saver by TomToad [ 1+ years ago ]  (Read 466 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Worm Screen Saver by TomToad [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Worm Screen Saver
Author : TomToad
Posted : 1+ years ago

Description : I was messing with vectors when I thought about moving particles being pulled by singularities in the world.  Each "pull" would be added and the particle updated by the resulting vector.  Kind of like planets being pulled by gravity from suns.  After adding a trail to the particles, I ended up with something that would be a good screensaver.  Each worm's direction is affected by the randomly placed singularities.  As it nears one, it will be pulled closer to it.

Press "S" to turn on and off the rendering of the singularities.


Code :
Code: BlitzMax
  1. SuperStrict
  2. SeedRnd(MilliSecs())'randomize the seed
  3.  
  4. Global Width:Int = DesktopWidth() 'get the desktop resolution
  5. Global Height:Int = DesktopHeight()
  6. Global Depth:Int = 32
  7. While Not GraphicsModeExists(Width,Height,Depth) 'find the highest bit depth we can use
  8.         Depth :- 8
  9.         If Depth = 0 Then RuntimeError("Cannot find depth for "+Width+"x"+Height)
  10. Wend
  11.  
  12. Type TVector 'Just a vector :)
  13.         Field x:Double
  14.         Field y:Double
  15.        
  16.         Function Zero:TVector() 'creates a 0 vector
  17.                 Local Vector:TVector = New TVector
  18.                 Vector.x = 0
  19.                 Vector.y = 0
  20.                 Return Vector
  21.         End Function
  22. End Type
  23.  
  24. ' A TSingularity is just an entity in space.  The worms direction is affected by the "pull" of each
  25. ' singularity it is close to.
  26. Type TSingularity
  27.         Global List:TList = CreateList() 'This holds a list of all the singularities in the world
  28.        
  29.         Field x:Double 'The singularity's position
  30.         Field y:Double
  31.        
  32.         'This function takes a location and returns a vector depending on the direction and the nearness
  33.         'relative to the singularity.  the closer to the singularity the point is, the greater the magnitude
  34.         'Any point which is more than 100 pixels away and the method returns a zero vector
  35.         Method CreateVector:TVector(x:Int,y:Int)
  36.                 Local dx:Double = Self.x - x 'calculate the offset from the point to the singularity
  37.                 Local dy:Double = Self.y - y
  38.                 Local Distance:Double = Sqr(dx*dx+Dy*dy) 'calculate the distance
  39.                 If Distance > 100 Then Return TVector.Zero() 'if the distance is more than 100, return 0 vector
  40.                
  41.                 Local Magnitude:Double = (100.0-Distance)*.006 'magnitude is inversely proportional to the distance
  42.                 If Magnitude < 0 Then Return TVector.Zero() 'shouldn't happen, but might due to rounding if distance is very near 100
  43.                 Local Direction:Double = ATan2(dy,dx) 'Get the direction to the singularity
  44.                
  45.                 Local Vector:TVector = New TVector
  46.                 Vector.x = Cos(direction)*Magnitude 'create a vector at the proper direction and distance
  47.                 vector.y = Sin(Direction)*Magnitude
  48.                 Return Vector 'return the vector
  49.         End Method
  50. End Type
  51.  
  52. For Local i:Int = 1 To 100 'We will create 100 singularities
  53.         Local Singularity:TSingularity = New TSingularity
  54.         Singularity.X = Rnd(0,Width) 'Place the singularity randomly within our space
  55.         Singularity.y = Rnd(0,Height)
  56.        
  57.         TSingularity.List.AddLast(Singularity)
  58. Next
  59.  
  60. 'Each particle represents the "head" of our worm.  The tail is created by the trail.  
  61. Type TParticle
  62.         Field Trail:TList = CreateList() 'A list used for the "Tail"
  63.         Field TrailCount:Int = 0
  64.         Field x:Double 'the position of the head particle
  65.         Field y:Double
  66.         Field momentum:TVector 'the movement vector last frame
  67.        
  68.         Method New() 'Creates the Momentum vector and sets it to zero
  69.                 Momentum = TVector.Zero()
  70.         End Method
  71.        
  72.         'this updates the position of the worm from the combined vectors of all the singularites and the momentum
  73.         'of the worm.  It also saves this movement for use in the next frame
  74.         Method Update(Vector:TVector)
  75.                 'if the particle is moving very, very, very slowly, then it might be too far from any singularities.
  76.                 'We need to give it a little nudge.
  77.                
  78.                 If Abs(Vector.x) < .00001 And Abs(Vector.y) < .00001
  79.                         Vector.x = Sgn(Width/2-x)*.06
  80.                         Vector.y = Sgn(Height/2-y)*.06
  81.                 End If
  82.                 Local TrailVector:TVector = New TVector 'We need to save the current position in the trail to create it's "tail"
  83.                 TrailVector.X = x
  84.                 TrailVector.Y = y
  85.                 Trail.AddLast(TrailVector)
  86.                 If TrailCount >= 20 'The tail will only be 20 units long
  87.                         Trail.RemoveFirst()
  88.                 Else
  89.                         TrailCount :+ 1
  90.                 End If
  91.                 Momentum.x = Vector.x 'Save the current vector for momentum
  92.                 Momentum.y = Vector.y
  93.                 x :+ Vector.x 'update the head's position
  94.                 y :+ Vector.y
  95.                 'if our worm leaves the world, we will reposition it randomly
  96.                 If x < -100 Or x > Width + 100 Or y < -100 Or y > Height + 100
  97.                         x = Rand(0,Width)
  98.                         y = Rand(0,Height)
  99.                         Momentum.X = 0
  100.                         Momentum.Y = 0
  101.                         Trail.Clear()
  102.                         TrailCount = 0
  103.                 End If
  104.         End Method
  105. End Type
  106.  
  107. Local List:TList = CreateList() 'this holds a list of all the particles in the world
  108. For Local i:Int = 1 To 30 '30 particles
  109.         Local Particle:TParticle = New TParticle
  110.         Particle.x = Rnd(0,Width) 'Pick a random position
  111.         Particle.y = Rnd(0,Height)
  112.         List.AddLast(Particle)
  113. Next
  114.  
  115. Graphics Width,Height,Depth 'Set the screen rez
  116. Local PVector:TVector = New TVector 'this will hold the combined vectors for our particle
  117. Local NextTime:Int = MilliSecs() + 16 'Implementing a fixed logic scheme.  this is roughly 60 frames a second
  118. Local SingularityFlag:Int = False 'true to drawsingularities
  119.  
  120. While Not KeyHit(KEY_ESCAPE)
  121.         Local time:Int = MilliSecs() 'current time
  122.         'The logic loop.
  123.         While Time >= NextTime
  124.                 For Local Particle:TParticle = EachIn List 'go through all the particles
  125.                         PVector.x = 0 'Set the PVector to 0.  I could just create a new 0 Vector, but I think that
  126.                         PVector.y = 0 'just setting this to 0 is quicker
  127.                        
  128.                         For Local Singularity:TSingularity = EachIn TSingularity.List 'Now we need to go through each singularity
  129.                                 Local Vector:Tvector = Singularity.CreateVector(Particle.x,Particle.y) 'Get the vector of this singularity
  130.                                 PVector.x :+ Vector.X 'Add the returned vector to the current particle vector
  131.                                 PVector.Y :+ Vector.Y
  132.                         Next
  133.                         PVector.X :+ particle.Momentum.X 'Now we add the momentum of the particle
  134.                         PVector.y :+ Particle.Momentum.Y
  135.                         Particle.Update(PVector) 'update the particle
  136.                 Next
  137.                 NextTime :+ 16 'Next logic frame
  138.         Wend
  139.         If KeyHit(KEY_S) Then SingularityFlag = Not SingularityFlag 'toggle whether singularities are drawn or not
  140.         Cls
  141.         If SingularityFlag 'If true, we draw the singularities
  142.                 SetColor 255,255,0
  143.                 For Local Singularity:TSingularity = EachIn TSingularity.List
  144.                         DrawOval Singularity.x-2,Singularity.y-2,4,4
  145.                 Next
  146.        
  147.        
  148.                 SetColor 255,255,255
  149.         End If
  150.         For Local Particle:TParticle = EachIn List 'go through each particle and draw it's "head" and "Tail"
  151.                 Local ox:Double
  152.                 Local oy:Double
  153.                 Local Define:Int = False
  154.                 For Local Trail:TVector = EachIn Particle.Trail
  155.                         If Not Define
  156.                                 Define = True
  157.                                 ox = Trail.x
  158.                                 oy = trail.y
  159.                                 Continue
  160.                         End If
  161.                         DrawLine ox,oy,Trail.x,Trail.y
  162.                         ox = Trail.x
  163.                         oy = Trail.y
  164.                 Next
  165.                 DrawOval Particle.x-2,Particle.y-2,4,4
  166.         Next
  167.         Flip
  168. Wend


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal