05-13-2024, 03:30 PM
Just noodling around today with vectors. Came up with this little interesting toy.
Code: (Select All)
'Vector noodling
' Move mouse to attract particles
' Press left mouse button to repel particles
CONST PARTICLES = 250000 ' maximum number of particles
CONST SWIDTH = 960 ' screen width
CONST SHEIGHT = 540 ' screen height
CONST MASS = 25 ' mass of particles
TYPE PARTICLE ' PARTICLE PROPERTIES
x AS SINGLE ' y location
y AS SINGLE ' x location
xv AS SINGLE ' y vector
yv AS SINGLE ' x vector
END TYPE
DIM Particle(PARTICLES - 1) AS PARTICLE ' create particles
DIM p AS LONG ' particle counter
DIM d AS SINGLE ' distance between particle and mouse
DIM xv AS SINGLE ' x vector from particle to mouse
DIM yv AS SINGLE ' y vector from particle to mouse
DIM Circ AS LONG ' picture of a solid circle
Circ = _NEWIMAGE(11, 11, 32) ' picture canvas
_DEST Circ ' craw circle
CIRCLE (5, 5), 5
PAINT (5, 5)
RANDOMIZE TIMER
DO ' randomize particles
Particle(p).x = RND * SWIDTH
Particle(p).y = RND * SHEIGHT
Particle(p).xv = RND - RND
Particle(p).yv = RND - RND
p = p + 1
LOOP UNTIL p = PARTICLES
SCREEN _NEWIMAGE(SWIDTH, SHEIGHT, 32) ' start the madness
_MOUSEHIDE
DO
CLS
_LIMIT 120
p = 0
DO
PSET (Particle(p).x, Particle(p).y) ' draw particle
WHILE _MOUSEINPUT: WEND
d = _HYPOT(_MOUSEX - Particle(p).x, _MOUSEY - Particle(p).y) ' distance to mouse
xv = (_MOUSEX - Particle(p).x) / d ' x vector to mouse
yv = (_MOUSEY - Particle(p).y) / d ' y vector to mouse
IF _MOUSEBUTTON(1) THEN ' left mouse button?
Particle(p).xv = (Particle(p).xv - xv) / d * MASS ' yes, repel
Particle(p).yv = (Particle(p).yv - yv) / d * MASS
ELSE ' no
Particle(p).xv = (Particle(p).xv + xv) / d * MASS ' attract
Particle(p).yv = (Particle(p).yv + yv) / d * MASS
END IF
Particle(p).x = Particle(p).x + Particle(p).xv ' add vector velocity to x
Particle(p).y = Particle(p).y + Particle(p).yv ' add vector velocity to y
IF Particle(p).x < 0 THEN ' keep particles on screen
Particle(p).x = 0
ELSEIF Particle(p).x > SWIDTH - 1 THEN
Particle(p).x = SWIDTH - 1
END IF
IF Particle(p).y < 0 THEN
Particle(p).y = 0
ELSEIF Particle(p).y > SHEIGHT - 1 THEN
Particle(p).y = SHEIGHT - 1
END IF
p = p + 1
LOOP UNTIL p = PARTICLES
_PUTIMAGE (_MOUSEX - 5, _MOUSEY - 5), Circ
_DISPLAY
LOOP UNTIL _KEYDOWN(27)
SYSTEM