_TITLE "Mouse school of critters - Click to toggle Mouse as Predator or Prey by bplus 2018-04-27" 'QB64 version 2017 1106/82 (the day before they switched to version 1.2)
'from: Mouse school critters separated.txt for JB 2.0 B+ 2018-04-24
'2018-04-27 update for Predator / Prey Toggle with Click
qb(0) = &HFF000000
qb(1) = &HFF000088
qb(2) = &HFF008800
qb(3) = &HFF008888
qb(4) = &HFF880000
qb(5) = &HFF880088
qb(6) = &HFF888800
qb(7) = &HFFCCCCCC
qb(8) = &HFF888888
qb(9) = &HFF0000FF
qb(10) = &HFF00FF00
qb(11) = &HFF00FFFF
qb(12) = &HFFFF0000
qb(13) = &HFFFF00FF
qb(14) = &HFFFFFF00
qb(15) = &HFFFFFFFF
na = 50
DIM SHARED x
(na
), y
(na
), v
(na
), r
(na
), c
(na
), predator
x(i) = rand(0, xmax)
y(i) = rand(0, ymax)
v(i) = rr * 1
r(i) = rand(10, 30)
c(i) = qb(rr)
'radian angle to mouse
ra
= _ATAN2(my
- y
(i
), mx
- x
(i
)) ' + pi kind of interesting too 'draw it
critter i, ra
'separate critters for next frame and further down i line
' The following is STATIC's adjustment of ball positions if overlapping
' before calcultion of new positions from collision
' Displacement vector and its magnitude. Thanks STxAxTIC !
nx = x(j) - x(i)
ny = y(j) - y(i)
nm
= SQR(nx
^ 2 + ny
^ 2) nx = nx / nm
ny = ny / nm
' Regardless of momentum exchange, separate the balls along the lone connecting them.
WHILE nm
< 10 + r
(i
) + r
(j
)
flub = 10 ' massively increased for JB to speed up code
x(j) = x(j) + flub * nx
y(j) = y(j) + flub * ny
x(i) = x(i) - flub * nx
y(i) = y(i) - flub * ny
nx = x(j) - x(i)
ny = y(j) - y(i)
nm
= SQR(nx
^ 2 + ny
^ 2) nx = nx / nm
ny = ny / nm
x
(i
) = x
(i
) + v
(i
) * COS(ra
+ _PI) y
(i
) = y
(i
) + v
(i
) * SIN(ra
+ _PI) x
(i
) = x
(i
) + v
(i
) * COS(ra
) y
(i
) = y
(i
) + v
(i
) * SIN(ra
)
fcirc x(i), y(i), r(i)
x1
= x
(i
) + .75 * r
(i
) * COS(ra
- _PI(1 / 9) + _PI) y1
= y
(i
) + .75 * r
(i
) * SIN(ra
- _PI(1 / 9) + _PI) x2
= x
(i
) + .75 * r
(i
) * COS(ra
+ _PI(1 / 9) + _PI) y2
= y
(i
) + .75 * r
(i
) * SIN(ra
+ _PI(1 / 9) + _PI) x1
= x
(i
) + .75 * r
(i
) * COS(ra
- _PI(1 / 9)) y1
= y
(i
) + .75 * r
(i
) * SIN(ra
- _PI(1 / 9)) x2
= x
(i
) + .75 * r
(i
) * COS(ra
+ _PI(1 / 9)) y2
= y
(i
) + .75 * r
(i
) * SIN(ra
+ _PI(1 / 9)) fcirc x1, y1, .25 * r(i)
fcirc x2, y2, .25 * r(i)
x3
= x1
+ .125 * r
(i
) * COS(ra
+ _PI) y3
= y1
+ .125 * r
(i
) * SIN(ra
+ _PI) x4
= x2
+ .125 * r
(i
) * COS(ra
+ _PI) y4
= y2
+ .125 * r
(i
) * SIN(ra
+ _PI) x3
= x1
+ .125 * r
(i
) * COS(ra
) y3
= y1
+ .125 * r
(i
) * SIN(ra
) x4
= x2
+ .125 * r
(i
) * COS(ra
) y4
= y2
+ .125 * r
(i
) * SIN(ra
) fcirc x3, y3, .125 * r(i)
fcirc x4, y4, .125 * r(i)
'Steve McNeil's copied from his forum note: Radius is too common a name
RadiusError = -subRadius
X = subRadius
Y = 0
' Draw the middle span here so we don't draw it twice in the main loop,
' which would be a problem with blending turned on.
LINE (CX
- X
, CY
)-(CX
+ X
, CY
), , BF
RadiusError = RadiusError + Y * 2 + 1
LINE (CX
- Y
, CY
- X
)-(CX
+ Y
, CY
- X
), , BF
LINE (CX
- Y
, CY
+ X
)-(CX
+ Y
, CY
+ X
), , BF
X = X - 1
RadiusError = RadiusError - X * 2
Y = Y + 1
LINE (CX
- X
, CY
- Y
)-(CX
+ X
, CY
- Y
), , BF
LINE (CX
- X
, CY
+ Y
)-(CX
+ X
, CY
+ Y
), , BF
rand%
= INT(RND * (hi%
- lo%
+ 1)) + lo%