Sprite Collision And Properties

,

I have a sprite class like car. and I have another sprite class called person. I'm able do collision detection, but on collision I want to check the sprite property, like it's color, or speed.

function Car:update()
    CarBase.super.update(self)

    local collisions = gfx.sprite.allOverlappingSprites()
    local length = #collisions
    --print("Length " .. length)
for i = 1, #collisions do
        local collisionPair = collisions[i]
        print(collisionPair)
        local sprite1 = collisionPair[1]
        local sprite2 = collisionPair[2]
        -- do something with the colliding sprites
        print("collision?")
        print("Length " .. length)
        print(sprite1)
        
        --print(sprite1.color)

end

I've tried other things like collisionResponse, but couldn't figure out how it worked.

I also tried looking at 2020 example. It does have code in there where it's checking collison.other.isEnemy but I couldn't figure out how they're getting that property. I assume it's because it's a function and it's all inside of the main.lua file. Mine is in separate class files.

Still, if I knew which sprites collided, I should be able to call another method on that sprite class.... right? I know I'm missing something, I just don't know what it is.
Tips on what to include in your question:

You're almost there!

One thing to remember is that sprite1 and sprite2 aren't always returned in the same order. At least in my experience.

So you need to check the type of the sprite first, I added another field, and then you can check the correct property.

I don't understand.. all I have to do is check the type and then I'm able to see the property? For kicks I added the same property to both and still didn't give me anything.

What I meant is you cannot assume which one will come first and if you look for on existent property it will not be there.

Let's check some fundamentals...

print(sprite1) gives you a table location address?

printTable(sprite1) gives you readable contents of table?

Right, print(sprite1) does give me just a hex address. I actually didn't know printTable was a thing. When I do that I do see information about the sprite. I still don't see the properties though.

Bleh, this is even more confusing... When I look at the collision example file it has

local function updatePlayer(dt, player)
	
  local speed = player.speed

  local dx, dy = 0, 0
  
-- Skipping the button stuff
  
  dx = player.velocityX * dt
  dy = player.velocityY * dt

	if dx ~= 0 or dy ~= 0 then
		
		local actualX, actualY, cols, cols_len = player:moveWithCollisions(player.x + dx, player.y + dy)

	    for i=1, cols_len do -- This is just like my collision loop
	      local col = cols[i]      -- Just like me getting the sprite1
	    	-- not trying to be physics-accurate
	    	if col.normal.x ~= 0 then -- hit something in the X direction
				-- if we were sliding we'd just want to set our velocityX to zero, but we're bouncing, so...
				if col.other:isa(Player) then
					-- collided with another player, so transfer some of our velocity to that player
					col.other.velocityX = col.other.velocityX + (player.velocityX * 0.2) -- How they getting other.velocityX???
					player.velocityX = -(player.velocityX * 0.8)
					player.velocityX = player.velocityX + (-col.other.velocityX * 0.2)

When I try using other it just gives me an error like "other" does not exist. O....O what the heck????

UPDATE

  • OOOK I've figured something out.

When I was making my class, I was using local variables at the top, initializing them in the constructor and using them throughout the class.

NOW I see that these local variables are only visible to the class, kind of like using private. If I just do something like car.color = "red" without using any type of local variable. I'm able to see it when I do the collision check.

(code tangent ahead O....O)
I'm not sure why this is. I'd think that anything not set to private (like local) would be global and all could see it. Doesn't make sense to me yet how a variable like (car.color) can be accessed when it's not even initialized in the file up front. I'd expect it would go (color not found) or something since I'm trying to use something that isn't even defined.
(k, I'm done)

If you're using a class like I am.

import "coreLibs/crank"
import "CoreLibs/sprites"
import "CoreLibs/animation"
import "CoreLibs/timer"

local pd <const> = playdate
local gfx <const> = pd.graphics

class('Car').extends(gfx.sprite)
local car = nil
function Car:init(x,y,base)
    
    car = gfx.sprite.new(base)
    car:setRotation(90)
    car:moveTo(x,y)
	car.color = "red" -- Since you're doing .color, you can access it later
    car:add()
  
end

function Car:update()
    Car.super.update(self)
    
    local collisions = gfx.sprite.allOverlappingSprites()
    local length = #collisions
	
    for i = 1, length do
		local collisionPair = collisions[i]
		local sprite1 = collisionPair[1]
		local sprite2 = collisionPair[2]
		
		print("SPRITE 2")
		printTable(sprite2)
		print(sprite2.color) -- should now print red
    end
end

I'll now change my variables to just be in the init, and see how that goes.

1 Like