yeah, you could do what Hugo Said, and go the library route. But it looks like what you're using is simple enough.
I can go the code way of explaining it, or the paragraph way. I'll go with the code way since writing a paragraph will likely confuse people more. I'll assume you want it pretty much exactly like the first zelda game, and the first pokemon games.
Code: Select all
function love.load()
player = {} --Make out player and all that
playerImage = love.graphics.newImage("graphics/player.png") --Here we load the players image from the graphics folder (if u put it here)
player.x, player.y, player.w, player.h = 0, 0, playerImage:getDimensions() --make the player at 0, 0 with image's width & height
player.xCollision = false --We'll use these to see if the player tried to run into a tree
objects = {} --have a table to hold objects.
objects[1] = {x = 100, y = 100, w = 50, h = 50, t = "tree", walkable = false} --Create the first object, let's make it a tree
treeImage = love.graphics.newImage("graphics/tree.png") --Here we load the tree image, that we have in the graphics folder.
function boxCollision(x1, y1, w1, h1, x2, y2, w2, h2) --to see if one box, is inside another box.
return y1 < y2 + h2 and y2 < y1 + h1 and x1 < x2 + w2 and x2 < x1 + w1
end
end
function love.update(dt)
if love.keyboard.isDown("s") then -- If the s key is down, then continue (this is checked every frame)
for i = 1, #objects do --iterate through every object in the objects table
if boxCollision(player.x, player.y + dt, player.w, player.h, objects[i].x, objects[i].y, objects[i].w, objects[i].h) and not walkable then
player.collision = true --I'll explain this after
break
end
end
if not player.collision then
player.y = player.y + dt
end
player.collision = false
elseif love.keyboard.isDown("w") then
for i = 1, #objects do
if boxCollision(player.x, player.y - dt, player.w, player.h, objects[i].x, objects[i].y, objects[i].w, objects[i].h) and not walkable then
player.collision = true
break
end
end
if not player.collision then
player.y = player.y - dt
end
player.collision = false
elseif love.keyboard.isDown("a") then
for i = 1, #objects do
if boxCollision(player.x - dt, player.y, player.w, player.h, objects[i].x, objects[i].y, objects[i].w, objects[i].h) and not walkable then
player.collision = true
break
end
end
if not player.collision then
player.x = player.x - dt
end
player.collision = false
elseif love.keyboard.isDown("d") then
for i = 1, #objects do
if boxCollision(player.x + dt, player.y, player.w, player.h, objects[i].x, objects[i].y, objects[i].w, objects[i].h) and not walkable then
player.collision = true
break
end
end
if not player.collision then
player.x = player.x + dt
end
player.collision = false
end
end
function love.draw()
for i = 1, #objects do
if objects[1].t == "tree" then
love.graphics.draw(treeImage, object[1].x, object[1].y)
elseif object[1].t == "whateverelse" then
end
end
love.graphics.draw(playerImage, player.x, player.y)
end
Alright, I haven't tested it, but read it over a few times and you'll maybe, just maybe, get the logic.
so for this block of code here:
Code: Select all
for i = 1, #objects do
if boxCollision(player.x, player.y + dt, player.w, player.h, objects[i].x, objects[i].y, objects[i].w, objects[i].h) and not walkable then
player.collision = true
break
end
end
if not player.collision then
player.y = player.y + dt
end
player.collision = false
what it's doing is going through every object in the object table, then testing to see if the player, with the the added dt (time in between frames) is inside of another box (we haven't moved the player yet.). if the player with the added dt is inside of another tile, then it checks to see if the tile is walkable. If it isn't, than a collision happened. Then we do a check to see if the collision
didn't happen, and then adds (or subtracts based on where we want the player to move) the dt to the players coordinates.
This is the most basic of collision testing. Although, to beginners (even when I started out) Concepts are the hardest to grasp. Especially if you have trouble with the coding language you're writting in. Or you may be very good in Lua, in which case this code will be easy to read. In any case, I'll be here, ready to answer questions, as will the rest of the great community.