Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [source] jello
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
zylum




PostPosted: Sun Jan 15, 2006 11:57 pm   Post subject: [source] jello

this is an spin off from my string/chain source... its a completely different effect so i decided to make a new thread Wink

code:
setscreen ("graphics:max;max,offscreenonly")

type Node :
    record
        x, y, vx, vy : real
    end record

const g := -0.1
const wind := -0.0
const N := 9
const d := 100
const k := 0.08
const decay := 0.98
const radius := 8

var nodes : array 1 .. N of Node
for i : 1 .. N
    if i = 1 then
        nodes (i).x := maxx / 2
        nodes (i).y := maxy / 2
    else
        nodes (i).x := maxx / 2 + cosd (360 / N * i) * d
        nodes (i).y := maxy / 2 + sind (360 / N * i) * d
    end if
    nodes (i).vx := 0
    nodes (i).vy := 0
end for

var F : real
var mx, my, md, mxl, myl : int
var holding : int := -1

var connected : array 1 .. N, 1 .. N of boolean
for i : 1 .. N
    for j : 1 .. N
        %connected (i, j) := i ~= j & (i = 1| j = 1| abs (i - j) = 1| abs (j - i) = 1| (i = 2 & j = N)| (i = N & j = 2)) %connected in a loop and to a center node
        connected (i, j) := i ~= j %everyone connected to each other
    end for
end for

function whatAngle (dx, dy : real) : real
    var ratio, angle : real
    if abs (dx) > 0.0000001 then
        ratio := dy / dx
    else
        ratio := dy / 0.000001
    end if
    angle := arctand (abs (ratio))
    if dx < 0 then
        angle := 180 - angle
    end if
    if dy < 0 then
        angle := 360 - angle
    end if
    result angle
end whatAngle

loop
    mousewhere (mx, my, md)

    for i : 1 .. N
        nodes (i).vx := nodes (i).vx * decay + wind
        nodes (i).vy := nodes (i).vy * decay + g
        for j : 1 .. N
            if connected (i, j) then
                F := -k * (Math.Distance (nodes (j).x, nodes (j).y, nodes (i).x, nodes (i).y) - d)
                nodes (i).vx += cosd (whatAngle (nodes (i).x - nodes (j).x, nodes (i).y - nodes (j).y)) * F
                nodes (i).vy += sind (whatAngle (nodes (i).x - nodes (j).x, nodes (i).y - nodes (j).y)) * F
            end if
        end for
    end for

    for i : 1 .. N
        for j : 1 .. N
            if connected (i, j) then
                drawline (nodes (i).x div 1, nodes (i).y div 1, nodes (j).x div 1, nodes (j).y div 1, grey)
            end if
        end for
    end for
    for i : 1 .. N
        if i ~= holding then
            nodes (i).x += nodes (i).vx
            nodes (i).y += nodes (i).vy
            if (nodes (i).x < radius| nodes (i).x > maxx - radius) then
                nodes (i).vx *= -1
            end if
            if (nodes (i).y < radius| nodes (i).y > maxy - radius) then
                nodes (i).vy *= -1
            end if
            nodes (i).x := min (max (radius, nodes (i).x), maxx - radius)
            nodes (i).y := min (max (radius, nodes (i).y), maxy - radius)
            if Math.Distance (mx, my, nodes (i).x, nodes (i).y) <= radius then
                drawfilloval (nodes (i).x div 1, nodes (i).y div 1, radius, radius, grey)
            else
                drawfilloval (nodes (i).x div 1, nodes (i).y div 1, radius, radius, white)
            end if
        else
            drawfilloval (nodes (i).x div 1, nodes (i).y div 1, radius, radius, yellow)
        end if
        drawoval (nodes (i).x div 1, nodes (i).y div 1, radius, radius, black)
    end for

    if md > 0 then
        if holding = -1 then
            for i : 1 .. N
                if Math.Distance (mx, my, nodes (i).x, nodes (i).y) <= radius then
                    holding := i
                    exit
                end if
            end for
        end if
    else
        if holding ~= -1 then
            nodes (holding).vx := mx - mxl
            nodes (holding).vy := my - myl
        end if
        holding := -1
    end if
    if holding ~= -1 then
        nodes (holding).x := mx
        nodes (holding).y := my
    end if
    mxl := mx
    myl := my

    Time.DelaySinceLast (10)
    View.Update
    cls
end loop


btw you can pick up the balls Wink

[edit]a little bit updated...[/edit]
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Mon Jan 16, 2006 12:12 am   Post subject: (No subject)

Haven't you done something very much like this before? Or was that based on different concepts...either way, good stuff as always. If only people could learn from you.
zylum




PostPosted: Mon Jan 16, 2006 1:15 am   Post subject: (No subject)

Delos wrote:
Haven't you done something very much like this before? Or was that based on different concepts...either way, good stuff as always. If only people could learn from you.


hmm i dont think so... the closest thing to this that i made was a program where you can throw a single ball... also a solar system simulation but thats something completely different. maybe you just saw a similar effect elsewhere. ive seen it many times.
Clayton




PostPosted: Mon Jan 16, 2006 2:30 am   Post subject: (No subject)

gotta love it, but do u think u could comment it, because i would like to see how u did this because its really cool
codemage




PostPosted: Mon Jan 16, 2006 12:40 pm   Post subject: (No subject)

Very cool.
Gets unstable sometimes if the nodes touch or get stuck in the walls.
do_pete




PostPosted: Mon Jan 16, 2006 12:51 pm   Post subject: (No subject)

That's awesome
sylvester-27




PostPosted: Mon Jan 16, 2006 10:34 pm   Post subject: (No subject)

wow thats wicked. but if you grab one of the dots and swing it back and forth touching both walls it messes up. Either i didn't wait long enough for it to slow down or there is a problem there. anyway very cool program
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: