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
#2
+1 this is almost like boids with so little code, very interesting thanks
b = b + ...
Reply
#3
(05-13-2024, 04:43 PM)bplus Wrote: +1 this is almost like boids with so little code, very interesting thanks
I'm playing around with more example snippets for the tutorial and stumbled on this. After I caught myself toying with it mindlessly for a few minutes I thought others might like to do the same. Smile
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#4
-----
Reply
#5
(05-13-2024, 05:42 PM)Sprezzo Wrote: Hey Terry,

Looks great, however I need to fix something I noticed years ago in your terminology.

You're mixing "vector" with "velocity". Reduced example being:

Code: (Select All)
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
In the above, the information (x,y) is the position vector, and (xv, yv) is the velocity vector. Both are ordered pairs in the plane, both are vectors.

So that's point #1, point #2 has to to with units. For instance:

Code: (Select All)
        Particle(p).x = Particle(p).x + Particle(p).xv
        Particle(p).y = Particle(p).y + Particle(p).yv
Here, it looks like you're adding the velocity vector to the position vector. This is technically a mistake, because you're adding distance units to distance/time units. I can't add 7 miles to 3.5 miles per hour, there's a unit mismatch. To make that read more properly, you want:

Code: (Select All)
        Particle(p).x = Particle(p).x + Particle(p).xv * TIMESTEP
        Particle(p).y = Particle(p).y + Particle(p).yv * TIMESTEP

Then, let velocity be a reasonable number, and then let TIMESTEP be a very small increment in time. Right now you've got an implied timestep of 1 and tiny velocity per particle. No problem for this demo, but in general doing it that way can be... not really representative of motion at worst.

Aaaaanyway, take it all or leave it all. Your qb64 tutorial is already the best prose on QB64 ever written, I just wanna see it that much closer to perfect.
@Sprezzo

I'm completely self taught when it comes to math and I'm sure, as you pointed out, my terminology is suspect. I really do appreciate when people correct me on these matters. Since you are better versed on this could you do me a huge favor?

- Correct the above code to how it should look/perform.
- When you find time look over Lesson 17 and let me know what I need to change in there.
- Write me a quick tutorial on the proper use and terminology of vector math.

I'm constantly getting terminology mixed up and a tutorial on the subject I can refer back to would be a huge benefit and help me to correct Lesson 17 issues where they occur. My goal is to make the tutorial 100% accurate but I'm lacking in certain areas and help from others is greatly appreciated.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#6
(05-13-2024, 03:30 PM)TerryRitchie Wrote: 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
This is like Lay's chips. Hard to stop.
Reply
#7
ha sprezzo made me look Smile

Code: (Select All)
TYPE PARTICLE '            PARTICLE PROPERTIES
    x AS SINGLE '          y location
    y AS SINGLE '          x location
    'xv AS SINGLE '         y vector ????
    dx as single    '       the change over the x-axis of particle position
    'yv AS SINGLE '         x vector ????
    dy as single '           the change over the y-axis of the particle position        
END TYPE
dx, dy are vector components sped up or slowed down by so called mass, just a scalar multiplier.
b = b + ...
Reply
#8
Very cool, Terry. Fun to play with.
Reply
#9
-----
Reply
#10
imho 'vector' is an over complication of the location of a simple point on a plane, not really basic is it?

@TerryRitchie why are you swapping x, y in this definition comments?, it's not how you use it in your code. are those typos in the comments?
Code: (Select All)
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
b = b + ...
Reply




Users browsing this thread: 9 Guest(s)