breakout collision

Hi everyone,
I’m contacting you cause I’ve got a code issue on “breackout”. I’ve inserted a code that allows the bricks to be indestructible and the code was working correctly on a single brick. After that I’ve inserted a table in the “cicle for” to create more bricks. When I used the table the code stopped working. An error doesn’t appear but the ball slides around the bricks. Where have I gone wrong? I will also post the code for you.

–table

local levels = {}

levels[2] = {{0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,1,0,1,0,0,0,0,0},

{0,0,0,0,0,1,0,0,0,0,0,0},

{0,0,0,0,0,1,0,0,0,0,0,0},

{1,0,1,0,0,1,0,1,0,0,1,0},}

– change level

function changeLevel()

– Pulizia del gruppo mattoni

mattoni:removeSelf()

mattoni.numChildren = 0

mattoni = display.newGroup()

if vy > 0 then vy = -vy

end

vx = vx + 0.5

vy = vy - 0.5

vLevel = vy

alertBox:removeEventListener(“tap”, changeLevel)

alertDisplayGroup:removeSelf()

alertDisplayGroup = nil

paddle.x = 240; paddle.y = 300

ball.x = 240; ball.y = 275

currentLevel = currentLevel + 1

levelNum = display.newText(currentLevel, 460, 8, “Arial”, 14)

levelNum:setFillColor(255, 255, 255, 255)

if vite == 3 then

lifeNum:removeSelf()

lifeNum = nil

lifeNum = display.newText(vite, 460, 25, “Arial”, 14)

lifeNum:setFillColor(255, 255, 255, 255)

end

gameLevels((levels[2]))

end

–Game Listener

function gameListeners(event)

if event == “add” then

Runtime:addEventListener(“accelerometer”, movePaddle)

– Utilizzo del trascinamento, atttivo solo nel simulatore

paddle:addEventListener(“touch”, dragPaddle)

– Gesione della palla

Runtime:addEventListener(“enterFrame”, updateBall)

paddle:addEventListener(“collision”, bounce)

ball:addEventListener(“collision”, removeBrick)

elseif event == “remove” then

Runtime:removeEventListener(“accelerometer”, movePaddle)

– Utilizzo del trascinamento, atttivo solo nel simulatore

paddle:removeEventListener(“touch”, dragPaddle)

– Gesione della palla

Runtime:removeEventListener(“enterFrame”, updateBall)

paddle:removeEventListener(“collision”, bounce)

ball:removeEventListener(“collision”, removeBrick)

end

end

–LEVEL

function gameLevels(level)

mattoni1:toFront()

local len = table.maxn(level)

for i = 1, len do

for j = 1, W_LEN do

if(level[i][j] == 1) then

local mattoneRosso = display.newImage(‘mattone02.png’)

mattoneRosso.name = ‘mattoneRosso’

mattoneRosso.x = BRICK_W * j - OFFSET

mattoneRosso.y = BRICK_H * i

physics.addBody(mattoneRosso, {density = 1, friction = 0, bounce = 0})

mattoneRosso.bodyType = ‘static’

mattoni1:insert( mattoneRosso )

end

end

end

background:addEventListener(“tap”, startGame)

end

function updateBall()

ball.x = ball.x + vx

ball.y = ball.y + vy

–CODE FOR MATTONEROSSO

if (ball.y + ball.height*.5 >= mattoneRosso.y - mattoneRosso.contentHeight*.5 and ball.y - ball.height*.5 <= mattoneRosso.y + mattoneRosso.contentHeight*.5) then

if (ball.x+ball.width*.5 >= mattoneRosso.x - mattoneRosso.width/2 and ball.x - ball.width*.5 <= mattoneRosso.x + mattoneRosso.width/2) then

vy = -vy

end

end

if ball.x < 0 or ball.x + ball.width > display.contentWidth then

vx = -vx

end – Collisione con le pareti laterali

if ball.y < 0 then

vy = -vy

end

if ball.y + ball.height > paddle.y + paddle.height then

gameListeners(“remove”)

ball.x = 240; ball.y = 275 – reset della palla

paddle.x = 240; paddle.y = 300 – reset del paddle

vy = -vy – reset direzione

vite = vite - 1

if vite < 0 then resetGame()

else

lifeNum:removeSelf()

lifeNum = nil

lifeNum = display.newText(vite, 460, 25, “Arial”, 14)

lifeNum:setFillColor(255, 255, 255, 255)

background:addEventListener(“tap”, startGame)

end

end

end 

No one can help me?

