Multi-element physics bodies with dynamic amount of body parts

Hi there,

we developed a game which creates physic bodies with a dynamic amount of body parts.

Currently the solution is pretty pretentious. Our game can have physic objects with a total of 200+ Body elements. Depending on how many parts there are, we have serveral functions to create this objects.

This is how it lookslike:

if(partAmount == 0) then

        physics.addBody(body,

            {shape=bodyPart[0], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}

        );

    elseif (partAmount == 1) then

        physics.addBody(body,

            {shape=bodyPart[0], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

            {shape=bodyPart[1], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}

        );

    elseif (partAmount == 2) then

        physics.addBody(body,

            {shape=bodyPart[0], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

            {shape=bodyPart[1], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

            {shape=bodyPart[2], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}

        );

    elseif (partAmount == 3) then

        physics.addBody(body,

            {shape=bodyPart[0], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

            {shape=bodyPart[1], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

            {shape=bodyPart[2], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

            {shape=bodyPart[3], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}

        );

    elseif (partAmount == 4) then

        physics.addBody(body,

            {shape=bodyPart[0], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

            {shape=bodyPart[1], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

            {shape=bodyPart[2], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

            {shape=bodyPart[3], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

            {shape=bodyPart[4], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}

        );

ect…

My question: Would it be possible that you modify the physics.addBody function so it can build objects by using a table.

For example:

parts= {

        {shape=bodyPart[0], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

        {shape=bodyPart[1], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

        {shape=bodyPart[2], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

        {shape=bodyPart[3], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

        {shape=bodyPart[4], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}}

}

physics.addBody(body, {parts})

This would be really helpfull and would save us alot (20k+ lines) of recurring code. 

Thank you!

Amplexor

I haven’t tested this, but I’m pretty sure you have what you need thanks to Lua unpack()

Try this:

parts= { {shape=bodyPart[0], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}, {shape=bodyPart[1], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}, {shape=bodyPart[2], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}, {shape=bodyPart[3], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}, {shape=bodyPart[4], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}} } physics.addBody(body, unpack(parts) );

and if that doesn’t work do this:

parts= { body, {shape=bodyPart[0], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}, {shape=bodyPart[1], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}, {shape=bodyPart[2], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}, {shape=bodyPart[3], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}, {shape=bodyPart[4], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}} } physics.addBody( unpack(parts) );

PS - You post was well formatted, but you can format code w/ a tool to make it that much more accessible to those who want to read it and help you:

formatyourcode.jpg

Man I am getting ready to go on vacation so I don’t have time to write this function all the way, but to answer your question yes. But you don’t need to need to dynamically add them by building the parts hard coded like you have them

parts= {

        {shape=bodyPart[0], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

        {shape=bodyPart[1], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

        {shape=bodyPart[2], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

        {shape=bodyPart[3], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

        {shape=bodyPart[4], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}}

}

instead create a function where you pass all of these items to it for whatever reason you have

  Create an empty lua table

parts = {}

Look at inserting into a LUA table / concat or copy

https://docs.coronalabs.com/api/library/table/insert.html

http://lua-users.org/wiki/TablesTutorial

https://docs.coronalabs.com/api/library/table/copy.html

( something like this )

Create a function that you want to pass in the body parts

   AddMyBodyParts (bodyPart,myfilter,dens,mFriction,mBounce)

        myParts = {shape=bodyPart, filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}

        nextPosition = #parts +1 – Get the number of items in the table then add one more for next position

    table.insert( parts, nextPosition, myParts )

 

  end

Hopefully this helps

Good luck Larry

I haven’t tested this, but I’m pretty sure you have what you need thanks to Lua unpack()

Try this:

parts= { {shape=bodyPart[0], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}, {shape=bodyPart[1], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}, {shape=bodyPart[2], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}, {shape=bodyPart[3], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}, {shape=bodyPart[4], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}} } physics.addBody(body, unpack(parts) );

and if that doesn’t work do this:

parts= { body, {shape=bodyPart[0], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}, {shape=bodyPart[1], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}, {shape=bodyPart[2], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}, {shape=bodyPart[3], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}, {shape=bodyPart[4], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}} } physics.addBody( unpack(parts) );

PS - You post was well formatted, but you can format code w/ a tool to make it that much more accessible to those who want to read it and help you:

formatyourcode.jpg

Man I am getting ready to go on vacation so I don’t have time to write this function all the way, but to answer your question yes. But you don’t need to need to dynamically add them by building the parts hard coded like you have them

parts= {

        {shape=bodyPart[0], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

        {shape=bodyPart[1], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

        {shape=bodyPart[2], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

        {shape=bodyPart[3], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce},

        {shape=bodyPart[4], filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}}

}

instead create a function where you pass all of these items to it for whatever reason you have

  Create an empty lua table

parts = {}

Look at inserting into a LUA table / concat or copy

https://docs.coronalabs.com/api/library/table/insert.html

http://lua-users.org/wiki/TablesTutorial

https://docs.coronalabs.com/api/library/table/copy.html

( something like this )

Create a function that you want to pass in the body parts

   AddMyBodyParts (bodyPart,myfilter,dens,mFriction,mBounce)

        myParts = {shape=bodyPart, filter=mFilter, density=dens, friction=mFriction, bounce=mBounce}

        nextPosition = #parts +1 – Get the number of items in the table then add one more for next position

    table.insert( parts, nextPosition, myParts )

 

  end

Hopefully this helps

Good luck Larry