Multiple Display Objects in one Physics body?

This seems to be possible, but it has some weird behaviors. I’m using this method to minimize texture memory usage when creating a big long platform (several thousand pixels long).

Anyone done this before? Are there any pointers as to how to get this to assign the physics body to the location of the display objects instead of the (0,0) location coordinates?

Thanks,

-Matt
W2MD [import]uid: 10211 topic_id: 15979 reply_id: 315979[/import]

Have you tried adding all the objects to a display group (if even just a temporary one, just record the original group of each object so you can re-insert them) and then adding the physics body to the group? [import]uid: 52430 topic_id: 15979 reply_id: 59224[/import]

That worked beautifully! Thanks Jon!

-Matt
W2MD

Here’s my test code in case anyone else can benefit from it. I just added the three images I was putting together into the “Hello Physics” project.

[lua]local physics = require( “physics” )
physics.start()
local sky = display.newImage( “bkg_clouds.png” )
sky.x = 160; sky.y = 195

local ground = display.newImage( “ground.png” )
ground.x = 160; ground.y = 445

physics.addBody( ground, “static”, { friction=0.5, bounce=0.3 } )

local num = 100 --this is the width of the middle

local Left = display.newImageRect( “Left.png”,121,187 )
Left.x = -num*0.5 - Left.width*0.5 ; Left.y = 0

local Middle = {}
for i = 1, num do
Middle[i] = display.newImageRect( “Middle.png” ,1,184)
Middle[i].x = num*0.5 - i; Middle[i].y = 2
end

local Right = display.newImageRect( “Right.png”,107,186 )
Right.x = num*0.5 + Right.width*0.5 -0.5; Right.y = 0;

local group = display.newGroup()

group:insert(Left)
for i = 1, num do
group:insert(Middle[i])
end
group:insert(Right)

group.x, group.y = 200, 200

physics.addBody( group, { density=3.0, friction=0.5, bounce=0.3 } )[/lua] [import]uid: 10211 topic_id: 15979 reply_id: 59351[/import]

After some testing, the massive number of display objects really bogs down the processor. The best way to do it is to take that one pixel wide middle image and stretch it as far as you want. It will look the same anyway, and the performance is awesome.

Here’s the updated section of code:

[lua]local num = 1000

local Left = display.newImageRect( “iceLevel/Left.png”,121,187 )
Left.x = -num*0.5 - Left.width*0.5 ; Left.y = 0

local Middle = display.newImageRect( “iceLevel/Middle.png” ,num,184)
Middle.x = 0; Middle.y = 2

local Right = display.newImageRect( “iceLevel/Right.png”,107,186 )
Right.x = num*0.5 + Right.width*0.5 -0.5; Right.y = 0;
local group = display.newGroup()

group:insert(Left)
group:insert(Middle)
group:insert(Right)[/lua] [import]uid: 10211 topic_id: 15979 reply_id: 59361[/import]