I’ve managed to isolate it – this is just an edited “level1.lua” file from one of the basic collision detection samples.
When the crate first drops and hits the platform, the event.x/y is reported as (45,45), which is relative to the crate which is 90x90. Makes sense, that’s what I would expect. However if you then click the re-add platform button (which destroys and re-adds the platform) and then redrop the crate, the event.x/y will report something like 70.6x-5.99 which can only be relative to the platform. Since the onCollision handler is attached to the crate, I would expect the event.x/y to always be relative to the crate and not change depending on the order the objects are added to the physics engine.
Interestingly, if you keep re-adding the platform and re-dropping the crate, the event.x/y coords seem to cycle between being relative to the crate and relative to the platform.
Is this a bug or am I doing something wrong?
[code]
local widget = require( “widget” );
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
– include Corona’s “physics” library
local physics = require “physics”
physics.start(); physics.pause()
local crate = false;
local platform = false;
local addbutton = false;
local resetbutton = false;
local platform_def = { x=60, y=400, width=150, height=10, bodyType=“static”, ob_type=“platform” }
– Called when the scene’s view does not exist:
function scene:createScene( event )
platform = createPlatform(platform_def);
– make a crate (off-screen), position it, and rotate slightly
crate = display.newImageRect( “crate.png”, 90, 90 )
crate.x, crate.y = 160, -100
crate.rotation = 0
– add physics to the crate
physics.addBody( crate, { density=1.0, friction=0.2, bounce=0.0 } )
crate.isFixedRotation = true;
crate:addEventListener( “collision”, function(event) onCollision(event); end );
addbutton = widget.newButton{
label=“Re-Add Platform”,
labelColor = { default={0}, over={0} },
width=125, height=40,
left = display.contentWidth - 150,
top = 25,
onRelease = function() platform:removeSelf(); platform=nil; platform = createPlatform(platform_def); return true; end
}
resetbutton = widget.newButton{
label=“Reset Crate”,
labelColor = { default={0}, over={0} },
width=124, height=40,
left = display.contentWidth - 150,
top = 90,
onRelease = function() crate.x,crate.y = 160, -100; return true; end
}
end
function createPlatform(params)
local object = nil;
object = display.newRoundedRect( params.x, params.y, params.width, params.height, 2 );
object.strokeWidth = 1;
object:setFillColor( 100, 100, 100 );
object:setStrokeColor( 0,0,0 );
object.density = params.density or 0;
object.friction = params.friction or 0;
object.bounce = params.bounce or 0;
object.isSensor = params.isSensor or false;
object.bodyType = params.bodyType or “dynamic”;
physics.addBody( object, object.bodyType, {density = object.density, friction = object.friction, bounce = object.bounce, isSensor = object.isSensor}) ;
object.ob_type = params.ob_type or “platform”;
return object;
end
function onCollision(event)
local other = event.other;
if( other.ob_type == “platform” ) then
– hit a platform
if( event.phase == “began” ) then
print("crate/platform collision at location: " … event.x … " x " … event.y );
end
end
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
– END OF YOUR IMPLEMENTATION
– “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
[/code] [import]uid: 119931 topic_id: 28600 reply_id: 116134[/import]