Detect Rotated Sprite Overlaping

Hello!
I have a script that is giving me a lot of headaches.
I have 2 sprites that I need to know if they collide at some point. My problem is that both sprites have a rotation, and as far as I have been able to check, you can't rotate a collider.
It occurred to me to check if both sprites are overlapping. Checking the documentation, I found this method:

playdate.graphics.sprite:overlappingSprites()

But according to the documentation, it only works if the sprites have a collide rects. So I still have the same problem.

Is there any method in the SDK or any simple trick to solve this? And if this overlapping takes into account the transparencies of the sprites, even better!
Thanks!

I think you should try what you've found.

  • the collide rect can simply be big enough to encompass all rotations footprint
  • or you can store a different collide rect per frame
  • or roll your own collision function

Then, once you know there's a ballpark collision, you can do a more accurate but slower check with: https://sdk.play.date/inside-playdate/#m-graphics.sprite.alphaCollision

1 Like

You could calculate the “rotated rectangle” vertices and store them as a gfx.polygon in a custom attribute in the sprite object. Then use polygon:intersects(otherpoly) to detect collision.

You could do that every frame, or you could set a maximum bounding box and do the poly check only if that initial tripwire goes off.

2 Likes

Thanks to both of you! I went for Matt's solution of making the collider bigger to cover the whole sprite rotation. And in the event playdate.graphics.sprite:collisionResponse(other) I ask for the method playdate.graphics.sprite:alphaCollision(anotherSprite).

class("MyClass").extends(gfx.sprite)

function MyClass:collisionResponse(other)
    if self:alphaCollision(other) then
        return gfx.sprite.kCollisionTypeFreeze
    else
        return gfx.sprite.kCollisionTypeOverlap
    end
end

This seems to work great!
Thank you!

3 Likes