Polygon management library

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Polygon management library

Post by Bigfoot71 »

Hellooo everyone :ultraglee:

I come back to you about the library GeoMan that I shared some time ago which was quite messy and I didn't really know where I was going with it. The first goal was one of the functions related to the polygons (most of which were themselves readjusted from another library itself not very "clean") to make my life easier in a game I was making at the time which required also full of geometric functions, which resulted in this big pile of unsightly code :?.

Some time ago I therefore tried to roll up my sleeves to make it a library only focused on polygons that can fully manage collisions, ie which also returns the necessary to replace the shapes. And so here it is, I share my work with you if it can be useful to others ^^

The link to the GitHub repo: https://github.com/Bigfoot71/PolyMan/tree/main

Here is a small "technical demo" :neko:
Image

Here is the list of functions, without going into details (I'll do a readme on github when I have time):
  • getSignedArea
    getArea
    getPerimeter
    getCenter
    getCentroid
    getClosestVertice
    getBoundingBox
    getDistanceBetweenPolys
    setReverse
    setTransform
    setTranslation
    setPosition
    setRotation
    setScale
    isCCW
    isConvex
    isComplex
  • create.triangle
    create.rectangle
    create.ellipse
    create.circle
    create.donut
    create.random
  • collisions.pointInPolyFast -> (Detect collision only)
    collisions.pointInPoly -> (Returns normals and distance)
    collisions.polyInPolyFast -> (Detect collision only)
    collisions.polyInPolyConvex -> (Returns normals and distance)
    collisions.polyInPolyAABB -> (Returns normals and distance)
    collisions.segmentPoly -> (Detect collision only)
  • operations.boolean
    operations.simplify
    operations.convexHull
    operations.reduceVerts
I'm all ears for your suggestions and criticisms if there are any. :D

NB: If you have better ideas for naming the functions I'm all ears!
Attachments
PolyMan-Demo-v2.love
(12.21 KiB) Downloaded 110 times
PolyMan-Demo.love
(11.2 KiB) Downloaded 103 times
Last edited by Bigfoot71 on Fri Apr 07, 2023 11:49 pm, edited 2 times in total.
My avatar code for the curious :D V1, V2, V3.
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: Polygon management library

Post by darkfrei »

Can it support polygons with holes?
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Polygon management library

Post by Bigfoot71 »

darkfrei wrote: Fri Feb 10, 2023 6:09 am Can it support polygons with holes?
For now no, the collision detection with replacement function only works with convex polygons, I will work on that, from what I saw the way that would work best would be to work with a BSP tree which will be quite expensive but thanks for the suggestion I will share my progress on this subject.

Otherwise I know in advance for this one but I haven't tested with the "fast" function, I'll try this later.
The "fast" functions returning only true or false work with concave polygons but not tested with polygons with holes.
My avatar code for the curious :D V1, V2, V3.
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Polygon management library

Post by Bigfoot71 »

Hey everyone, I'm coming back here following some additions and optimizations made on this module but I'm currently stuck on one point if anyone can help me. I can't seem to find a way to handle the repositioning of concave polygons as efficiently as for convex polygons, here is an example.

The replacement of the second concave polygon with the function written for this case:
Image

Replace only the first polygon with this same function:
Image

No matter how I do it, I always get more or less the same result, the last way I tried (which is the one in the picture) is to decompose the polygons into triangles and average the normals and overlap, here is the current draft (which does not yet use a detection function suitable for triangles):

Code: Select all

local function polyInPolyComplex(polygon1, polygon2)

    local displacement_x, displacement_y = 0, 0
    local num_collisions = 0

    -- Triangulate polygons (using love.math.triangulate)
    local triangles1 = love.math.triangulate(polygon1)
    local triangles2 = love.math.triangulate(polygon2)

    -- Check each triangle for collision using polyInPolyConvex (SAT) function
    for i, tri1 in ipairs(triangles1) do
        for j, tri2 in ipairs(triangles2) do

            local collides, nx, ny, dist = polyInPolyConvex(tri1, tri2)

            if collides then

                -- Calculate displacement needed to separate polygons
                local displacement = dist / math.sqrt(nx * nx + ny * ny)
                local dx, dy = nx * displacement, ny * displacement

                -- Accumulate displacement
                displacement_x = displacement_x + dx
                displacement_y = displacement_y + dy
                num_collisions = num_collisions + 1

            end
        end
    end

    if num_collisions > 0 then
        -- Calculate average displacement
        displacement_x = displacement_x / num_collisions
        displacement_y = displacement_y / num_collisions
        return true, displacement_x, displacement_y
    else
        return false, 0, 0
    end

end
Does anyone have an idea to help me tackle this problem correctly, I'm starting to have a smoking brain trying a thousand and one things :death:
My avatar code for the curious :D V1, V2, V3.
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: Polygon management library

Post by darkfrei »

If the point is not in polygon before moving then it cannot be inside after movement, return the old position, not changed.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3551
Joined: Sun Oct 18, 2015 2:58 pm

Re: Polygon management library

Post by pgimeno »

Nah, that would create gaps. I don't think the question belongs to this subforum though.
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Polygon management library

Post by Bigfoot71 »

Thank you for your messages, it's true that I hesitated to post here, I thought there would be more context at hand, I reposted in the right sub-forum if interested. https://love2d.org/forums/viewtopic.php?f=4&t=94468
My avatar code for the curious :D V1, V2, V3.
Post Reply

Who is online

Users browsing this forum: No registered users and 42 guests