Map isn't scaling properly with lighting

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
Sammm
Prole
Posts: 37
Joined: Fri Sep 09, 2022 4:39 pm

Map isn't scaling properly with lighting

Post by Sammm »

Hello! I'm using the lighworld library for lighting, as seen here: https://github.com/tanema/light_world.lua

I followed the example code very closely, and the lights work my map at a scaling of 1 (e.g. no scaling at all), but when I try to scale the map up to 2, the light appears offset improperly with the map. This is my `main.lua` code:

Code: Select all

local Camera = require("lib.camera")
local map = require("scripts.map")

local LightWorld = require("lib.lightworld")

function love.load()
    love.graphics.setBackgroundColor(love.math.colorFromBytes(41, 45, 65))

    love.physics.setMeter(64)
    world = love.physics.newWorld(0, 9.81*64, true)

    windowWidth = love.graphics.getWidth()
    windowHeight = love.graphics.getHeight()

    lightWorld = LightWorld({
        ambient = {0.21, 0.21, 0.21},
    })

    lightMouse = lightWorld:newLight(0, 0, 1, 0.49, 0.24, 300)
    lightMouse:setGlowStrength(0.3)

    map:load()
end

function love.update(dt)
    if love.keyboard.isDown('escape') then love.event.quit() end

    lightWorld:update(dt)
    lightWorld:setTranslation(0, 0, map.scale)
    lightMouse:setPosition(love.mouse.getX(), love.mouse.getY())
end

function love.draw()
    love.graphics.push()

    love.graphics.scale(map.scale)
    lightWorld:draw(function()
        map:draw()
    end)

    love.graphics.pop()
end
and `map.lua`:

Code: Select all

local sti = require("lib.sti")

local map = {
    scale = 2
}

function map:load()
    self.map = sti("maps/testmap.lua")
    
    self.width = self.map.width * self.map.tilewidth * self.scale
    self.height = self.map.height * self.map.tileheight * self.scale

    local solidColliders = {}
    for i, obj in pairs(self.map.layers["Solids"].objects) do
        solidColliders.body = love.physics.newBody(world, (obj.x + obj.width/2) * self.scale, (obj.y + obj.height/2) * self.scale, "static")
        solidColliders.shape = love.physics.newRectangleShape(obj.width * self.scale, obj.height * self.scale)
        solidColliders.fixture = love.physics.newFixture(solidColliders.body, solidColliders.shape)
        solidColliders.fixture:setUserData("Solids")

        lightWorld:newRectangle((obj.x + obj.width/2) * self.scale, (obj.y + obj.height/2) * self.scale, obj.width * self.scale, obj.height * self.scale)

    end
end

function map:draw()
    self.map:drawImageLayer("Map")
end

return map
If someone could help me fix this, that would be great!

Here is the full source and `.love` file that you can try out (scaling is set to 2)
lighting.love
(8.01 MiB) Downloaded 46 times
Lua is LÖVE, lua is life
User avatar
GVovkiv
Party member
Posts: 686
Joined: Fri Jan 15, 2021 7:29 am

Re: Map isn't scaling properly with lighting

Post by GVovkiv »

I think this happening because you didn't apply scaling offset to your mouse calculations:

Code: Select all

lightMouse:setPosition(love.mouse.getX() / map.scale, love.mouse.getY() / map.scale)
Instead of:

Code: Select all

 lightMouse:setPosition(love.mouse.getX(), love.mouse.getY())
Reason why it worked is because on 1 scale there no offsets happening and if we take formulas above as example:
if mouse x will be 100; 100 * 1 (which is map scale) = 100
if you even slightly change scale, let's say 1.5;
100 * 1.5 = 150 - this is what *corrected* mouse x should look like, but since you didn't applied scaling, you still passing 100 to your lighting library, so it obviously draw light sources on wrong place.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 9 guests