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

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
dusoft
Party member
Posts: 715
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

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

Post 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)
Last edited by dusoft on Fri Apr 14, 2023 11:49 am, edited 1 time in total.
User avatar
darkfrei
Party member
Posts: 1216
Joined: Sat Feb 08, 2020 11:09 pm

Re: love.physics.newPolygonShape limit to 8 vertices

Post by darkfrei »

3.Use several polygons and join them together.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
MrFariator
Party member
Posts: 563
Joined: Wed Oct 05, 2016 11:53 am

Re: love.physics.newPolygonShape limit to 8 vertices

Post 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.
User avatar
dusoft
Party member
Posts: 715
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: love.physics.newPolygonShape limit to 8 vertices

Post by dusoft »

darkfrei wrote: Fri Apr 07, 2023 8:07 am 3.Use several polygons and join them together.
Thanks!
User avatar
pgimeno
Party member
Posts: 3691
Joined: Sun Oct 18, 2015 2:58 pm

Re: love.physics.newPolygonShape limit to 8 vertices

Post by pgimeno »

But don't use a joint, use fixtures. That's a mistake some people make.
User avatar
dusoft
Party member
Posts: 715
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: love.physics.newPolygonShape limit to 8 vertices

Post 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.
User avatar
pgimeno
Party member
Posts: 3691
Joined: Sun Oct 18, 2015 2:58 pm

Re: love.physics.newPolygonShape limit to 8 vertices

Post 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.
User avatar
dusoft
Party member
Posts: 715
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: love.physics.newPolygonShape limit to 8 vertices

Post 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)
User avatar
pgimeno
Party member
Posts: 3691
Joined: Sun Oct 18, 2015 2:58 pm

Re: love.physics.newPolygonShape limit to 8 vertices

Post by pgimeno »

Yes, that's the idea.
User avatar
dusoft
Party member
Posts: 715
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: love.physics.newPolygonShape limit to 8 vertices

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

Who is online

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