Character passing thru platform one-way // removing preCollision event listener

The code below:

  • creates a static physics object at bottom of screen (“bottomWall”)

  • creates a static physics object in the lower part of screen (“platform”)

  • creates a ball as a dynamic physics object and gives it an initial linear velocity

  • the ball then moves straight down, it passes thru the platform from above, then bounces back at the bottom wall and then also bounces back at the platform as it hits it from below

The intention of the code is to move a physics object thru another physics object but only one way. Thus, the ball passes thru the platform from above, but bounces back from below.

The code works, having arrived at it by trial and error.

My question is: is there a better / cleaner way to code this, particularly the removing of the event listener?

  • Are those two nested performWithDelay really necessary when removing the event listener?

  • And is it good practice to use event.contact.isEnabled = false here to allow the ball to pass through the platform from one direction?

    display.setStatusBar(display.HiddenStatusBar) displayHeight = display.contentHeight displayWidth = display.contentWidth local physics = require “physics” physics.start() physics.setGravity(0,0) local bottomWall= display.newRect(displayWidth*0.5, displayHeight+25, displayWidth, 50) physics.addBody(bottomWall, ‘static’, {density=1, friction=0, bounce=1}) local platform = display.newRect( 0, 0, 280, 30 ) platform.x, platform.y = display.contentCenterX, display.contentCenterY+200 platform.collType = “passthru” physics.addBody( platform, “static”, { density=1, friction=0, bounce=1 } ) local ball = display.newCircle( 0, 0, 15 ) ball.x, ball.y = display.contentCenterX, display.contentCenterY-40 physics.addBody( ball, “dynamic”, { bounce=1, radius=20 } ) function removePreCollisionEventListener () timer.performWithDelay(10, ball:removeEventListener(“preCollision”), removePreCollisionEvent) end local function preCollisionEvent( self, event ) local collideObject = event.other local ballLinearVelocityX, ballLinearVelocityY = ball:getLinearVelocity() if ( collideObject.collType == “passthru” and ballLinearVelocityY > 0) then event.contact.isEnabled = false --disable this specific collision timer.performWithDelay(100, removePreCollisionEventListener) end end ball.preCollision = preCollisionEvent ball:addEventListener( “preCollision” ) ball:setLinearVelocity( 0, 300)

Hi @rene.andersen,

I assume you saw the one-sided platforms sample project? It looks like some of this code is based on that. If you haven’t seen it, I encourage you to check out how one-sided platforms were implemented there.

Brent

Thanks @Brent - is it the “Sticker Knight Platformer” you mean? I see that the hero can pass through platforms one way, which is exactly what I’m interested in. I checked out the code but can’t find for the life of me the bit that governs the one way pass thru the platform. I see the code is spread across five lua files and suspect the one-sided platform behaviour must be in “game.lua” but just can’t find it in there. There doesn’t seem to be any documentation for the sample project either, other than the comments in the code?

Hi again,

Actually it’s a Corona sample project, but Sticker Knight might use similar code. You can find it in your Corona application folder here:

Corona > SampleCode > Physics > PhysicsContact

Brent

Hi @rene.andersen,

I assume you saw the one-sided platforms sample project? It looks like some of this code is based on that. If you haven’t seen it, I encourage you to check out how one-sided platforms were implemented there.

Brent

Thanks @Brent - is it the “Sticker Knight Platformer” you mean? I see that the hero can pass through platforms one way, which is exactly what I’m interested in. I checked out the code but can’t find for the life of me the bit that governs the one way pass thru the platform. I see the code is spread across five lua files and suspect the one-sided platform behaviour must be in “game.lua” but just can’t find it in there. There doesn’t seem to be any documentation for the sample project either, other than the comments in the code?

Hi again,

Actually it’s a Corona sample project, but Sticker Knight might use similar code. You can find it in your Corona application folder here:

Corona > SampleCode > Physics > PhysicsContact

Brent