It’s quite hard to debug code anyway and it isn’t helped when the indentation is lost and half of it is in Italian (not your fault). I can’t see any indestructible brick code.

When bounces go ‘around the edge’, it is sometimes because the velocity has changed but it is still within the capture area ; so it keeps bouncing because it never gets sufficiently far away from the thing its bouncing off.

When you have a brick collision (this is CODE FOR MATTONEROSSO ?) try deducting vx,vy from the ball positions before reversing the velocity, this *might* fix it. 

I translated the code in english

–table

local levels = {}

levels[2] = {{0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,1,0,1,0,0,0,0,0},

{0,0,0,0,0,1,0,0,0,0,0,0},

{0,0,0,0,0,1,0,0,0,0,0,0},

{1,0,1,0,0,1,0,1,0,0,1,0},}

– change level

function changeLevel()

–reset bricks

bricks:removeSelf()

bricks.numChildren = 0

bricks = display.newGroup()

if vy > 0 then vy = -vy

end

vx = vx + 0.5

vy = vy - 0.5

vLevel = vy

–reset alertBox

alertBox:removeEventListener(“tap”, changeLevel)

alertDisplayGroup:removeSelf()

alertDisplayGroup = nil

 --reset paddle / ball

paddle.x = 240; paddle.y = 300

ball.x = 240; ball.y = 275

currentLevel = currentLevel + 1

levelNum = display.newText(currentLevel, 460, 8, “Arial”, 14)

levelNum:setFillColor(255, 255, 255, 255)

if life == 3 then

lifeNum:removeSelf()

lifeNum = nil

lifeNum = display.newText(vite, 460, 25, “Arial”, 14)

lifeNum:setFillColor(255, 255, 255, 255)

end

gameLevels((levels[2]))

end

–Game Listener

function gameListeners(event)

if event == “add” then

Runtime:addEventListener(“accelerometer”, movePaddle)

paddle:addEventListener(“touch”, dragPaddle)

Runtime:addEventListener(“enterFrame”, updateBall)

paddle:addEventListener(“collision”, bounce)

ball:addEventListener(“collision”, removeBrick)

elseif event == “remove” then

Runtime:removeEventListener(“accelerometer”, movePaddle)

paddle:removeEventListener(“touch”, dragPaddle)

Runtime:removeEventListener(“enterFrame”, updateBall)

paddle:removeEventListener(“collision”, bounce)

ball:removeEventListener(“collision”, removeBrick)

end

end

–LEVEL

function gameLevels(level)

bricks:toFront()

local len = table.maxn(level)

for i = 1, len do

for j = 1, W_LEN do

if(level[i][j] == 1) then

local redBrick = display.newImage(‘mattone02.png’)

redBrick.name = ‘redBrick’

redBrick.x = BRICK_W * j - OFFSET

redBrick.y = BRICK_H * i

physics.addBody(redBrick, {density = 1, friction = 0, bounce = 0})

redBrick.bodyType = ‘static’

bricks:insert( redBrick )

end

end

end

background:addEventListener(“tap”, startGame)

end

function updateBall()

ball.x = ball.x + vx

ball.y = ball.y + vy

–collision with redBrick

if (ball.y + ball.height*.5 >= redBrick.y - redBrick.contentHeight*.5 and ball.y - ball.height*.5 <= redBrick.y + redBrick.contentHeight*.5) then

if (ball.x+ball.width*.5 >= redBrick.x - redBrick.width/2 and ball.x - ball.width*.5 <= redBrick.x + redBrick.width/2) then

vy = -vy

end

end

–collision with lateral walls

if ball.x < 0 or ball.x + ball.width > display.contentWidth then

vx = -vx

end

if ball.y < 0 then

vy = -vy

end

if ball.y + ball.height > paddle.y + paddle.height then

gameListeners(“remove”)

ball.x = 240; ball.y = 275 – reset ball

paddle.x = 240; paddle.y = 300 – reset paddle

vy = -vy – reset direction ball

vite = vite - 1

if vite < 0 then resetGame()

else

lifeNum:removeSelf()

lifeNum = nil

lifeNum = display.newText(vite, 460, 25, “Arial”, 14)

lifeNum:setFillColor(255, 255, 255, 255)

background:addEventListener(“tap”, startGame)

end

end

end

Thanks for your solution but this doesn’t work. The redbrick function starts only if the table is not uploaded, if I insert the table with “cicle for” doesn’t work.  When I put the red brick outside table all goes okay.

This is my problem. Thanks for solutions.

No one can help me?

