Author Topic: Particle System  (Read 8798 times)

0 Members and 1 Guest are viewing this topic.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Particle System
« on: April 10, 2022, 03:46:13 am »
I have been searching for a document(s), video, tutorials etc., on how to create a particle system. The problem is, I found heaps... My biggest problem is that the lack of information when it comes to Basic. I know that particle systems have been used in games etc. on this site. Which in itself is great. But, I seem to learn more from a tutorial as opposed to 'Monkey see. Monkey do.'  Copy and paste is fine, if only a result is needed, but that does not teach me how and why the code works... I am not sure I am making much sense. If I knew how to use all the other language examples that I have looked at, I probably would not be asking... Basic is what I know best... Basic examples/tutorials are what I am looking for... I would appreciate any assistance that you are willing to offer. Thank you.
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Particle System
« Reply #1 on: April 10, 2022, 07:45:00 am »
@johnno56

This surprises me! You've done particle systems way back at SdlBasic, just fine!

You probably would like to do them with an array for each... I don't know what to call them Property? is the VB term, characteristic? oh what are they called?!

You could setup an UDT for them like this:
Type Particle
    As Single x, y, dx, dy, life, active
    As _unsigned Long colr
end Type
nParticles = 100
Dim Shared parts(nParticles) as Particle

or do it the old fashioned way
dim x(nParticles), y(nParticles), dx(nParticles), dy(nParticles), life(nParticles), active(nParticles)
dim as _unsigned long colr(nParticles)

Then it's good to have a Sub for creating a new particle depends on your app what to do
Sub NewParticle(index) 'where index is for the shared array Parts
     ' setup Parts(index) for you app here
end sub

I could probably dig up some of your code from SdlBasic for doing particles :)




Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Particle System
« Reply #2 on: April 10, 2022, 07:57:09 am »
Found it:
http://sdlbasic.epizy.com/showthread.php?tid=235

From 6 years ago!

You call the properties parameters here, OK :)

Oh come to think of it, I think you mentioned losing your computer and a bunch of old work. Well that link is to fine Particle System you did.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Particle System
« Reply #3 on: April 10, 2022, 09:38:24 am »
Found it:
http://sdlbasic.epizy.com/showthread.php?tid=235

Here is a simple particle system that I converted from Basic256. It has a lot of comments that explain what each step does.

Note: On my machine, the 'polygon' command, causes sdlbasic to freeze. SetPixel, Bar, fillcircle and Ployline all work just fine.

