Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How about some vector noodles?
#1
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
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply


Messages In This Thread
How about some vector noodles? - by TerryRitchie - 05-13-2024, 03:30 PM
RE: How about some vector noodles? - by bplus - 05-13-2024, 04:43 PM
RE: How about some vector noodles? - by Sprezzo - 05-13-2024, 05:42 PM
RE: How about some vector noodles? - by GareBear - 05-13-2024, 08:17 PM
RE: How about some vector noodles? - by bplus - 05-13-2024, 11:33 PM
RE: How about some vector noodles? - by NakedApe - 05-14-2024, 01:15 AM
RE: How about some vector noodles? - by Sprezzo - 05-14-2024, 04:11 PM
RE: How about some vector noodles? - by bplus - 05-14-2024, 04:26 PM
RE: How about some vector noodles? - by Sprezzo - 05-14-2024, 04:28 PM
RE: How about some vector noodles? - by bplus - 05-14-2024, 04:40 PM
RE: How about some vector noodles? - by Sprezzo - 05-14-2024, 06:44 PM
RE: How about some vector noodles? - by Dav - 09-17-2024, 11:06 AM
RE: How about some vector noodles? - by bplus - 09-17-2024, 03:01 PM
RE: How about some vector noodles? - by bplus - 09-18-2024, 08:07 PM
RE: How about some vector noodles? - by Dav - 09-18-2024, 10:37 PM
RE: How about some vector noodles? - by bplus - 09-18-2024, 10:51 PM
RE: How about some vector noodles? - by sbblank - 09-18-2024, 11:13 PM



Users browsing this thread: 1 Guest(s)