It’s quite hard to debug code anyway and it isn’t helped when the indentation is lost and half of it is in Italian (not your fault). I can’t see any indestructible brick code.

When bounces go ‘around the edge’, it is sometimes because the velocity has changed but it is still within the capture area ; so it keeps bouncing because it never gets sufficiently far away from the thing its bouncing off.

When you have a brick collision (this is CODE FOR MATTONEROSSO ?) try deducting vx,vy from the ball positions before reversing the velocity, this *might* fix it. 

I translated the code in english

–table

local levels = {}

levels[2] = {{0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,1,0,1,0,0,0,0,0},

{0,0,0,0,0,1,0,0,0,0,0,0},

{0,0,0,0,0,1,0,0,0,0,0,0},

{1,0,1,0,0,1,0,1,0,0,1,0},}

– change level

function changeLevel()

–reset bricks

bricks:removeSelf()

bricks.numChildren = 0

bricks = display.newGroup()

if vy > 0 then vy = -vy

end

vx = vx + 0.5

vy = vy - 0.5

vLevel = vy

–reset alertBox

alertBox:removeEventListener(“tap”, changeLevel)

alertDisplayGroup:removeSelf()

alertDisplayGroup = nil

 --reset paddle / ball

paddle.x = 240; paddle.y = 300

ball.x = 240; ball.y = 275

currentLevel = currentLevel + 1

levelNum = display.newText(currentLevel, 460, 8, “Arial”, 14)

levelNum:setFillColor(255, 255, 255, 255)

if life == 3 then

lifeNum:removeSelf()

lifeNum = nil

lifeNum = display.newText(vite, 460, 25, “Arial”, 14)

lifeNum:setFillColor(255, 255, 255, 255)

end

gameLevels((levels[2]))

end

–Game Listener

function gameListeners(event)

if event == “add” then

Runtime:addEventListener(“accelerometer”, movePaddle)

paddle:addEventListener(“touch”, dragPaddle)

Runtime:addEventListener(“enterFrame”, updateBall)

paddle:addEventListener(“collision”, bounce)

ball:addEventListener(“collision”, removeBrick)

elseif event == “remove” then

Runtime:removeEventListener(“accelerometer”, movePaddle)

paddle:removeEventListener(“touch”, dragPaddle)

Runtime:removeEventListener(“enterFrame”, updateBall)

paddle:removeEventListener(“collision”, bounce)

ball:removeEventListener(“collision”, removeBrick)

end

end

–LEVEL

function gameLevels(level)

bricks:toFront()

local len = table.maxn(level)

for i = 1, len do

for j = 1, W_LEN do

if(level[i][j] == 1) then

local redBrick = display.newImage(‘mattone02.png’)

redBrick.name = ‘redBrick’

redBrick.x = BRICK_W * j - OFFSET

redBrick.y = BRICK_H * i

physics.addBody(redBrick, {density = 1, friction = 0, bounce = 0})

redBrick.bodyType = ‘static’

bricks:insert( redBrick )

end

end

end

background:addEventListener(“tap”, startGame)

end

function updateBall()

ball.x = ball.x + vx

ball.y = ball.y + vy

–collision with redBrick

if (ball.y + ball.height*.5 >= redBrick.y - redBrick.contentHeight*.5 and ball.y - ball.height*.5 <= redBrick.y + redBrick.contentHeight*.5) then

if (ball.x+ball.width*.5 >= redBrick.x - redBrick.width/2 and ball.x - ball.width*.5 <= redBrick.x + redBrick.width/2) then

vy = -vy

end

end

–collision with lateral walls

if ball.x < 0 or ball.x + ball.width > display.contentWidth then

vx = -vx

end

if ball.y < 0 then

vy = -vy

end

if ball.y + ball.height > paddle.y + paddle.height then

gameListeners(“remove”)

ball.x = 240; ball.y = 275 – reset ball

paddle.x = 240; paddle.y = 300 – reset paddle

vy = -vy – reset direction ball

vite = vite - 1

if vite < 0 then resetGame()

else

lifeNum:removeSelf()

lifeNum = nil

lifeNum = display.newText(vite, 460, 25, “Arial”, 14)

lifeNum:setFillColor(255, 255, 255, 255)

background:addEventListener(“tap”, startGame)

end

end

end

Thanks for your solution but this doesn’t work. The redbrick function starts only if the table is not uploaded, if I insert the table with “cicle for” doesn’t work.  When I put the red brick outside table all goes okay.

This is my problem. Thanks for solutions.