How to make collision detection between block and block like this video :
http://www.youtube.com/watch?v=cl0fINVYizo&list=PL62822CEFBB8E7F7D
How to make collision detection between block and block like this video :
http://www.youtube.com/watch?v=cl0fINVYizo&list=PL62822CEFBB8E7F7D
Hi @martinlim520,
Please review the resources under “Physics” on this page:
http://docs.coronalabs.com/guide/index.html
Regards,
Brent Sorrentino
If use physics the blocks will falling down when stack too high .
Would you be interested in non-physics collision detection? Here’s a tutorial on it:
http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/
Brent Sorrentino
Is there any other way because I’m self teaching corona programming , I can’t undestand the http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/ .Someone say use the array to check the space empty and move down but how to do it .
Is there any other way because I’m self teaching corona programming , I can’t undestand the http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/ .Someone say use the array to check the space empty and move down but how to do it .
Hello again,
You could build a basic array, like a grid, and then check if a particular space is “empty” or not. The first thing you’ll need to learn is how to build multi-dimensional arrays (tables) in Lua. Do you know how to do that?
Regards,
Brent
I’ve got code somewhere in the forums from about 2 years ago that does this I think been a long time ago. I’ll see if I can find it
its not exactly what you need but this may help you figure out what you need
https://developer.coronalabs.com/forum/2011/09/06/checking-grid-array-line-patterns
https://developer.coronalabs.com/forum/2011/09/06/checking-grid-array-line-patterns . Is this code for detecting the same object .
Yes like a bejeweled style
Actually I want to know how to make the block stop falling down and stack when touch with other block .Do you know how to do it .
Is anyone know how to do that .This is my moveDown function :
local function moveDown(self) if self.y \< board[2][11].y then self.y =self.y + 1 else end end
Not at computer at the moment but the steps you need to take are
Say you have a grid of 5 by 5, just keeping it simple
You want to set each array entry to 0,
grid={} for a=1,5 do grid[a]={} for b=1,5 do grid[a][b]=0 end end
Now say you have a block enter at top
You check the grid at, lets say it’s coming in in row 2, position (1,2) if it 0 then it’s ok to move down one position and you set (1,2)=1 meaning there’s a block in that position now. Now the block is at (1,2) so now you check (2,2) if it’s 0 you move change (1,2) back to 0 and set (2,2)=1. Same thing if you move left or right. You just keep doing this until the position below the block is not a 0 which means now it’s stacked on top or it’s reached the bottom.
This is my grid array
local grid ={} for i =1,6 do grid[i] = {} for j =1 ,11 do grid[i][j] = 0 grid[i][j] = display.newRect(0,0,36,36) grid[i][j].x = i\*37 + 22 grid[i][j].y = j\*37 + 22 grid[i][j].alpha = 0.1 end end
then how to set the position be 1 and drop the block .
First thing is your setting grid[i][j]=0 then your overwriting it with newRect so first you need to correct that. Then let’s say you pick a random starting column and it’s 4 so you will check grid[4][1] to see if its 0 if it is then you move block to that location then check grid[4][2] and repeat until you get a 1 or reach grid[4][11] and where the block stops you change grid[][] =1
.
So , I don’t need this part
grid[i][j] = display.newRect(0,0,36,36) grid[i][j].x = i\*37 + 22 grid[i][j].y = j\*37 + 22 grid[i][j].alpha = 0.1
I still don’t know how to check 0 and 1 , can you give me a example code for the move and set the position be 1 function please ?
At work now will late
FALLING BLOCK EXAMPLE
here’s something i threw together quick hope this helps you
display.setStatusBar(display.HiddenStatusBar) -- setup variables local cols = 10 -- number of columns local rows = 5 -- number of rows local blockSize = 32 -- size of blocks local blocks = {} -- table of blocks on screen local gridTable = {} -- table of open slots local ready = true -- ready for next block local count = 1 -- just using this for example -- setup gridTable and draw grid function setupGrid() for c = 1, cols do gridTable[c] = {} vL = display.newLine((c-1)\*blockSize,0, (c-1)\*blockSize, blockSize\*rows) vL.width = 2 for r = 0, rows do gridTable[c][r] = 0 vH = display.newLine(0,r\*blockSize, cols\*blockSize, blockSize\*r) vH.width = 2 end end vL = display.newLine((cols)\*blockSize,0, (cols)\*blockSize, blockSize\*rows) vL.width = 2 end -- moves blocks down function moveBlockDown( arg ) local c,r = arg.loc[1], arg.loc[2] local newy = arg.y + blockSize arg.loc[1] = c arg.loc[2] = r+1 if gridTable[c+1][r+1] == 0 then transition.to(arg, {time = 250, y = newy, onComplete = function() arg.y = newy moveBlockDown(arg) end}) else -- add a little bounce newy = arg.y - blockSize/10 transition.to( arg, { time = 100, y = newy, onComplete = function() newy = arg.y+blockSize/10 transition.to( arg, { time = 100, y = newy }) end}) -- set gridTable to 1 gridTable[c+1][r] = 1 ready = true end end -- releases new block function newBlock() pick = math.random(0, cols-1) if gridTable[pick+1][1] == 0 then ready = false -- prevents new block until done with prev. block blocks[#blocks+1] = display.newRect( pick\*blockSize, 0-blockSize, blockSize, blockSize ) blocks[#blocks]:setFillColor(math.random(255), math.random(255), math.random(255), 255) blocks[#blocks].loc = { pick, 0 } arg = blocks[#blocks] print("move "..#blocks) moveBlockDown(blocks[#blocks]) elseif gridTable[pick+1][1] == 1 then print("picking new start") newBlock() end end -- call setupGrid setupGrid() -- this is just here to produce new blocks function callNewBlock() if ready == true and count \< rows\*cols/2 then count = count + 1 newBlock() callNewBlock() elseif ready == false and count \< rows\*cols/2 then timer.performWithDelay(250, callNewBlock) else print("done") end end -- start calling new block callNewBlock()