Playdate.geometry.polygon:intersects(p)

I’ve been trying to use polygon:intersects() for some collision detection and it seems to always return true. If I instead use the bounding boxes for the polygons (via polygon:getBoundsRect) then I get results as expected.

Has anyone tried this and had better results? I’m on the 0.11.1 version of the simulator. Same behaviour seen on physical console.

Just a thought: are your polygons closed?

playdate.geometry.polygon:setClosed(bool)

Good question, just checked and yes isClosed() returns true:

Apologies for quantity of text but this is my test program. No doubt fresh eyes will spot an obvious mistake on my part.

import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/timer"

local gfx <const> = playdate.graphics

local p1 = playdate.geometry.polygon.new(0, 0, 100, 0, 100, 50, 0, 50, 0, 0)
local p2 = playdate.geometry.polygon.new(75, 25, 125, 25, 125, 125, 75, 125, 75, 25)
local selected_poly = p1

function playdate.update()
    
    local dx, dy = 0, 0
    
    if playdate.buttonJustReleased( playdate.kButtonB ) then
        if selected_poly == p1 then
            selected_poly = p2
        else
            selected_poly = p1
        end
    end

    if playdate.buttonIsPressed( playdate.kButtonUp ) then
        dy = dy - 1
    end

    if playdate.buttonIsPressed( playdate.kButtonDown ) then
        dy = dy + 1
    end

    if playdate.buttonIsPressed( playdate.kButtonRight ) then
        dx = dx + 1
    end
    
    if playdate.buttonIsPressed( playdate.kButtonLeft ) then
        dx = dx - 1
    end

    if dx ~= 0 or dy ~= 0 then
        local tf = playdate.geometry.affineTransform.new()
        
        tf:translate(dx, dy)
        tf:transformPolygon(selected_poly)
    end
    
    gfx.clear()
    gfx.setColor(playdate.graphics.kColorBlack)

    gfx.drawPolygon(p1)
    gfx.drawPolygon(p2)
    
    gfx.drawText("closed[1]: " .. tostring(p1:isClosed()), 250, 20)
    gfx.drawText("closed[2]: " .. tostring(p2:isClosed()), 250, 40)
    
    i = p1:intersects(p2)
    text = "poly: " .. tostring(i)
    gfx.drawText(text, 20, 160)
    
    local r1 = p1:getBoundsRect()
    local r2 = p2:getBoundsRect()
    
    i = r1:intersects(r2)
    text = "rect: " .. tostring(i)
    gfx.drawText(text, 20, 180)

    i = false
    for j = 1, p1:count() do
        if p2:containsPoint(p1:getPointAt(j)) then
            i = true
        end
    end
    for j = 1, p2:count() do
        if p1:containsPoint(p2:getPointAt(j)) then
            i = true
        end
    end
    text = "points: " .. tostring(i)
    gfx.drawText(text, 20, 200)

    if playdate.buttonJustReleased( playdate.kButtonA ) then
        print(p1)
        print(r1)
        print(p2)
        print(r2)
    end
end

Rob, this does appear to be a bug in our SDK. I am filing an issue for it, and writing a unit test so hopefully we do not have this problem in the future.

Sorry for the trouble/confusion!

Greg

1 Like

Many thanks, appreciate you taking a look at it.