Page 1 of 1

TILED Map Collision Using Physics

Posted: Sun Sep 08, 2013 4:59 pm
by jkash4
I am working on a game and wish to know how to use TILED in conjunction with love 2d's built in physics module. I was thinking it would be something like:

Code: Select all

for i,v in ipairs(tile.properties.obstacle) do
v.body = love.physics.newBody(world, tile.x, tile.y, "static")
v.shape = love.physics.newRectangleShape(tile.width, tile.height)
v.fixture = love.physics.newFixture(v.body, v.shape, 1)
end
All help appreciated.

[]

Posted: Sun Sep 08, 2013 5:43 pm
by bekey
-snip-

Re: TILED Map Collision Using Physics

Posted: Sun Sep 08, 2013 8:57 pm
by jkash4
bekey wrote:Solution!
Thanks! But it's really laggy, I may have to do some optimization or if you had a lag issue and you fixed it, please post how.

Re: TILED Map Collision Using Physics

Posted: Mon Sep 09, 2013 3:12 am
by Jpfed
Let's say your tiles are either solid or not. Here's some pseudocode for creating fewer physics shapes, by consolidating horizontally-adjacent tiles into larger rectangles.

More aggressive consolidation should be possible (e.g. rectangles that include tiles from more than one row), but I haven't figured out how to do it yet.

Code: Select all

Create one body B for the immobile tiles, with mass zero
For every row,
    Initialize a boolean PreviousSolid to false
    Initialize an int StartSolid to 0
    For every tile in this row,
        If this tile is solid and PreviousSolid is false
            set StartSolid to the X coordinate of this tile
        If PreviousSolid is true and (this is the last tile in the row OR this tile is not solid)
            create a new rectangle Shape
            with Y coordinates spanning from the top of this row to the bottom of this row
            with X coordinates spanning from StartShape to the X coordinate of this tile
            attach this shape to B
        PreviousSolid = this tile is solid
Whenever I mention "the X coordinate of this tile" or similar, you may need to do some +-0.5 tweakery depending on what conventions you are using for the size/placement of your tiles. But it gets the gist across.