Search code examples
lua2dlove2d

LOVE2D game engine ; 2D platformer collision systems


I have attached 2 photos below. Currently I'm working on simple 2d platformer game; the idea is the player starts from left corner of the screen and if player captures the flag on the right corner of screen player wins and will take the player to the next level.

So I have my player and the ground set up and I applied the gravity and collision bounds to them (picture 1 reflects the situation). Anyways, here's the tricky part, how do I add the collision system to platform1, 2, and 3? I didn't want to use "Tiled Map Editor" or such. Goal is to create the levels using Photoshop & Illustrator and bring those to the game world.

Any thoughts and ideas? Or any advise?

enter image description here

enter image description here


Solution

  • You may want to create a file for each level specifying the location and sizes of the platforms and anything else like background image start location etc. Then work on coding the logic to load that data file and create the physics bodies etc. for the level from there.

    an example data file might look like

     return {platforms={
                 {x=200, y=200, width=10, height=20},
                 {x=200, y=200, width=10, height=20},
                 {x=200, y=200, width=10, height=20, type="brick"},
             },
             size={width=300,height=200},
             start={x=100,y=200},
             goal={x=200,y=100, nextlevel="desert"},
             background={image="bluelevel.png"}
     }
    

    And then process these files to create the levels in a generic way:

    function load_level(filename)
       data = dofile(filename)
        platforms = {}
        for i, p in ipairs(data.platforms) do
            body=love.physics.newBody(world, p.x, p.y, "static")
            shape=love.physics.newRectangle(p.width, p.height)
            fix=love.physics.newFixture(body, shape)
            platforms[i] = {body=body, shape=shape, fix=fix}
        end
        -- .... finish loading data
    end