Changing Group.x

Here’s my problem. I create a bunch of enemies and add them to physics then to a display group called enemyGroup. I move enemmGroup.x + 40 pixels. Now the collision listener is in the same location even though the enemies have moved.

Has anyone encountered this before? Try this code to see what I mean

[lua]local group1 = display.newGroup()

local physics = require(“physics”)
physics.start()
physics.setGravity(0, 9)

local box = display.newRect(50,50,30,30)
physics.addBody(box, “static”)
box.name = “box”
group1:insert(box)

group1.x = group1.x+100

local function fire( event )
    local bullet = display.newCircle(event.x,event.y,5)
    physics.addBody(bullet, “dynamic”)
    bullet.gravityScale = -1
    bullet.name = “bullet”
end

local function onCollision ( event )
    if event.object1.name == “box” or event.object2.name == “box” then
        print("hit at: "…event.object1.x)
    end
end

Runtime:addEventListener(“collision”, onCollision)
Runtime:addEventListener(“touch”, fire)
[/lua]

Click under the box, then click to where the box used to be.

I believe the solution is to do a for loop for each item in enemyGroup and move them, but that uses more processing power?

is this a bug??

This is a known problem. I don’t see it changing any time soon.

The only ‘fix’ I know of is to have all physics objects in the same group as any objects they need to interact with. Not sure if you can nest your enemy group and player group inside another group and still have physics work correctly?

Okay so if I move the large group over 100, then both bullets and enemies are moved over 100. So the solution is to just move the bullets back 100 and it “looks okay.” Here’s the “fixed” version of what I posted above in case anyone stumbles across this in the future.

I don’t know if I can make it work, but it’s good to know.

[lua]local bigGroup = display.newGroup()  --CHANGE
local bulletGroup = display.newGroup()
  --CHANGE
local group1 = display.newGroup()
bigGroup:insert(group1)  --CHANGE
bigGroup:insert(bulletGroup)
--CHANGE

local physics = require(“physics”)
physics.start()
physics.setGravity(0, 9)

local box = display.newRect(50,50,30,30)
physics.addBody(box, “static”)
box.name = “box”
group1:insert(box)

bigGroup.x = bigGroup.x+100 --CHANGE

local function fire( event )
    local bullet = display.newCircle(event.x -100 ,event.y,5) --CHANGE
    physics.addBody(bullet, “dynamic”)
    bullet.gravityScale = -1
    bullet.name = “bullet”
    bulletGroup:insert(bullet) --CHANGE
end

local function onCollision ( event )
    if event.object1.name == “box” or event.object2.name == “box” then
        print("hit at: "…event.object1.x)
    end
end

Runtime:addEventListener(“collision”, onCollision)
Runtime:addEventListener(“touch”, fire)

[/lua]

changes noted

Hi @colt.sliva,

This is standard behavior (for groups with collisions). We investigated the possibility of fixing it, but it would be considerably intense behind the scenes for a somewhat minimal ROI, thus it isn’t a high priority.

Basically, in Box2D/Corona, items in different display groups that may collide must share the same 0,0 coordinate foundation point (the groups they’re in), because that’s how the collision engine works out the math to determine collisions. You can move your entire “world” around, i.e. you can shift the entire stage coordinates to 40,0 if you want, and the collisions should work. You just can’t shift one group while keeping another one at 0,0.

Regards,

Brent

is this a bug??

This is a known problem. I don’t see it changing any time soon.

The only ‘fix’ I know of is to have all physics objects in the same group as any objects they need to interact with. Not sure if you can nest your enemy group and player group inside another group and still have physics work correctly?

Okay so if I move the large group over 100, then both bullets and enemies are moved over 100. So the solution is to just move the bullets back 100 and it “looks okay.” Here’s the “fixed” version of what I posted above in case anyone stumbles across this in the future.

I don’t know if I can make it work, but it’s good to know.

[lua]local bigGroup = display.newGroup()  --CHANGE
local bulletGroup = display.newGroup()
  --CHANGE
local group1 = display.newGroup()
bigGroup:insert(group1)  --CHANGE
bigGroup:insert(bulletGroup)
--CHANGE

local physics = require(“physics”)
physics.start()
physics.setGravity(0, 9)

local box = display.newRect(50,50,30,30)
physics.addBody(box, “static”)
box.name = “box”
group1:insert(box)

bigGroup.x = bigGroup.x+100 --CHANGE

local function fire( event )
    local bullet = display.newCircle(event.x -100 ,event.y,5) --CHANGE
    physics.addBody(bullet, “dynamic”)
    bullet.gravityScale = -1
    bullet.name = “bullet”
    bulletGroup:insert(bullet) --CHANGE
end

local function onCollision ( event )
    if event.object1.name == “box” or event.object2.name == “box” then
        print("hit at: "…event.object1.x)
    end
end

Runtime:addEventListener(“collision”, onCollision)
Runtime:addEventListener(“touch”, fire)

[/lua]

changes noted

Hi @colt.sliva,

This is standard behavior (for groups with collisions). We investigated the possibility of fixing it, but it would be considerably intense behind the scenes for a somewhat minimal ROI, thus it isn’t a high priority.

Basically, in Box2D/Corona, items in different display groups that may collide must share the same 0,0 coordinate foundation point (the groups they’re in), because that’s how the collision engine works out the math to determine collisions. You can move your entire “world” around, i.e. you can shift the entire stage coordinates to 40,0 if you want, and the collisions should work. You just can’t shift one group while keeping another one at 0,0.

Regards,

Brent