Simple Collision Detection
Posted: Wed Oct 17, 2012 4:45 pm
I am trying to make a basic game. Just a box you can control with "wsad" and you can go outside the window.
My collision detector only stops when it outside the box and does not allow it to move afterwards, i am stuck and cant find out what to do.
Here is my code.
I have attached the .zip of the files
My collision detector only stops when it outside the box and does not allow it to move afterwards, i am stuck and cant find out what to do.
Here is my code.
Code: Select all
function CheckCollision(ax1,ay1,aw,ah, bx1,by1,bw,bh)
local ax2,ay2,bx2,by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
return ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1
end
function MapCollision()
end
function InMap()
return CheckCollision(0, 0, love.graphics.getWidth(), love.graphics.getHeight(), playerx, playery, 10,10)
end
function love.load()
waterimage = love.graphics.newImage("images/water.png")
playerimage = love.graphics.newImage("images/player.png")
screenWidth = love.graphics.getWidth()
screenHeight = love.graphics.getHeight()
playerx = 10
playery = 10
end
function love.update(dt)
if love.keyboard.isDown("w") and InMap() then
playery = playery - 100 * dt
end
if love.keyboard.isDown("s") and InMap() then
playery = playery + 100 * dt
end
if love.keyboard.isDown("d") and InMap() then
playerx = playerx + 100 * dt
end
if love.keyboard.isDown("a") and InMap() then
playerx = playerx - 100 * dt
end
function love.draw()
love.graphics.setBackgroundColor( 255, 255, 255 )
love.graphics.setColor( 0, 0, 0, 255 )
love.graphics.rectangle("fill", playerx, playery, 10, 10 )
end
end