Get Actual Collision X,y?

I guess this was one I had converted from a Runtime listener, as the instantiation of the listener is this:

bg2:addEventListener( "collision", onEm3Collision )

Even if I convert it to this:

bg2.collision = onEm3Collision bg2:addEventListener( "collision", bg2 )

and change the listener function definition to this:

local function onEm3Collision( self, event )

I still get event.x and event.y values that don’t correspond to the global coordinates.

I believe I saw similar behavior with the Runtime listener, but I will have to go back and try it again.

Which build has this?  The build I am currently using 2013.1135 always returns 0 for event.x and event.y.

since brent’s post was in april and 1135 was released in june i would say its in 1135

Hi @tonygod,

Can you post your collision code? Did you remember to add this line around where you instantiate physics?

[lua]

physics.setReportCollisionsInContentCoordinates( true )

[/lua]

http://docs.coronalabs.com/api/library/physics/setReportCollisionsInContentCoordinates.html

Brent

local function onEm3Collision( event ) print( "onEm3Collision: " .. event.phase ) if ( event.phase == "began" ) then audio.play( sounds["splash"] ) -- TRIGGER THE EMITTERS em3.x = event.x em3.y = event.y Particles.StartEmitter("em3", true) elseif ( event.phase == "ended" ) then bg2:removeEventListener( "collision", onEm3Collision ) waitingForSplash = false end end

I was not previously using physics.setReportCollisionsInContentCoordinates( true ), as I was unaware it was necessary.  The documentation merely states “The x and y position can be influenced by physics.getAverageCollisionPositions() and physics.setReportCollisionsInContentCoordinates().”  

I don’t know what influenced means. For me, it seems to mean I get event.x and event.y coordinates that do not match what I would expect to be the global x/y coordinates when I set it to true and I get 0 when it is not there.

Maybe the fact that I am using scenes or DisplayGroups is affecting this in some way?

Hi @tonygod,

At first glance, it appears you’re not utilizing the proper parameters in the collision listener. Since this is a “local” collision listener (not a Runtime one), the listener supports both “self” and “event”… you’re using just “event”, but that should be the second additional parameter.

Please see the guide on “Local collision listeners” here:

https://developer.coronalabs.com/content/game-edition-collision-detection

I guess this was one I had converted from a Runtime listener, as the instantiation of the listener is this:

bg2:addEventListener( "collision", onEm3Collision )

Even if I convert it to this:

bg2.collision = onEm3Collision bg2:addEventListener( "collision", bg2 )

and change the listener function definition to this:

local function onEm3Collision( self, event )

I still get event.x and event.y values that don’t correspond to the global coordinates.

I believe I saw similar behavior with the Runtime listener, but I will have to go back and try it again.

Any followup re gottcha’s here?

I’m getting 0’s returned also for event.x, event.y, but I note the local object is within larger display groups.  I have:

* called “setReportCollisionsInContentCoordinates” after physics start, although from the sample physics tutorial “CollisionDetection” you don’t seem to need it

* have followed the same layout for setting up the local listener

    function myCollList (self, event)        print("x/y: ", event.x, event.y)      end      myObject.collision = myCollList     myObject:addEventListener( "collision", myObject )  

* noted that the “event.target” and “event.other” return what I expect fine.  It’s just the event.x and event.y that are giving zero’s back

I could not get this to reliably return correct coordinates for event.x and event.y and just ended up using x and y coordinates for one of the objects.

Hi Greg, Tony,

I just tested this and it appears to work fine. The collision point of the falling box is accurate in my tests.

[lua]

physics.setReportCollisionsInContentCoordinates( true )

local function myCollList( self, event )

    if (event.phase == “began”) then

        print( event.x,event.y )

    end

end

local t = display.newRect( 100,0,50,50)

physics.addBody( t, “dynamic” )

t.rotation = 45

t.collision = myCollList

t:addEventListener( “collision”, t )

local tx = display.newRect( 100,200,50,50)

physics.addBody( tx, “static” )

print(tx.x,tx.y)

[/lua]

Brent

Worked in a basic test for me too. However in my app where I have embedded display objects within groups etc it wasn’t working. I’ve just worked around by using “target” and “other” position info

Hi Greg,

I assume you weren’t moving around the display groups independently of each other?

It was a player object hitting an obstacle object both within a larger level display group. The larger level group gets moved, horizontal scrolling game, but I think in the case I had it wasn’t being moved at the time. Hard to track something like this down when the function seems to work in the most basic scenario.

Hi Greg,

I just tried the sample that I wrote in nested display groups, and it works the same (correctly). So, I think there must be some other issue here beyond the Corona APIs. Let me know if you figure it out…

Brent

Ok. Spent of bit of time already so might need to move on with the work around for the moment.

Any followup re gottcha’s here?

I’m getting 0’s returned also for event.x, event.y, but I note the local object is within larger display groups.  I have:

* called “setReportCollisionsInContentCoordinates” after physics start, although from the sample physics tutorial “CollisionDetection” you don’t seem to need it

* have followed the same layout for setting up the local listener

    function myCollList (self, event)        print("x/y: ", event.x, event.y)      end      myObject.collision = myCollList     myObject:addEventListener( "collision", myObject )  

* noted that the “event.target” and “event.other” return what I expect fine.  It’s just the event.x and event.y that are giving zero’s back

I could not get this to reliably return correct coordinates for event.x and event.y and just ended up using x and y coordinates for one of the objects.

Hi Greg, Tony,

I just tested this and it appears to work fine. The collision point of the falling box is accurate in my tests.

[lua]

physics.setReportCollisionsInContentCoordinates( true )

local function myCollList( self, event )

    if (event.phase == “began”) then

        print( event.x,event.y )

    end

end

local t = display.newRect( 100,0,50,50)

physics.addBody( t, “dynamic” )

t.rotation = 45

t.collision = myCollList

t:addEventListener( “collision”, t )

local tx = display.newRect( 100,200,50,50)

physics.addBody( tx, “static” )

print(tx.x,tx.y)

[/lua]

Brent

Worked in a basic test for me too. However in my app where I have embedded display objects within groups etc it wasn’t working. I’ve just worked around by using “target” and “other” position info

Hi Greg,

I assume you weren’t moving around the display groups independently of each other?