Collision issues - unpredictable behaviour

Running on both the playdate and on the mac simulator I am experiencing some unpredictable collision behaviour, and at this point i don't really know what the course is.

Here in the first gif I hit the meteor but it does not trigger the health bar:

Here is a couple of situations where sometimes it works, sometimes it does not:

I have tried various things, but I also don't have a very good understanding of how this works so I might have missed something obvious.

I have tried:
> playerSprite:setCollideRect( 0, 0, playerSprite:getSize() )

and I also tried setting it manually to values both above and below the sprite size
> playerSprite:setCollideRect( 0, 0, 16,11 )

I have tried having
> playerSprite:moveWithCollisions(playerSprite.x, playerSprite.y)

both in the playdate update and outside - when outside the update function the health bar function never runs.

I have the healthcare function running outside playdate update:

> function playerSprite:collisionResponse()
>     moveHpBar()
> end

This is the function it runs

> function moveHpBar() 
>     print('Collision! :(')
>     hpLevel = hpLevel - 10
>     if hpBarInnerSprite.x > 83 then
>     hpBarInnerSprite:moveBy(-20,0)
>     end
>     if hpBarInnerSprite.x <= 83 then
>         hpBarInnerSprite:moveTo(83,16)
>         print('you are dead')
>     end
>     laserState = 'off'
>     print(hpBarInnerSprite.x,hpLevel)
> end


Are you using moveWithCollisions on the meteor as well as the player? It’s hard to tell, but looks like player -> meteor collision triggers the response, but meteor -> player doesn’t.

1 Like

I am moving collision on both, and it is happening inside playdate update for both of them. But maybe that is not correct? This is what I have in the function that moves the meteor.

self.meteorSprite:moveWithCollisions(self.meteorSprite.x, self.meteorSprite.y)

Are your x and y positions integers?

You can quickly round a value using //1 (integer divide by 1). So do you see any difference with:

self.meteorSprite:moveWithCollisions(self.meteorSprite.x//1, self.meteorSprite.y//1)

And similarly in any other moves in your code.

1 Like

Hi Matt, yes all my values are integer as far as I can tell, adding the //1 did not seem to have any effect, this situation where I hit it head on or straight from behind seems to consistently not trigger the moveHealth:
Here is a snapshot of some player and meteorSprite.x,y

meteor: -313.0 334.0
meteor: -85.0 226.0
meteor: 275.0 23.0
meteor: 211.0 206.0
meteor: 192.0 60.0
meteor: -200.0 -92.0
meteor: -200.0 -120.0
meteor: -200.0 -120.0
meteor: -200.0 -120.0
PlayerSprite 161.0 54.0

So you’ll need the meteor collision with player to trigger moveHpBar, as well as the player collision with meteor.

I’d add some print statements to diagnose what’s happening when it works vs when it doesn’t.


I see, I did not know there was a difference between player hitting a meteor or meteor hitting player. I will see if I can dig a bit deeper into this. Thanks!