Problem with global collision's event.x and event.y

According the the documentation at http://docs.coronalabs.com/api/event/collision/x.html, event.x (and event.y) should mark where the collision occurs in global coordinates. I’m having trouble getting this to happen. How can I create a circle where two objects collide?

Here’s a sample modified from the basic Physics Based Game for a new project:



– level1.lua


local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

– include Corona’s “physics” library
local physics = require “physics”
physics.start(); physics.pause()


– forward declarations and other locals
local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5


– BEGINNING OF YOUR IMPLEMENTATION

– NOTE: Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.


– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view

– create a grey rectangle as the backdrop
local background = display.newRect( 0, 0, screenW, screenH )
background:setFillColor( 128 )

– make a crate (off-screen), position it, and rotate slightly
local crate = display.newImageRect( “crate.png”, 90, 90 )
crate.x, crate.y = 160, -100
crate.rotation = 15
crate.myName = “crate”;

– add physics to the crate
physics.addBody( crate, { density=1.0, friction=0.3, bounce=0.3 } )

– create a grass object and add physics (with custom shape)
local grass = display.newImageRect( “grass.png”, screenW, 82 )
grass:setReferencePoint( display.BottomLeftReferencePoint )
grass.x, grass.y = 0, display.contentHeight
grass.myName = “grass”;

– define a shape that’s slightly shorter than image bounds (set draw mode to “hybrid” or “debug” to see)
local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 }
physics.addBody( grass, “static”, { friction=0.3, shape=grassShape } )

– all display objects must be inserted into group
group:insert( background )
group:insert( grass)
group:insert( crate )
end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view

physics.start()

end

– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view

physics.stop()

end

– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view

package.loaded[physics] = nil
physics = nil
end

local function onCollision( event )
if ( event.phase == “began” ) then

print( "began: " … event.object1.myName … " & " … event.object2.myName … " at " … event.x … “,” … event.y )
local myCircle = display.newCircle( 0, 0, 10 )
myCircle.x = event.x;
myCircle.y = event.y;
myCircle:setFillColor(0,0, 255)
elseif ( event.phase == “ended” ) then

print( "ended: " … event.object1.myName … " & " … event.object2.myName … " at " … event.x … “,” … event.y )

end
end


– END OF YOUR IMPLEMENTATION

Runtime:addEventListener( “collision”, onCollision )

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )


return scene [import]uid: 177725 topic_id: 30886 reply_id: 330886[/import]

Looks like either the documentation is incorrect or it’s a bug. The coordinate sit relates are relative to object1. Change

[lua]myCircle.x = event.x;
myCircle.y = event.y;[/lua]

to

[lua]myCircle.x = event.object1.x + event.x;
myCircle.y = event.object1.y + event.y;[/lua]

and you get something closer to what you expect. Kind of does a circle in a weird spot on the third bounce but you might be able to figure out what’s up with that. [import]uid: 147305 topic_id: 30886 reply_id: 123521[/import]

I think what you’re seing is what I’ve came across as well here:
https://developer.coronalabs.com/forum/2012/09/05/physics-collision-position-events
Possibly i a bug. [import]uid: 117906 topic_id: 30886 reply_id: 123522[/import]

Looks like either the documentation is incorrect or it’s a bug. The coordinate sit relates are relative to object1. Change

[lua]myCircle.x = event.x;
myCircle.y = event.y;[/lua]

to

[lua]myCircle.x = event.object1.x + event.x;
myCircle.y = event.object1.y + event.y;[/lua]

and you get something closer to what you expect. Kind of does a circle in a weird spot on the third bounce but you might be able to figure out what’s up with that. [import]uid: 147305 topic_id: 30886 reply_id: 123521[/import]

I think what you’re seing is what I’ve came across as well here:
https://developer.coronalabs.com/forum/2012/09/05/physics-collision-position-events
Possibly i a bug. [import]uid: 117906 topic_id: 30886 reply_id: 123522[/import]