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