Need help with alphaCollision method

I've got a game in development called "Fling". Here's a screenshot.

Screen Shot 2022-09-21 at 9.59.23 AM

Basically, I'm trying to get a ball shot up in between that gap. However, since these sprites are squares instead of circles, I'm trying to utilize alphaCollision to detect if the circles are overlapping or not.

Specifically, I'm trying to figure out how to utilize alphaCollision in tandem with moveWithCollisions. Here is a snippet of code demonstrating what I'm trying to do:


function FlyingBall:update()
	self.xCoord = self.xCoord + self.xVelocity
	self.yCoord = self.yCoord + self.yVelocity

	local actualX, actualY, collisions, _ = self:moveWithCollisions(self.xCoord, self.yCoord)
	for i = 1, #collisions do
		local collision = collisions[i]
		if(collision.sprite:alphaCollision(collision.other)) then
			print("You are _actually_ colliding right now")
			local newBallXIndex, newBallYIndex = calculateIndices(actualX, actualY, 'ball')
			self:remove()
			Ball(newBallXIndex, newBallYIndex, ballSelector.patternName)
		end
	end
end

I never get inside the collision.sprite:alphaCollision(collision.other) conditional.

I think I'm doing something fundamentally wrong, but the docs don't provide any code examples that might help guide me to use this function.

Any help is much appreciated! Thanks in advance.

For simple geometric shapes it's usually better to do the collision purely with algebra.

Distance between two circles, etc.

So you can use the rectangle overlap to get a collision to check and then use the algebra approach.

That said, I have zero experience with moveWithCollisions so apologies if you have other plans that rule out the algebra approach.

I haven’t worked extensively with moveWithCollisions, but my guess is that you have a non-overlap collision response on one or both of the sprites which isn’t allowing them to get close enough together for the alpha collision to be true.

This was the approach I’ve been thinking about in my head since writing this post. I’ll give this a shot. I’d still love to hear from panic or - would love to have the docs updated with a proper example of using this method.

1 Like

I thought the same thing, but I slowed everything down and shot a FlyingBall through two rows of Balls - the entire time (alphaOverlap certain, right) I had debug statement saying “no collision”. When it hit the top wall it collided successfully with moveWithCollision.

Thanks for the replies!

Did you try the Examples that use it?

  • SpriteCollisionMasks
  • SingleFileExamples/Collisions
  • FlippyFish
  • Asteroids
  • 2020
  • Level 1-1

I tried your code and it works fine. It just needs
FlyingBall:setCollideRect(0, 0, width, height)
and
FlyingBall.collisionResponse = playdate.graphics.sprite.kCollisionTypeOverlap

alphaCollision
main.zip (1.2 KB)

1 Like