Intereseting physics when using display.newGroup

I am just playing with different ways of doing the same program. This is a simple ball dropping down, following the ramps, hitting the bottom then resetting to the top and repeating. When I put the balls in a newGroup the balls “see” the ramps as not being where the graphic is, the ball “see” the ramps as being below their visual placement. When I do the same program the long way without newGroup and give each ball it’s physics without using the array index method the physics works fine. The balls are the same ones used in the sample pool program. I see nothing obvious causing the dislocation.
require ( “physics” )
physics.start()

local ramp1 = display.newRect(0,200,300,10)
ramp1:setFillColor(255,0,0)
ramp1.rotation = 30
local ramp2 = display.newRect(200,400,300,10)
ramp2:setFillColor(255,0,0)
ramp2.rotation = -30
local ramp3 = display.newRect(0,600,300,10)
ramp3:setFillColor(255,0,0)
ramp3.rotation = 30
local leftWall = display.newRect(0,0,10,850)
leftWall:setFillColor(255,0,0)
local rightWall = display.newRect(470,0,10,850)
rightWall:setFillColor(255,0,0)
local bottom = display.newRect(0,850,470,10)
bottom:setFillColor(255,255,0)

function InitializeBall()
ball_blue = display.newImage( “ball_blue.png” )
ball_red = display.newImage( “ball_red.png” )
ball_green = display.newImage( “ball_green.png” )
ball_orange = display.newImage( “ball_orange.png” )
ball_pink = display.newImage( “ball_pink.png” )
ball_purple = display.newImage( “ball_purple.png” )
ball_yellow = display.newImage( “ball_yellow.png” )

balls = display.newGroup(ball_blue, ball_red, ball_green, ball_orange, ball_pink,ball_purple, ball_yellow)

for i = 1, balls.numChildren do
balls[i].x = 200
balls[i].y = -40 * i
physics.addBody( balls[i], { bounce=0, radius = 15 } )
end
end

InitializeBall()

function CheckBottom(cnt)
if balls[cnt].y > 800 then
balls[cnt]:setLinearVelocity(0,0)
balls[cnt].y = -40
balls[cnt].x = 40
end
end

–Function to check the ball’s location
local function checkPos ()
CheckBottom(1)
CheckBottom(2)
CheckBottom(3)
CheckBottom(4)
CheckBottom(5)
CheckBottom(6)
CheckBottom(7)
end

–Add a runtime listener
Runtime: addEventListener (“enterFrame”, checkPos)

physics.addBody(bottom, “static”, {bounce = 0})
physics.addBody(rightWall, “static”)
physics.addBody(leftWall, “static”)
physics.addBody(ramp1, “static”, {bounce = 0})
physics.addBody(ramp2, “static”, {bounce = 0})
physics.addBody(ramp3, “static”, {bounce = 0}) [import]uid: 23256 topic_id: 23325 reply_id: 323325[/import]

Physics objects can have some issues if in different groups, a search for this will come up with lots of results.

Also, please post your code inside tags, like so;

< lua >
– Code goes here
< /lua >

Without the spaces.

Makes it readable :wink: [import]uid: 52491 topic_id: 23325 reply_id: 93462[/import]

Peach is absolutely correct, however I have found that physic objects in different groups are fine as long as the parent groups are all at 0,0.

I think it is the difference in coordinates which throws things off - the physics world will effectively be using a different relative base location. [import]uid: 8271 topic_id: 23325 reply_id: 93480[/import]

Bummer. My solution seemed just so clean. I guess I will leave newGroup out of this lesson and do it the old fashioned way.

Tags? I wondered how people were getting their code to be so pretty in the forum.

Thanks [import]uid: 23256 topic_id: 23325 reply_id: 93526[/import]

I honestly don’t have a problem making use of multiple display groups when I want to group physics objects or alter their display height. You just need to make sure that all display groups being used as parent groups are never moved from 0,0 [import]uid: 8271 topic_id: 23325 reply_id: 93535[/import]