How to offset a POLY physics body from a objects center point.

I have several graphics of buildings that i want to have the same physics object on.  

but i need to apply a x/y offset to the physics object based on which picture its being attached to.

How do you get a offset to x/y on a object? OTHER then having to calculate the POLY originally from a offset center.  Since i am just re-using the same poly several times.  its seems like there should be a simple way to get the Physics Object to be offset from the main objects center point.

Example picture:

10269478_10204725023498259_5124545550144

Hi @lab1,

Are these building separate display objects (separate images), or are the just part of the overall map image? If they’re separate objects, it’s probably easier to just apply a physics body to each one, instead of mapping out a multi-element body for the overall map.

If you want to do the second option (all one image), there’s not really any “easier” way, except that you could declare the basic shape as a table:

[lua]

local buildingShape = { 0,-12,20,0,0,12,-20,0 }

[/lua]

Then, for a building, determine its relative offset on the overall map (say -20,-100 from center). Set up an initial table with those offsets, then add the basic shape’s coordinate to that:

[lua]

local thisBuildingShape = { -20,-100,-20,-100,-20,-100,-20,-100 }

for i=1,#thisBuildingShape do

   thisBuildingShape[i] = thisBuildingShape[i] + buildingShape[i]

end

[/lua]

After that, you should be able to use “thisBuildingShape” as the shape declaration for the physics body of that building.

Hope this helps,

Brent

Ahh i should have mentioned that…  

the buildings are individual items displayed on a background tiled map.   Each building is actually representing 1 building that has 10 display lifecycles  (i.e. a spritesheet with 10 frames of different sizes)

![10392510_10204729593812514_7075969502878](https://fbcdn-sphotos-d-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/10392510_10204729593812514_7075969502878533803_n.jpg?oh=d64482ae37af5a43a3469ae3a7b003c7&oe=55616328& gda =1429053565_159d1aa95c10e60375ab38554a50ee62)

So at level 1 the building would look 1 way… and at level 7 …  totally different.  etc…   So on the map picture I am showing the same building represented at different display levels.  (Note this artwork is barowed artwork…  it wont be used in any final work)

The POLY physics object is then applied to the displayed building …   ( i should note i am also using a display MASK on each level of the building to make the touch region exact)

So with the poly object being applied to the display object…  I need to offset the poly based on what level the building is.  so…  the base …  is always at the base of the spritesheet frame thats being displayed.  :)

So being able to simply set a x/y offset on the physics body itself is what i am looking for.

Right now in the forums all i am seeing is that i have to manually draw my poly offset from the center of the object.  but as you can see if i have  to do this 10 times…  for each structure i plan to have…  and each structure possibly having a different size poly…     It just seems like there should be a easier way to offset a poly from the center of a display object.

Hi @lab1,

Unfortunately, once a physics body is “installed”, you can’t alter it (change its shape or shift it) without destroying the existing body and putting a new one on. But this shouldn’t be a problem for you, since I assume building upgrades don’t happen in any kind of constant Runtime loop… meaning, destroying and re-adding physics bodies isn’t a trivial operation, performance-wise, but you’d only see an impact if you tried doing it like 1000 consecutive times in a loop. If you only change the body on an occasional building upgrade, this is the best solution.

Brent

i agree that changing the poly on the object is a simple remove the old and add the new.    but i am still trying to avoid having to calculate all new poly points based on  a offset center based on what level of the building…  based on which building i am working with.

if there was a Physics Object offset x/y   80% of the extra math would go away…    

Not sure why the forums is showing my name as @lab1  my account is named Team1 and have published about 28 smaller educational apps in the past.  

Hi @team1,

It looks like your forum avatar/name is appearing correctly now.

Could you just create a simple function which takes an offset X/Y and returns a table of new coordinates based on the “base” coordinates? That would probably be my approach, and it would only be a few lines of code…

Best regards,

Brent

Hi @lab1,

Are these building separate display objects (separate images), or are the just part of the overall map image? If they’re separate objects, it’s probably easier to just apply a physics body to each one, instead of mapping out a multi-element body for the overall map.

If you want to do the second option (all one image), there’s not really any “easier” way, except that you could declare the basic shape as a table:

[lua]

local buildingShape = { 0,-12,20,0,0,12,-20,0 }

[/lua]

Then, for a building, determine its relative offset on the overall map (say -20,-100 from center). Set up an initial table with those offsets, then add the basic shape’s coordinate to that:

[lua]

local thisBuildingShape = { -20,-100,-20,-100,-20,-100,-20,-100 }

for i=1,#thisBuildingShape do

   thisBuildingShape[i] = thisBuildingShape[i] + buildingShape[i]

end

[/lua]

After that, you should be able to use “thisBuildingShape” as the shape declaration for the physics body of that building.

Hope this helps,

Brent

Ahh i should have mentioned that…  

the buildings are individual items displayed on a background tiled map.   Each building is actually representing 1 building that has 10 display lifecycles  (i.e. a spritesheet with 10 frames of different sizes)

![10392510_10204729593812514_7075969502878](https://fbcdn-sphotos-d-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/10392510_10204729593812514_7075969502878533803_n.jpg?oh=d64482ae37af5a43a3469ae3a7b003c7&oe=55616328& gda =1429053565_159d1aa95c10e60375ab38554a50ee62)

So at level 1 the building would look 1 way… and at level 7 …  totally different.  etc…   So on the map picture I am showing the same building represented at different display levels.  (Note this artwork is barowed artwork…  it wont be used in any final work)

The POLY physics object is then applied to the displayed building …   ( i should note i am also using a display MASK on each level of the building to make the touch region exact)

So with the poly object being applied to the display object…  I need to offset the poly based on what level the building is.  so…  the base …  is always at the base of the spritesheet frame thats being displayed.  :)

So being able to simply set a x/y offset on the physics body itself is what i am looking for.

Right now in the forums all i am seeing is that i have to manually draw my poly offset from the center of the object.  but as you can see if i have  to do this 10 times…  for each structure i plan to have…  and each structure possibly having a different size poly…     It just seems like there should be a easier way to offset a poly from the center of a display object.

Hi @lab1,

Unfortunately, once a physics body is “installed”, you can’t alter it (change its shape or shift it) without destroying the existing body and putting a new one on. But this shouldn’t be a problem for you, since I assume building upgrades don’t happen in any kind of constant Runtime loop… meaning, destroying and re-adding physics bodies isn’t a trivial operation, performance-wise, but you’d only see an impact if you tried doing it like 1000 consecutive times in a loop. If you only change the body on an occasional building upgrade, this is the best solution.

Brent

i agree that changing the poly on the object is a simple remove the old and add the new.    but i am still trying to avoid having to calculate all new poly points based on  a offset center based on what level of the building…  based on which building i am working with.

if there was a Physics Object offset x/y   80% of the extra math would go away…    

Not sure why the forums is showing my name as @lab1  my account is named Team1 and have published about 28 smaller educational apps in the past.  

Hi @team1,

It looks like your forum avatar/name is appearing correctly now.

Could you just create a simple function which takes an offset X/Y and returns a table of new coordinates based on the “base” coordinates? That would probably be my approach, and it would only be a few lines of code…

Best regards,

Brent