Tile-based Collisions: getting stuck

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.
kcirbmab
Prole
Posts: 5
Joined: Mon May 07, 2012 5:40 pm

Tile-based Collisions: getting stuck

Post by kcirbmab »

I'm currently using Tiled with Advanced Tile Loader for maps for a simple platforming game. I'm using the default LOVE physics engine to create a body for each solid tile. The player object successfully collides with solid tiles and, for the most part, everything seems to be working well. The problem is that if the player object is at the exact position in between two adjacent, solid blocks, it will "trip" and fall over. If rotation is turn off, the player simply gets stuck and can't move forward unless you jump.

Is there a way to prevent this? Does HardonCollider have the same issues?

Here is the code used to create the bodies:

Code: Select all

for x=0, map.width do
    for y=0, map.height do
        if foreground.tileData(x,y) then
            local ctile = {}
            ctile.body = love.physics.newBody(world,x*16,y*16)
            ctile.shape = love.physics.newRectangleShape(16,16)
            ctile.fixture = love.physics.newFixture(ctile.body, ctile.shape)
        end
    end
end
I've also attached the love file.
Attachments
testgame.love
(56.61 KiB) Downloaded 230 times
User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: Tile-based Collisions: getting stuck

Post by Puzzlem00n »

Okay, first of all, why use love.physics for a platform game? It's not meant for stuff like that. You're better off using Hardon Collider or your own collision code.

I quote the wiki:
love.physics is not lightweight, and not even remotely simple to use. It's a ten-ton hammer designed for heavy-lifting (er...hammer...lifting?).
If you are just trying to make a character jump around on blocks or the likes, then move along, nothing to see here.
I LÖVE, therefore I am.
kcirbmab
Prole
Posts: 5
Joined: Mon May 07, 2012 5:40 pm

Re: Tile-based Collisions: getting stuck

Post by kcirbmab »

I've looked into HardonCollider but I've run into another issue. Since the map is tile based, each tile is it's own shape. When the player is touching two tiles at once, it gets pushed up to much and bounces. Is there any work around for this? The only way around it that I can find is to use Tiled objects that span multiple tiles for collisions, but it's a lot more time consuming to set up maps. Perhaps there's no way around it?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Tile-based Collisions: getting stuck

Post by kikito »

Doing it manually is possible, and the code can be actually very easy to understand; but writing it is a bit trickier than it might seem. I've been wanting to write an article on this, but I just lack the time right now.

In the meantime, if you feel adventurous, you might want to give a look at this function. It should work provided 4 conditions:
  • Your entities and tiles are all axis-aligned rectangles.
  • Your entities rectangles are always smaller than your tiles.
  • Your entities x,y coordinates point to the center of their "collision rectangle"
  • Your entities don't move faster than 1 tile per frame
I've tried to make the code easy to understand and adapt, but I just can't explain everything right now. I hope it helps you in any case.
When I write def I mean function.
User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: Tile-based Collisions: getting stuck

Post by Puzzlem00n »

Alright, you know what, just for you I'm going to upload a .love of a platformer I made just for fun. It's very basic and has some flaws, but it works well enough for learning purposes. Tell me if you have any trouble understanding how it works.
Attachments
Fun with Platformers.love
(1.62 KiB) Downloaded 306 times
I LÖVE, therefore I am.
Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: Tile-based Collisions: getting stuck

Post by Zeliarden »

Yo!
Use a Circle instead of a Rectangle as your "feet". You can attach a several shapes and bodys to a fixture I think?
User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: Tile-based Collisions: getting stuck

Post by Puzzlem00n »

Zeliarden wrote:Yo!
Use a Circle instead of a Rectangle as your "feet". You can attach a several shapes and bodys to a fixture I think?
We kind of just all agreed he should avoid the physics module, but yeah, I guess what you're saying may be true.
I LÖVE, therefore I am.
Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: Tile-based Collisions: getting stuck

Post by Zeliarden »

We kind of just all agreed he should avoid the physics module, but yeah, I guess what you're saying may be true.
Why shouldnt he use the love.physics module? He already have the love.physics working.
Okay, first of all, why use love.physics for a platform game? It's not meant for stuff like that.
What about this then?
viewtopic.php?f=5&t=3794
You're better off using Hardon Collider or your own collision code.
I say love.physics is pretty easy to use. (maybe even easier than Hardon Collider or aleast at same difficulty)
I quote the wiki:
love.physics is not lightweight, and not even remotely simple to use. It's a ten-ton hammer designed for heavy-lifting (er...hammer...lifting?).
If you are just trying to make a character jump around on blocks or the likes, then move along, nothing to see here.
That "warning sign" should be removed or changed to something that dont scare off new lövers. (is it for an older ver.?)
User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: Tile-based Collisions: getting stuck

Post by Puzzlem00n »

Zeliarden wrote:
I quote the wiki:
love.physics is not lightweight, and not even remotely simple to use. It's a ten-ton hammer designed for heavy-lifting (er...hammer...lifting?).
If you are just trying to make a character jump around on blocks or the likes, then move along, nothing to see here.
That "warning sign" should be removed or changed to something that dont scare off new lövers. (is it for an older ver.?)
No, it's not, it's there because of exactly why it says its there. It's not referring to the syntax, it's about the engine and how much unnecessary processing power (in this case) it takes to run, which has not changed. To be honest, the physics module should scare off new lövers because it's really not worth using unless you need heavy physics, and the run-of-the mill platformer doesn't need them.
Zeliarden wrote:
Okay, first of all, why use love.physics for a platform game? It's not meant for stuff like that.
What about this then?
viewtopic.php?f=5&t=3794
That's a different story. That is a game that requires heavy physics and has physics-based mechanics, which an SMB clone does not. (see Mari0.)
Zeliarden wrote:
You're better off using Hardon Collider or your own collision code.
I say love.physics is pretty easy to use. (maybe even easier than Hardon Collider or aleast at same difficulty)
It's not a matter of how easy it is, it's just that, as I said above, it takes a lot of unnecessary processing power that could be saved by making the same thing with a different method.

I'm not trying to start an argument here, I'm just explaining what I've said. :|
I LÖVE, therefore I am.
SudoCode
Citizen
Posts: 61
Joined: Fri May 04, 2012 7:05 pm

Re: Tile-based Collisions: getting stuck

Post by SudoCode »

kikito wrote:Doing it manually is possible, and the code can be actually very easy to understand; but writing it is a bit trickier than it might seem. I've been wanting to write an article on this, but I just lack the time right now.

In the meantime, if you feel adventurous, you might want to give a look at this function. It should work provided 4 conditions:
  • Your entities and tiles are all axis-aligned rectangles.
  • Your entities rectangles are always smaller than your tiles.
  • Your entities x,y coordinates point to the center of their "collision rectangle"
  • Your entities don't move faster than 1 tile per frame
I've tried to make the code easy to understand and adapt, but I just can't explain everything right now. I hope it helps you in any case.
Reading through that makes me want to go back and rewrite all my code in a neater fashion.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests