Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Expanding Universe

dave1707dave1707 Mod
edited January 4 in General Posts: 8,383

They say the Universe is expanding. Each point is pushing every other point away from itself. The farther an object is away from us, the faster it’s moving away from us. This program has a bunch of circles that are expanding in size each frame and pushing each surrounding circle away from each other. We’re the red circle in the middle, and the green circle is about twice as far away than the blue one. Once you start the expansion by tapping the screen, you’ll see the speed and distance that the blue and green circle are moving away from the red circle. This isn’t varying exciting to watch, but it does show how the expansion is pushing everything away from each other.

PS. The distance is in pixels and the speed is pixels per draw cycle.

displayMode(FULLSCREEN)

function setup()
    --physics.continuous=true
    physics.gravity(0,0)
    rad=2
    nbr=8
    h2,h3=0,0
    cen=((2*nbr+1)*(2*nbr+1))//2

    tab={}
    for x=-nbr,nbr do
        for y=-nbr,nbr do
            p1=physics.body(CIRCLE,rad)
            p1.x=WIDTH/2+rad*x
            p1.y=HEIGHT/2+rad*y
            p1.restitution=1
            p1.radius=rad
            p1.type=DYNAMIC
            p1.sleepingAllowed=false
            p1.val=0
            if x==0 and y==0 then
                p1.val=1
            elseif x==0 and y==3 then
                p1.val=2
            elseif x==0 and y==8 then
                p1.val=3
            end
            table.insert(tab,p1)
        end
    end
end

function draw()
    background(112, 181, 168, 255)
    for a,b in pairs(tab) do
        fill(255)
        if b.val==1 then
            fill(255,0,0)
            d1=vec2(b.x,b.y)
        elseif b.val==2 then
            fill(0,0,255)
            d2=vec2(b.x,b.y)
        elseif b.val==3 then
            fill(0,255,0)
            d3=vec2(b.x,b.y)
        end
        ellipse(b.x,b.y,b.radius*2)
        if exp then
            b.radius=b.radius+.003
        end
    end

    fill(255)    
    if not exp then
        text("Tap the screen to start the Universe expansion",WIDTH/2,HEIGHT-100)
    else   
        v1=d1
        dist2=v1:dist(d2)
        fill(0,0,255)
        text(string.format("Distance  %3d  and speed   %.4f  from red circle",dist2//1,dist2-h2,dist2//1),WIDTH/2,HEIGHT-50)
        h2=dist2

        dist3=v1:dist(d3)
        fill(0,255,0)
        text(string.format("Distance  %3d  and speed   %.4f  from red circle",dist3//1,dist3-h3,dist3//1),WIDTH/2,HEIGHT-100)
        h3=dist3
    end
end

function touched(t)
    if t.state==BEGAN then
        exp=true
    end
end
Sign In or Register to comment.