Physics Simulator Test 8 can you explain clearly the following code blocks?

Thank you ^.^

@Invad3rZIM Is this something like what you were asking about. Use you finger to scroll the screen to follow the ball.


displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    tab={}
    dx=0
    dy=0
    for x=0,7200,10 do
        y=math.sin(math.rad(x+120))*50
        table.insert(tab,vec2(x,600-x/2+y))
    end   
    c1=physics.body(CHAIN,false,unpack(tab))    
    b1=physics.body(CIRCLE,10)
    b1.x=20
    b1.y=HEIGHT
end

function draw()
    background(40,40,50)
    fill(255)  
    pushMatrix()
    translate(dx,dy)
    for a,b in pairs(tab) do
        ellipse(b.x,b.y,4)
    end   
    ellipse(b1.x,b1.y,20)   
    popMatrix()
end    

function touched(t)
    if t.state==MOVING then
        dx=dx+t.deltaX
        dy=dy+t.deltaY
    end
end

Sort of…is there any way for the chain to go on indefinitely? To reset the points again?

In this example, the points can keep going as far as you want. If you’re after a Roller Coaster type program, that example doesn’t use a CHAIN. It’s just a table of points that it shows in 3D space. So this program and the Roller Coaster are completely different in what they do.

When I used the term roller coaster above, I just meant a roller coaster in that the points form a sine curve.

In the program you made after about 40 seconds the track runs out…how would one go about perpetuating the chain-track?

In setup, the for loop goes to 7200, with a step count of 10. That’s only 720 table entries. That value can be changed to whatever value you want. As for making it go on forever, I think the chain can be destroyed and another one created where the previous one left off. I can try that tomorrow unless someone else comes up with a way before then.

Challenge accepted

By the way, thank you for everything Mr. Dave

@dave1707 here we go


displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    smooth()
    tab2={}
    tab={}
    dx=0
    dy=0
    iteration = 0
  --  scr = 2
    scrx = -40
    scry = -0*HEIGHT/2-20
    tab = makeNewChain()
        
    c1=physics.body(CHAIN,false,unpack(tab))    
    b1=physics.body(CIRCLE,10)
    b1.x=20
    b1.y=HEIGHT
end

function draw()
    if b1.x > (3600 * iteration) - WIDTH/2 then
        tab2=tab
        c2 = c1
       tab = makeNewChain()
        c1=physics.body(CHAIN,false,unpack(tab))
    end
    
    if(b1.x > 3600*(iteration-1) + WIDTH/2)then
      tab2={} end
    
    local s, d = b1.x%5100, b1.y%5100
    if( s > 2550) then
        s = (5100-s)/10
    end
    

    if( d > 2550) then
        d = (5100-d)/10
    end
    background(s,d,50)
    fill(255)  
 --   pushMatrix()
  --  translate(b1.x,b1.y)
    moveScreen()
    for a,b in pairs(tab) do
        fill(81, 75, 75, 255)
        ellipse(b.x-1,b.y-1,4)
        fill(255, 255, 255, 255)
        ellipse(b.x,b.y,4)
    end   
    for a,b in pairs(tab2) do
        fill(81, 75, 75, 255)
        ellipse(b.x-1,b.y-1,4)
        fill(255, 255, 255, 255)
        ellipse(b.x,b.y,4)
    end   
    
    fill(255-s,255-d,205)
    ellipse(b1.x,b1.y,20)   
  --  popMatrix()
end    



function moveScreen()
   if(b1.x > WIDTH/2 -scrx) then
   scrx = WIDTH/2-b1.x end
   -- if(b1.y > HEIGHT- 300 - scry) then
       -- scry = HEIGHT - 300 - b1.y end
    if b1.y < HEIGHT/2 - scry then
        scry = HEIGHT/2 - b1.y 
    end
    
    if b1.x < WIDTH/2-scrx then
        scrx = WIDTH/2-b1.x
    end
    if b1.y > HEIGHT/2 - scry then
        scry = HEIGHT/2-b1.y
    end
  --  print(b1.x.." "..b1.y)
   translate(scrx,scry)--scrx,scry)
end

function makeNewChain()
    local loctab = {}
    for x=(iteration*3600)+0,3600+(iteration*3600),10 do
        y=math.sin(math.rad(x+120))*50
        table.insert(loctab,vec2(x,600-x/2+y))
    end   
    
    iteration = iteration + 1 
    
    return loctab
end

So that goes on infinitely…one thing: it gets a tiny bit laggy for the the length it takes for the ball to pass the screen length once…could you please optimize my draw functions so they still retain the original function (color manipulation that’s always fun) but make it less laggy? I’m not quite sure how to do push/pops/ whatever magic tricks you know what I have yet to learn and I’d like to dissect your corrections to advance :slight_smile:

@Invad3rZIM I made some minor changes and removed some code that wasn’t being used. Other than that, you have it coded pretty good. On my IPad Air, it runs smooth. One thing, some of the color combinations make it hard to see the chain.


displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    smooth()
    tab2={}
    iteration = 0
    scrx = -40
    scry = -20
    tab = makeNewChain()
    c1=physics.body(CHAIN,false,unpack(tab))    
    b1=physics.body(CIRCLE,10)
    b1.x=20
    b1.y=HEIGHT
end

