Improving Collision Resolution System
Posted: Sun May 20, 2018 8:35 pm
I'm trying to develop a simple collision system and its okay but I was wondering if someone could suggest improvements. One issue is the fact that I cannot slide against a wall when going diagonally.
Here is the Collision code
Here is the Collision code
Code: Select all
Player = {}
Player.x = 400
Player.y = 300
Player.newX = 0
Player.newY = 0
Player.moveX = false
Player.moveY = false
Player.velX = 0
Player.velY = 0
function Player.update(dt)
if(love.keyboard.isDown("w")) then
Player.velY = -200
end
if(love.keyboard.isDown("a")) then
Player.velX = -200
end
if(love.keyboard.isDown("s")) then
Player.velY = 200
end
if(love.keyboard.isDown("d")) then
Player.velX = 200
end
Player.newX = Player.x + Player.velX * dt
Player.newY = Player.y + Player.velY * dt
Player.moveX = true
Player.moveY = true
for _,v in ipairs(Tile) do
if(checkCollisionXY(Player.newX,Player.newY,25,25,v.x,v.y,50,50)) then
if(checkCollisionY(Player.newY,25,v.y,50)) then
Player.moveY = false
end
if(checkCollisionX(Player.newX,25,v.x,50)) then
Player.moveX = false
end
if(not Player.moveX or not Player.moveY) then
break
end
end
end
if(Player.moveY) then
Player.y = Player.newY
Player.newY = 0
end
if(Player.moveX) then
Player.x = Player.newX
Player.newX = 0
end
Player.velX = 0
Player.velY = 0
end
function checkCollisionXY(x1,y1,w1,h1,x2,y2,w2,h2)
return(x1 + w1 > x2 and x1 < x2 + w2 and y1 + h1 > y2 and y1 < y2 + h2)
end
function checkCollisionX(x1,w1,x2,w2)
return(x1 + w1 > x2 and x1 < x2 + w2 )
end
function checkCollisionY(y1,h1,y2,h2)
return(y1 + h1 > y2 and y1 < y2 + h2)
end
function Player.draw()
love.graphics.setColor(100,200,50,150)
love.graphics.rectangle("fill",Player.x,Player.y,25,25)
love.graphics.setColor(255,255,255,255)
end