You are not logged in or registered. Please login or register to use the full functionality of this board...
SIGN IN Join Our Community For FREE


Magnetic Repulsion Demo
07-06-2014, 10:48 AM (This post was last modified: 07-20-2014 11:51 PM by Waltersmind.)
Post: #1
 (Print Post)
Magnetic Repulsion Demo
Code Snippet: [Select]
TYPE BallInfo

    x AS INTEGER
    y AS INTEGER
    charge AS INTEGER
    xmomentium AS SINGLE
    ymomentium AS SINGLE
    size AS INTEGER
END TYPE

SCREEN _NEWIMAGE(800, 600, 32)
_FULLSCREEN
_SCREENMOVE _MIDDLE
CLS , _RGB32(0, 0, 0)
_DEFINE A-Z AS _FLOAT
RANDOMIZE TIMER

BallCount = 1000
DIM Ball(BallCount) AS BallInfo
DIM TempBall(BallCount) AS BallInfo
CONST MagneticRange = 40
CONST ClosedTable = -1
CONST FPS = 3
CONST Velocity = 50 'percent.  This is our rate of decay on our momentium.  100 would be no decay to movement, 0 would be no additional movement.
CONST size = 0 'The size of our balls, 0 to let the intensity size them for us

FOR i = 1 TO BallCount
    Ball(i).x = INT(RND(1) * 800)
    Ball(i).y = INT(RND(1) * 600)
    Ball(i).charge = INT(RND(1) * MagneticRange - MagneticRange / 2)
    Ball(i).xmomentium = 0
    Ball(i).ymomentium = 0
    IF size = 0 THEN
        Ball(i).size = 1 + 10 * Ball(i).charge / MagneticRange
    ELSE
        Ball(i).size = size
    END IF
NEXT

DO
    _LIMIT FPS
    '    SLEEP
    CLS
    a$ = INKEY$
    IF a$ <> "" THEN
        SELECT CASE UCASE$(a$)
            CASE "R": RUN
            CASE ELSE: SYSTEM
        END SELECT
    END IF


    FOR i = 1 TO BallCount
        IF Ball(i).charge > 0 THEN
            clr& = _RGB32(100 + Ball(i).charge * (100 / MagneticRange), 0, 0)
        ELSEIF Ball(i).charge = 0 THEN
            clr& = _RGB32(0, 100, 0)
        ELSE
            clr& = _RGB32(0, 0, 100 + (256 / MagneticRange * -Ball(i).charge))
        END IF
        LINE (Ball(i).x - Ball(i).size, Ball(i).y - Ball(i).size)-(Ball(i).x + Ball(i).size, Ball(i).y + Ball(i).size), clr&, BF
    NEXT
    _DISPLAY
    FOR i = 1 TO BallCount
        TempBall(i).x = Ball(i).x
        TempBall(i).y = Ball(i).y
        TempBall(i).charge = Ball(i).charge
        TempBall(i).xmomentium = Ball(i).xmomentium
        TempBall(i).ymomentium = Ball(i).ymomentium
    NEXT

    FOR i = 1 TO BallCount
        charge = Ball(i).charge
        FOR l = 1 TO BallCount
            IF i <> l THEN
                charge2 = Ball(l).charge
                directionx = 0: directiony = 0
                distancex = ABS(Ball(i).x - Ball(l).x)
                distancey = ABS(Ball(i).y - Ball(l).y)
                pushx = 0: pushy = 0: power = 0: force = 0
                power = ABS(charge) + ABS(charge2)
                pushx = power - distancex: IF pushx < 1 THEN pushx = 0
                pushy = power - distancey: IF pushy < 1 THEN pushy = 0
                directionx = Ball(i).x - Ball(l).x
                IF directionx > 0 THEN
                    directionx = 1
                ELSEIF directionx < 0 THEN
                    directionx = -1
                END IF
                directiony = Ball(i).y - Ball(l).y
                IF directiony > 0 THEN
                    directiony = 1
                ELSEIF directiony < 0 THEN
                    directiony = -1
                END IF

                IF Ball(l).x >= Ball(i).x - ABS(charge) AND Ball(l).x <= Ball(i).x + ABS(charge) AND Ball(l).y >= Ball(i).y - ABS(charge) AND Ball(l).y <= Ball(i).y + ABS(charge) THEN
                    'Balls are close enough together that we have interaction between balls!
                    'if charges are +/+ or -/- then push apart else move together
                    IF (charge > 0 AND charge2 > 0) THEN
                        'positive and positive charge
                        forcex = pushx * ABS(Ball(i).charge / power)
                        forcey = pushy * ABS(Ball(i).charge / power)
                        TempBall(l).x = TempBall(l).x - directionx * forcex
                        TempBall(l).y = TempBall(l).y - directiony * forcey
                        Ball(l).xmomentium = Ball(l).xmomentium - directionx * forcex
                        Ball(l).ymomentium = Ball(l).ymomentium - directiony * forcey

                    ELSEIF (charge < 0 AND charge2 < 0) THEN
                        'negative and negative charge
                        forcex = pushx * ABS(Ball(i).charge / power)
                        forcey = pushy * ABS(Ball(i).charge / power)
                        TempBall(l).x = TempBall(l).x - directionx * forcex
                        TempBall(l).y = TempBall(l).y - directiony * forcey
                        Ball(l).xmomentium = Ball(l).xmomentium - directionx * forcex
                        Ball(l).ymomentium = Ball(l).ymomentium - directiony * forcey

                    ELSEIF charge = 0 OR charge2 = 0 THEN
                        'We have an inert ball.  Do nothing to it at the moment

                    ELSE
                        'positive and negative charge
                        'These attract each other,  so our push becomes a negative push - which is a pull
                        IF pushx > distancex THEN pushx = distancex
                        forcex = pushx * ABS(Ball(i).charge / power)
                        TempBall(l).x = TempBall(l).x + directionx * forcex
                        IF pushy > distancey THEN pushy = distancey
                        forcey = pushy * ABS(Ball(i).charge / power)
                        TempBall(l).y = TempBall(l).y + directiony * forcey
                        Ball(l).xmomentium = Ball(l).xmomentium + directionx * forcex
                        Ball(l).ymomentium = Ball(l).ymomentium + directiony * forcey
                        IF ABS(pushx) >= distancex THEN Ball(l).xmomentium = 0 'The balls crashed together!  Don't let them pass each other!
                        IF ABS(pushy) >= distancey THEN Ball(l).ymomentium = 0 'The balls crashed together!  Don't let them pass each other!
                    END IF
                END IF
            END IF
        NEXT
    NEXT

    FOR i = 1 TO BallCount
        Ball(i).x = TempBall(i).x + Ball(i).xmomentium
        Ball(i).y = TempBall(i).y + Ball(i).ymomentium
        Ball(i).charge = TempBall(i).charge
        IF ClosedTable THEN
            IF Ball(i).x < 0 THEN Ball(i).x = 800 - Ball(i).x
            IF Ball(i).y < 0 THEN Ball(i).y = 680 - Ball(i).y
            IF Ball(i).x > 800 THEN Ball(i).x = Ball(i).x - 800
            IF Ball(i).y > 600 THEN Ball(i).y = Ball(i).y - 600
        END IF
        Ball(i).xmomentium = Ball(i).xmomentium * Velocity / 100
        Ball(i).ymomentium = Ball(i).ymomentium * Velocity / 100

    NEXT
