Very new to Lua. Collision stuff i guess.
Posted: Wed Apr 25, 2012 10:56 pm
Ok so this is really the last thing i need for my game to have a basic shape. How do i make it so that my character will not pass an object that i draw?
Im using the CheckCollision function to find if there is a collision, only problem is, i dont know what to do with the result.
Not sure what the procedure here is yet but im just going to copy and paste the little code that i have;
function love.load()
require("entities.lua")
ents.Startup()
love.graphics.setBackgroundColor( 255, 255, 255 )
imageHouse = love.graphics.newImage("textures/house.png")
imagePlayer = love.graphics.newImage("textures/player.png")
x = 50
y = 50
speed = 100
end
function love.draw()
love.graphics.setColor( 255, 255, 255, 255 )
love.graphics.draw( imageHouse, 0, 0, 0, 1, 1, 0, 0 )
love.graphics.setColor( 255, 255, 255, 255 )
love.graphics.rectangle( "fill", 100, 75, 20, 20)
love.graphics.draw(imagePlayer, x, y)
end
end
function love.update(dt)
if love.keyboard.isDown("right") then
x = x + (speed * dt)
elseif love.keyboard.isDown("left") then
x = x - (speed * dt)
end
if love.keyboard.isDown("down") then
y = y + (speed * dt)
elseif love.keyboard.isDown("up") then
y = y - (speed * dt)
end
end
function CheckCollision(x1, y1, w1, h1, x2, y2, w2, h2) --This function checks to see if two objects are in a collision
if x1 > (x2 + w2) or (x1 + w1) < x2 then return false end
if y1 > (y2 + h2) or (y1 + h1) < y2 then return false end
return true
end
What im asking is, how can i make it so that 'imagePlayer' can not move through this;
love.graphics.setColor( 255, 255, 255, 255 )
love.graphics.rectangle( "fill", 100, 75, 20, 20)
Any help guys?
Im using the CheckCollision function to find if there is a collision, only problem is, i dont know what to do with the result.
Not sure what the procedure here is yet but im just going to copy and paste the little code that i have;
function love.load()
require("entities.lua")
ents.Startup()
love.graphics.setBackgroundColor( 255, 255, 255 )
imageHouse = love.graphics.newImage("textures/house.png")
imagePlayer = love.graphics.newImage("textures/player.png")
x = 50
y = 50
speed = 100
end
function love.draw()
love.graphics.setColor( 255, 255, 255, 255 )
love.graphics.draw( imageHouse, 0, 0, 0, 1, 1, 0, 0 )
love.graphics.setColor( 255, 255, 255, 255 )
love.graphics.rectangle( "fill", 100, 75, 20, 20)
love.graphics.draw(imagePlayer, x, y)
end
end
function love.update(dt)
if love.keyboard.isDown("right") then
x = x + (speed * dt)
elseif love.keyboard.isDown("left") then
x = x - (speed * dt)
end
if love.keyboard.isDown("down") then
y = y + (speed * dt)
elseif love.keyboard.isDown("up") then
y = y - (speed * dt)
end
end
function CheckCollision(x1, y1, w1, h1, x2, y2, w2, h2) --This function checks to see if two objects are in a collision
if x1 > (x2 + w2) or (x1 + w1) < x2 then return false end
if y1 > (y2 + h2) or (y1 + h1) < y2 then return false end
return true
end
What im asking is, how can i make it so that 'imagePlayer' can not move through this;
love.graphics.setColor( 255, 255, 255, 255 )
love.graphics.rectangle( "fill", 100, 75, 20, 20)
Any help guys?