Wow, what is Basic256!?  There's a successor to ModernBASIC 128?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Particle System
« Reply #4 on: April 10, 2022, 01:31:12 pm »
A little translation:
Code: QB64: [Select]
  1. ' 2022-04-10 b+ translation of  http://sdlbasic.epizy.com/showthread.php?tid=235  
  2. ' ***************
  3. ' Particle System
  4. ' ***************
  5. ' maximum number of particles slots
  6. ' lower this to get more fluent effect
  7. maximum = 100
  8.  
  9. ' Each particle has 6 parameters
  10. ' 0 : location on the x-axis
  11. ' 1 : location on the y-axis
  12. ' 2 : movement on the x-axis
  13. ' 3 : movement on the y-axis
  14. ' 4 : how old the particle can get
  15. ' 5 : period where particle keeps the same color
  16. Dim particle(maximum, 6)
  17.  
  18. ' ******************************
  19. ' Boiler plate for a nice canvas
  20. ' ******************************
  21. 'setDisplay(700, 400, 32, 1)
  22. Screen _NewImage(700, 400, 32)
  23. f& = _LoadFont("Arial.ttf", 40) ' everyone has arial?
  24.  
  25. 'setCaption("Particles Example")
  26. _Title "Particles Example"
  27. 'autoback(-2)
  28. 'hidemouse
  29. Dim As _Unsigned Long darkred, red, orange, yellow, white
  30. darkred = _RGB32(128, 0, 0)
  31. red = _RGB32(255, 0, 0)
  32. orange = _RGB32(255, 128, 0)
  33. yellow = _RGB32(255, 255, 0)
  34. white = _RGB32(255, 255, 255)
  35.  
  36. ' *************************************************
  37. ' pre-cache sin/cos calculations (small speed gain)
  38. ' *************************************************
  39. Dim degree(361, 2)
  40. pi = 3.141592654
  41. k = 0
  42. For i = 0 To 2 * pi Step (2 * pi / 359)
  43.     k = k + 1
  44.     degree(k, 0) = Sin(i)
  45.     degree(k, 1) = Cos(i)
  46.  
  47. 'initialize text
  48. text_count = 0
  49. t1$ = "Particles Example"
  50. t2$ = "( Cool, isn't it? )"
  51. t3$ = "(   useless too  )"
  52. t4$ = t2$
  53. sw_text = 0
  54.  
  55. ' *************************************************
  56. ' initial particle definition: all particles 'dead'
  57. ' *************************************************
  58. For i = 0 To maximum - 1
  59.     particle(i, 4) = 0
  60.  
  61. '**************************************************
  62. ' main loop
  63. '**************************************************
  64. While _KeyDown(27) = 0
  65.     Cls
  66.     ' create new particles
  67.     ' ********************
  68.     ' select 7 random particles at each cycle
  69.     ' play with this value to get the best result on your computer
  70.     For k = 1 To 7
  71.         spawni = rand(maximum)
  72.  
  73.         ' if age of the random particle = 0, create one
  74.         If particle(spawni, 4) = 0 Then
  75.  
  76.             ' horizontal location
  77.             If rand(100) / 100 > 0.5 Then
  78.                 particle(spawni, 0) = 20
  79.                 lings = 1
  80.             Else
  81.                 particle(spawni, 0) = 660
  82.                 lings = 0
  83.             End If
  84.  
  85.             ' vertical location
  86.             particle(spawni, 1) = 350
  87.             direction = rand(35) + 157
  88.  
  89.             ' horizontal speed vector
  90.             If lings = 1 Then
  91.                 particle(spawni, 2) = rand(6)
  92.             Else
  93.                 particle(spawni, 2) = 0 - (rand(6))
  94.             End If
  95.  
  96.             ' falling speed vector
  97.             particle(spawni, 3) = (0 - rand(6)) - 5
  98.  
  99.             ' particle age
  100.             particle(spawni, 4) = 30 + rand(50)
  101.  
  102.             ' color aging
  103.             particle(spawni, 5) = particle(spawni, 4) / 5
  104.         End If
  105.     Next
  106.  
  107.     ' update position on ALL live particles
  108.     ' *************************************
  109.     For i = 0 To maximum - 1
  110.  
  111.         ' for all live particles
  112.         If particle(i, 4) > 0 Then
  113.  
  114.             ' calculate the new location
  115.             particle(i, 0) = particle(i, 0) + particle(i, 2)
  116.             particle(i, 1) = particle(i, 1) + particle(i, 3)
  117.  
  118.             ' increase the speed of falling
  119.             particle(i, 3) = particle(i, 3) + (rand(100) / 100) / 3
  120.  
  121.             ' make the particle older
  122.             particle(i, 4) = particle(i, 4) - 1
  123.  
  124.             ' if it hits the bottom, make it bounce up
  125.             ' 0=no bounce, 1= full bounce,no damping
  126.             If particle(i, 1) > 370 Then
  127.                 particle(i, 3) = -particle(i, 3) * ((rand(100) / 100) / 2)
  128.             End If
  129.         End If
  130.     Next
  131.  
  132.     ' Color determination of the particle
  133.     '************************************
  134.     For i = 0 To maximum - 1
  135.  
  136.         ' for all live particles
  137.         If particle(i, 4) > 0 Then
  138.  
  139.             ' color it darkred if less than 20% life left
  140.             If particle(i, 4) < particle(i, 5) Then
  141.                 k = darkred
  142.             End If
  143.  
  144.             ' color it red if more than 20% life left
  145.             If particle(i, 4) > (particle(i, 5)) Then
  146.                 k = red
  147.             End If
  148.  
  149.             ' color it orange if more than 40% life left
  150.             If particle(i, 4) > (particle(i, 5) * 2) Then
  151.                 k = orange
  152.             End If
  153.  
  154.             ' color it yellow if more than 60% life left
  155.             If particle(i, 4) > (particle(i, 5) * 3) Then
  156.                 k = yellow
  157.             End If
  158.  
  159.             ' color it white if more than 80% life left
  160.             If particle(i, 4) > (particle(i, 5) * 4) Then
  161.                 k = white
  162.             End If
  163.  
  164.  
  165.             '----------------------------
  166.             ' PLOT ALL THE LIVE PARTICLES
  167.             '----------------------------
  168.  
  169.  
  170.             '--- circles ---
  171.             'circle(particle(i,0), particle(i,1), 2+rand(4))
  172.             '--- filled circles ---
  173.             'fillcircle(particle(i,0), particle(i,1), 2+rand(4))
  174.             '--- squares ---
  175.             'polyline(particle(i,0), particle(i,1), particle(i,0)+6, particle(i,1), particle(i,0)+6, particle(i,1)+6, particle(i,0), particle(i,1)+6, particle(i,0), particle(i,1))
  176.             '--- stars ---
  177.             'polyline(particle(i,0)+4, particle(i,1), particle(i,0)+8, particle(i,1)+12, particle(i,0), particle(i,1)+4, particle(i,0)+12, particle(i,1)+4, particle(i,0)+2, particle(i,1)+12)
  178.              star particle(i, 0), particle(i, 1), 8, 20, 5, 90, k
  179.             '--- polygon seems to 'lock-up' ---
  180.             'polygon(particle(i,0), particle(i,1), particle(i,0)+6, particle(i,1), particle(i,0)+6, particle(i,1)+6, particle(i,0), particle(i,1)+6, particle(i,0), particle(i,1))
  181.             '--- filled squares ---
  182.             'bar(particle(i,0), particle(i,1), particle(i,0)+2+rand(4), particle(i,1)+2+rand(4))
  183.  
  184.         End If
  185.     Next
  186.  
  187.     Color _RGB32(255, 255, 255)
  188.     _PrintString (190, 140), t1$
  189.     Color _RGB32(0, 0, 255)
  190.     _PrintString (192, 142), t1$
  191.     If text_count < 300 Then
  192.         'Text 223, 180, 24, t4$, &HFFFF0000
  193.         Color _RGB32(255, 255, 255)
  194.         _PrintString (213, 190), t4$
  195.         Color _RGB32(255, 0, 0)
  196.         _PrintString (215, 192), t4$
  197.         text_count = text_count + 1
  198.     Else
  199.         If sw_text = 1 Then
  200.             t4$ = t3$
  201.             sw_text = 0
  202.         Else
  203.             t4$ = t2$
  204.             sw_text = 1
  205.         End If
  206.         text_count = 0
  207.     End If
  208.     _Display
  209.     _Limit 10
  210.  
  211. Function rand% (n)
  212.     rand% = Int((n + 1) * Rnd)
  213.  
  214. Sub star (x, y, rInner, rOuter, nPoints, angleOffset, K As _Unsigned Long)
  215.     ' x, y are same as for circle,
  216.     ' rInner is center circle radius
  217.     ' rOuter is the outer most point of star
  218.     ' nPoints is the number of points,
  219.     ' angleOffset = angle offset IN DEGREES, it will be converted to radians in sub
  220.     ' this is to allow us to spin the polygon of n sides
  221.     Dim pAngle, radAngleOffset, x1, y1, x2, y2, x3, y3, i As Long
  222.  
  223.     pAngle = _D2R(360 / nPoints): radAngleOffset = _D2R(angleOffset)
  224.     x1 = x + rInner * Cos(radAngleOffset)
  225.     y1 = y + rInner * Sin(radAngleOffset)
  226.     For i = 0 To nPoints - 1
  227.         x2 = x + rOuter * Cos(i * pAngle + radAngleOffset + .5 * pAngle)
  228.         y2 = y + rOuter * Sin(i * pAngle + radAngleOffset + .5 * pAngle)
  229.         x3 = x + rInner * Cos((i + 1) * pAngle + radAngleOffset)
  230.         y3 = y + rInner * Sin((i + 1) * pAngle + radAngleOffset)
  231.         ftri x1, y1, x2, y2, x3, y3, K
  232.         'triangles leaked
  233.         Line (x1, y1)-(x2, y2), K
  234.         Line (x2, y2)-(x3, y3), K
  235.         Line (x3, y3)-(x1, y1), K
  236.         x1 = x3: y1 = y3
  237.     Next
  238.     Paint (x, y), K, K
  239.  
  240. '2019-12-16 fix by Steve saves some time with STATIC and saves and restores last dest
  241. Sub ftri (x1, y1, x2, y2, x3, y3, K As _Unsigned Long)
  242.     Dim D As Long
  243.     Static a&
  244.     D = _Dest
  245.     If a& = 0 Then a& = _NewImage(1, 1, 32)
  246.     _Dest a&
  247.     _DontBlend a& '  '<<<< new 2019-12-16 fix
  248.     PSet (0, 0), K
  249.     _Blend a& '<<<< new 2019-12-16 fix
  250.     _Dest D
  251.     _MapTriangle _Seamless(0, 0)-(0, 0)-(0, 0), a& To(x1, y1)-(x2, y2)-(x3, y3)
  252.  
  253. 'Sub Text (x, y, textHeight, txt$, k As _Unsigned Long)
  254. '    Dim fg As _Unsigned Long, cur&, I&, multi, xlen
  255. '    fg = _DefaultColor
  256. '    'screen snapshot
  257. '    cur& = _Dest
  258. '    I& = _NewImage(8 * Len(txt$), 16, 32)
  259. '    _Dest I&
  260. '    Color k
  261. '    _PrintString (0, 0), txt$
  262. '    multi = textHeight / 16
  263. '    xlen = Len(txt$) * 8 * multi
  264. '    _PutImage (x, y)-Step(xlen, textHeight), I&, cur&
  265. '    Color fg
  266. '    _FreeImage I&
  267. 'End Sub
  268.  
  269.  
