none
Water simulation performance issues RRS feed

  • Question

  • Is there any way to optimise this code? Such as a different way to display the particles or something

    Instructions

    left click to add a particle and right click to move the particle around

    CMH379

    smallbasic.com/smallbasic.com/program/?CMH379

    Tuesday, October 27, 2015 5:05 PM

Answers

  • Hi,

    I really like your program - physics type simulation is cool.

    First I would modify the game loop using a delay to keep the frame rate constant, below is about 50 fps.  For the same reason no need to do the updt stuff - just every 20 ms.

    While 1=1
      start = Clock.ElapsedMilliseconds
      
      update()
      
      delay = 20 - (Clock.ElapsedMilliseconds-start)
      If (delay > 0) Then
        Program.Delay(delay)
      EndIf
    EndWhile

    This makes it slow, but 50 fps is plenty, so the movement is too small each frame.

    We can increse speed with bigger steps:

        x[i] = x[i] + fx[i] * speed
        y[i] = y[i] + fy[i] * speed
    

    Some of the ifs are exclusive so use if - elseif construct

    For c = 1 To am could be For c= i+1 To am (i.e each pair of balls)

    Import CMH379-0 for these mods - SB graphics can be slow and the logic you have is pretty nice.

    Again - I really like this - well done.

    • Marked as answer by _'-'_ Monday, November 2, 2015 8:27 PM
    Sunday, November 1, 2015 7:25 PM
    Moderator

All replies

  • Hi,

    I really like your program - physics type simulation is cool.

    First I would modify the game loop using a delay to keep the frame rate constant, below is about 50 fps.  For the same reason no need to do the updt stuff - just every 20 ms.

    While 1=1
      start = Clock.ElapsedMilliseconds
      
      update()
      
      delay = 20 - (Clock.ElapsedMilliseconds-start)
      If (delay > 0) Then
        Program.Delay(delay)
      EndIf
    EndWhile

    This makes it slow, but 50 fps is plenty, so the movement is too small each frame.

    We can increse speed with bigger steps:

        x[i] = x[i] + fx[i] * speed
        y[i] = y[i] + fy[i] * speed
    

    Some of the ifs are exclusive so use if - elseif construct

    For c = 1 To am could be For c= i+1 To am (i.e each pair of balls)

    Import CMH379-0 for these mods - SB graphics can be slow and the logic you have is pretty nice.

    Again - I really like this - well done.

    • Marked as answer by _'-'_ Monday, November 2, 2015 8:27 PM
    Sunday, November 1, 2015 7:25 PM
    Moderator
  • Thank you! 

    it runs so much faster, I'm thinking about making the program so it finds all the connections to start of with but if the connection is lost it cannot re-join.

    Monday, November 2, 2015 7:11 PM