Author Topic: Pandemic simulation (simple particle system)  (Read 2773 times)

0 Members and 1 Guest are viewing this topic.

FellippeHeitor

  • Guest
Pandemic simulation (simple particle system)
« on: July 21, 2020, 11:29:44 pm »


A simple particle system written in QB64, which can serve of basis for cloning the video seen on https://www.reddit.com/r/dataisbeautiful/comments/hpigft/oc_simple_pandemic_simulation/?utm_source=share&utm_medium=ios_app&utm_name=iossmf

The code written in this video:
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3.  
  4. TYPE object
  5.     x AS SINGLE
  6.     y AS SINGLE
  7.     xv AS SINGLE
  8.     yv AS SINGLE
  9.     state AS _BYTE
  10.     size AS INTEGER
  11.  
  12. DIM SHARED o(500) AS object
  13.  
  14. 'generate individuals
  15. FOR i = 1 TO UBOUND(o)
  16.     o(i).x = RND * _WIDTH
  17.     o(i).y = RND * _HEIGHT
  18.     o(i).xv = 1 - RND * 2
  19.     o(i).yv = 1 - RND * 2
  20.     o(i).state = 1 'healthy
  21.     o(i).size = 10
  22.  
  23. infected = _CEIL(RND * UBOUND(o))
  24. o(infected).state = 2 'infected
  25.  
  26. c(1) = _RGB32(11, 255, 0)
  27. c(2) = _RGB32(200, 67, 17)
  28.  
  29.     CLS
  30.     FOR i = 1 TO UBOUND(o)
  31.         move o(i)
  32.         IF o(i).state = 2 THEN spread o(i)
  33.         CircleFill o(i).x, o(i).y, o(i).size, c(o(i).state)
  34.     NEXT
  35.  
  36.     _DISPLAY
  37.     _LIMIT 120
  38.  
  39. SUB spread (obj AS object)
  40.     FOR i = 1 TO UBOUND(o)
  41.         IF dist(obj, o(i)) < obj.size * 2 THEN
  42.             o(i).state = 2
  43.         END IF
  44.     NEXT
  45.  
  46. FUNCTION dist! (o1 AS object, o2 AS object)
  47.     x1! = o1.x
  48.     y1! = o1.y
  49.     x2! = o2.x
  50.     y2! = o2.y
  51.     dist! = _HYPOT((x2! - x1!), (y2! - y1!))
  52.  
  53. SUB move (obj AS object)
  54.     obj.x = obj.x + obj.xv
  55.     IF obj.x < 0 OR obj.x > _WIDTH THEN obj.xv = obj.xv * -1
  56.  
  57.     obj.y = obj.y + obj.yv
  58.     IF obj.y < 0 OR obj.y > _HEIGHT THEN obj.yv = obj.yv * -1
  59.  
  60. SUB CircleFill (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  61.     ' CX = center x coordinate
  62.     ' CY = center y coordinate
  63.     '  R = radius
  64.     '  C = fill color
  65.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  66.     DIM X AS INTEGER, Y AS INTEGER
  67.     Radius = ABS(R)
  68.     RadiusError = -Radius
  69.     X = Radius
  70.     Y = 0
  71.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  72.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  73.     WHILE X > Y
  74.         RadiusError = RadiusError + Y * 2 + 1
  75.         IF RadiusError >= 0 THEN
  76.             IF X <> Y + 1 THEN
  77.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  78.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  79.             END IF
  80.             X = X - 1
  81.             RadiusError = RadiusError - X * 2
  82.         END IF
  83.         Y = Y + 1
  84.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  85.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  86.     WEND
  87.  

Loudar's mods here in our forum: https://www.qb64.org/forum/index.php?topic=2827.msg120934#msg120934

Marked as best answer by on May 20, 2024, 03:00:10 pm

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Pandemic simulation (simple particle system)
« Reply #1 on: July 22, 2020, 01:00:12 pm »
  • Undo Best Answer
  • Pretty much same as particle system used in this Game Exercise:
    https://www.qb64.org/forum/index.php?topic=2812.msg120831#msg120831

    FellippeHeitor

    • Guest
    Re: Pandemic simulation (simple particle system)
    « Reply #2 on: July 22, 2020, 01:14:09 pm »
    True!

    Offline Kiara87

    • Forum Regular
    • Posts: 164
      • View Profile
    Re: Pandemic simulation (simple particle system)
    « Reply #3 on: July 22, 2020, 05:13:22 pm »
    True!

    thank you friend it can be a great exercise
    se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

    Offline johnno56

    • Forum Resident
    • Posts: 1270
    • Live long and prosper.
      • View Profile
    Re: Pandemic simulation (simple particle system)
    « Reply #4 on: July 22, 2020, 06:46:08 pm »
    Cool... Reminded me of the old "Forest Fire" simulator. Nice job!
    Logic is the beginning of wisdom.

    FellippeHeitor

    • Guest
    Re: Pandemic simulation (simple particle system)
    « Reply #5 on: July 22, 2020, 06:51:39 pm »
    Thanks @Kiara87 and @johnno56

    Offline bplus

    • Global Moderator
    • Forum Resident
    • Posts: 8053
    • b = b + ...
      • View Profile
    Re: Pandemic simulation (simple particle system)
    « Reply #6 on: July 23, 2020, 09:49:28 am »
    Hi all, I fixed the spread so that it does not slow down as more cells get infected, I changed a couple other minor things.

    Press space bar to rerun the sim with new setup.

    Code: QB64: [Select]
    1. _TITLE "Pandemic Sim & Particles Demo" 'b+ mod Fellippe's  2020-07-23
    2. ' ref 2020-07-22   https://www.qb64.org/forum/index.php?topic=2839.msg121033#msg121033
    3.  
    4. SCREEN _NEWIMAGE(800, 600, 32) ' create a graphics screen
    5. _DELAY .25 '                     give time for screen to load
    6. _SCREENMOVE _MIDDLE '            before moving it
    7. RANDOMIZE TIMER '                for different results each run
    8.  
    9. CONST objSize = 5 'the size of all our objects is constant
    10.  
    11. TYPE object
    12.     x AS SINGLE '         screen location x coordinate
    13.     y AS SINGLE '         screen location y coordinate
    14.     xv AS SINGLE '        velocity parallel to x axis, this is the x component of the movement vector
    15.     yv AS SINGLE '        velocity parallel to y axis, this is the y component of the movement vector
    16.     state AS _BYTE '      healthy = 1, infected = 2
    17.  
    18. DIM SHARED o(200) AS object
    19.  
    20. DIM SHARED c(1 TO 2) AS _UNSIGNED LONG ' colors for healthy cell 1 and infected cell 2
    21. c(1) = _RGB32(11, 255, 0)
    22. c(2) = _RGB32(200, 67, 17)
    23.  
    24. restart:
    25. generate
    26. infected = _CEIL(RND * UBOUND(o)) '   pick a random cell  use _CEIL to round up, don't want 0 infected
    27. o(infected).state = 2 '               infect it
    28.     IF INKEY$ = " " THEN GOTO restart
    29.     CLS
    30.     move
    31.     spread
    32.     show
    33.     _DISPLAY
    34.     _LIMIT 120
    35.  
    36. SUB generate 'individuals
    37.     FOR i = 1 TO UBOUND(o)
    38.         o(i).x = RND * _WIDTH
    39.         o(i).y = RND * _HEIGHT
    40.         o(i).xv = 1 - RND * 2
    41.         o(i).yv = 1 - RND * 2
    42.         o(i).state = 1 'healthy
    43.     NEXT
    44.  
    45. SUB move
    46.     FOR i = 1 TO UBOUND(o) ' i = 1 to TO UBOUND(o)
    47.         o(i).x = o(i).x + o(i).xv
    48.         IF o(i).x < 0 OR o(i).x > _WIDTH THEN o(i).xv = o(i).xv * -1 ' change direction when out of bounds
    49.         o(i).y = o(i).y + o(i).yv
    50.         IF o(i).y < 0 OR o(i).y > _HEIGHT THEN o(i).yv = o(i).yv * -1 ' change direction when out of bounds
    51.     NEXT
    52.  
    53. SUB spread
    54.     FOR i = 1 TO UBOUND(o) - 1 ' compare every cell to every other if too close and infected then all around are
    55.         FOR j = i + 1 TO UBOUND(o)
    56.             IF _HYPOT(o(j).x - o(i).x, o(j).y - o(i).y) < objSize * 2 THEN 'check distance 2 cells touching
    57.                 IF o(i).state = 2 OR o(j).state = 2 THEN o(i).state = 2: o(j).state = 2 ' set both to infected
    58.                 ' note: just setting both states to infected is faster than ckecking and changing only the healthy
    59.             END IF
    60.         NEXT
    61.     NEXT
    62.  
    63. SUB show
    64.     FOR i = 1 TO UBOUND(o)
    65.         FOR r = 0 TO objSize STEP .25 'Ken's way to circle fill without extra subroutine
    66.             CIRCLE (o(i).x, o(i).y), r, c(o(i).state)
    67.         NEXT
    68.     NEXT
    69.  
    70.  

    Funny story, I watched Fellippe's video yesterday around lunch time, looked in here a couple of times but spent most of day reading. I went to bed and as soon as I started to relax it hit me how easy it was to fix the slow down as cells got infected.
    « Last Edit: July 23, 2020, 10:02:12 am by bplus »

    FellippeHeitor

    • Guest
    Re: Pandemic simulation (simple particle system)
    « Reply #7 on: July 23, 2020, 10:03:43 am »
    That's cool, bplus! Thanks for the new ideas ;-)

    About the late-night eureka moment, I can identify!

      [ You are not allowed to view this attachment ]  
    « Last Edit: July 23, 2020, 10:09:55 am by FellippeHeitor »

    Offline johnno56

    • Forum Resident
    • Posts: 1270
    • Live long and prosper.
      • View Profile
    Re: Pandemic simulation (simple particle system)
    « Reply #8 on: July 23, 2020, 04:54:13 pm »
    Aww... Now you guys are just showing off... We mortals have to be content with trying to problem solve whilst we are still out of bed... *sigh*
    Logic is the beginning of wisdom.

    Offline SMcNeill

    • QB64 Developer
    • Forum Resident
    • Posts: 3972
      • View Profile
      • Steve’s QB64 Archive Forum
    Re: Pandemic simulation (simple particle system)
    « Reply #9 on: July 23, 2020, 07:14:05 pm »
    Aww... Now you guys are just showing off... We mortals have to be content with trying to problem solve whilst we are still out of bed... *sigh*

    Whereas we Steve’s simply are too good to have any problems to begin with...   ;D
    https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

    Offline OldMoses

    • Seasoned Forum Regular
    • Posts: 469
      • View Profile
    Re: Pandemic simulation (simple particle system)
    « Reply #10 on: July 23, 2020, 09:19:39 pm »
    This was so intriguing that I decided to play around with it myself, adding immunity, random population health, strain virulence, turns to death and turns required to achieve immunity before death. All such parameters are just pulled out of my tinfoil hat.

    EDIT: Oops back to the drawing board on that one. It will end up with more dead than it started with....LOL

    It must be the blue zombie virus...


    Code: QB64: [Select]
    1. _TITLE "Pandemic Sim & Particles Demo" 'OldMoses' mod of b+ mod Fellippe's  2020-07-23
    2. ' ref 2020-07-22   https://www.qb64.org/forum/index.php?topic=2839.msg121033#msg121033
    3.  
    4. SCREEN _NEWIMAGE(800, 600, 32) ' create a graphics screen
    5. _DELAY .25 '                     give time for screen to load
    6. _SCREENMOVE _MIDDLE '            before moving it
    7. RANDOMIZE TIMER '                for different results each run
    8.  
    9. CONST objSize = 5 'the size of all our objects is constant
    10.  
    11. TYPE object
    12.     x AS SINGLE '         screen location x coordinate
    13.     y AS SINGLE '         screen location y coordinate
    14.     xv AS SINGLE '        velocity parallel to x axis, this is the x component of the movement vector
    15.     yv AS SINGLE '        velocity parallel to y axis, this is the y component of the movement vector
    16.     state AS _BYTE '      healthy = 1, infected = 2, 3 = immune
    17.     health AS _BYTE
    18.     sick AS _BYTE
    19.     dead AS _BYTE
    20.  
    21. DIM SHARED o(200) AS object
    22.  
    23. DIM SHARED c(1 TO 3) AS _UNSIGNED LONG ' colors for healthy cell 1 and infected cell 2
    24. c(1) = _RGB32(11, 255, 0)
    25. c(2) = _RGB32(200, 67, 17)
    26. c(3) = _RGB32(11, 67, 200)
    27.  
    28. restart:
    29. generate
    30. infected = _CEIL(RND * UBOUND(o)) '   pick a random cell  use _CEIL to round up, don't want 0 infected
    31. o(infected).state = 2 '               infect it
    32.     IF INKEY$ = " " THEN GOTO restart
    33.     CLS
    34.     move
    35.     spread
    36.     show
    37.     _DISPLAY
    38.     _LIMIT 120
    39.  
    40. SUB generate 'individuals
    41.     FOR i = 1 TO UBOUND(o)
    42.         o(i).x = RND * _WIDTH
    43.         o(i).y = RND * _HEIGHT
    44.         o(i).xv = 1 - RND * 2
    45.         o(i).yv = 1 - RND * 2
    46.         o(i).state = 1 'healthy
    47.         o(i).health = INT(RND * 90) + 10
    48.         o(i).sick = 0
    49.         o(i).dead = 3
    50.     NEXT
    51.  
    52. SUB move
    53.     FOR i = 1 TO UBOUND(o) ' i = 1 to TO UBOUND(o)
    54.         o(i).x = o(i).x + o(i).xv
    55.         IF o(i).x < 0 OR o(i).x > _WIDTH THEN o(i).xv = o(i).xv * -1 ' change direction when out of bounds
    56.         o(i).y = o(i).y + o(i).yv
    57.         IF o(i).y < 0 OR o(i).y > _HEIGHT THEN o(i).yv = o(i).yv * -1 ' change direction when out of bounds
    58.         IF o(i).state = 2 THEN
    59.             virulence% = 90 ' INT(RND * 90) + 1
    60.             o(i).health = o(i).health - virulence%
    61.             IF virulence% > o(i).health THEN o(i).dead = o(i).dead - 1
    62.             o(i).sick = o(i).sick + 1
    63.             IF o(i).sick > 10 AND o(i).dead > 0 THEN
    64.                 o(i).state = 3
    65.             END IF
    66.         END IF
    67.     NEXT
    68.  
    69. SUB spread
    70.     FOR i = 1 TO UBOUND(o) - 1 ' compare every cell to every other if too close and infected then all around are
    71.         FOR j = i + 1 TO UBOUND(o)
    72.             IF _HYPOT(o(j).x - o(i).x, o(j).y - o(i).y) < objSize * 2 THEN 'check distance 2 cells touching
    73.                 IF o(i).state <> 3 OR o(j).state <> 3 THEN
    74.                     IF o(i).state = 2 OR o(j).state = 2 THEN o(i).state = 2: o(j).state = 2 ' set both to infected
    75.                     ' note: just setting both states to infected is faster than ckecking and changing only the healthy
    76.                 END IF
    77.             END IF
    78.         NEXT
    79.     NEXT
    80.  
    81. SUB show
    82.     FOR i = 1 TO UBOUND(o)
    83.         IF o(i).dead > 0 THEN
    84.             FOR r = 0 TO objSize STEP .25 'Ken's way to circle fill without extra subroutine
    85.                 CIRCLE (o(i).x, o(i).y), r, c(o(i).state)
    86.             NEXT
    87.         END IF
    88.     NEXT
    89.  
    90.  
    91.  
    « Last Edit: July 23, 2020, 09:43:56 pm by OldMoses »

    FellippeHeitor

    • Guest
    Re: Pandemic simulation (simple particle system)
    « Reply #11 on: July 23, 2020, 10:02:41 pm »
    Lol @ blue zombie virus. Thanks for your contribution, OldMoses!

    Offline TempodiBasic

    • Forum Resident
    • Posts: 1792
      • View Profile
    Re: Pandemic simulation (simple particle system)
    « Reply #12 on: July 25, 2020, 01:40:06 pm »
    That's cool, bplus! Thanks for the new ideas ;-)

    About the late-night eureka moment, I can identify!

      [ You are not allowed to view this attachment ]

    LOL
    it is an our little trueness... when we put in the oven the problem it will be cooked!
    Programming isn't difficult, only it's  consuming time and coffee

    Offline Dav

    • Forum Resident
    • Posts: 792
      • View Profile
    Re: Pandemic simulation (simple particle system)
    « Reply #13 on: July 29, 2020, 05:41:52 pm »
    I like your video tutorials Fellippe.  You present them well.  Have you done one on using the QB64 IDE?  Changing settings, etc.  I think it would be a good introduction and help for new QB64 users.

    Nice job on the simulator.

    - Dav
    « Last Edit: July 29, 2020, 06:15:55 pm by Dav »

    FellippeHeitor

    • Guest
    Re: Pandemic simulation (simple particle system)
    « Reply #14 on: July 29, 2020, 07:24:16 pm »
    That'd be a good idea indeed, Dav. Thank you very much!