Well I started to program a very easy version of breakout (no extra's or other things, just break out the blocks). I have a idea how to do the "physiks" and the collision detection. But then I recognized that I have to check every block's coordinates and now I wonder how to solve the problem more easy.
A friend of mine has nearly the same problem. He wants to make a vertical shooter, but doesn't want to check the coordinates of each objec by him self....
btw. I looked at the source of the breakout clone which can be downloadet on luaplayer.org, but it was very confusing (because most of the variable names are not in english)
Please excuse my bad english, but I'm german ;D
Collision Detection
Moderators: Shine, Insert_witty_name
Re: Collision Detection
damn I wantet to edit my last post and came on the quote button.... sorry
You do have to check every block's coordinates. That is the only way to do it. If you had 10,000 blocks you would probably need an additional data structure to filter the blocks spatially, but that shouldn't be necessary here.
If you have an array of blocks that are all the same size (I assume):
Then you just write a function to test a block:
Then test every block. Something like:
If you have an array of blocks that are all the same size (I assume):
Code: Select all
blockWidth = 20
blockHeight = 10
blocks = {}
table.insert(blocks, {x:10,y:20})
table.insert(blocks, {x:30,y:20})
table.insert(blocks, {x:10,y:30})
Code: Select all
function hitBlock(x,y,block)
if x >= block.x and x <= block.x+blockWidth and
y >= block.y and y <= block.y+blockHeight then
return true
else
return false
end
end
Code: Select all
local blockCount = #blocks
for i=1,blockCount do
if (hitBlock(blocks[i])) then
-- process it
end
end