Creating boundaries and collisions?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 10
- Joined: Thu Sep 04, 2014 7:38 pm
Creating boundaries and collisions?
Alright so I am trying to make a top down zelda/pokemon-esque game using tiles. Now I need to prevent my character from walking out of the map, as well as I am attempting to prevent him from passing through props like trees, chests, structures, etc. Can somebody please explain the simplest way to do this, along with any resources that may allow me to do this? Thanks in advance!
- HugoBDesigner
- Party member
- Posts: 403
- Joined: Mon Feb 24, 2014 6:54 pm
- Location: Above the Pocket Dimension
- Contact:
Re: Creating boundaries and collisions?
The easiest for you would be using a physics library, like Bump, for example. I also have a physics engine I've just finished working on and may release really soon. Making a physics engine is a bit hard and takes a bunch of time. If it is okay for you to use a library, just try searching a few here in the forums (or using the one I linked). You can also use Löve's built-in physics system from [wiki]love.physics[/wiki], but it is a bit more complicated than you actually need (although it still works, of course). Lastly, you can private message me and I will help you however I can
- Zilarrezko
- Party member
- Posts: 345
- Joined: Mon Dec 10, 2012 5:50 am
- Location: Oregon
Re: Creating boundaries and collisions?
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.
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:
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.
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
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
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.
Re: Creating boundaries and collisions?
Zilarrezko you are awesome. I have been spending the last few weeks learning about tile collision and such
and keep hearing (well seeing) people on here talking about checking for collision BEFORE moving the player,
and I never understood it. Now I do thanks to your code and post!
Thanks
and keep hearing (well seeing) people on here talking about checking for collision BEFORE moving the player,
and I never understood it. Now I do thanks to your code and post!
Thanks
- Zilarrezko
- Party member
- Posts: 345
- Joined: Mon Dec 10, 2012 5:50 am
- Location: Oregon
Re: Creating boundaries and collisions?
Very happy to help man Have a good one!Rockford wrote:Zilarrezko you are awesome. I have been spending the last few weeks learning about tile collision and such
and keep hearing (well seeing) people on here talking about checking for collision BEFORE moving the player,
and I never understood it. Now I do thanks to your code and post!
Thanks
Who is online
Users browsing this forum: Bing [Bot], swuare and 2 guests