[SOLVED] for Smooth tile-based collision :D

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Con_Quister
Prole
Posts: 6
Joined: Fri Jun 02, 2017 1:12 am

[SOLVED] for Smooth tile-based collision :D

I'm a beginner trying to implement collision to a Tile-map array. the goal was to have a system where I could define a wall on the map with(for example) a '1' and then take the player bounding box and compare it to the array of tiles and have a collision.

so how do you compare a tile map array to a player object?

Below is the current main.lua
Attachments
main.lua
Last edited by Con_Quister on Sun Aug 06, 2017 5:40 am, edited 2 times in total.
[MEATBAG DETECTED!!! ]
|___
(O O) ! ------------> (._.)
/(__)\
-/---\--
-----------
-----

Joelrodiel
Prole
Posts: 27
Joined: Wed Apr 20, 2016 3:40 am

Re: [Help] for Smooth tile-based collision

Hi, Im also a beginner like you and I spent soooo much time trying to figure out tile collision. I just couldnt, and I didnt want to use libraries. So dont wast time like me and use a collision library, trust me its way better. I recently started using Bump and I feel like I wasted my time.

Hope it helps!

Bump: https://github.com/kikito/bump.lua

Con_Quister
Prole
Posts: 6
Joined: Fri Jun 02, 2017 1:12 am

Re: [Help] for Smooth tile-based collision

thanks, but the type of collision I'm going for is constrained to an array. I've made basic aabb collision from scratch without the BUMP library. The challenge comes from using an array to dictate collision with a player object.
I'd also like to do the collision without a library because I would learn a lot more about programming by figuring out how to it form the ground up.
[MEATBAG DETECTED!!! ]
|___
(O O) ! ------------> (._.)
/(__)\
-/---\--
-----------
-----

Мэтю
Prole
Posts: 31
Joined: Mon Jan 06, 2014 1:24 pm
Location: Espírito Santo, Brazil
Contact:

Re: [Help] for Smooth tile-based collision

Talking a little 'bout your current code:

- I think it might be better to each table inside your tilemap table to be all the same length or you may get some troubles or need to do some extra calculations ...

- Try reading a bit about code and variable scope. Some nice articles:
- http://blogs.love2d.org/content/scope-within-lua
- https://love2d.org/wiki/local

Back to the main problem...

You got you player's position, and also the its width and height, which is the same as any cell size from the tilemap.
So it being said, the area your player occupies is obviously that is from its position( x and y ) to its dimensions( w and h), so player.x+player.w and player.y+player.h;

Now we need to define which blocks or places we need to detect collision

Let's say we'll handle it with a table that holds the blocks positions which may collide:

Code: Select all

local cols = {}
cols[1] = {x=64, y=64}
cols[2] = {x=128, y=128}

So, each object of the table would be another table containing the positions of blocks that you want to detect collision.
Now we just need to calculate with the area before said is intercepting any of these blocks

Code: Select all

-- We define a function, it may be useful later for any other purposes
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2) -- Here we check if certain area transpass another area
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end

-- We loop over all blocks stored at cols and check if the player hits it
for k, v in ipairs(cols) do

if CheckCollision(player.x, player.y, cellsize, cellsize, v.x, v.y, cellsize, cellsize) then
-- If the collides do any action here
end

end

Well, now you can do some callbacks whenever the player hits something. I've made some modifications to your code, and I'm going to attach it here. That's the basic.

Now the next step is a bit of work to do, which I'm not going to explain, but I got a reference for you:

https://www.gamedev.net/articles/progra ... nse-r3084/
Attachments
main.lua
World needs love.

Con_Quister
Prole
Posts: 6
Joined: Fri Jun 02, 2017 1:12 am

Re: [Help] for Smooth tile-based collision

Thanks for the feed back and resources. real cool.
[MEATBAG DETECTED!!! ]
|___
(O O) ! ------------> (._.)
/(__)\
-/---\--
-----------
-----

Con_Quister
Prole
Posts: 6
Joined: Fri Jun 02, 2017 1:12 am

Re: [Help] for Smooth tile-based collision

Thanks so much, piggy backing-off what you showed me, I used the collision system to make walls for the player object. That's exactly the kind of stuff I wanted to do, thanks for the info, I've been trying to figure out how to do this type of thing for months
Attachments
main.lua