Collision event listener not firing, what am I doing wrong?

Hello, here is my setup;

    local autoTurret = display.newGroup()     autoTurret.display = display.newRect(aT.x, aT.y, aT.width, aT.height)     autoTurret:insert(autoTurret.display) placeNewObj(autoTurret)

function placeNewObj(obj)     local function onLocalCollision( self, event )                  print("onLocalCollision")              end          physics.addBody(obj.display, "dynamic")     obj.display.isSensor = true          obj.display.collision = onLocalCollision     obj.display:addEventListener( "collision", obj.display )         end

And I don’t see the print line “onLocalCollision” when I’m supposed to with this setup. I used to have the similar setup, but without autoTurret being a display group. Something like this;

    local autoTurret = display.newRect(aT.x, aT.y, aT.width, aT.height)     placeNewObj(autoTurret)

function placeNewObj(obj)     local function onLocalCollision( self, event )                  print("onLocalCollision")              end          physics.addBody(obj, "dynamic")     obj.isSensor = true          obj.collision = onLocalCollision     obj:addEventListener( "collision", obj)         end

And 2nd way used to work. Am I missing something?

Any help? I really need to make the autoTurret a group, but I want to add collision listener for only 1 element of it (.display). I’m really clueless as to what’s going wrong…

I think my objects position is 0,0 for the collision ‘system’ even in reality obj.display is on another position.

Can I get some help please?

Please try and wait 24 hours before bumping your posts.  You need to give the community time to answer you.

Rob

I’m now almost sure it is because my dragDrop() function changes the display group’s position but it doesn’t get updated for the physics body.

If I change every elements position one by one, I see no more problem. Like this;

local function dragDrop(event)     --event.target is the autoTurret object     event.target.display.x = event.x     event.target.display.y = event.y     event.target.infoDisplay.x = event.x     event.target.infoDisplay.y = event.y end

But, the actual group’s ( autoTurret ) position remains at (0,0).

I’m basically having problem doing drag&drop on a display group which has physics bodies as elements.

If I use the group instead of its elements like the following, the autoTurret just goes to a very far location;

local function dragDrop(event)     --event.target is the autoTurret object     event.target.x = event.x     event.target.y = event.y end

I also tried localToContent() and contentToLocal() but no luck.

Hi @cbt,

You can’t shift display groups full of physics objects around independently of each other. This is explained here:

http://docs.coronalabs.com/guide/physics/limitations/index.html

Take care,

Brent

So only actual way I can achieve what I want is by changing every display elemens position, with respect to each other’s position, in my dragDrop() function? This seems like a nightmare…

Yes, this is an inherent aspect of Box2D and collisions, as the math that calculates collisions is based on a common coordinate “world”. If you don’t need collision detection during the actual drag (but only after they’re in the place the user wants them), then you could move the display group around and then update/offset the position of all the objects only after (on the “ended” phase).

Best regards,

Brent

Thanks for the insightful answer. The main reason I’m doing this is that I want to check if a ‘turret’ is colliding with another one when placing it on-screen. I’m doing this to see if a new turret overlaps with an already existing one. I couldn’t think of any other way, maybe storing each turret’s position and iterate thru it to see if positions overlap? I’d appreciate any help…

Any help? I really need to make the autoTurret a group, but I want to add collision listener for only 1 element of it (.display). I’m really clueless as to what’s going wrong…

I think my objects position is 0,0 for the collision ‘system’ even in reality obj.display is on another position.

Can I get some help please?

Please try and wait 24 hours before bumping your posts.  You need to give the community time to answer you.

Rob

I’m now almost sure it is because my dragDrop() function changes the display group’s position but it doesn’t get updated for the physics body.

If I change every elements position one by one, I see no more problem. Like this;

local function dragDrop(event)     --event.target is the autoTurret object     event.target.display.x = event.x     event.target.display.y = event.y     event.target.infoDisplay.x = event.x     event.target.infoDisplay.y = event.y end

But, the actual group’s ( autoTurret ) position remains at (0,0).

I’m basically having problem doing drag&drop on a display group which has physics bodies as elements.

If I use the group instead of its elements like the following, the autoTurret just goes to a very far location;

local function dragDrop(event)     --event.target is the autoTurret object     event.target.x = event.x     event.target.y = event.y end

I also tried localToContent() and contentToLocal() but no luck.

Hi @cbt,

You can’t shift display groups full of physics objects around independently of each other. This is explained here:

http://docs.coronalabs.com/guide/physics/limitations/index.html

Take care,

Brent

So only actual way I can achieve what I want is by changing every display elemens position, with respect to each other’s position, in my dragDrop() function? This seems like a nightmare…

Yes, this is an inherent aspect of Box2D and collisions, as the math that calculates collisions is based on a common coordinate “world”. If you don’t need collision detection during the actual drag (but only after they’re in the place the user wants them), then you could move the display group around and then update/offset the position of all the objects only after (on the “ended” phase).

Best regards,

Brent

Thanks for the insightful answer. The main reason I’m doing this is that I want to check if a ‘turret’ is colliding with another one when placing it on-screen. I’m doing this to see if a new turret overlaps with an already existing one. I couldn’t think of any other way, maybe storing each turret’s position and iterate thru it to see if positions overlap? I’d appreciate any help…