Custom Physics Engine Help

Hello,

I am working on a custom physics engine for my side scroller platform game. I’ve gotten it to somewhat work, but I need help with some of the issues.

  • When the player is on a platform and moves off the edge, the play does not fall.

  • The player will, when landing, fall halfway through the platforms.

  • Player will not collide with all floating platforms.

Will anyone please fix and refine the engine? I really need some help!

Controls:

  • Swipe up to jump.

  • Swipe down to stop moving.

  • Swipe left to move left.

  • Swipe right to move right.

    –======================================================================================-- – -- ENGINE – -- --======================================================================================-- display.setStatusBar(display.HiddenStatusBar) --====================================Images========================================-- player = display.newRect(display.contentWidth/2,610,20,20) player.alpha = 0.5 player.sideL = player.x-player.width/2 player.sideR = player.x+player.width/2 player.sideT = player.y-player.height/2 player.sideB = player.y+player.height/2 ---- Tblock = display.newRect(display.contentWidth/2,10,display.contentWidth,20) Bblock = display.newRect(display.contentWidth/2,display.contentHeight-10,display.contentWidth,20) Lblock = display.newRect(10,display.contentHeight/2,20,display.contentHeight) Rblock = display.newRect(display.contentWidth-10,display.contentHeight/2,20,display.contentHeight) ---- block1 = display.newRect(display.contentWidth/2-325,555,200,20) block2 = display.newRect(display.contentWidth/2,555,200,20) block3 = display.newRect(display.contentWidth/2+325,490,200,20) --===================================Gravity========================================-- function gravityControl() if gravity == “on” then player.y = player.y +g if g < 20 then g = g +0.5 else g = 20 end else g = 0 end end Runtime:addEventListener(“enterFrame”, gravityControl) --============================CollisionDetection====================================-- function hasCollidedRect(obj1, obj2) if obj1 == nil then return false end if obj2 == nil then return false end R = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin L = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax B = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin T = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax return (L or R) and (T or B) end --==============================CollisionControl====================================-- function collisionControl(self) if hasCollidedRect(player, self) then self.sideL = self.x-self.width/2 self.sideR = self.x+self.width/2 self.sideT = self.y-self.height/2 self.sideB = self.y+self.height/2 if R and (player.sideT < self.sideB and player.sideB > self.sideT) then player.moveR = false elseif L and (player.sideT < self.sideB and player.sideB > self.sideT) then player.moveL = false elseif T and (player.sideL < self.sideR and player.sideR > self.sideL) then jumpDist = 0 elseif B and (player.sideL < self.sideR and player.sideR > self.sideL) then gravity = “off” jump = false end end end Tblock.enterFrame = collisionControl Bblock.enterFrame = collisionControl Lblock.enterFrame = collisionControl Rblock.enterFrame = collisionControl block1.enterFrame = collisionControl block2.enterFrame = collisionControl block3.enterFrame = collisionControl Runtime:addEventListener(“enterFrame”, Tblock) Runtime:addEventListener(“enterFrame”, Bblock) Runtime:addEventListener(“enterFrame”, Lblock) Runtime:addEventListener(“enterFrame”, Rblock) Runtime:addEventListener(“enterFrame”, block1) Runtime:addEventListener(“enterFrame”, block2) Runtime:addEventListener(“enterFrame”, block3) --===================================Jump===========================================-- function jumpControl() if jump == true then if jumpDist > 0 then player.y = player.y -jumpDist jumpDist = jumpDist -0.5 gravity = “off” else jumpDist = 0 gravity = “on” end else jumpDist = 10 end end Runtime:addEventListener(“enterFrame”, jumpControl) --==================================Swipe===========================================-- local beginX local beginY local endX local endY local xDistance local yDistance function checkSwipeDirection() xDistance = math.abs(endX - beginX) yDistance = math.abs(endY - beginY) if xDistance > yDistance then – Swipe Left – if beginX > endX +20 then player.moveL = true player.moveR = false ---------------- canSwipe = false ---------------- end – Swipe Left – -- Swipe Right – if beginX < endX -20 then player.moveR = true player.moveL = false ---------------- canSwipe = false ---------------- end – Swipe Right – else – Swipe Up – if beginY > endY +20 then jump = true ---------------- canSwipe = false ---------------- end – Swipe Up – -- Swipe Down – if beginY < endY -20 then player.moveR = false player.moveL = false ---------------- canSwipe = false ---------------- end – Swipe Down – end end function touchControl(e) if e.phase == “began” then beginX = e.x beginY = e.y canSwipe = true end if e.phase == “moved” then if canSwipe == true then endX = e.x endY = e.y checkSwipeDirection(); end end if e.phase == “ended” then end end Runtime:addEventListener(“touch”, touchControl) --===================================Move===========================================-- function moveControl() if player.moveL == true then player.x = player.x -5 end if player.moveR == true then player.x = player.x +5 end end Runtime:addEventListener(“enterFrame”, moveControl) --======================================================================================-- – -- ENGINE – -- --======================================================================================–