Problem on getting postCollision event.

Hi Coronaville,

I have a problem regarding getting postCollision event. I register my preCollision and postCollision this way:

 function foodObject:preCollision(event) -- This enables us to disable collision for top border so we can make it one sided collider -- As in food can come into the screen and go over the top border but after it's over top -- top border, it cannot go through it from bottom and will get deleted. -- print("Just got in FoodPreCollisionHandler") if event.contact then if (event.other.myType == "TopWall") then vx, vy = self:getLinearVelocity() if vy \> 0 then -- Only disable the collision if we are moving upward event.contact.isEnabled = false --disable this specific collision! print("disabling collision") end end else print("IMPORTANT: event does NOT have a 'contact' variable with it!") end end ---------------------------------------------------------------------- function foodObject:postCollision( event ) print( "Remove listeners") self:removeEventListener( "preCollision" ) self:removeEventListener( "postCollision" ) end ---------------------------------------------------------------------- foodObject:addEventListener( "preCollision") foodObject:addEventListener( "postCollision") 

I also have a collision handler but since it’s a lot of code, I didn’t put it here.

Strange thing is if I comment the “event.contact.isEnabled = false” line, I get the postCollision event but if I don’t, I won’t get that event!

I tested the sample preCollision and postCollision code and doing the same thing on that code won’t affect the postCollision event:

local physics = require("physics") physics.start() local platform1 = display.newRect( 20, 200, 280, 15 ) platform1.myName = "platform1" physics.addBody(platform1, "static") local platform2 = display.newRect( 20, 320, 280, 15 ) platform2.myName = "platform2" physics.addBody(platform2, "static") local box = display.newRect( 20, 20, 50, 50 ) box:setFillColor(255,255,0,255) physics.addBody(box) local box2 = display.newRect( 100, 20, 50, 50 ) box2:setFillColor(255,0,0,255) physics.addBody(box2) function box:preCollision( event ) local platform = event.other if platform.myName == "platform1" then -- Let box pass through platform 1 event.contact.isEnabled = false print( "preCollision", platform.myName ) end end box:addEventListener( "preCollision" ) function box:postCollision( event ) print( "Remove listeners") self:removeEventListener( "preCollision" ) self:removeEventListener( "postCollision" ) end box:addEventListener( "postCollision" )

Any ideas?

Thanks in advance.

Hi Aidin,

I’m pretty sure (like 99%) that if you override the collision with an “event.contact” (false), it effectively voids the entire collision, even the postCollision.

By your description, it looks like you want items to cross a boundary from one side only (a completely valid use of the event.contact) and then register some event after it crosses that boundary. If so, I might suggest using both a preCollision and normal collision handler… if you saw my demo project on Physics Contact with the bouncing/jumping balls, it used both types.

If I understand your scenario, the food objects should fall from the top (off the screen?), hit a boundary, pass through the boundary, and then interact with everything else? If so, could you just use a standard collision listener, have the food begin as a “sensor” type, then on the “ended” phase of it passing through the top boundary, change it back to a normal non-sensor object?

Brent

Thanks man, your help finally led me to solve this.

But in your sample code if I comment that line that causes to falsify the isEnabled, I still get postCollision event!

I checked your project and I was extremely curious that why didn’t you use the isEnabled and used isSensor. Didn’t what box2D suggest to do recently rather than isSensor?

Thanks, actually setting isSensor to true solved everything because I didn’t need bouncy stuff and every object’s fate is determined by the first object it touches.

Thing I was not aware of was that isSensor objects still produce collision calls, as in IIRC in Unity was not the case.

But I’m still utterly disappointed by the way that collision stuff won’t work properly in Corona.

Also the old documents that lay around that people usually find them through Googling rather than browsing and in case of Corona there were a few times that I was facing a bug that was lead to reading an old doc.

Thanks.
 

Hi Aidin,

I’m pretty sure (like 99%) that if you override the collision with an “event.contact” (false), it effectively voids the entire collision, even the postCollision.

By your description, it looks like you want items to cross a boundary from one side only (a completely valid use of the event.contact) and then register some event after it crosses that boundary. If so, I might suggest using both a preCollision and normal collision handler… if you saw my demo project on Physics Contact with the bouncing/jumping balls, it used both types.

If I understand your scenario, the food objects should fall from the top (off the screen?), hit a boundary, pass through the boundary, and then interact with everything else? If so, could you just use a standard collision listener, have the food begin as a “sensor” type, then on the “ended” phase of it passing through the top boundary, change it back to a normal non-sensor object?

Brent

Thanks man, your help finally led me to solve this.

But in your sample code if I comment that line that causes to falsify the isEnabled, I still get postCollision event!

I checked your project and I was extremely curious that why didn’t you use the isEnabled and used isSensor. Didn’t what box2D suggest to do recently rather than isSensor?

Thanks, actually setting isSensor to true solved everything because I didn’t need bouncy stuff and every object’s fate is determined by the first object it touches.

Thing I was not aware of was that isSensor objects still produce collision calls, as in IIRC in Unity was not the case.

But I’m still utterly disappointed by the way that collision stuff won’t work properly in Corona.

Also the old documents that lay around that people usually find them through Googling rather than browsing and in case of Corona there were a few times that I was facing a bug that was lead to reading an old doc.

Thanks.