Setting a Windfield collision type for colliders generated from a table

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
meaningless [C]ode
Prole
Posts: 2
Joined: Mon Oct 11, 2021 2:12 am

Setting a Windfield collision type for colliders generated from a table

Post by meaningless [C]ode »

Hi, I'm currently learning love2D by experimenting with making some simple versions of a few different 2D games and after learning some basics over the past week or so I'm starting with a sidescrolling platformer.

I'm using Tiled to create my maps, and Windfield for my physics and collisions. After a bit of work I was able to find and mostly understand some code that converts one of my Tiled map layers into Windfield colliders by creating a table with the location of the solid tiles and generating rectangle colliders the same size as those tiles in those locations.

However, I say I mostly understand it because the problem I'm running into is actually setting the type of these colliders to be 'static'. I know normally you'd use :setType('static') but currently I'm not sure which variable I need to actually set to static, or where in the loop to do it.

Here's the code that I'm using:

Code: Select all

function love.load()
    currentMap = sti('maps/plat_map1.lua')
    currentWorld = wf.newWorld()
    
    --searches for the "solids" layer of the map file and then creates collision boxes on top of the tiles
    local map = love.filesystem.load('maps/plat_map1.lua')()
    local mapW = map.width
    local mapH = map.height
    local colliderList = {}

    local layerNum = 1
    local layer = map.layers[layerNum]
    while layer do
        if layer.name == 'solids' then
            local y = 0
            while y < mapH do
                local x = 0
                while x < mapW do
                    local tileID = (y * mapW) + x + 1
                    local tile = layer.data[tileID]
                    if tile > 0 then
                        table.insert(colliderList, currentWorld:newRectangleCollider(x * 16, y * 16, 16, 16))
                    end
                    x = x + 1
                end
                y = y + 1
            end
        end
        layerNum = layerNum + 1
        layer = map.layers[layerNum]
    end
end

function love.update(dt)
    currentWorld:update(dt)
end

function love.draw()
    currentMap:drawLayer(currentMap.layers["background"])
    love.graphics.push()
        love.graphics.setColor(1, 0, 0)
        currentWorld:draw()
    love.graphics.pop()
end
This works exactly how I need it to in terms of scanning my maps and generating colliders in the correct positions but I have absolutely no idea which variable to change, and where, to make these colliders 'static'. I also imagine this could probably be done much more cleanly but I'm not entirely sure how.

If anyone could explain what I'm missing that would be greatly appreciated!

EDIT: Sorry for bumping, attaching the .love

I'm wondering if what I'm trying to do is even possible? Would I be better off just not using Windfield?
Attachments
Platformer-Test.love
(56.25 KiB) Downloaded 134 times
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Setting a Windfield collision type for colliders generated from a table

Post by Gunroar:Cannon() »

meaningless [C]ode wrote: Mon Oct 11, 2021 2:58 am
However, I say I mostly understand it because the problem I'm running into is actually setting the type of these colliders to be 'static'. I know normally you'd use :setType('static') but currently I'm not sure which variable I need to actually set to static, or where in the loop to do it.
Oh, wow, I've skipped this (and more stuff in Support and Dev) cause I thought someone answered. You use ":setType" on the new collider before addin it to colliderList. Though by now you may have figured it out :?

Code: Select all

function love.load()
    currentMap = sti('maps/plat_map1.lua')
    currentWorld = wf.newWorld()
    
    --searches for the "solids" layer of the map file and then creates collision boxes on top of the tiles
    local map = love.filesystem.load('maps/plat_map1.lua')()
    local mapW = map.width
    local mapH = map.height
    local colliderList = {}

    local layerNum = 1
    local layer = map.layers[layerNum]
    while layer do
        if layer.name == 'solids' then
            local y = 0
            while y < mapH do
                local x = 0
                while x < mapW do
                    local tileID = (y * mapW) + x + 1
                    local tile = layer.data[tileID]
                    if tile > 0 then
                        local collider = currentWorld:newRectangleCollider(x * 16, y * 16, 16, 16))
                        collider:setType( "static") --<-- here
                        table.insert(colliderList,collider)
                    end
                    x = x + 1
                end
                y = y + 1
            end
        end
        layerNum = layerNum + 1
        layer = map.layers[layerNum]
    end
end

function love.update(dt)
    currentWorld:update(dt)
end

function love.draw()
    currentMap:drawLayer(currentMap.layers["background"])
    love.graphics.push()
        love.graphics.setColor(1, 0, 0)
        currentWorld:draw()
    love.graphics.pop()
end
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
meaningless [C]ode
Prole
Posts: 2
Joined: Mon Oct 11, 2021 2:12 am

Re: Setting a Windfield collision type for colliders generated from a table

Post by meaningless [C]ode »

Thank you so much! I actually hadn't figured this out yet, and was just resigning myself to having to use an object layer in Tiled when I saw your response, so it made my evening!
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Setting a Windfield collision type for colliders generated from a table

Post by Gunroar:Cannon() »

meaningless [C]ode wrote: Mon Oct 25, 2021 11:23 pm Thank you so much! I actually hadn't figured this out yet, and was just resigning myself to having to use an object layer in Tiled when I saw your response, so it made my evening!
Glad to help. It's easy to overlook the littlest things :ultrahappy:
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 27 guests