Howdy, Stranger!

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

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

edited July 2014 in General Posts: 134

So tonight I'm trying to make a tiny wings simulation to exercise the use of translating the screen to center the x value of the bird as well as figuring out how to make the roller coaster track repeat...

I'm dissecting the physics simulator code and I'm having a few issues understanding it..
Mainly could someone please explain how CHAIN works and how I could use it to make an array of points that repeat?

Also I assume unpack just lays out the values for each key in the table points. Could someone verify?

Tank you :)

local points = {}
    for i = 0,WIDTH,WIDTH/30 do
        table.insert(points, vec2(i,math.sin(i*4)*20+60))
    end

    local ground = physics.body(CHAIN, true, unpack(points))
    debugDraw:addBody(ground)

Comments

  • Posts: 134

    unpack function check! Thanks ignatz!

  • Posts: 134

    How can I manipulate the chain function to make it repeat the series of points in definately?

  • dave1707dave1707 Mod
    Posts: 6,963

    @Invad3rZIM What are you trying to do that you want the chain to repeat. Not sure about the question.

  • Posts: 134

    I'm trying to make a roller coaster track using sine and making it so that to the track goes on indefinately should the screen be translated to to center there character that the player controls

  • dave1707dave1707 Mod
    Posts: 6,963

    You can create a table of points. Create a chain using the table. You can keep adding points to the table, destroy the existing chain and recreate a new chain with the new points in the table. You can start and end at any position in the table when you create the chain.

  • Posts: 134

    And the loop function?

  • dave1707dave1707 Mod
    Posts: 6,963

    If you're talking about the for loop in your 1st post above, that for loop can be extended as far as you want in either direction. Instead of starting at 0 and going to WIDTH, you can start at -2 * WIDTH or go to 2 * WIDTH and you can use the points at any position in that range.

  • edited July 2014 Posts: 134

    I mean the body declaration

    physics.body(CHAIN,true,unpack(tableOfPoints))
    

    The true value is a Boolean for a loop (so says the codea manual) But I'm not sure what that means

  • dave1707dave1707 Mod
    Posts: 6,963

    Oh, that loop. I believe that's used to connect the 1st point to the last point if it's set to true.

  • dave1707dave1707 Mod
    Posts: 6,963

    Here's an example showing what "loop" is used for in the physics body CHAIN. With it set to "false", the ball rolls down the chain (dots). When "loop" is changed to "true", the first and last points are connected. The ball then rolls down the invisible chain formed between the first and last point.


    displayMode(FULLSCREEN) supportedOrientations(PORTRAIT_ANY) function setup() pt={} pt[1]=vec2(30,330) for z=2,15 do pt[z]=vec2(20+z*40,200+z*40) end loop=false -- change to true to connect 1st and last point of the chain c1=physics.body(CHAIN,loop,unpack(pt)) b1=physics.body(CIRCLE,10) b1.x=600 b1.y=HEIGHT end function draw() background(40,40,50) fill(255) for a,b in pairs(pt) do ellipse(b.x,b.y,4) end ellipse(b1.x,b1.y,20) end
  • Posts: 134

    Thank you mr dave1707; you truly are absolutely fantastic :)

    Can you name an instance when you'd want the loop set to true?

  • dave1707dave1707 Mod
    Posts: 6,963

    I guess if "loop" was set to true, it would work the same as POLYGON. Other than that I don't know of a reason.

  • Posts: 134

    Yeah It looks like it'd just be a static polygon aha...so I'm going to go try to write a simple game using loops and my first translation....another thing: I know that dynamic objects have a gravityScale, but is there any way to make objects be attracted to other dynamic objects ? I tried fiddling with the masses making them super large because gravity = 6.67E-11m1m2/d^2 but that didn't seem to work aha..any thoughts?

  • dave1707dave1707 Mod
    Posts: 6,963

    Codea gravity doesn't work like real gravity does. Giving an object a lot of mass doesn't do anything for its gravity. Gravity tends to go down, but you can change it to go some to the left or right.

  • Posts: 134

    Is there any way to make gravity centered around a single point on the screen?

  • dave1707dave1707 Mod
    Posts: 6,963

    Yes and no. Yes: if you write code to do it yourself. There are several orbit programs already written, some by me. No: if you want Codea to do it for you.

  • Posts: 134

    Could I please see your barest orbit program, dissect it, and use it as a sample?

  • dave1707dave1707 Mod
    Posts: 6,963

    displayMode(FULLSCREEN) supportedOrientations(PORTRAIT_ANY) function setup() s1=physics.body(CIRCLE,5) s1.x=300 s1.y=400 s1.gravityScale=0 s1:applyForce(vec2(0,8)) s1g=400 s2=physics.body(CIRCLE,5) s2.x=400 s2.y=400 s2.gravityScale=0 s2:applyForce(vec2(0,-8)) s2g=400 end function draw() background(40,40,50) fill(255) d = s2g / ((s1.x - s2.x)^2 + (s1.y - s2.y)^2 )^1.5 s1dx = ( s2.x - s1.x ) * d * 5 s1dy = ( s2.y - s1.y ) * d * 5 d = s1g / ((s2.x - s1.x)^2 + (s2.y - s1.y)^2 )^1.5 s2dx = ( s1.x - s2.x ) * d * 5 s2dy = ( s1.y - s2.y ) * d * 5 s1:applyForce(vec2(s1dx,s1dy)) s2:applyForce(vec2(s2dx,s2dy)) ellipse(s1.x,s1.y,10) ellipse(s2.x,s2.y,10) end
  • Posts: 134

    Thank you ^.^

  • dave1707dave1707 Mod
    Posts: 6,963

    @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
  • Posts: 134

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

  • dave1707dave1707 Mod
    Posts: 6,963

    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.

  • Posts: 134

    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?

  • dave1707dave1707 Mod
    Posts: 6,963

    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.

  • Posts: 134

    Challenge accepted

    By the way, thank you for everything Mr. Dave

  • Posts: 134

    @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 :)

  • dave1707dave1707 Mod
    Posts: 6,963

    @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
  • Posts: 134

    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?

  • Posts: 134

    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 ^.^

  • dave1707dave1707 Mod
    Posts: 6,963

    @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
  • dave1707dave1707 Mod
    Posts: 6,963

    @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
Sign In or Register to comment.