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?
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.
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
local dx, dy = 0, 0
if playdate.buttonJustReleased( playdate.kButtonB ) then
if selected_poly == p1 then
selected_poly = p2
selected_poly = p1
if playdate.buttonIsPressed( playdate.kButtonUp ) then
dy = dy - 1
if playdate.buttonIsPressed( playdate.kButtonDown ) then
dy = dy + 1
if playdate.buttonIsPressed( playdate.kButtonRight ) then
dx = dx + 1
if playdate.buttonIsPressed( playdate.kButtonLeft ) then
dx = dx - 1
if dx ~= 0 or dy ~= 0 then
local tf = playdate.geometry.affineTransform.new()
gfx.drawText("closed: " .. tostring(p1:isClosed()), 250, 20)
gfx.drawText("closed: " .. 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
for j = 1, p2:count() do
if p1:containsPoint(p2:getPointAt(j)) then
i = true
text = "points: " .. tostring(i)
gfx.drawText(text, 20, 200)
if playdate.buttonJustReleased( playdate.kButtonA ) then
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!
Many thanks, appreciate you taking a look at it.