Tile based collision for top down game?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
kxait
Prole
Posts: 11
Joined: Wed Sep 17, 2014 7:52 am

Tile based collision for top down game?

Post by kxait » Sun Oct 25, 2015 7:23 pm

This is a gif i made of my game:
Image

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
Attachments
lemonjuice.love
(6.67 KiB) Downloaded 41 times
Hey.

User avatar
MadByte
Party member
Posts: 495
Joined: Fri May 03, 2013 6:42 pm
Location: Germany

Re: Tile based collision for top down game?

Post by MadByte » Sun Oct 25, 2015 8:07 pm

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.

User avatar
kxait
Prole
Posts: 11
Joined: Wed Sep 17, 2014 7:52 am

Re: Tile based collision for top down game?

Post by kxait » Sun Oct 25, 2015 8:33 pm

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.


User avatar
0x72
Citizen
Posts: 51
Joined: Thu Jun 18, 2015 9:02 am

Re: Tile based collision for top down game?

Post by 0x72 » Mon Oct 26, 2015 6:18 pm

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)

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
- 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...

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

bobbyjones
Party member
Posts: 720
Joined: Sat Apr 26, 2014 7:46 pm

Re: Tile based collision for top down game?

Post by bobbyjones » Tue Oct 27, 2015 4:27 am

If you want an easy solution that will take five seconds use STI + Bump plugin.
Do you frequently have great ideas but immediately lose them? Check out the MVP for my website called IdeaVault. It is designed to solve that problem. Desktop browsers only currently.

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests