Collision location appears incorrect

I’ve not looked at this before and frankly I’m stumped as to why I can’t solve this problem, however it appears that the x,y location of a collision is either (a) incorrect or (b) relative to one of the objects involved in the collision. It’s just not clear which one. As demonstrated with the sample code here:

http://docs.coronalabs.com/api/event/collision/x.html

Does anyone have any light to shed on this?

Cheers,
Matt. [import]uid: 8271 topic_id: 31601 reply_id: 331601[/import]

Ah, I’m not the first to this problem, but it still appears persistent:

http://developer.coronalabs.com/forum/2012/07/13/collision-eventxeventy-inconsistent-newly-added-bodies [import]uid: 8271 topic_id: 31601 reply_id: 126237[/import]

Ah, I’m not the first to this problem, but it still appears persistent:

http://developer.coronalabs.com/forum/2012/07/13/collision-eventxeventy-inconsistent-newly-added-bodies [import]uid: 8271 topic_id: 31601 reply_id: 126237[/import]

Experiencing the same issue myself

Some screenshots:

Physics does not work on the actual object!

[![Photobucket](http://i1323.photobucket.com/albums/u592/mercsten/NoCollisionTriggered_zps5806d03f.jpg)](http://s1323.beta.photobucket.com/user/mercsten/library/)
Invisible object colliding with the sprite animation!
 [![Photobucket](http://i1323.photobucket.com/albums/u592/mercsten/InvisblePhysicsObject_zps873133c2.jpg)](http://s1323.beta.photobucket.com/user/mercsten/library/)

I have a test sample project that isolates the problem (don’t know how to attach it in this thread though). I hope i’m just doing something wrong and this is not an actual bug . . . . bugs take time to resolve. [import]uid: 179529 topic_id: 31601 reply_id: 127302[/import]

Experiencing the same issue myself

Some screenshots:

Physics does not work on the actual object!

[![Photobucket](http://i1323.photobucket.com/albums/u592/mercsten/NoCollisionTriggered_zps5806d03f.jpg)](http://s1323.beta.photobucket.com/user/mercsten/library/)
Invisible object colliding with the sprite animation!
 [![Photobucket](http://i1323.photobucket.com/albums/u592/mercsten/InvisblePhysicsObject_zps873133c2.jpg)](http://s1323.beta.photobucket.com/user/mercsten/library/)

I have a test sample project that isolates the problem (don’t know how to attach it in this thread though). I hope i’m just doing something wrong and this is not an actual bug . . . . bugs take time to resolve. [import]uid: 179529 topic_id: 31601 reply_id: 127302[/import]

bumpity . . . . bump bump [import]uid: 179529 topic_id: 31601 reply_id: 127575[/import]

bumpity . . . . bump bump [import]uid: 179529 topic_id: 31601 reply_id: 127575[/import]

Hi @merc,
Can you try to parse out at least some of your code and include it here within code tags? The screenshots would be helpful if you showed how you’re setting up your physics bodies, collision handler, etc… or at least, provide a detailed description on what’s going wrong.

Thanks!
Brent
[import]uid: 9747 topic_id: 31601 reply_id: 128303[/import]

Relevant code of the trial project:

-----------------------------------------------------------------------------------------  
--  
-- level1.lua  
--  
-----------------------------------------------------------------------------------------  
  
local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
local sprite = require ("sprite")  
  
-- include Corona's "physics" library  
local physics = require ("physics")  
physics.start()  
physics.pause()  
physics.setGravity( 0, 0 ) -- no gravity vector  
physics.setScale( 5 )  
physics.setDrawMode( "hybrid" )  
  
--------------------------------------------  
  
-- forward declarations and other locals  
local screenW, screenH, halfW, halfH = display.contentWidth, display.contentHeight, display.contentWidth\*0.5, display.contentHeight\*0.5  
local bg  
-----------------------------------------------------------------------------------------  
-- 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  
 bg = display.newImage("bg2.jpg",0,0)  
 bg:setReferencePoint( display.TopLeftReferencePoint )  
 bg.x, bg.y = 0, 0  
 local displayGroup = display.newGroup()  
 displayGroup:insert( bg )  
 displayGroup:setReferencePoint( display.TopLeftReferencePoint )  
 displayGroup.x, displayGroup.y = bg.x, bg.y  
  
 local activeGroup = display.newGroup()  
  
 local distanceTravelled = 4  
 local minX = 0  
 local maxX = bg.width  
 local minY = 0  
 local maxY = bg.height  
  
 local hasCollidedRight = false  
 local hasCollidedLeft = false  
 local hasCollidedBottom = false  
 local hasCollidedTop = false  
  
 local tapEventX = 0  
 local tapEventY = 0  
  
 -- filepath, width, height  
 local pteroSheet = sprite.newSpriteSheet("ptero.png",104, 50)  
 -- spritesheet, starting frame, number of frames  
 local pteroSet = sprite.newSpriteSet(pteroSheet, 1, 6 )  
 --spriteSet,sprite name, starting frame,# of frames,time in millis for a full animation,-2 for loop indefinite  
 sprite.add( pteroSet, "ptero", 1, 6, 300, -2 )  
 local ptero = sprite.newSprite( pteroSet )  
 local pteroShape = {-30,-30, 30,-30, 30,30, -30,30}  
 physics.addBody( ptero, {density=6.0, friction=0.3, bounce=0.0, shape=pteroShape} )  
  
 ptero.myName = "ptero"  
 ptero.x = screenW \* 0.5  
 ptero.y = screenH \* 0.25  
  
 ptero:prepare( "ptero" )  
 ptero:play()  
 activeGroup:insert(ptero)  
  
 local eventRect = display.newRect( 0, 0, bg.width, bg.height \*0.95)  
 --eventRect:setReferencePoint( display.TopLeftReferencePoint )  
 eventRect.strokeWidth = 2  
 eventRect.alpha = 0.01  
 eventRect:setStrokeColor(180,0,200)  
 displayGroup:insert( eventRect)  
  
 -- create a grass object and add physics (with custom shape)  
 local grass = display.newImageRect( "grass.png", screenW \*0.25, 82 )  
 --grass:setReferencePoint( display.TopLeftReferencePoint )  
 grass.x, grass.y = halfH, halfW  
 physics.addBody( grass, "static", {density=6.0, friction=0.3 } )  
 displayGroup:insert( grass )  
 -- all display objects must be inserted into group  
 group:insert( displayGroup )  
 group:insert( activeGroup)  
  
 local function onPteroFly(eventX,eventY)  
 -- unless when on the edges of the bg, ptero stays on the middle of the screen while sidescrolling  
  
 local function moveNextX()  
 if not hasCollidedRight then  
  
 if ptero.x \<= eventX then -- to the right  
  
 if (ptero.x ) \>= halfW then -- greater or equal than half screen size  
  
 if (displayGroup.x \>= -(maxX - screenW)) then --if less than the screen size of the max bg size from the right edge. bg is negative  
 displayGroup.x=displayGroup.x - distanceTravelled -- subtract bg and the event instead of adding values to ptero.x  
 eventX =eventX-distanceTravelled  
 else  
 ptero.x = ptero.x + distanceTravelled  
 end  
 else  
  
 ptero.x = ptero.x + distanceTravelled  
  
 end  
 else  
 -- done, so remove listener  
 Runtime:removeEventListener("enterFrame", moveNextX )  
 eventRect.isVisible=true  
 end  
 else  
 hasCollidedRight=false  
 ptero.x = ptero.x - distanceTravelled -- bump/move to the left a bit  
 Runtime:removeEventListener("enterFrame", moveNextX )  
 eventRect.isVisible=true  
 end  
 end  
  
 local function moveBackX()  
 if not hasCollidedLeft then  
  
 if ptero.x \>= eventX then -- to the left  
  
 if (ptero.x) \<= halfW then -- less or equal than half screen size  
  
 if (displayGroup.x \<= minX) then --if less than the screen size of the max bg size from the left edge. bg is negative  
 displayGroup.x=displayGroup.x + distanceTravelled -- add bg and the event instead of subtracting values to ptero.x  
 eventX =eventX+distanceTravelled  
 else  
 ptero.x = ptero.x - distanceTravelled  
 end  
 else  
  
 ptero.x = ptero.x - distanceTravelled  
 end  
 else  
 -- done, so remove listener  
 Runtime:removeEventListener("enterFrame", moveBackX )  
 eventRect.isVisible=true  
 end  
  
 else  
 hasCollidedLeft=false  
 ptero.x = ptero.x + distanceTravelled -- bump/move to the right a bit  
 Runtime:removeEventListener("enterFrame", moveBackX )  
 eventRect.isVisible=true  
 end  
 end  
  
 local function moveNextY()  
 if not hasCollidedBottom then  
  
 if ptero.y \<= eventY then -- to the bottom  
  
 if (ptero.y ) \>= halfH then -- greater or equal than half screen size.  
  
 if (displayGroup.y \>= -(maxY - screenH)) then --if less than the screen size of the max bg size from the bottom edge. bg is negative  
 displayGroup.y=displayGroup.y - distanceTravelled -- subtract bg and the event instead of adding values to ptero.y  
 eventY =eventY-distanceTravelled  
 else  
 ptero.y = ptero.y + distanceTravelled  
 end  
 else  
  
 ptero.y = ptero.y + distanceTravelled  
 end  
 else  
 -- done, so remove listener  
 Runtime:removeEventListener("enterFrame", moveNextY )  
 eventRect.isVisible=true  
 end  
  
 else  
 hasCollidedBottom=false  
 ptero.y = ptero.y - distanceTravelled -- bump/move to the up a bit  
 Runtime:removeEventListener("enterFrame", moveNextY )  
 eventRect.isVisible=true  
 end  
 end  
  
 local function moveBackY()  
 if not hasCollidedTop then  
  
 if ptero.y \>= eventY then -- to the top  
  
 if (ptero.y) \<= halfH then -- less or equal than half screen size  
  
 if (displayGroup.y \<= minY) then --if less than the screen size of the max bg size from the top edge. bg is negative  
 displayGroup.y=displayGroup.y + distanceTravelled -- add bg and the event instead of subtracting values to ptero.y  
 eventY =eventY+distanceTravelled  
 else  
 ptero.y = ptero.y - distanceTravelled  
 end  
 else  
  
 ptero.y = ptero.y - distanceTravelled  
 end  
 else  
 -- done, so remove listener  
 Runtime:removeEventListener("enterFrame", moveBackY )  
 eventRect.isVisible=true  
 end  
  
 else  
 hasCollidedTop=false  
 ptero.y = ptero.y + distanceTravelled -- bump/move to the down a bit  
 Runtime:removeEventListener("enterFrame", moveBackY )  
 eventRect.isVisible=true  
 end  
 end  
  
 eventRect.isVisible=false  
  
 if ptero.x \< eventX then  
 Runtime:removeEventListener("enterFrame", moveNextX )  
 ptero.xScale=1 -- the beak of ptero gives an indication of the left-right direction it is moving  
 Runtime:addEventListener("enterFrame", moveNextX )  
  
 else  
 Runtime:removeEventListener("enterFrame", moveBackX )  
 ptero.xScale=-1  
 Runtime:addEventListener("enterFrame", moveBackX)  
 end  
  
 if ptero.y \< eventY then  
 Runtime:removeEventListener("enterFrame", moveNextY )  
 ptero.yScale=-1 -- the right-side up/upside-down of ptero gives an indication of the up-down direction it is moving  
 Runtime:addEventListener("enterFrame", moveNextY )  
  
 else  
 Runtime:removeEventListener("enterFrame", moveBackY )  
 ptero.yScale=1  
 Runtime:addEventListener("enterFrame", moveBackY)  
 end  
 end  
  
 local function onTapEventRect(event)  
 onPteroFly(event.x,event.y)  
 tapEventX = event.x  
 tapEventY = event.y  
  
 end  
  
 local function onLocalCollision( self, event )  
  
 if ( event.phase == "began" ) then  
 if (self.myName == "ptero") then  
--~ self.isBodyActive = false  
 if ptero.x \< tapEventX then  
 hasCollidedRight = true  
 print( self.myName .. ": collision going right" )  
 else  
 hasCollidedLeft = true  
 print( self.myName .. ": collision going left" )  
 end  
  
 if ptero.y \< tapEventY then  
 hasCollidedBottom = true  
  
 print( self.myName .. ": collision going down" )  
 else  
 hasCollidedTop = true  
 print( self.myName .. ": collision going up" )  
 end  
  
 print( "\n Collision hasCollidedRight: " .. tostring(hasCollidedRight))  
 print( " Collision hasCollidedLeft: " .. tostring(hasCollidedLeft))  
 print( " Collision hasCollidedBottom: " .. tostring(hasCollidedBottom))  
 print( " Collision hasCollidedTop: " .. tostring(hasCollidedTop))  
  
 print( "\n Collision pterox: " .. ptero.x .. " tapEventX: " .. tapEventX)  
 print( " Collision pteroy: " .. ptero.y .. " tapEventY: " .. tapEventY)  
  
 end  
  
 elseif ( event.phase == "ended" ) then  
  
 end  
 return true  
  
 end  
  
 eventRect:addEventListener( "tap",onTapEventRect )  
 ptero.collision = onLocalCollision  
 ptero:addEventListener( "collision",ptero)  
  
end  
  

Sorry for the long post.

I’m using the “old” sprite api demo from here: http://leetr.com/posts/93/corona-sdk-tutorial-animated-sprite/

Thanks for taking the time bro! Looking forward to know what’s wrong! [import]uid: 179529 topic_id: 31601 reply_id: 128313[/import]

Hi @merc,
Can you try to parse out at least some of your code and include it here within code tags? The screenshots would be helpful if you showed how you’re setting up your physics bodies, collision handler, etc… or at least, provide a detailed description on what’s going wrong.

Thanks!
Brent
[import]uid: 9747 topic_id: 31601 reply_id: 128303[/import]

Relevant code of the trial project:

-----------------------------------------------------------------------------------------  
--  
-- level1.lua  
--  
-----------------------------------------------------------------------------------------  
  
local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
local sprite = require ("sprite")  
  
-- include Corona's "physics" library  
local physics = require ("physics")  
physics.start()  
physics.pause()  
physics.setGravity( 0, 0 ) -- no gravity vector  
physics.setScale( 5 )  
physics.setDrawMode( "hybrid" )  
  
--------------------------------------------  
  
-- forward declarations and other locals  
local screenW, screenH, halfW, halfH = display.contentWidth, display.contentHeight, display.contentWidth\*0.5, display.contentHeight\*0.5  
local bg  
-----------------------------------------------------------------------------------------  
-- 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  
 bg = display.newImage("bg2.jpg",0,0)  
 bg:setReferencePoint( display.TopLeftReferencePoint )  
 bg.x, bg.y = 0, 0  
 local displayGroup = display.newGroup()  
 displayGroup:insert( bg )  
 displayGroup:setReferencePoint( display.TopLeftReferencePoint )  
 displayGroup.x, displayGroup.y = bg.x, bg.y  
  
 local activeGroup = display.newGroup()  
  
 local distanceTravelled = 4  
 local minX = 0  
 local maxX = bg.width  
 local minY = 0  
 local maxY = bg.height  
  
 local hasCollidedRight = false  
 local hasCollidedLeft = false  
 local hasCollidedBottom = false  
 local hasCollidedTop = false  
  
 local tapEventX = 0  
 local tapEventY = 0  
  
 -- filepath, width, height  
 local pteroSheet = sprite.newSpriteSheet("ptero.png",104, 50)  
 -- spritesheet, starting frame, number of frames  
 local pteroSet = sprite.newSpriteSet(pteroSheet, 1, 6 )  
 --spriteSet,sprite name, starting frame,# of frames,time in millis for a full animation,-2 for loop indefinite  
 sprite.add( pteroSet, "ptero", 1, 6, 300, -2 )  
 local ptero = sprite.newSprite( pteroSet )  
 local pteroShape = {-30,-30, 30,-30, 30,30, -30,30}  
 physics.addBody( ptero, {density=6.0, friction=0.3, bounce=0.0, shape=pteroShape} )  
  
 ptero.myName = "ptero"  
 ptero.x = screenW \* 0.5  
 ptero.y = screenH \* 0.25  
  
 ptero:prepare( "ptero" )  
 ptero:play()  
 activeGroup:insert(ptero)  
  
 local eventRect = display.newRect( 0, 0, bg.width, bg.height \*0.95)  
 --eventRect:setReferencePoint( display.TopLeftReferencePoint )  
 eventRect.strokeWidth = 2  
 eventRect.alpha = 0.01  
 eventRect:setStrokeColor(180,0,200)  
 displayGroup:insert( eventRect)  
  
 -- create a grass object and add physics (with custom shape)  
 local grass = display.newImageRect( "grass.png", screenW \*0.25, 82 )  
 --grass:setReferencePoint( display.TopLeftReferencePoint )  
 grass.x, grass.y = halfH, halfW  
 physics.addBody( grass, "static", {density=6.0, friction=0.3 } )  
 displayGroup:insert( grass )  
 -- all display objects must be inserted into group  
 group:insert( displayGroup )  
 group:insert( activeGroup)  
  
 local function onPteroFly(eventX,eventY)  
 -- unless when on the edges of the bg, ptero stays on the middle of the screen while sidescrolling  
  
 local function moveNextX()  
 if not hasCollidedRight then  
  
 if ptero.x \<= eventX then -- to the right  
  
 if (ptero.x ) \>= halfW then -- greater or equal than half screen size  
  
 if (displayGroup.x \>= -(maxX - screenW)) then --if less than the screen size of the max bg size from the right edge. bg is negative  
 displayGroup.x=displayGroup.x - distanceTravelled -- subtract bg and the event instead of adding values to ptero.x  
 eventX =eventX-distanceTravelled  
 else  
 ptero.x = ptero.x + distanceTravelled  
 end  
 else  
  
 ptero.x = ptero.x + distanceTravelled  
  
 end  
 else  
 -- done, so remove listener  
 Runtime:removeEventListener("enterFrame", moveNextX )  
 eventRect.isVisible=true  
 end  
 else  
 hasCollidedRight=false  
 ptero.x = ptero.x - distanceTravelled -- bump/move to the left a bit  
 Runtime:removeEventListener("enterFrame", moveNextX )  
 eventRect.isVisible=true  
 end  
 end  
  
 local function moveBackX()  
 if not hasCollidedLeft then  
  
 if ptero.x \>= eventX then -- to the left  
  
 if (ptero.x) \<= halfW then -- less or equal than half screen size  
  
 if (displayGroup.x \<= minX) then --if less than the screen size of the max bg size from the left edge. bg is negative  
 displayGroup.x=displayGroup.x + distanceTravelled -- add bg and the event instead of subtracting values to ptero.x  
 eventX =eventX+distanceTravelled  
 else  
 ptero.x = ptero.x - distanceTravelled  
 end  
 else  
  
 ptero.x = ptero.x - distanceTravelled  
 end  
 else  
 -- done, so remove listener  
 Runtime:removeEventListener("enterFrame", moveBackX )  
 eventRect.isVisible=true  
 end  
  
 else  
 hasCollidedLeft=false  
 ptero.x = ptero.x + distanceTravelled -- bump/move to the right a bit  
 Runtime:removeEventListener("enterFrame", moveBackX )  
 eventRect.isVisible=true  
 end  
 end  
  
 local function moveNextY()  
 if not hasCollidedBottom then  
  
 if ptero.y \<= eventY then -- to the bottom  
  
 if (ptero.y ) \>= halfH then -- greater or equal than half screen size.  
  
 if (displayGroup.y \>= -(maxY - screenH)) then --if less than the screen size of the max bg size from the bottom edge. bg is negative  
 displayGroup.y=displayGroup.y - distanceTravelled -- subtract bg and the event instead of adding values to ptero.y  
 eventY =eventY-distanceTravelled  
 else  
 ptero.y = ptero.y + distanceTravelled  
 end  
 else  
  
 ptero.y = ptero.y + distanceTravelled  
 end  
 else  
 -- done, so remove listener  
 Runtime:removeEventListener("enterFrame", moveNextY )  
 eventRect.isVisible=true  
 end  
  
 else  
 hasCollidedBottom=false  
 ptero.y = ptero.y - distanceTravelled -- bump/move to the up a bit  
 Runtime:removeEventListener("enterFrame", moveNextY )  
 eventRect.isVisible=true  
 end  
 end  
  
 local function moveBackY()  
 if not hasCollidedTop then  
  
 if ptero.y \>= eventY then -- to the top  
  
 if (ptero.y) \<= halfH then -- less or equal than half screen size  
  
 if (displayGroup.y \<= minY) then --if less than the screen size of the max bg size from the top edge. bg is negative  
 displayGroup.y=displayGroup.y + distanceTravelled -- add bg and the event instead of subtracting values to ptero.y  
 eventY =eventY+distanceTravelled  
 else  
 ptero.y = ptero.y - distanceTravelled  
 end  
 else  
  
 ptero.y = ptero.y - distanceTravelled  
 end  
 else  
 -- done, so remove listener  
 Runtime:removeEventListener("enterFrame", moveBackY )  
 eventRect.isVisible=true  
 end  
  
 else  
 hasCollidedTop=false  
 ptero.y = ptero.y + distanceTravelled -- bump/move to the down a bit  
 Runtime:removeEventListener("enterFrame", moveBackY )  
 eventRect.isVisible=true  
 end  
 end  
  
 eventRect.isVisible=false  
  
 if ptero.x \< eventX then  
 Runtime:removeEventListener("enterFrame", moveNextX )  
 ptero.xScale=1 -- the beak of ptero gives an indication of the left-right direction it is moving  
 Runtime:addEventListener("enterFrame", moveNextX )  
  
 else  
 Runtime:removeEventListener("enterFrame", moveBackX )  
 ptero.xScale=-1  
 Runtime:addEventListener("enterFrame", moveBackX)  
 end  
  
 if ptero.y \< eventY then  
 Runtime:removeEventListener("enterFrame", moveNextY )  
 ptero.yScale=-1 -- the right-side up/upside-down of ptero gives an indication of the up-down direction it is moving  
 Runtime:addEventListener("enterFrame", moveNextY )  
  
 else  
 Runtime:removeEventListener("enterFrame", moveBackY )  
 ptero.yScale=1  
 Runtime:addEventListener("enterFrame", moveBackY)  
 end  
 end  
  
 local function onTapEventRect(event)  
 onPteroFly(event.x,event.y)  
 tapEventX = event.x  
 tapEventY = event.y  
  
 end  
  
 local function onLocalCollision( self, event )  
  
 if ( event.phase == "began" ) then  
 if (self.myName == "ptero") then  
--~ self.isBodyActive = false  
 if ptero.x \< tapEventX then  
 hasCollidedRight = true  
 print( self.myName .. ": collision going right" )  
 else  
 hasCollidedLeft = true  
 print( self.myName .. ": collision going left" )  
 end  
  
 if ptero.y \< tapEventY then  
 hasCollidedBottom = true  
  
 print( self.myName .. ": collision going down" )  
 else  
 hasCollidedTop = true  
 print( self.myName .. ": collision going up" )  
 end  
  
 print( "\n Collision hasCollidedRight: " .. tostring(hasCollidedRight))  
 print( " Collision hasCollidedLeft: " .. tostring(hasCollidedLeft))  
 print( " Collision hasCollidedBottom: " .. tostring(hasCollidedBottom))  
 print( " Collision hasCollidedTop: " .. tostring(hasCollidedTop))  
  
 print( "\n Collision pterox: " .. ptero.x .. " tapEventX: " .. tapEventX)  
 print( " Collision pteroy: " .. ptero.y .. " tapEventY: " .. tapEventY)  
  
 end  
  
 elseif ( event.phase == "ended" ) then  
  
 end  
 return true  
  
 end  
  
 eventRect:addEventListener( "tap",onTapEventRect )  
 ptero.collision = onLocalCollision  
 ptero:addEventListener( "collision",ptero)  
  
end  
  

Sorry for the long post.

I’m using the “old” sprite api demo from here: http://leetr.com/posts/93/corona-sdk-tutorial-animated-sprite/

Thanks for taking the time bro! Looking forward to know what’s wrong! [import]uid: 179529 topic_id: 31601 reply_id: 128313[/import]

So . . . . umm . . . . any suggestions? Are there any errors i need to correct? [import]uid: 179529 topic_id: 31601 reply_id: 129222[/import]

Hi @merc,
Sorry for the delay, I’m just swamped at the moment…

I haven’t dissected your entire code, but it generally looks OK. What I notice at first glance, however, is all of those “flags” … hasCollidedRight, hasCollidedLeft, etc. … have you checked and re-checked and RE-re-checked that all of those flags are being managed correctly (turned on and off when necessary, and every time)? I use flags alot to determine the “state” of something (many developers do, of course) and sometimes the most perplexing error is that I forgot to set a flag to T or F somewhere.

If you could just isolate this issue down to the bare bones of the collision event, that would help determine if it’s a collision bug or something else. What I mean is, set up just two objects and the listener and send your “ptero” flying into it, and see if you get a response… no flag switching, no back-and-forth Runtime listeners being turned on and off, etc… just the collision code.

Thanks for your patience!
Brent
[import]uid: 9747 topic_id: 31601 reply_id: 129241[/import]

So . . . . umm . . . . any suggestions? Are there any errors i need to correct? [import]uid: 179529 topic_id: 31601 reply_id: 129222[/import]

Hi @merc,
Sorry for the delay, I’m just swamped at the moment…

I haven’t dissected your entire code, but it generally looks OK. What I notice at first glance, however, is all of those “flags” … hasCollidedRight, hasCollidedLeft, etc. … have you checked and re-checked and RE-re-checked that all of those flags are being managed correctly (turned on and off when necessary, and every time)? I use flags alot to determine the “state” of something (many developers do, of course) and sometimes the most perplexing error is that I forgot to set a flag to T or F somewhere.

If you could just isolate this issue down to the bare bones of the collision event, that would help determine if it’s a collision bug or something else. What I mean is, set up just two objects and the listener and send your “ptero” flying into it, and see if you get a response… no flag switching, no back-and-forth Runtime listeners being turned on and off, etc… just the collision code.

Thanks for your patience!
Brent
[import]uid: 9747 topic_id: 31601 reply_id: 129241[/import]

Hi Brent,

Thanks for the advice. I removed all the “hasCollided…” flags, redundant add/remove EventListeners and removed all the contents of the localCollisionEvent except for a single print line. And so far . . . . both collision errors STILL occurs.

Btw, i’m running this on Windows XP using Corona Simulator 2012.894. Maybe this info would help. I have no access to the current daily builds - yet.

-----------------------------------------------------------------------------------------  
--  
-- level1.lua  
--  
-----------------------------------------------------------------------------------------  
  
local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
local sprite = require ("sprite")  
  
-- include Corona's "physics" library  
local physics = require ("physics")  
physics.start()  
physics.pause()  
physics.setGravity( 0, 0 ) -- no gravity vector  
physics.setScale( 5 )  
physics.setDrawMode( "hybrid" )  
  
--------------------------------------------  
  
-- forward declarations and other locals  
local screenW, screenH, halfW, halfH = display.contentWidth, display.contentHeight, display.contentWidth\*0.5, display.contentHeight\*0.5  
local bg  
-----------------------------------------------------------------------------------------  
-- 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  
 bg = display.newImage("bg2.jpg",0,0)  
 bg:setReferencePoint( display.TopLeftReferencePoint )  
 bg.x, bg.y = 0, 0  
  
 local displayGroup = display.newGroup()  
 displayGroup:insert( bg )  
 displayGroup:setReferencePoint( display.TopLeftReferencePoint )  
 displayGroup.x, displayGroup.y = bg.x, bg.y  
  
 local activeGroup = display.newGroup()  
  
 local distanceTravelled = 4  
 local minX = 0  
 local maxX = bg.width  
 local minY = 0  
 local maxY = bg.height  
  
 -- filepath, width, height  
 local pteroSheet = sprite.newSpriteSheet("ptero.png",104, 50)  
 -- spritesheet, starting frame, number of frames  
 local pteroSet = sprite.newSpriteSet(pteroSheet, 1, 6 )  
 --spriteSet,sprite name, starting frame,# of frames,time in millis for a full animation,-2 for loop indefinite  
 sprite.add( pteroSet, "ptero", 1, 6, 300, -2 )  
 local ptero = sprite.newSprite( pteroSet )  
 local pteroShape = {-30,-30, 30,-30, 30,30, -30,30}  
 physics.addBody( ptero, {density=6.0, friction=0.3, bounce=0.0, shape=pteroShape} )  
  
 ptero.myName = "ptero"  
 ptero.x = screenW \* 0.5  
 ptero.y = screenH \* 0.25  
  
 ptero:prepare( "ptero" )  
 ptero:play()  
 activeGroup:insert(ptero)  
  
 local eventRect = display.newRect( 0, 0, bg.width, bg.height \*0.95)  
 eventRect.strokeWidth = 2  
 eventRect.alpha = 0.01  
 eventRect:setStrokeColor(180,0,200)  
 displayGroup:insert( eventRect)  
  
 -- create a grass object and add physics (with custom shape)  
 local grass = display.newImageRect( "grass.png", screenW \*0.25, 82 )  
 grass.x, grass.y = halfH, halfW  
 physics.addBody( grass, "static", {density=3.0, friction=0.3 } )  
 displayGroup:insert( grass )  
  
 -- all display objects must be inserted into group  
 group:insert( displayGroup )  
 group:insert( activeGroup)  
  
 local function onPteroFly(eventX,eventY)  
 -- unless when on the edges of the bg, ptero stays on the middle of the screen while sidescrolling  
  
 local function moveNextX() -- going right  
 --print("NextX eventX: " .. eventX .. " pteroX:" .. ptero.x .. " displayGroup.x: " .. displayGroup.x .. " maxX: " .. maxX)  
 if ptero.x \<= eventX then   
 if (ptero.x ) \>= halfW then -- greater or equal than half screen width size  
 if (displayGroup.x \>= -(maxX - screenW)) then --if less than the screen size of the max bg size from the right edge.  
 displayGroup.x=displayGroup.x - distanceTravelled -- subtract bg and the event instead of adding values to ptero.x  
 eventX =eventX-distanceTravelled  
 else  
 ptero.x = ptero.x + distanceTravelled  
 end  
 else  
 ptero.x = ptero.x + distanceTravelled  
 end  
 else  
 -- done, so remove listener  
 Runtime:removeEventListener("enterFrame", moveNextX )  
 eventRect.isVisible=true  
 end  
 end  
  
 local function moveBackX() -- going left  
 --print("BackX eventX: " .. eventX .. " pteroX:" .. ptero.x .. " displayGroup.x: " .. displayGroup.x .. " minX: " .. minX)  
 if ptero.x \>= eventX then   
 if (ptero.x) \<= halfW then -- less or equal than half screen width size  
 if (displayGroup.x \<= minX) then --if less than the screen size of the max bg size from the left edge.  
 displayGroup.x=displayGroup.x + distanceTravelled -- add bg and the event instead of subtracting values to ptero.x  
 eventX =eventX+distanceTravelled  
 else  
 ptero.x = ptero.x - distanceTravelled  
 end  
 else  
 ptero.x = ptero.x - distanceTravelled  
 end  
 else  
 -- done, so remove listener  
 Runtime:removeEventListener("enterFrame", moveBackX )  
 eventRect.isVisible=true  
 end  
 end  
  
 local function moveNextY() -- going down  
 --print("NextY eventY: " .. eventY .." pteroY:" .. ptero.y .. " displayGroup.y: " .. displayGroup.y .. " maxY: " .. maxY)  
 if ptero.y \<= eventY then  
 if (ptero.y ) \>= halfH then -- greater or equal than half screen height size.  
 if (displayGroup.y \>= -(maxY - screenH)) then --if less than the screen size of the max bg size from the bottom edge.  
 displayGroup.y=displayGroup.y - distanceTravelled -- subtract bg and the eventY instead of adding values to ptero.y  
 eventY =eventY-distanceTravelled  
 else  
 ptero.y = ptero.y + distanceTravelled  
 end  
 else  
 ptero.y = ptero.y + distanceTravelled  
 end  
 else  
 -- done, so remove listener  
 Runtime:removeEventListener("enterFrame", moveNextY )  
 eventRect.isVisible=true  
 end  
 end  
  
 local function moveBackY() -- going up  
 --print("BackY eventY: " .. eventY .. " pteroY:" .. ptero.y .. " displayGroup.y: " .. displayGroup.y .. " minY: " .. minY)  
 if ptero.y \>= eventY then   
 if (ptero.y) \<= halfH then -- less or equal than half screen height size  
 if (displayGroup.y \<= minY) then --if less than the screen size of the max bg size from the top edge.  
 displayGroup.y=displayGroup.y + distanceTravelled -- add bg and the eventY instead of subtracting values to ptero.y  
 eventY =eventY+distanceTravelled  
 else  
 ptero.y = ptero.y - distanceTravelled  
 end  
 else  
 ptero.y = ptero.y - distanceTravelled  
 end  
 else  
 -- done, so remove listener  
 Runtime:removeEventListener("enterFrame", moveBackY )  
 eventRect.isVisible=true  
 end  
 end  
 --Determines which local function to attach to the Runtime Event Listener  
 eventRect.isVisible=false  
 if ptero.x \< eventX then  
 ptero.xScale=1 -- the beak of ptero gives an indication of the left-right direction it is moving  
 Runtime:addEventListener("enterFrame", moveNextX )  
 else  
 ptero.xScale=-1  
 Runtime:addEventListener("enterFrame", moveBackX)  
 end  
  
 if ptero.y \< eventY then  
 ptero.yScale=-1 -- the right-side up/upside-down of ptero gives an indication of the up-down direction it is moving  
 Runtime:addEventListener("enterFrame", moveNextY )  
 else  
 ptero.yScale=1  
 Runtime:addEventListener("enterFrame", moveBackY)  
 end  
 end  
  
 local function onTapEventRect(event)  
 onPteroFly(event.x,event.y)  
 end  
  
 local function onLocalCollision( self, event )  
 print( "\n Collision Detected \n")  
 end  
  
 eventRect:addEventListener( "tap",onTapEventRect )  
 ptero.collision = onLocalCollision  
 ptero:addEventListener( "collision",ptero)  
  
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  
  

Thanks again!,
merc
[import]uid: 179529 topic_id: 31601 reply_id: 129325[/import]

Hi Brent,

Thanks for the advice. I removed all the “hasCollided…” flags, redundant add/remove EventListeners and removed all the contents of the localCollisionEvent except for a single print line. And so far . . . . both collision errors STILL occurs.

Btw, i’m running this on Windows XP using Corona Simulator 2012.894. Maybe this info would help. I have no access to the current daily builds - yet.

-----------------------------------------------------------------------------------------  
--  
-- level1.lua  
--  
-----------------------------------------------------------------------------------------  
  
local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
local sprite = require ("sprite")  
  
-- include Corona's "physics" library  
local physics = require ("physics")  
physics.start()  
physics.pause()  
physics.setGravity( 0, 0 ) -- no gravity vector  
physics.setScale( 5 )  
physics.setDrawMode( "hybrid" )  
  
--------------------------------------------  
  
-- forward declarations and other locals  
local screenW, screenH, halfW, halfH = display.contentWidth, display.contentHeight, display.contentWidth\*0.5, display.contentHeight\*0.5  
local bg  
-----------------------------------------------------------------------------------------  
-- 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  
 bg = display.newImage("bg2.jpg",0,0)  
 bg:setReferencePoint( display.TopLeftReferencePoint )  
 bg.x, bg.y = 0, 0  
  
 local displayGroup = display.newGroup()  
 displayGroup:insert( bg )  
 displayGroup:setReferencePoint( display.TopLeftReferencePoint )  
 displayGroup.x, displayGroup.y = bg.x, bg.y  
  
 local activeGroup = display.newGroup()  
  
 local distanceTravelled = 4  
 local minX = 0  
 local maxX = bg.width  
 local minY = 0  
 local maxY = bg.height  
  
 -- filepath, width, height  
 local pteroSheet = sprite.newSpriteSheet("ptero.png",104, 50)  
 -- spritesheet, starting frame, number of frames  
 local pteroSet = sprite.newSpriteSet(pteroSheet, 1, 6 )  
 --spriteSet,sprite name, starting frame,# of frames,time in millis for a full animation,-2 for loop indefinite  
 sprite.add( pteroSet, "ptero", 1, 6, 300, -2 )  
 local ptero = sprite.newSprite( pteroSet )  
 local pteroShape = {-30,-30, 30,-30, 30,30, -30,30}  
 physics.addBody( ptero, {density=6.0, friction=0.3, bounce=0.0, shape=pteroShape} )  
  
 ptero.myName = "ptero"  
 ptero.x = screenW \* 0.5  
 ptero.y = screenH \* 0.25  
  
 ptero:prepare( "ptero" )  
 ptero:play()  
 activeGroup:insert(ptero)  
  
 local eventRect = display.newRect( 0, 0, bg.width, bg.height \*0.95)  
 eventRect.strokeWidth = 2  
 eventRect.alpha = 0.01  
 eventRect:setStrokeColor(180,0,200)  
 displayGroup:insert( eventRect)  
  
 -- create a grass object and add physics (with custom shape)  
 local grass = display.newImageRect( "grass.png", screenW \*0.25, 82 )  
 grass.x, grass.y = halfH, halfW  
 physics.addBody( grass, "static", {density=3.0, friction=0.3 } )  
 displayGroup:insert( grass )  
  
 -- all display objects must be inserted into group  
 group:insert( displayGroup )  
 group:insert( activeGroup)  
  
 local function onPteroFly(eventX,eventY)  
 -- unless when on the edges of the bg, ptero stays on the middle of the screen while sidescrolling  
  
 local function moveNextX() -- going right  
 --print("NextX eventX: " .. eventX .. " pteroX:" .. ptero.x .. " displayGroup.x: " .. displayGroup.x .. " maxX: " .. maxX)  
 if ptero.x \<= eventX then   
 if (ptero.x ) \>= halfW then -- greater or equal than half screen width size  
 if (displayGroup.x \>= -(maxX - screenW)) then --if less than the screen size of the max bg size from the right edge.  
 displayGroup.x=displayGroup.x - distanceTravelled -- subtract bg and the event instead of adding values to ptero.x  
 eventX =eventX-distanceTravelled  
 else  
 ptero.x = ptero.x + distanceTravelled  
 end  
 else  
 ptero.x = ptero.x + distanceTravelled  
 end  
 else  
 -- done, so remove listener  
 Runtime:removeEventListener("enterFrame", moveNextX )  
 eventRect.isVisible=true  
 end  
 end  
  
 local function moveBackX() -- going left  
 --print("BackX eventX: " .. eventX .. " pteroX:" .. ptero.x .. " displayGroup.x: " .. displayGroup.x .. " minX: " .. minX)  
 if ptero.x \>= eventX then   
 if (ptero.x) \<= halfW then -- less or equal than half screen width size  
 if (displayGroup.x \<= minX) then --if less than the screen size of the max bg size from the left edge.  
 displayGroup.x=displayGroup.x + distanceTravelled -- add bg and the event instead of subtracting values to ptero.x  
 eventX =eventX+distanceTravelled  
 else  
 ptero.x = ptero.x - distanceTravelled  
 end  
 else  
 ptero.x = ptero.x - distanceTravelled  
 end  
 else  
 -- done, so remove listener  
 Runtime:removeEventListener("enterFrame", moveBackX )  
 eventRect.isVisible=true  
 end  
 end  
  
 local function moveNextY() -- going down  
 --print("NextY eventY: " .. eventY .." pteroY:" .. ptero.y .. " displayGroup.y: " .. displayGroup.y .. " maxY: " .. maxY)  
 if ptero.y \<= eventY then  
 if (ptero.y ) \>= halfH then -- greater or equal than half screen height size.  
 if (displayGroup.y \>= -(maxY - screenH)) then --if less than the screen size of the max bg size from the bottom edge.  
 displayGroup.y=displayGroup.y - distanceTravelled -- subtract bg and the eventY instead of adding values to ptero.y  
 eventY =eventY-distanceTravelled  
 else  
 ptero.y = ptero.y + distanceTravelled  
 end  
 else  
 ptero.y = ptero.y + distanceTravelled  
 end  
 else  
 -- done, so remove listener  
 Runtime:removeEventListener("enterFrame", moveNextY )  
 eventRect.isVisible=true  
 end  
 end  
  
 local function moveBackY() -- going up  
 --print("BackY eventY: " .. eventY .. " pteroY:" .. ptero.y .. " displayGroup.y: " .. displayGroup.y .. " minY: " .. minY)  
 if ptero.y \>= eventY then   
 if (ptero.y) \<= halfH then -- less or equal than half screen height size  
 if (displayGroup.y \<= minY) then --if less than the screen size of the max bg size from the top edge.  
 displayGroup.y=displayGroup.y + distanceTravelled -- add bg and the eventY instead of subtracting values to ptero.y  
 eventY =eventY+distanceTravelled  
 else  
 ptero.y = ptero.y - distanceTravelled  
 end  
 else  
 ptero.y = ptero.y - distanceTravelled  
 end  
 else  
 -- done, so remove listener  
 Runtime:removeEventListener("enterFrame", moveBackY )  
 eventRect.isVisible=true  
 end  
 end  
 --Determines which local function to attach to the Runtime Event Listener  
 eventRect.isVisible=false  
 if ptero.x \< eventX then  
 ptero.xScale=1 -- the beak of ptero gives an indication of the left-right direction it is moving  
 Runtime:addEventListener("enterFrame", moveNextX )  
 else  
 ptero.xScale=-1  
 Runtime:addEventListener("enterFrame", moveBackX)  
 end  
  
 if ptero.y \< eventY then  
 ptero.yScale=-1 -- the right-side up/upside-down of ptero gives an indication of the up-down direction it is moving  
 Runtime:addEventListener("enterFrame", moveNextY )  
 else  
 ptero.yScale=1  
 Runtime:addEventListener("enterFrame", moveBackY)  
 end  
 end  
  
 local function onTapEventRect(event)  
 onPteroFly(event.x,event.y)  
 end  
  
 local function onLocalCollision( self, event )  
 print( "\n Collision Detected \n")  
 end  
  
 eventRect:addEventListener( "tap",onTapEventRect )  
 ptero.collision = onLocalCollision  
 ptero:addEventListener( "collision",ptero)  
  
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  
  

Thanks again!,
merc
[import]uid: 179529 topic_id: 31601 reply_id: 129325[/import]

Can you zip up your project (all Lua files and associated images or sprite sheets) and e-mail it to me? I can try it on my computer and see if it’s a platform (Mac vs. Windows) issue, or see what else might be happening.

brent (at) ignisdesign (dot) com

Thanks!
[import]uid: 9747 topic_id: 31601 reply_id: 129541[/import]

Can you zip up your project (all Lua files and associated images or sprite sheets) and e-mail it to me? I can try it on my computer and see if it’s a platform (Mac vs. Windows) issue, or see what else might be happening.

brent (at) ignisdesign (dot) com

Thanks!
[import]uid: 9747 topic_id: 31601 reply_id: 129541[/import]

Brent, I believe the code on the API doc page does not work as advertised…
http://docs.coronalabs.com/api/event/collision/x.html [import]uid: 8271 topic_id: 31601 reply_id: 129632[/import]