Page 1 of 1

[Solved] Body:getWorldPoints for polygon (doubling position value)

Posted: Sat Dec 03, 2022 4:50 pm
by brenus
Hi, everyone.

I really don't know why this happening. I'm trying to set body/shape/fixture to my player (triangle) and the value of the body when I try to get from getWorldPoints always doubles the original value.

I tried using a circle (getWorldPoint) and works perfectly the circle is printed in the middle screen, why in my case using a polygon doesn't? :?

Code: Select all

require("love")

WIDTH = love.graphics.getWidth()
HEIGHT = love.graphics.getHeight()

function love.load()
    world = love.physics.newWorld(0, 0, false)
    obj = {}
    obj.x = WIDTH / 2
    obj.y = HEIGHT / 2
    obj.l = 70
    obj.vertices = {
        obj.x - obj.l/2,
        obj.y + obj.l/2,
        obj.x,
        obj.y - obj.l/2,
        obj.x + obj.l/2,
        obj.y + obj.l/2
    }
    obj.body = love.physics.newBody(world, obj.x, obj.y, "dynamic")
    obj.shape = love.physics.newPolygonShape(obj.vertices)
    obj.fixture = love.physics.newFixture(obj.body, obj.shape, 1)

end

function love.draw()
    for _, body in pairs(world:getBodies()) do
        for _, fixture in pairs(body:getFixtures()) do
            local shape = fixture:getShape()

            if shape:typeOf("CircleShape") then
                local cx, cy = body:getWorldPoints(shape:getPoint())
                love.graphics.circle("fill", cx, cy, shape:getRadius())
            elseif shape:typeOf("PolygonShape") then
                love.graphics.polygon("fill", body:getWorldPoints(shape:getPoints()))
            else
                love.graphics.line(body:getWorldPoints(shape:getPoints()))
            end
        end
    end
end
Image

Re: Body:getWorldPoints for polygon (doubling position value)

Posted: Sat Dec 03, 2022 5:56 pm
by darkfrei

Code: Select all

    obj.vertices = {
       -obj.l/2, obj.l/2,
       0, -obj.l/2,
       obj.l/2, obj.l/2
    }

Re: Body:getWorldPoints for polygon (doubling position value)

Posted: Sat Dec 03, 2022 6:14 pm
by brenus
darkfrei wrote: Sat Dec 03, 2022 5:56 pm

Code: Select all

    obj.vertices = {
       -obj.l/2, obj.l/2,
       0, -obj.l/2,
       obj.l/2, obj.l/2
    }
Facepalm. For some reason, I was thinking in love.graphics.polygon("fill", vertices).

Thank you so much @darkfrei

Solved!