« Last Edit: April 10, 2022, 01:40:30 pm by bplus »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Particle System
« Reply #5 on: April 10, 2022, 04:04:45 pm »
I have a feeling that I may have asked these questions before... Probably my age is showing... *sigh*

Bplus. Thank you for the sdl reference... Those programs were direct conversion from basic256 (No, Vince... Not a successor by a long mark... - Basic256 was used because it was easy for me to convert... lol) These were just plain conversions. No reference or documentation as to why or how it all works. A prime example, on my part, of Monkey see... Monkey do... But, I have to admit, their examples were indeed impressive... Not exactly Fireworks but still impressive...

There are many examples of particle systems 'out there' that explain, in detail, how it all works, but the coding languages that are used, may as well have been written in Romulan...  I would prefer references in Basic as I have spent my most of my adult life learning and tinkering with Basic. I cannot afford to dedicate a similar period of time in leaning a new language...

Some of the games I have cobbled together in the past are a little, shall we say boring or plain, and figured that particle effects might add a little extra... might be fun...

Many thanks for the pseudo code... That helps a lot...
Logic is the beginning of wisdom.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Particle System
« Reply #6 on: April 10, 2022, 10:21:14 pm »
There are two "particle systems" in my old Moon Lander you could
study - the volcano and fireworks. 

The volcano demonstrates my theory that the Earth is made of cheese.
Why else would lava be orange?
It works better if you plug it in.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Particle System
« Reply #7 on: April 11, 2022, 12:16:50 am »
Cheese and lava... I would not have made the connection... I will be sure to check out your Moon Lander. Thank you.
Logic is the beginning of wisdom.