Author Topic: Molecules simulation (simple particle system, part II)  (Read 3857 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

FellippeHeitor

  • Guest
Molecules simulation (simple particle system, part II)
« on: July 24, 2020, 12:32:31 am »


Similarly to the previous video, I create a simple particle system but this time I get them to notice each other in a different way, to simulate atoms coming together to form molecules.

Code:
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.     size AS INTEGER
  10.     c AS _UNSIGNED LONG
  11.  
  12. DIM SHARED o(130) AS object
  13.  
  14. 'initialize array
  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).size = 10
  21.     o(i).c = _RGB32(RND * 255, RND * 255, RND * 255, 100)
  22.  
  23. DIM SHARED minDistance AS INTEGER
  24. minDistance = 50
  25.  
  26.     mx = _MOUSEX
  27.  
  28.     minDistance = map(mx, 0, _WIDTH, 0, 200)
  29.  
  30.     CLS
  31.  
  32.     LINE (0, _HEIGHT - 20)-(mx, _HEIGHT), _RGB32(255, 50), BF
  33.  
  34.     FOR i = 1 TO UBOUND(o)
  35.         move o(i)
  36.         connect o(i)
  37.         CircleFill o(i).x, o(i).y, o(i).size, o(i).c
  38.     NEXT
  39.  
  40.     _DISPLAY
  41.     _LIMIT 60
  42.  
  43. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  44.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  45.  
  46. SUB connect (this AS object)
  47.     DIM j AS INTEGER
  48.     FOR j = 1 TO UBOUND(o)
  49.         IF dist(o(j), this) <= minDistance THEN
  50.             LINE (this.x, this.y)-(o(j).x, o(j).y), this.c
  51.         END IF
  52.     NEXT
  53.  
  54. FUNCTION dist! (o1 AS object, o2 AS object)
  55.     x1! = o1.x
  56.     y1! = o1.y
  57.     x2! = o2.x
  58.     y2! = o2.y
  59.     dist! = _HYPOT((x2! - x1!), (y2! - y1!))
  60.  
  61. SUB move (this AS object)
  62.     this.x = this.x + this.xv
  63.     IF this.x < 0 OR this.x > _WIDTH THEN this.xv = this.xv * -1
  64.  
  65.     this.y = this.y + this.yv
  66.     IF this.y < 0 OR this.y > _HEIGHT THEN this.yv = this.yv * -1
  67.  
  68.  
  69. SUB CircleFill (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  70.     ' CX = center x coordinate
  71.     ' CY = center y coordinate
  72.     '  R = radius
  73.     '  C = fill color
  74.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  75.     DIM X AS INTEGER, Y AS INTEGER
  76.     Radius = ABS(R)
  77.     RadiusError = -Radius
  78.     X = Radius
  79.     Y = 0
  80.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  81.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  82.     WHILE X > Y
  83.         RadiusError = RadiusError + Y * 2 + 1
  84.         IF RadiusError >= 0 THEN
  85.             IF X <> Y + 1 THEN
  86.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  87.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  88.             END IF
  89.             X = X - 1
  90.             RadiusError = RadiusError - X * 2
  91.         END IF
  92.         Y = Y + 1
  93.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  94.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  95.     WEND
  96.  

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Molecules simulation (simple particle system, part II)
« Reply #1 on: July 24, 2020, 04:58:20 am »
See, what I found :D . It does something similar to your code. -
Code: QB64: [Select]
  1. 'Coded by Ashish in Qb64
  2. 'inspire from Shiffman's video.
  3. TYPE vector
  4.     x AS _FLOAT
  5.     y AS _FLOAT
  6.     z AS _FLOAT
  7.  
  8. TYPE particle
  9.     pos AS vector
  10.     rr AS _FLOAT
  11.     i AS INTEGER
  12.     j AS INTEGER
  13.     r AS INTEGER
  14.     g AS INTEGER
  15.     b AS INTEGER
  16. _TITLE "Processing Network"
  17. SCREEN _NEWIMAGE(800, 500, 32)
  18.  
  19. CONST speed = .05
  20. DIM SHARED Particles(100) AS particle
  21. DIM SHARED glAllow AS _BYTE
  22. FOR i = 0 TO UBOUND(Particles)
  23.     Particles(i).pos.x = RND * _WIDTH
  24.     Particles(i).pos.y = RND * _HEIGHT
  25.     Particles(i).pos.z = RND * 100
  26.     Particles(i).j = RND * 7
  27.     Particles(i).i = 5
  28.     IF RND * 2 > 1 THEN Particles(i).i = -Particles(i).i
  29.     Particles(i).rr = RND * 3
  30.     SELECT CASE Particles(i).j
  31.         CASE 0: Particles(i).r = 5: Particles(i).g = 205: Particles(i).b = 229
  32.         CASE 1: Particles(i).r = 255: Particles(i).g = 184: Particles(i).b = 3
  33.         CASE 2: Particles(i).r = 91: Particles(i).g = 3: Particles(i).b = 255
  34.         CASE 3: Particles(i).r = 61: Particles(i).g = 62: Particles(i).b = 62
  35.         CASE 4: Particles(i).r = 255: Particles(i).g = 0: Particles(i).b = 255
  36.         CASE 5: Particles(i).r = 255: Particles(i).g = 0: Particles(i).b = 0
  37.         CASE 6: Particles(i).r = 0: Particles(i).g = 255: Particles(i).b = 0
  38.         CASE 7: Particles(i).r = 0: Particles(i).g = 0: Particles(i).b = 255
  39.     END SELECT
  40.     IF RND * 2 > 1 THEN Particles(i).j = -Particles(i).j
  41.  
  42. glAllow = -1
  43.     CLS , _RGB(255, 255, 255)
  44.     FOR i = 0 TO UBOUND(particles)
  45.         CIRCLE (Particles(i).pos.x, Particles(i).pos.y), Particles(i).rr, _RGB(Particles(i).r, Particles(i).g, Particles(i).b)
  46.         PAINT (Particles(i).pos.x, Particles(i).pos.y), _RGB(Particles(i).r, Particles(i).g, Particles(i).b), _RGB(Particles(i).r, Particles(i).g, Particles(i).b)
  47.         Particles(i).pos.x = Particles(i).pos.x + Particles(i).j * speed
  48.         Particles(i).pos.y = Particles(i).pos.y + Particles(i).i * speed
  49.         IF Particles(i).pos.y > _HEIGHT - Particles(i).rr THEN Particles(i).i = -Particles(i).i
  50.         IF Particles(i).pos.y < 0 + Particles(i).rr THEN Particles(i).i = -Particles(i).i
  51.         IF Particles(i).pos.x > _WIDTH - Particles(i).rr THEN Particles(i).j = -Particles(i).j
  52.         IF Particles(i).pos.x < 0 + Particles(i).rr THEN Particles(i).j = -Particles(i).j
  53.     NEXT
  54.     '   _LIMIT 60
  55.     _DISPLAY
  56.  
  57.  
  58.     IF NOT glAllow THEN EXIT SUB
  59.     '  _glClearColor 1, 1, 1, 1
  60.     ' _glClear _GL_COLOR_BUFFER_BIT OR _GL_DEPTH_BUFFER_BIT
  61.  
  62.     _glEnable _GL_BLEND
  63.     '   _glColor4f 1, 1, 1, .05
  64.     FOR i = 0 TO UBOUND(particles)
  65.         FOR j = i + 1 TO UBOUND(particles)
  66.             IF distance(Particles(i).pos.x, Particles(i).pos.y, Particles(j).pos.x, Particles(j).pos.y) < 80 THEN
  67.                 FOR n = j + 1 TO UBOUND(particles)
  68.                     IF distance(Particles(j).pos.x, Particles(j).pos.y, Particles(n).pos.x, Particles(n).pos.y) < 80 THEN
  69.                         _glColor4f Particles(i).r / 255, Particles(i).g / 255, Particles(i).b / 255, .1
  70.                         _glBegin _GL_TRIANGLES
  71.                         _glVertex3f toGlX(Particles(i).pos.x), toGlY(Particles(i).pos.y), 0
  72.                         _glVertex3f toGlX(Particles(j).pos.x), toGlY(Particles(j).pos.y), 0
  73.                         _glVertex3f toGlX(Particles(n).pos.x), toGlY(Particles(n).pos.y), 0
  74.                         _glEnd
  75.                     END IF
  76.                 NEXT
  77.             END IF
  78.         NEXT
  79.     NEXT
  80.     _glFlush
  81.  
  82. FUNCTION toGlX## (x#)
  83.     x# = (_WIDTH / 2) - x#
  84.     toGlX## = map(x#, -(_WIDTH / 2), _WIDTH / 2, 1, -1)
  85.  
  86. FUNCTION toGlY## (y#)
  87.     y# = (_HEIGHT / 2) - y#
  88.     toGlY## = map(y#, -(_HEIGHT / 2), _HEIGHT / 2, -1, 1)
  89. FUNCTION toGlZ## (z#)
  90.     toGlZ## = map(z#, 0, 100, 0, 1)
  91.  
  92. FUNCTION map## (value##, minRange##, maxRange##, newMinRange##, newMaxRange##)
  93.     map## = ((value## - minRange##) / (maxRange## - minRange##)) * (newMaxRange## - newMinRange##) + newMinRange##
  94.  
  95. FUNCTION distance## (x1##, y1##, x2##, y2##)
  96.     IF x1## > x2## THEN dx## = x1## - x2## ELSE dx## = x2## - x1##
  97.     IF y1## > y2## THEN dy## = y1## - y2## ELSE dy## = y2## - y1##
  98.     distance## = SQR(dx## * dx## + dy## * dy##)
  99.  
  100.  
  101.  
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

FellippeHeitor

  • Guest
Re: Molecules simulation (simple particle system, part II)
« Reply #2 on: July 24, 2020, 05:34:47 am »
Aha! That's why this was at the back of my mind! We'd written it before 😂

Thanks for sharing!

Marked as best answer by on October 06, 2024, 08:14:50 am

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
@FellippeHeitor Ah so that's that map function you are always using, very clear now that I've seen it applied. :)

Also liked the mouse bar at bottom of screen.

Next mod up is translucent triangular planes between 3 points.

FellippeHeitor

  • Guest
Re: Molecules simulation (simple particle system, part II)
« Reply #4 on: July 24, 2020, 12:10:54 pm »
@bplus that's the one!

Looking forward to your mod!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile