Requesting guidance in terms of tile collision

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
frozii
Prole
Posts: 1
Joined: Mon Aug 01, 2016 4:55 pm

Requesting guidance in terms of tile collision

Post by frozii »

Hello, I'm trying to make a simple 2D platformer using LOVE2D and I need to create collisions between my player and the drawn tiles on the map. Sadly I have no idea how to do it because I suck, so I'm wondering if someone might lend a helping hand.

I left some of my files as attachments so you can have a look around :)
Attachments
player.lua
(3.06 KiB) Downloaded 136 times
Map1.lua
(3.84 KiB) Downloaded 129 times
levels.lua
(1.14 KiB) Downloaded 132 times
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Requesting guidance in terms of tile collision

Post by Jasoco »

I recommend looking into Bump as long as you don't need slopes.

https://github.com/kikito/bump.lua
User avatar
peterrust
Prole
Posts: 42
Joined: Thu Dec 29, 2016 8:49 pm
Location: Bellingham, WA, USA
Contact:

Re: Requesting guidance in terms of tile collision

Post by peterrust »

forzii: I second Jasoco's recommendation of bump. In fact, even if you're not planning on using the library and you want to write the code yourself, I recommend skimming the README of kikito's bump library because it'll give you some bearings for how collisions are typically handled.

I see you have a player_collision_check() stubbed in, but it's not connected to player:physics(). One of the core ideas is that your game's draw() or update() functions are probably not going to be called at the exact moment in time when two objects touch each-other. Usually they'll be called before the two objects touch and then after they are overlapping. So the collision code can't just drop the velocity to zero when it detects that the objects are touching. It needs to detect how much they have overlapped and back out the overlap so that the images aren't drawn as overlapping.

There are different ways you might want to back out the overlap -- for a platformer like this, when dealing with collisions between your player object and solid tiles, you'll want to use the "slide" method. So if the player is coming down (via gravity) on to a tile and overlaps it in the y dimension -- but the player is also moving in the x dimension -- you'll want to keep the x position change, but back out the y position change. If your player is running sideways and jumps but collides sideways with a wall, you'll want to do the opposite (keep the y position change, but back out the x position change). If your player runs and jumps and comes down at an angle onto the corner of a tile then you would want to pick whichever overlap is larger (x or y) and keep that one and back out the other one.

You'll want to do the above collision check in your physics method, after picking the player's new X and Y, but before setting these properties. The bump library calls these the goalX and goalY and -- after doing the collision check -- returns actualX and actualY. If you didn't collide with anything, these will be the same as goalX and goalY, but if you did collide with something, these will be different.

You already have code that loops through your tiles; you'll need to do this looping every time you do a collision check, to check for overlaps. This could get inefficient, so that's why collision libraries typically group objects into "zones" or "regions", so that the library only has to check for overlaps between items that are in the same zone or region. The bump library calls these "cells" and defaults to a cell size of 64x64.

Don't be too hard on yourself, this kind of thing seems obvious and simple when playing a game, but it's surprisingly tricky when creating a game. That's why there are articles and libraries for collision detection and collision detection algorithms.
User avatar
peterrust
Prole
Posts: 42
Joined: Thu Dec 29, 2016 8:49 pm
Location: Bellingham, WA, USA
Contact:

Re: Requesting guidance in terms of tile collision

Post by peterrust »

Follow-up: if you want to the collision-detection code yourself, without using a library, sheepolution did a nice tutorial on it here: http://sheepolution.com/learn/book/13 (technically this kind of simple collision detection between two rectangles is called AABB for Axis-Aligned Bounding Boxes).

Unfortunately, I couldn't find a love2d tutorial that covers responding to the collision, just detecting that a collision exists.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 6 guests