To learn LOVE2D I am creating a simple project. Player moves the square with arrows keys until it hits the goal square, at which point the game is won.
function isCollision(x1,y1,w1,h1,x2,y2,w2,h2) return (x1<x2+w2)and(x2<x1+w1)and(y1<y2+h2)and(y2<y1+h1) end function love.update(dt) if love.keyboard.isDown("escape") then love.event.quit() end if isCollision(player.x,player.y,player.w,player.h,goal.x,goal.y,goal.w,goal.h) then gameOver=true if love.keyboard.isDown("space") then love.load() end else delta = player.speed*dt if love.keyboard.isDown("right") and player.x<=800-player.w then player.x=player.x+delta end if love.keyboard.isDown("left") and player.x>=0 then player.x=player.x-delta end if love.keyboard.isDown("down") and player.y<=600-player.h then player.y=player.y+delta end if love.keyboard.isDown("up") and player.y>=0 then player.y=player.y-delta end end end
But sometimes the square overshoots the goal and it becomes lodged in the goal.
How do I make it so that engine detects when the player simply touches the goal and stops there, without moving further in the frame?
Disclaimer: Using a collision detection library like bump.lua would be too easy. I want to learn the relevant practices so that I can apply it to retro consoles and computers in the future.