function draw()
    if b1.x > 3600 * iteration - WIDTH/2 then
        tab2=tab
        tab = makeNewChain()
        c1=physics.body(CHAIN,false,unpack(tab))
    end
    if b1.x > 3600*(iteration-1) + WIDTH/2 then
        tab2={} 
    end
    local s, d = b1.x%5100, b1.y%5100
    if s > 2550 then
        s = (5100-s)/10
    end
    if d > 2550 then
        d = (5100-d)/10
    end
    background(s,d,50)
    fill(255)  
    moveScreen()
    for a,b in pairs(tab) do
        fill(81, 75, 75, 255)
        ellipse(b.x-1,b.y-1,4)
        fill(255, 255, 255, 255)
        ellipse(b.x,b.y,4)
    end   
    for a,b in pairs(tab2) do
        fill(81, 75, 75, 255)
        ellipse(b.x-1,b.y-1,4)
        fill(255, 255, 255, 255)
        ellipse(b.x,b.y,4)
    end   
    fill(255-s,255-d,205)
    ellipse(b1.x,b1.y,20)   
end    

function moveScreen()
    if b1.x > WIDTH/2 -scrx then
        scrx = WIDTH/2-b1.x 
    end
    if b1.y < HEIGHT/2 - scry then
        scry = HEIGHT/2 - b1.y 
    end
    if b1.x < WIDTH/2-scrx then
        scrx = WIDTH/2-b1.x
    end
    if b1.y > HEIGHT/2 - scry then
        scry = HEIGHT/2-b1.y
    end
    translate(scrx,scry)
end

function makeNewChain()
    local loctab = {}
    for x=iteration*3600,3600+iteration*3600,10 do
        y=math.cos(math.rad(x))*50
        table.insert(loctab,vec2(x,600-x/2+y))
    end   
    iteration = iteration + 1 
    return loctab
end

Can you take a look at another (somewhat similar, but far more advanced in chaining) program I made and I’m not sure how to scale it(so the ball and the chain are in view at all times)/make it repeat…could you try your hand at it?

Could you please make a simple scale program that also translates the screen to follow that ball above and scales the screen to include both the ball and the coaster line? Having trouble wrapping my head around it. Thank you ^.^

@Invad3rZIM Change the “sc” parameter to shrink the display and increase the “cc” slider to move the display up the screen.


--displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    parameter.number("sc",.2,1,1)
    parameter.integer("cc",1,10000)
    smooth()
    tab2={}
    iteration = 0
    scrx = -40
    scry = -20
    tab = makeNewChain()
    c1=physics.body(CHAIN,false,unpack(tab))    
    b1=physics.body(CIRCLE,10)
    b1.x=20
    b1.y=HEIGHT
end

function draw()
    scale(sc,sc)
    if b1.x > 3600 * iteration - WIDTH/2 then
        tab2=tab
        tab = makeNewChain()
        c1=physics.body(CHAIN,false,unpack(tab))
    end
    if b1.x > 3600*(iteration-1) + WIDTH/2 then
        tab2={} 
    end
    local s, d = b1.x%5100, b1.y%5100
    if s > 2550 then
        s = (5100-s)/10
    end
    if d > 2550 then
        d = (5100-d)/10
    end
    --background(s,d,50)
    background(40,40,50)
    fill(255)  
    moveScreen()
    for a,b in pairs(tab) do
        fill(81, 75, 75, 255)
        ellipse(b.x-1,b.y-1+cc,4)
        fill(255, 255, 255, 255)
        ellipse(b.x,b.y+cc,4)
    end   
    for a,b in pairs(tab2) do
        fill(81, 75, 75, 255)
        ellipse(b.x-1,b.y-1+cc,4)
        fill(255, 255, 255, 255)
        ellipse(b.x,b.y+cc,4)
    end   
    fill(255-s,255-d,205)
    ellipse(b1.x,b1.y+cc,20)   
end    

function moveScreen()
    if b1.x > WIDTH/2 -scrx then
        scrx = WIDTH/2-b1.x 
    end
    if b1.y < HEIGHT/2 - scry then
        scry = HEIGHT/2 - b1.y 
    end
    if b1.x < WIDTH/2-scrx then
        scrx = WIDTH/2-b1.x
    end
    if b1.y > HEIGHT/2 - scry then
        scry = HEIGHT/2-b1.y
    end
    translate(scrx,scry)
end

function makeNewChain()
    local loctab = {}
    for x=iteration*3600,3600+iteration*3600,10 do
        y=math.cos(math.rad(x))*50
        table.insert(loctab,vec2(x,600-x/2+y))
    end   
    iteration = iteration + 1 
    return loctab
end

@Invad3rZIM Here’s a smaller version that does a continuous scroll.


displayMode(FULLSCREEN)

function setup()
    limit=2880
    print(limit)
    tab={}
    for z=-limit,limit,20 do
        h1=math.cos(math.rad(z/2))*8
        h2=math.cos(math.rad(z/4))*16
        table.insert(tab,vec2(z,h1*h2+200))
    end
    c1=physics.body(CHAIN,false,unpack(tab))
    b1=physics.body(CIRCLE,10)
    b1.x=0
    b1.y=350
end

function draw()
    background(40, 40, 50)
    fill(255)
    translate(200,0)
    for a,b in pairs(tab) do
        ellipse(b.x-b1.x,b.y,4)
    end
    ellipse(0,b1.y,20)
    if b1.linearVelocity.x<125 then
        b1:applyForce(vec2(50,0))
    end
    if b1.x>limit-900 then
        b1.x=b1.x-limit
    end
end