Search code examples
game-physicsvelocityshapessmallbasic

How do I make shapes move independently without jitter?


Here's the full code:

GraphicsWindow.Clear()
GraphicsWindow.CanResize = "false"
GraphicsWindow.Height = Desktop.Height-200
GraphicsWindow.Width = Desktop.Width-200

scount = Math.GetRandomNumber(25)
For s = 1 To scount
  Shx[s] = Math.GetRandomNumber(GraphicsWindow.Width-100)
  Shy[s] = Math.GetRandomNumber(GraphicsWindow.Height-100)
  shsize[s] = Math.GetRandomNumber(50)
  Sh[s] = Shapes.AddEllipse(shsize[s],shsize[s])
  Shapes.Move(Sh[s],Shx[s],Shy[s])
EndFor

loop:
For s = 1 to scount
  op[s] = Math.GetRandomNumber(2)
  If op[s] = 1 Then
    vel[s] = .5
  EndIf
  If op[s] = 2 Then
    vel[s] = -.5
  EndIf
  Shx[s] = Shx[s] + vel[s]
  Shy[s] = Shy[s] + vel[s]  
  Shapes.Move(Sh[s],Shx[s],Shy[s])
EndFor
Goto loop

My guess is the problem is here:

op[s] = Math.GetRandomNumber(2)
  If op[s] = 1 Then
    vel[s] = .5
  EndIf
  If op[s] = 2 Then
    vel[s] = -.5
  EndIf

What do I need to do to make the shapes move in independent directions without them jittering?


Solution

  • Another thing you could do is do frames per second. let all the objects do the math then update it. tHis is ran after all the math has been done so like usually its 60 frames a second so 1/60 to get amount of seconds per frame. you also need a timer to check the amount of time lapsed so you can then give the computer time to move each shape in proper time. if you need more code I will happily provide some.

      GraphicsWindow.Clear()
    GraphicsWindow.CanResize = "false"
    GraphicsWindow.Height = Desktop.Height-200
    GraphicsWindow.Width = Desktop.Width-200
    
    scount = Math.GetRandomNumber(25)
    For s = 1 To scount
      Shx[s] = Math.GetRandomNumber(GraphicsWindow.Width-100)
      Shy[s] = Math.GetRandomNumber(GraphicsWindow.Height-100)
      shsize[s] = Math.GetRandomNumber(50)
      Sh[s] = Shapes.AddEllipse(shsize[s],shsize[s])
      Shapes.Move(Sh[s],Shx[s],Shy[s])
       op[s] = Math.GetRandomNumber(2)
    EndFor
    
    loop:
    Time = Clock.ElapsedMilliseconds
    For s = 1 to scount
    
      If op[s] = 1 Then
        vel[s] = .5
      EndIf
      If op[s] = 2 Then
        vel[s] = -.5
      EndIf
      Shx[s] = Shx[s] + vel[s]
      Shy[s] = Shy[s] + vel[s]  
      Shapes.Move(Sh[s],Shx[s],Shy[s])
    EndFor
    frames()
    Goto loop
    
    Sub frames
         timeend = Clock.ElapsedMilliseconds
      TextWindow.WriteLine((timeend - Time )/1000)
      framelength = 60
      Timeperframe = 1/framelength
      while (((timeend - Time )/1000) <  Timeperframe)
        timeend = Clock.ElapsedMilliseconds
    
      EndWhile
      Time = Clock.ElapsedMilliseconds
    
      endsub