Author Topic: Mouse school of critters  (Read 4031 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Mouse school of critters
« on: April 28, 2018, 10:00:58 pm »
Practice code for Boid Watching

Code: QB64: [Select]
  1. _TITLE "Mouse school of critters - Click to toggle Mouse as Predator or Prey    by bplus 2018-04-27"
  2. 'QB64 version 2017 1106/82 (the day before they switched to version 1.2)
  3. 'from: Mouse school critters separated.txt for JB 2.0 B+ 2018-04-24
  4. '2018-04-27 update for Predator / Prey Toggle with Click
  5.  
  6. CONST xmax = 1200
  7. CONST ymax = 700
  8. SCREEN _NEWIMAGE(xmax, ymax, 32)
  9. _SCREENMOVE 100, 40
  10. DIM SHARED qb(15)
  11. qb(0) = &HFF000000
  12. qb(1) = &HFF000088
  13. qb(2) = &HFF008800
  14. qb(3) = &HFF008888
  15. qb(4) = &HFF880000
  16. qb(5) = &HFF880088
  17. qb(6) = &HFF888800
  18. qb(7) = &HFFCCCCCC
  19. qb(8) = &HFF888888
  20. qb(9) = &HFF0000FF
  21. qb(10) = &HFF00FF00
  22. qb(11) = &HFF00FFFF
  23. qb(12) = &HFFFF0000
  24. qb(13) = &HFFFF00FF
  25. qb(14) = &HFFFFFF00
  26. qb(15) = &HFFFFFFFF
  27.  
  28. na = 50
  29. DIM SHARED x(na), y(na), v(na), r(na), c(na), predator
  30. FOR i = 1 TO na
  31.     x(i) = rand(0, xmax)
  32.     y(i) = rand(0, ymax)
  33.     rr = INT(RND * 15)
  34.     v(i) = rr * 1
  35.     r(i) = rand(10, 30)
  36.     c(i) = qb(rr)
  37.  
  38.     CLS
  39.     IF INKEY$ = "q" THEN END
  40.     FOR i = 1 TO na
  41.         m = _MOUSEINPUT: mb = _MOUSEBUTTON(1): mx = _MOUSEX: my = _MOUSEY
  42.         IF mb THEN
  43.             WHILE mb
  44.                 m = _MOUSEINPUT: mb = _MOUSEBUTTON(1): mx = _MOUSEX: my = _MOUSEY
  45.                 _LIMIT 200
  46.             WEND
  47.             IF predator THEN predator = 0 ELSE predator = 1
  48.         END IF
  49.  
  50.         'radian angle to mouse
  51.         ra = _ATAN2(my - y(i), mx - x(i)) '  + pi kind of interesting too
  52.         'draw it
  53.         critter i, ra
  54.  
  55.         'separate critters for next frame and further down i line
  56.         FOR j = i + 1 TO na
  57.  
  58.             ' The following is STATIC's adjustment of ball positions if overlapping
  59.             ' before calcultion of new positions from collision
  60.             ' Displacement vector and its magnitude.  Thanks STxAxTIC !
  61.             nx = x(j) - x(i)
  62.             ny = y(j) - y(i)
  63.             nm = SQR(nx ^ 2 + ny ^ 2)
  64.             IF nm < 10 + r(i) + r(j) THEN
  65.                 nx = nx / nm
  66.                 ny = ny / nm
  67.  
  68.                 ' Regardless of momentum exchange, separate the balls along the lone connecting them.
  69.                 WHILE nm < 10 + r(i) + r(j)
  70.  
  71.                     flub = 10 '  massively increased for JB to speed up code
  72.  
  73.                     x(j) = x(j) + flub * nx
  74.                     y(j) = y(j) + flub * ny
  75.  
  76.                     x(i) = x(i) - flub * nx
  77.                     y(i) = y(i) - flub * ny
  78.  
  79.                     nx = x(j) - x(i)
  80.                     ny = y(j) - y(i)
  81.                     nm = SQR(nx ^ 2 + ny ^ 2)
  82.                     nx = nx / nm
  83.                     ny = ny / nm
  84.                 WEND
  85.             END IF
  86.         NEXT
  87.         IF predator THEN
  88.             x(i) = x(i) + v(i) * COS(ra + _PI)
  89.             y(i) = y(i) + v(i) * SIN(ra + _PI)
  90.         ELSE
  91.             x(i) = x(i) + v(i) * COS(ra)
  92.             y(i) = y(i) + v(i) * SIN(ra)
  93.         END IF
  94.     NEXT
  95.     _DISPLAY
  96.     _LIMIT 20
  97.  
  98. SUB critter (i, ra)
  99.     COLOR c(i)
  100.     fcirc x(i), y(i), r(i)
  101.     IF predator THEN
  102.         x1 = x(i) + .75 * r(i) * COS(ra - _PI(1 / 9) + _PI)
  103.         y1 = y(i) + .75 * r(i) * SIN(ra - _PI(1 / 9) + _PI)
  104.         x2 = x(i) + .75 * r(i) * COS(ra + _PI(1 / 9) + _PI)
  105.         y2 = y(i) + .75 * r(i) * SIN(ra + _PI(1 / 9) + _PI)
  106.     ELSE
  107.         x1 = x(i) + .75 * r(i) * COS(ra - _PI(1 / 9))
  108.         y1 = y(i) + .75 * r(i) * SIN(ra - _PI(1 / 9))
  109.         x2 = x(i) + .75 * r(i) * COS(ra + _PI(1 / 9))
  110.         y2 = y(i) + .75 * r(i) * SIN(ra + _PI(1 / 9))
  111.     END IF
  112.     COLOR qb(15)
  113.     fcirc x1, y1, .25 * r(i)
  114.     fcirc x2, y2, .25 * r(i)
  115.     IF predator THEN
  116.         x3 = x1 + .125 * r(i) * COS(ra + _PI)
  117.         y3 = y1 + .125 * r(i) * SIN(ra + _PI)
  118.         x4 = x2 + .125 * r(i) * COS(ra + _PI)
  119.         y4 = y2 + .125 * r(i) * SIN(ra + _PI)
  120.     ELSE
  121.         x3 = x1 + .125 * r(i) * COS(ra)
  122.         y3 = y1 + .125 * r(i) * SIN(ra)
  123.         x4 = x2 + .125 * r(i) * COS(ra)
  124.         y4 = y2 + .125 * r(i) * SIN(ra)
  125.     END IF
  126.     COLOR qb(0)
  127.     fcirc x3, y3, .125 * r(i)
  128.     fcirc x4, y4, .125 * r(i)
  129.  
  130. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  131. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  132.     DIM subRadius AS LONG, RadiusError AS LONG
  133.     DIM X AS LONG, Y AS LONG
  134.  
  135.     subRadius = ABS(R)
  136.     RadiusError = -subRadius
  137.     X = subRadius
  138.     Y = 0
  139.  
  140.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  141.  
  142.     ' Draw the middle span here so we don't draw it twice in the main loop,
  143.     ' which would be a problem with blending turned on.
  144.     LINE (CX - X, CY)-(CX + X, CY), , BF
  145.  
  146.     WHILE X > Y
  147.         RadiusError = RadiusError + Y * 2 + 1
  148.         IF RadiusError >= 0 THEN
  149.             IF X <> Y + 1 THEN
  150.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  151.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  152.             END IF
  153.             X = X - 1
  154.             RadiusError = RadiusError - X * 2
  155.         END IF
  156.         Y = Y + 1
  157.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  158.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  159.     WEND
  160.  
  161. FUNCTION rand% (lo%, hi%)
  162.     rand% = INT(RND * (hi% - lo% + 1)) + lo%
  163.  
  164.