This is a gif i made of my game:
The problem I have, is I can't figure out how to do the collisions for the light gray (#) tiles. I'm uploading a .love file.
In the demo, press f11 to turn on the debugger. (it lags the game alot) WASD to move the yellow square
Tile based collision for top down game?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Tile based collision for top down game?
- Attachments
-
- lemonjuice.love
- (6.67 KiB) Downloaded 106 times
Hey.
Re: Tile based collision for top down game?
Easiest way should be to add a tile check before a entity / the player actually move to a new tile and add a "solid" variable to each.
Now if the tile the entity wants to move to is solid, stop the movement, otherwise move the entity.
Now if the tile the entity wants to move to is solid, stop the movement, otherwise move the entity.
Re: Tile based collision for top down game?
How would I go about doing that? Should I check if the player is touching / in a tile that is solid and block movement?
Hey.
- Jasoco
- Inner party member
- Posts: 3725
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Tile based collision for top down game?
Just look into Bump.
Re: Tile based collision for top down game?
You should use bump - you won't write anything better anytime soon (no offence - I neither).
But, if you want to write something on your own (educational thing) here are some tips for start (although it might be better to skip those and go straight to bump as my code below lack quality ):
- first of all your map.getTileOnAbsXY has a bug - you messed x and y
Instead of blocking movement you can move and then check if the entity is inside an obstacle. If so, set position so it just touches the obstacle. Also note that I'm checking only the left side when moving left and only right site if moving right (so they don't interfere).
(based on your code, the naming is confusing to me but I'm sure you'll get it , my lines are commented)
- mind that it might miss an obstacle if dt/speed is too big (search for "tunnelling")
- it will probably break if an entity happen to be outside of the map, you might or might not care
good luck
EDIT:
there is a bug in my code is that I'm checking only one corner while it should check 2 (e.g. when moving left you should check left-top and left-bottom corners, and it assumes that your entity is smaller then a tile) - this really need to be split into some functions...
But, if you want to write something on your own (educational thing) here are some tips for start (although it might be better to skip those and go straight to bump as my code below lack quality ):
- first of all your map.getTileOnAbsXY has a bug - you messed x and y
Instead of blocking movement you can move and then check if the entity is inside an obstacle. If so, set position so it just touches the obstacle. Also note that I'm checking only the left side when moving left and only right site if moving right (so they don't interfere).
(based on your code, the naming is confusing to me but I'm sure you'll get it , my lines are commented)
Code: Select all
function entity.update(dt)
for k,v in pairs(entity.table) do
if v.health > 0 or v.type == "static" then
local entity_width = game.TILESIZE*game.TILESCALE * v.scaleX -- entity.width might be useful
v.x = v.x + v.sx * dt
if v.sx > 0 then -- moving right
if map.getTileOnAbsXY(v.x + entity_width, v.y).char == '#' then -- map.isAbsXYSolid(x, y) ?
v.x = map.convertCordTile(map.convertCordAbs(v.x) + 1) - entity_width -- align to the obstacle
v.sx = 0 -- stop
end
elseif v.sx < 0 then -- moving left
if map.getTileOnAbsXY(v.x, v.y).char == '#' then -- map.isAbsXYSolid(x, y) ?
v.x = map.convertCordTile(map.convertCordAbs(v.x) + 1) -- align to the obstacle
v.sx = 0 -- stop
end
end
-- almost the same for y axis here - you'll figure this out. important note: you need to separate axes
--- i.e one and then another - otherwise nasty things will happen
end
end
-- rest of this function.
end
- it will probably break if an entity happen to be outside of the map, you might or might not care
good luck
EDIT:
there is a bug in my code is that I'm checking only one corner while it should check 2 (e.g. when moving left you should check left-top and left-bottom corners, and it assumes that your entity is smaller then a tile) - this really need to be split into some functions...
Code: Select all
function map.isAbsXYSolid(x, y)
return map.getTileOnAbsXY(x, y).char == '#'
end
function map.isAbsSideSolid(x, y, w, h, w2, h2)
return map.isAbsXYSolid(x + w, y + h) or map.isAbsXYSolid(x + w2, y + h2)
end
-----
-- moving right:
if map.isAbsSideSolid(v.x, v.y, v.width, 0, v.width, v.height) then
-- ...
end
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: Tile based collision for top down game?
If you want an easy solution that will take five seconds use STI + Bump plugin.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 187 guests