TILED Map Collision Using Physics

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
jkash4
Prole
Posts: 5
Joined: Wed Jul 03, 2013 3:26 am
Contact:

TILED Map Collision Using Physics

Post 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.
bekey
Party member
Posts: 255
Joined: Tue Sep 03, 2013 6:27 pm

[]

Post by bekey »

-snip-
Last edited by bekey on Fri Jan 24, 2014 2:14 am, edited 1 time in total.
User avatar
jkash4
Prole
Posts: 5
Joined: Wed Jul 03, 2013 3:26 am
Contact:

Re: TILED Map Collision Using Physics

Post 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.
Jpfed
Prole
Posts: 4
Joined: Tue Jun 02, 2009 10:54 pm

Re: TILED Map Collision Using Physics

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

Who is online

Users browsing this forum: No registered users and 3 guests