Hi there,
I have a little problem with “jumping” onto a platform and determining if the player hits the side or lands on top of the platform. When I do a basic position test, checking the x pos of the player and the platform, the collision event is firing early, which means that the player hasn’t even collided yet with the platform even though it fires the event. The collision is fired between 2 to 5 pixels early.
I have created a demo, when you click the background a linear pulse is applied to a box (which represents the player). I would like it make something happen when the player successfully lands on the platform (rather than hits the side), but I cannot achieve this because when I check the x positions the player’s (box) right hand side is still left of the platform.
I am struggling to explain exactly what I mean, but the following code illustrates the problem:
-- include Corona's "physics" library local physics = require "physics" physics.setScale(80) physics.start() physics.setDrawMode( "hybrid" ) local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth\*0.5 local box local background local hasTouched = false -- display a background image background = display.newRect(0,0, display.contentWidth, display.contentHeight ) background.anchorX = 0 background.anchorY = 0 background:setFillColor(1,1,1) -- make a box and position it box = display.newRect( 150, 800, 78, 74 ) box.anchorX = 0 box.anchorY = 0 box:setFillColor(182/255, 243/255, 226/255) -- add physics to the box physics.addBody( box, { density=1.0, friction=1, bounce=0 } ) box.objectName = "box" box.isFixedRotation = true -- create a floor object and add physics local floor = display.newRect( 0 , display.contentHeight, screenW, 82 ) floor.anchorX = 0 floor.anchorY = 1 floor:setFillColor(122/255, 207/255, 30/255) floor.objectName = "floor" physics.addBody( floor, "static", { friction=0.3 } ) -- create platform local platform = display.newRect(300, floor.y - floor.height - 200, 200, 10) platform.anchorX = 0 platform.anchorY = 0 platform:setFillColor(86/255, 115/255, 207/255) platform.objectName = "platform" physics.addBody( platform, "static", { density=1.0, friction=1000, bounce=0 } ) function onCollision( event ) if ( event.phase == "began" ) then local boxRightX = event.target.x + event.target.width local platformLeftX = event.other.x local boxTopY = event.target.y local platformTopY = event.other.y -- check that box is not hitting the side or underneath of the platform -- but the problem is, is that this fires even when we don't hit the side if boxRightX \< platformLeftX or boxTopY \> platformTopY then blueLine = display.newLine( boxRightX , 0, boxRightX, screenH ) blueLine:setStrokeColor( 0, 0, 1 ) blueLine.strokeWidth = 1 redLine = display.newLine( platformLeftX ,0, platformLeftX, screenH ) redLine:setStrokeColor( 1, 0, 0 ) redLine.strokeWidth = 1 print("boxRightX: " .. boxRightX) print("platformLeftX: " .. platformLeftX) print("boxTopY: " .. boxTopY) print("platformTopY: " .. platformTopY) print( "box hit side and should fall, but is infact sitting on the platform") return end elseif ( event.phase == "ended") then end end function onTap() if not hasTouched then box:applyLinearImpulse(1.2, -9.8, box.x + box.width/2, box.y + box.height/2 ) hasTouched = true end end physics.start() physics.setGravity( 0, 20 ) background:addEventListener( "tap", onTap ) box:addEventListener( "collision", onCollision )
I hope that makes sense,
Many thanks for your help,
Mike