Phsyics Body Array

i have went through the api sample (below)

local car = display.newImage("big\_red\_car.png")  
roofShape = { -20,-10, 20,-10, 20,10, -20,10 }  
hoodShape = { 0,-35, 37,30, -37,30 }  
trunkShape = { 0,-37, 37,-10, 23,34, -23,34, -37,-10 }  
  
physics.addBody( car, "dynamic",  
 { density=3.0, friction=0.5, bounce=0.2, shape=roofShape },  
 { density=6.0, friction=0.6, bounce=0.4, shape=hoodShape },  
 { density=4.0, friction=0.5, bounce=0.4, shape=trunkShape }  
)  

================================================================
and i am planning to put all the setup into a setup.lua file, which contain all physic object setup
and in main.lua, i should be able to call

main.lua  
physics.addBody( myBody[index], myBody[index].bodyType,  
 myBody[index].elementSetup  
)  
  
setup.lua  
myBody[index].elementSetup = "dynamic"  
myBody[index].bodyType = {  
 { density=3.0, friction=0.5, bounce=0.2, shape=roofShape },  
 { density=6.0, friction=0.6, bounce=0.4, shape=hoodShape },  
 { density=4.0, friction=0.5, bounce=0.4, shape=trunkShape }  
}  

but i am hitting compile error for code above.
I am quite new to lua can some lua master tell me, how suppose the two portion of code should be?

Thanks for the guide [import]uid: 10373 topic_id: 3755 reply_id: 303755[/import]

post your error

j [import]uid: 6645 topic_id: 3755 reply_id: 11410[/import]

Dear Corona Sifu,

if i use

local car = display.newImage("big\_red\_car.png")  
  
myElement = { -20,-10, 20,-10, 20,10, -20,10 }, { 0,-35, 37,30, -37,30 }, { 0,-37, 37,-10, 23,34, -23,34, -37,-10 }  
  
physics.addBody( car, "dynamic", myElement)  

my object can only detect first element, which is { -20,-10, 20,-10, 20,10, -20,10 }

but if i use

local car = display.newImage("big\_red\_car.png")  
  
myElement = {{ -20,-10, 20,-10, 20,10, -20,10 }, { 0,-35, 37,30, -37,30 }, { 0,-37, 37,-10, 23,34, -23,34, -37,-10 }}  
  
physics.addBody( car, "dynamic", myElement)  

it will detect a square box
how can we assign to ONE variable, and let this variable to tell our physic body

 { density=3.0, friction=0.5, bounce=0.2, shape=roofShape },  
 { density=6.0, friction=0.6, bounce=0.4, shape=hoodShape },  
 { density=4.0, friction=0.5, bounce=0.4, shape=trunkShape }  

reason i need to seperate the definition is to put everything into my def.lua, and during my main logic, it will jz read setup from def.lua and smarty loop all objects into game level [import]uid: 10373 topic_id: 3755 reply_id: 11462[/import]

how about this?

setup.lua

[lua]local bodydefs = {

[1] =
{
id=“car”,
img = “big_red_car.png”,
btype = “dynamic”,
def =
{
{density=3.0, friction=0.5, bounce=0.2, shape={-20,-10, 20,-10, 20,10, -20,10}},
{density=6.0, friction=0.6, bounce=0.4, shape={0,-35, 37,30, -37,30}},
{density=4.0, friction=0.5, bounce=0.4, shape={0,-37, 37,-10, 23,34, -23,34, -37,-10}}
}
},

[2] =
{
id=“ball”,
img = “ball.png”,
btype = “dynamic”,
def = {density=1, friction=1, bounce=1, radius=20}
}

}[/lua]

main.lua
[lua]require(“setup”)

local objects={}

for b=1, #bodydefs, 1 do

local object = display.newImage(bodydefs[b].img)
local btype = bodydefs[b].btype
local def = bodydefs[b].def
local id = boydefs[b].id

physics.addBody(gfx, btype, def)

objects[id] = object

end[/lua] [import]uid: 6645 topic_id: 3755 reply_id: 11483[/import]

you could also do eg

[lua]local bodydefs = {

[“car”] =
{
img = “big_red_car.png”,

etc
},

[“ball”] =
{
img=“ball.png”,

etc
}[/lua]

and then loop through it using key/value pairs

[import]uid: 6645 topic_id: 3755 reply_id: 11484[/import]

thanks for your reply jmp909, but i really believe your code is not working (sorry to say that)

i have put your code into my demo, my complex shape is always show a squarebox in physic detection [import]uid: 10373 topic_id: 3755 reply_id: 11488[/import]

if it’s short post it here… or zip it up on sendspace.com [import]uid: 6645 topic_id: 3755 reply_id: 11492[/import]

here you go
https://docs.google.com/leaf?id=0B60HuoC9ULZ-ODQwNTZmODEtNjYzNy00ZDAwLWExZDctMGJkYWJlNmJiZGRl&hl=en&authkey=CMyNrpsM

no login required
Thanks for tracing. Btw, are you Corona Staff? jmp909 [import]uid: 10373 topic_id: 3755 reply_id: 11583[/import]

no weilies, just a newbie corona programmer [import]uid: 6645 topic_id: 3755 reply_id: 11632[/import]

it appears the arguments to physics.addBody need to be separate arguments not a table…

for this to work you need to call unpack() on your table

ie

[lua]physics.addBody(object, ‘dynamic’, unpack(myElement))[/lua]

which is the equivalent of

[lua]physics.addBody(object, ‘dynamic’, myElement[0], myElement[1], myElement[2],…, myElement[n])[/lua]

[import]uid: 6645 topic_id: 3755 reply_id: 11633[/import]

you are my man! [import]uid: 10373 topic_id: 3755 reply_id: 11675[/import]