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

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 :slight_smile:

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)

Google knows all.
http://www.gammon.com.au/scripts/doc.php?lua=unpack

unpack function check! Thanks ignatz!

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

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

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

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.

And the loop function?

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.

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

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

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


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

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

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.

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?

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.

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

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.

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


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