[SOLVED] Attaching multiple physics bodies to a single object

I know this can be done in this way:-

 shapeTail = {149,300,124,257,133,208,147,197,165,197,169,241,174,258}  
 shapeBase = {174,258,204,260,219,250,277,251,252,299,199,319,156,308}  
 shapeTop = {221,247,205,193,166,87,277,120,275,260}  
  
 physics.addBody(tmp, {shape = shapeTail, bounce = 0.3, friction = 0.3}, {shape = shapeBase, bounce = 0.3, friction = 0.3}, {shape = shapeTop, bounce = 0.3, friction = 0.3})  

But what I’m looking for is a system where I can generate the data before the function is called and looking as to how it can be passed in to generate multiple objects. I have tried an array like this:-

 shapeTail = {149,300,124,257,133,208,147,197,165,197,169,241,174,258}  
 shapeBase = {174,258,204,260,219,250,277,251,252,299,199,319,156,308}  
 shapeTop = {221,247,205,193,166,87,277,120,275,260}  
  
 test = {{shape = shapeTail, bounce = 0.3, friction = 0.3}, {shape = shapeBase, bounce = 0.3, friction = 0.3}, {shape = shapeTop, bounce = 0.3, friction = 0.3}}  
  
 physics.addBody(tmp, test)  

I also tried adding the bodies individually similar to this:-

 physics.addBody(tmp, {shape = shapeTail, bounce = 0.3, friction = 0.3})  
 physics.addBody(tmp, {shape = shapeBase, bounce = 0.3, friction = 0.3})  

But this only display a single physics body still.

Basically long story short, I’m looping through an array /database call which has different object bodies in it an trying to attach them all to a single object. Is anyone able to point me in the right direction for this?

Thanks in advance for your help!

I hope that makes sense… [import]uid: 133056 topic_id: 25100 reply_id: 325100[/import]

Wow, that was a pretty big load of dribble above. Re-reading it confused myself, so hate to think how other people would go with trying to understand that :wink:

Let me try again and see if I can explain myself a bit better this time. What I’m looking at is having an image, lets say for example a car, then in a database I want to store the various physics bodies attached to this. So for example we might have information stored to make a polygone for the front, another for the body, another for the rear of the car, and others for the wheels. Now I do a call on the database to get this information, so I do a loop on this information an receive each of the physics bodies one at a time. What is the best way to apply these to a single object?

As I was saying above, I know how to do it in a single call by passing them all in together, but as I’m looping through each of them individually I can’t do this. This is way I tried storing it as an array (as you can see above) which again didn’t work. So then I tried attaching the body the loop, so in each loop step I tried to add the body, this again didn’t work.

The way I’m storing the data is as a comma separated string which I then parse to get the correct details. This system worked when using a single physics body.
Hopefully that’s a bit easier to understand the issue I have and what I’m trying to achieve! [import]uid: 133056 topic_id: 25100 reply_id: 101999[/import]

Ah, something similar frustrated me way back when, but I solved it.

You’ve got the right idea with building an array (a “holding” table containing your individual shape tables). Build your array just like you did in your 2nd code snippet above… each shape separated by commas.

Now the trick is, you can’t just pass that array directly to the physics “addBody” API. You need to UNPACK that array IN the body addition… unpack(table). What this Lua function does is to recursively unpack a table that contains other elements. This is how you can pass your array “test” to your body addition as ONE reference, and unpack its children just as if you explicitly defined them.

physics.addBody( tmp, unpack(test) )

Note that “unpack()” is not the fastest Lua function. Don’t use it 100 times in some game-time loop. Do it during a loading cycle or something, which is probably where you’d use it anyway. :slight_smile:

Brent Sorrentino
[import]uid: 9747 topic_id: 25100 reply_id: 102035[/import]

Thanks heaps for that, works just as you said. Did a bit of research on the unpack command to find out exactly what it does. So for anyone that finds this trying to solve this problem, here is what unpack does.

“Returns the elements from the given table.”

I’ll post my code for it when I have time to finish it off.

Thanks again Brent! [import]uid: 133056 topic_id: 25100 reply_id: 102154[/import]

Here’s what I came up with, it include scaling for objects (just for the fun of it!)

function dbToPoly(obj)  
 bodies = explode("\n", obj.polygon\_body)  
 bodyReturn = {}  
 xScale = obj.width / obj.original\_width  
 yScale = obj.height / obj.original\_height  
 for a = 1, #bodies, 1 do  
 points = explode(",", bodies[a])  
 body = {}  
 for b = 1, #points, 1 do  
 if points[b] == '' then  
 --if blank record  
 else  
 if b % 2 == 0 then  
 body[b] = (points[b] - (obj.height / 2)) \* yScale  
 else  
 body[b] = (points[b] - (obj.width / 2)) \* xScale   
 end  
 end  
 end  
 bodyReturn[a] = {  
 bounce = 0.3,  
 friction = 0.3,  
 shape = body  
 }  
 end  
 return bodyReturn  
end  
  
for row in db:nrows("SELECT \* FROM objects") do  
 tmp = display.newImageRect( "assets/objects/" .. row.image, row.width, row.height)   
 tmp.y = row.ypos  
 tmp.x = row.xpos  
 shapePoly = dbToPoly (row)  
 physics.addBody(tmp, "static", unpack(shapePoly))  
end  

I haven’t clearned this up, or optimised it yet, but it’s enough to show people how it’s done. [import]uid: 133056 topic_id: 25100 reply_id: 102168[/import]