Collision issues with multiple objects

I’m currently trying to replicate “Swipe Brick Breaker” a clone of one of the games made by 111% games.

https://www.youtube.com/watch?v=NrjuN6Vx7KM  <------ (this is what I’m trying to make)

 

I currently have 3 balls moving on screen but only the first ball spawned can properly collide with the bricks. The balls are being iterated through a ballTable with the enterFrame listener so that they can all move. I thought I would do the same for collision by having my collision function iterate through the balls so that all balls can collide with my bricks. Here’s what I have:

 

[spoiler]

 

Spawning the balls and adding them to Balltable

function spawnnewballs() local ball = display.newCircle( 100, 100, 5 ) ball.name = "ball" ball.collision = ballsCollision ball:addEventListener( "collision" ) -- add collision listener for each new ball ball.x = startx ball.y = starty ball.velocityX = newballvelocityX ball.velocityY = newballvelocityY physics.addBody(ball, "dynamic", {density = 1, friction = 0, bounce = 0, filter= ballCollisionFilter }) ballGroup.insert(ballGroup, ball) table.insert(ballTable, ball) end &nbsp;

Iterating through table for enterFrame

function updatenewbullet() for key,ball in pairs(ballTable) do if ball ~= nil then -- Movement ball.x = ball.x + ball.velocityX ball.y = ball.y + ball.velocityY -- If ball hits the ceiling or left or right wall, bounce off of it if ball.x \< 0 or ball.x + ball.width \> display.contentWidth then ball.velocityX = -ball.velocityX end -- Bounce off top wall if ball.y \< -30 then ball.velocityY = -ball.velocityY end -- stop if ball reaches bottem wall if ball.y \> display.contentHeight + 20 then ball.velocityY = 0 ball.velocityX = 0 ball:removeSelf() ball = nil end end end end &nbsp;

ISSUE: function only works for first ball spawned 

function ballsCollision(self, event) if ( event.phase == "began" ) then for key,ball in pairs(ballTable) do if ball ~= nil then if ball.y + ball.height \* 0.5 \< event.other.y + event.other.height \* 0.5 then if hitchecky == true then ball.velocityY = - ball.velocityY hitchecky = false --event.other:removeSelf() bricklocationx = event.other.x / 43 bricklocationy = event.other.y / 30 spawngrid[bricklocationx][bricklocationy] = 0 timer.performWithDelay( 20, changehitchecky ) end end if ball.x - ball.width \* 0.5 \> event.other.x - event.other.width \* 0.5 then if hitcheckx == true then ball.velocityX = - ball.velocityX hitcheckx = false --event.other:removeSelf() bricklocationx = event.other.x / 43 bricklocationy = event.other.y / 30 spawngrid[bricklocationx][bricklocationy] = 0 timer.performWithDelay( 20, changehitcheckx ) end end if ball.x + ball.width \* 0.5 \< event.other.x + event.other.width \* 0.5 then if hitcheckx == true then ball.velocityX = - ball.velocityX hitcheckx = false --event.other:removeSelf() bricklocationx = event.other.x / 43 bricklocationy = event.other.y / 30 spawngrid[bricklocationx][bricklocationy] = 0 timer.performWithDelay( 20, changehitcheckx ) end end if bullet.y - bullet.height \* 0.5 \> event.other.y - event.other.height \* 0.5 then if hitchecky == true then ball.velocityY = - ball.velocityY hitchecky = false --event.other:removeSelf() bricklocationx = event.other.x / 43 bricklocationy = event.other.y / 30 spawngrid[bricklocationx][bricklocationy] = 0 timer.performWithDelay( 20, changehitchecky ) end end end end end end &nbsp;

[/spoiler]

 

 

Not sure if I’m missing anything so help would be greatly appreciated.  

 

