Page 1 of 1

love.physics.newPolygonShape limit to 8 vertices (polygon with more than 8 vertices is possible)

Posted: Fri Apr 07, 2023 7:08 am
by dusoft
Is there a reason why love.physics.newPolygonShape is limited to 8 vertices? Is it resources?

Would you recommend to:
  1. Simplify polygons
  2. Use external library to handle complicated polygon shapes (that allows for unlimited vertices)

Re: love.physics.newPolygonShape limit to 8 vertices

Posted: Fri Apr 07, 2023 8:07 am
by darkfrei
3.Use several polygons and join them together.

Re: love.physics.newPolygonShape limit to 8 vertices

Posted: Fri Apr 07, 2023 8:57 am
by MrFariator
The love.physics module is basically just a wrapper for box2d, which has a vertex limit between 3 and 8 vertices. It's been a while since I last checked, but I think this limit is a constant, and as such would require recompiling löve with the altered box2d source code. Presumably the reason for this limit is just the computational intensity of handling very complex shapes, as well as box2d not handling concave shapes.

As such, I concur with darkfrei. You can make complex shapes out of simpler shapes.

Re: love.physics.newPolygonShape limit to 8 vertices

Posted: Fri Apr 07, 2023 9:06 am
by dusoft
darkfrei wrote: Fri Apr 07, 2023 8:07 am 3.Use several polygons and join them together.
Thanks!

Re: love.physics.newPolygonShape limit to 8 vertices

Posted: Fri Apr 07, 2023 9:29 am
by pgimeno
But don't use a joint, use fixtures. That's a mistake some people make.

Re: love.physics.newPolygonShape limit to 8 vertices

Posted: Fri Apr 07, 2023 11:50 am
by dusoft
pgimeno wrote: Fri Apr 07, 2023 9:29 am But don't use a joint, use fixtures. That's a mistake some people make.
Are you sure? A fixture connects a body with a shape. But a polygon shape can have only 8 vertices (https://love2d.org/wiki/love.physics.newPolygonShape).

So it sounds logical to use e.g. WeldJoint to connect two rigid bodies with 8 vertices each. Fixture is meant to connect 2 shapes with these 2 bodies, not to connect bodies together AFAIK.

Re: love.physics.newPolygonShape limit to 8 vertices

Posted: Fri Apr 07, 2023 12:57 pm
by pgimeno
You only have 1 body. You connect different fixtures to the same body.

Joints, even weld joints, are elastic and cause all kinds of trouble in a situation like this. Shapes attached to the same body via different fixtures, on the other hand, make it behave like a single rigid body.

Re: love.physics.newPolygonShape limit to 8 vertices

Posted: Fri Apr 07, 2023 2:12 pm
by dusoft
pgimeno wrote: Fri Apr 07, 2023 12:57 pm You only have 1 body. You connect different fixtures to the same body.

Joints, even weld joints, are elastic and cause all kinds of trouble in a situation like this. Shapes attached to the same body via different fixtures, on the other hand, make it behave like a single rigid body.
I see. I will experiment with that using multiples fixtures.
If I understand correctly, one would call:

Code: Select all

player_fixtures = {}
player_fixtures[1]=love.physics.newFixture( player_body, shape1)
player_fixtures[2]=love.physics.newFixture( player_body, shape2)
player_fixtures[3]=love.physics.newFixture( player_body, shape3)

Re: love.physics.newPolygonShape limit to 8 vertices

Posted: Sat Apr 08, 2023 8:33 am
by pgimeno
Yes, that's the idea.

Re: love.physics.newPolygonShape limit to 8 vertices

Posted: Thu Apr 13, 2023 9:29 pm
by dusoft
Just confirming that works well, so thanks again. It is a bit difficult to prepare points for such polygons as each shape is independent, but otherwise physics works OK.

example code in question would be:

Code: Select all

function createObject(world, params)
local body = love.physics.newBody(world, params.x, params.y, params.type)
    local shapes = {}
    local fixtures = {}
        if type(params.vertices[1]) == 'number' then
            shapes[1] = love.physics.newPolygonShape(params.vertices)
        else -- we have table with multiple shapes
            for i, shape in ipairs(params.vertices) do
                shapes[i] = love.physics.newPolygonShape(shape)
            end
        end
        for i, shape in ipairs(shapes) do
        fixtures[i] = love.physics.newFixture(body, shape, params.density)
    end
    return fixtures
end
Once you have fixtures for an object reference, you can easily access body or shape(s). Drawing works by looping over fixtures.