LOOP

A little work in progress (aren't they all??), code snip-it to simulate the effects of magnetic repulsion between objects.


This gives random "Balls" a variable magnetic charge and then tosses them out onto a plane.  (Think charging metal ball bearings and tossing them on a pool table.)  We then can watch how the balls all interact with each other -- stronger charges push away from weaker opposite charges.   matching charges pull towards each other.   Momentum and bounce are taken into account...


Sometimes if we set our variables properly, we can reach a state of "magnetic harmony" where all the balls find their spot and go to rest.   Sometimes we'll see a ball get trapped in a small cluster of opposite charges and it'll just "bounce" from one to the other, constantly getting kicked back and forth.   And sometimes we'll see balls "orbit" each other.


I'm not sure if all this is 100% up to the physics standard, but it was fun to play around with and create, and I hope others like studying it and playing around with it as much as I did.  Smile

SCREENSHOT ADDED BY ADMINISTRATOR:


   
Find all posts by this user
Like Post
The following 2 users Like SMcNeill's post:
Donald Foster, Waltersmind (Admin)
12-06-2014, 07:16 PM
Post: #2
 (Print Post)
RE: Magnetic Repulsion Demo
I like the demo, but I think I totaly missed what was taking place. Maybe because of my lack of knowedge of physics.

Anyway, It's a neat demo.

Donald
Find all posts by this user
Like Post



Forum Jump:


User(s) browsing this thread: 1 Guest(s)




QB64 Member Project - Spiro Roses
QB64 Member Project - Overboard
QB64 Member Project - Input
QB64 Member Project - OpenGL Triangles
QB64 Member Project - Kings Court
QB64 Member Project - Kobolts Monopoly
QB64 Member Project - Quarto
QB64 Member Project - Color Rotating Text
QB64 Member Project - 9 Board
QB64 Member Project - Touche
QB64 Member Project - Qubic
QB64 Member Project - Color Triangles
QB64 Member Project - Line Thickness
QB64 Member Project - Pivet version one
QB64 Member Project - Bowditch curve
QB64 Member Project - Martin Fractals version four
QB64 Member Project - Sabotage
QB64 Member Project - MAPTRIANGLE
QB64 Member Project - Red Scrolling LED Sign
QB64 Member Project - Connect Four
QB64 Member Project - Swirl
QB64 Member Project - Amazon
QB64 Member Project - Point Blank
QB64 Member Project - Full Color LED Sign
QB64 Member Project - ARB Checkers
QB64 Member Project - Algeria Weather
QB64 Member Project - Rotating Background
QB64 Member Project - Blokus
QB64 Member Project - Splatter
QB64 Member Project - Exit
QB64 Member Project - Isolation
QB64 Member Project - Score 4
QB64 Member Project - Rubix's Magic
QB64 Member Project - Martin Fractals version two
QB64 Member Project - Kings Valley verion one
QB64 Member Project - Martin Fractals version three
QB64 Member Project - Pivot version two
QB64 Member Project - RGB Color Wheel
QB64 Member Project - Dreamy Clock
QB64 Member Project - Foursight
QB64 Member Project - Martin Fractals version one
QB64 Member Project - Kings Vallery version two
QB64 Member Project - Domain
QB64 Member Project - Basic Dithering
QB64 Member Project - Spinning Color Wheel
QB64 Member Project - Dakapo
QB64 Member Project - STxAxTIC 3D World
QB64 Member Project - Othello
QB64 Member Project - Inside Moves