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

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
brenus
Prole
Posts: 5
Joined: Sat Dec 03, 2022 4:33 pm

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

Post 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
Last edited by brenus on Sat Dec 03, 2022 6:15 pm, edited 1 time in total.
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

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

Post by darkfrei »

Code: Select all

    obj.vertices = {
       -obj.l/2, obj.l/2,
       0, -obj.l/2,
       obj.l/2, obj.l/2
    }
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
brenus
Prole
Posts: 5
Joined: Sat Dec 03, 2022 4:33 pm

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

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

Who is online

Users browsing this forum: No registered users and 19 guests