Help with how Collision Functions work (and bonus OOP class identification)

Hello!

So for reference, I'm trying to slowly develop a top down game. My current struggle is to get a super basic way to check what type of object an object is when the player collides into it, specifically so that I can create a basic "when the player interacts with an object with "npc class" and presses the A button, do "x".

One of my problems lies in the fact that I can't wrap my head around the way that playdate.graphics.sprite:overlappingSprites() works. If I want to find out what sprites are overlapping with the player sprite, should the code in the update function look like playerImage:overlappingSprites()? playdate.graphics.sprite:overlappingSprites(playerImage)? What exactly is being returned, and how can I access it?

The other problem is about how to label each object type in order to, say, tie the interaction to npc specific dialogue. (As I'm typing this, I'm realizing that it may be better to store this function in the base npc class, but it should be the same idea, just slightly different.

As you can see, my current attempt at it boils down to

local collisions = playdate.graphics.sprite:overlappingSprites(playerImage)
local firstCollided = collisions[1]:getTag()

if playdate.buttonIsPressed(playdate.kButtonA) and firstCollided == 2 then
print("This is an NPC")
end

With the "getTag" that I'm referencing being a part of the main.lua file (see below), is there a better way to do this?

Tags = {
player = 1,
npc = 2
}

I hope this makes sense, and I'll try to answer any other questions ya'll may have. Thanks so much!

When I did this in Daily Driver the order of the overlapping objects always varied. I'm not sure if this is because Lua tables aren't ordered or because of something else.

Anyway, I made sure tech had an embedded type ID that could be checked easily, and I have an if/else to check bloth.

Does that help?

It helps in so much as it explains one thing, but overall I'm still lost when it comes to what exactly I'm supposed to do for either of my problems. Is there something else I should be doing to check overlaps? What specific code should I be using to check tags? I know that may be too much for a question on these forums, but if you had anything you could point me in the direction of, I'm sure I could figure it out with a little more guidance.

The overlappingSprites() functions returns an array of sprites that are currently somehow overlapping the base sprite.

Typically what you might do is iterate through this array, checking the tag on each sprite.

for key, sprite in pairs( collisions ) do
    if ( sprite:getTag() == 1) then
    elseif ( sprite:getTag() == 2 ) then
        if ( playdate.buttonIsPressed( playdate.kButtonA ) ) then
            -- handle player clicking [A] while on NPC
        end
    end
end

Obviously you're doing almost this already.

1 Like

Thank you for this! I just have a couple follow up questions.

Do I need to define the array at some point in the code leading up to the code that you have? Or does it know that automatically if I'm calling the overlappingSprites() function? Is that where I would use the local collisions = Player:overlappingSprites()?

I'm a bit foggy on arrays, so apologies if this is basic, but what is the "key" and what is the "sprite" here? I know that the key is used to bring up a value in an array, but do I know the value of keys or is that something I'll also have to define earlier in the code? The sprite I'm assuming is the sprite that is being returned in the overlappingSprites() function, but please correct me if I'm wrong.

Does overlappingSprites() work when the collision boxes are not actually overlapping, but are right up against each other?

Does a tag apply if a class extends out of the class that has been tagged? For instance, would a helpfulNPC class that extends out of the "npc" class automatically be tagged as an "npc", or do I have to reapply the tag in each individual instance?

Otherwise, this has been extremely helpful, so thank you so much! I'll try to read up more on how to deal with arrays, as they seem like they're super helpful and used frequently in development.

Right, first a caveat: I know lots of languages, but not lua. I had to look stuff up for that response.

The API says it returns an array, so I would assume how you've assigned it to collisions is correct.

Lua arrays can have a key and a value, where the key is not necessarily an integer. (This is different to most other programming languages). So if you define an array element:

my_array[1] = "blah"
my_array["colour"] = "green"

The key is 1 and the value "blah". And for the second item, they key is "colour" and the item "green".

So it's not clear from the API doco what the keys would contain. My guess would just be an ordinal number; e.g.: 1,2,3,...

I would expect the tag still works with an extended class.

I don't know about overlapping Vs adjacent rectangles. If it's an issue, you could make the bounds rectangle bigger for the NPC sprites so they collide even when looking just "nearby".

You could also handle the A-Button separately, and when it's pushed iterate through your NPC objects working out if any of them is nearby. You could use the 2-point distance formula (euclidean distance) with their position coordinates Vs the player.

1 Like

Thank you again so much for this! I really appreciate it!

No worries. If you have more questions, just whack 'em up here. I'll answer if I can.