Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How about some vector noodles?
#11
-----
Reply
#12
gots to remember target audience for tutorial is not for physics phd wanna-be's.
b = b + ...
Reply
#13
-----
Reply
#14
(05-14-2024, 04:26 PM)bplus Wrote: 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
LOL, yeah, typos. Good catch, I didn't even notice that. The older I get the more my brain fills in the missing stuff for me. It's kinda like AI I suppose ... hallucinations.
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply
#15
Just now came across this little gem.  Nice snippet, Terry!

Did you make any updates to this one?

- Dav

Find my programs here in Dav's QB64 Corner
Reply
#16
All waaay above my head, but playing with this, I could see an interesting game possibly along the lines of "Clear the cobwebS", or something more catchy. Fun effect!
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#17
Here is what a vector is:
Quote:What is the definition of a vector?
: a quantity that has magnitude and direction and that is commonly represented by a directed line segment whose length represents the magnitude and whose orientation in space represents the direction. broadly : an element of a vector space. b. : a course or compass direction especially of an airplane.

Quote: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

using 2 magnitutes xv, yv is not a vector but 2 components from which you can derive the vector magnitude and direction.

Imagine a vector as an arrow on the screen or graph, the length of arrow represents the magnitude and the direction from base to arrow head is the angle or heading.

So there are 2 points base location and head location (x1, y1) and (x2, y2)
dx = (x2 - x1) : dy = (y2 - y1) are the vector COMPONENTS
the magnitude of arrow or vector = SQR(dx^2 + dy^2) just the distance or length of the arrow, good old Pythagorean Theorem here.
the angle or heading of arrow or vector = _Atan2(dy, dx) (so sorry for the Advanced Trig but easiest way to get there in Basic).

Yes point locations can be considered vectors from the origin of a graph (x, y) can be polarized (ha my term meaning converted to Polar coodinates x = dx, y = dy and using the above formula for magnitude and angle.

(10, 10) converts to SQR(200) magnitude and 45 degrees heading which on the Basic Screen is down and to the right from (0,0) the top left of screen. Remember on Basic Screens 0 degrees is due East and the y axis INCREASE going down the screen which is NOT how you learn it in math and physics.

In summary a vector is an arrow Smile different from a line segment because it has a direction it is going.

Terry is dividing by d the magnitude and skipping the angle calcs so xv and yv can be considered velocities eg, how fast and pos or negative direction on x and y axis on each loop. An arrow going straight up will not change in x axis but will decrease on y axis only, xv = 0 yv = -whatever
b = b + ...
Reply
#18
(09-17-2024, 11:06 AM)Dav Wrote: Just now came across this little gem.  Nice snippet, Terry!

Did you make any updates to this one?

- Dav
No, no updates. One of the many things I've played with and moved on. Smile
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply
#19
I did this yesterday, thought better of it and didn't post. Now I think worse of it Big Grin Big Grin Big Grin

Code: (Select All)
_Title "bplus overhaul Vector noodling of Terry Ritchie" ' b+ 2nd x 2024-09-18

' 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

Type PARTICLE '            PARTICLE PROPERTIES
    x As Single '          y location
    y As Single '          x location
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 dx As Single '                        difference in x values
Dim dy As Single '                        difference in y values
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
    p = p + 1
Loop Until p = PARTICLES

Screen _NewImage(SWIDTH, SHEIGHT, 32) '   start the madness
_ScreenMove 250, 70
_MouseHide
Color &HFF000000, &HFFFFFFFF
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
        dx = (_MouseX - Particle(p).x)
        dy = (_MouseY - Particle(p).y)
        If _MouseButton(1) Then ' '  (Rnd - Rnd) * 5 this keeps everything moving                                        left mouse button?
            Particle(p).x = Particle(p).x - dx / d + (Rnd - Rnd) * 5 '/d so farther away the slower the effect
            Particle(p).y = Particle(p).y - dy / d + (Rnd - Rnd) * 5
        Else '                                                         no button
            Particle(p).x = Particle(p).x + dx / d + (Rnd - Rnd) * 5 '/d so farther away the slower the effect
            Particle(p).y = Particle(p).y + dy / d + (Rnd - Rnd) * 5
        End If
        p = p + 1
    Loop Until p = PARTICLES
    _PutImage (_MouseX - 5, _MouseY - 5), Circ
    _Display
Loop Until _KeyDown(27)
System

Let us be saved from vectors!
b = b + ...
Reply
#20
That's neat too, bplus.  I was playing around with your mod some.  You know I've been messing with bouncing and velocity lately, so couldn't resist using it.  Here's a swarm.   Mouse still controls.

Really surprised so many particles can be handled smoothly. 

- Dav

Code: (Select All)
_Title "Dav's overhaul of bplus' overhaul Vector noodling of Terry Ritchie"

' Move mouse to attract particles
' Press left mouse button to repel particles


Const PARTICLES = 250000
Const SWIDTH = 960
Const SHEIGHT = 540

Type PARTICLE
    x As Single
    y As Single
    vx As Single
    vy As Single
End Type

Dim Particle(PARTICLES - 1) As PARTICLE
Dim p As Long
Dim dx As Single
Dim dy As Single
Dim Circ As Long

Circ = _NewImage(11, 11, 32)
_Dest Circ
Circle (5, 5), 5
Paint (5, 5)

Randomize Timer
Do
    Particle(p).x = Rnd * SWIDTH
    Particle(p).y = Rnd * SHEIGHT
    Particle(p).vx = Rnd * 2 - 1 'velocity
    Particle(p).vy = Rnd * 2 - 1
    p = p + 1
Loop Until p = PARTICLES

Screen _NewImage(SWIDTH, SHEIGHT, 32)
_ScreenMove 250, 70
_MouseHide
Color &HFF000000, &HFFFFFFFF
Do
    Cls
    _Limit 120
    p = 0
    Do
        PSet (Particle(p).x, Particle(p).y)
        While _MouseInput: Wend
        dx = _MouseX - Particle(p).x
        dy = _MouseY - Particle(p).y
        d = _Hypot(dx, dy)
        If d > 1 Then
            If _MouseButton(1) Then
                Particle(p).vx = Particle(p).vx - (dx / d) * .5 'scale down velocity (.5)
                Particle(p).vy = Particle(p).vy - (dy / d) * .5 '
            Else
                Particle(p).vx = Particle(p).vx + (dx / d) * .5
                Particle(p).vy = Particle(p).vy + (dy / d) * .5
            End If
        End If
        Particle(p).vx = Particle(p).vx * .99 'damper factor (.99)
        Particle(p).vy = Particle(p).vy * .99
        Particle(p).x = Particle(p).x + Particle(p).vx
        Particle(p).y = Particle(p).y + Particle(p).vy
        If Particle(p).x < 0 Or Particle(p).x >= SWIDTH Then Particle(p).vx = -Particle(p).vx
        If Particle(p).y < 0 Or Particle(p).y >= SHEIGHT Then Particle(p).vy = -Particle(p).vy
        p = p + 1
    Loop Until p = PARTICLES
    _PutImage (_MouseX - 5, _MouseY - 5), Circ
    _Display
Loop Until _KeyDown(27)
System

Find my programs here in Dav's QB64 Corner
Reply




Users browsing this thread: 12 Guest(s)