Another problem I have is implementing pickups. I’m using collision filters so that my balls only collide with my bricks and not other balls but in my collision function the parameter “event” doesn’t specify what my balls are colliding with. So if I add pickups my balls just end up doing what they’re supposed to do if they collide with bricks. I’ve tried first checking if the collided objet with a brick with: if event.other == brick or event.other.name = brick before going doing the collision results but none of those work. So I think I’m again missing something. Here’s what I have:

 

[spoiler]

 

How to check what event parameter is? 

function onLocalCollision(self, event) if ( event.phase == "began" ) then if bullet.y + bullet.height \* 0.5 \< event.other.y + event.other.height \* 0.5 and event.other = brick then -- DOESN"T WORK if hitchecky == true then velocityY = -velocityY hitchecky = false bricklocationx = event.other.x / 43 bricklocationy = event.other.y / 30 spawngrid[bricklocationx][bricklocationy] = spawngrid[bricklocationx][bricklocationy] - 1 if spawngrid[bricklocationx][bricklocationy] \< 1 then spawngrid[bricklocationx][bricklocationy] = 0 event.other:removeSelf() end brickCheck() --Remove brick instance --event.other:removeSelf() --score = score + 1 timer.performWithDelay( 20, changehitchecky ) end end if bullet.x - bullet.width \* 0.5 \> event.other.x - event.other.width \* 0.5 then if hitcheckx == true then velocityX = -velocityX hitcheckx = false --event.other:removeSelf() bricklocationx = event.other.x / 43 bricklocationy = event.other.y / 30 spawngrid[bricklocationx][bricklocationy] = spawngrid[bricklocationx][bricklocationy] - 1 if spawngrid[bricklocationx][bricklocationy] \< 1 then spawngrid[bricklocationx][bricklocationy] = 0 event.other:removeSelf() end brickCheck() timer.performWithDelay( 20, changehitcheckx ) end end if bullet.x + bullet.width \* 0.5 \< event.other.x + event.other.width \* 0.5 then if hitcheckx == true then velocityX = -velocityX hitcheckx = false --event.other:removeSelf() bricklocationx = event.other.x / 43 bricklocationy = event.other.y / 30 spawngrid[bricklocationx][bricklocationy] = spawngrid[bricklocationx][bricklocationy] - 1 if spawngrid[bricklocationx][bricklocationy] \< 1 then spawngrid[bricklocationx][bricklocationy] = 0 event.other:removeSelf() end brickCheck() timer.performWithDelay( 20, changehitcheckx ) end end if bullet.y - bullet.height \* 0.5 \> event.other.y - event.other.height \* 0.5 then if hitchecky == true then velocityY = -velocityY hitchecky = false --event.other:removeSelf() bricklocationx = event.other.x / 43 bricklocationy = event.other.y / 30 spawngrid[bricklocationx][bricklocationy] = spawngrid[bricklocationx][bricklocationy] - 1 if spawngrid[bricklocationx][bricklocationy] \< 1 then spawngrid[bricklocationx][bricklocationy] = 0 event.other:removeSelf() end timer.performWithDelay( 20, changehitchecky ) end end end end

[/spoiler]

 

My final question is how I can detect my bricks reaching a certain part of the screen. My bricks are made from a two dimensional array which is used to render the bricks. Hence I’m not to sure how to detect if any of my bricks have reached the bottem of the screen  (to signify it being game over). Here’s what I have for my bricks: 

 

[spoiler]

function renderBricks() bricks:removeSelf() bricks = display.newGroup() for i = 1,5 do for j = 1,8 do if spawngrid[i][j] \> 1 then local brick = display.newImage("images/brick.png") --brickGroup:insert(brick) brick.y = i \* 30 brick.x = j \* 43 brick.name = "brick" physics.addBody(brick, "static", {density = 1, friction = 0, bounce = 0, filter = brickCollisionFilter}) bricks.insert(bricks, brick) end end end end

[/spoiler]

 

Some direction to solving this would be great. 

 

And thank you for taking your time to read this lengthy post of mine :slight_smile: