| View previous topic :: View next topic |
| Author |
Message |
Flashware
Joined: 04 Jul 2006 Posts: 3
|
Posted: Tue Jul 04, 2006 11:34 pm Post subject: Collision Detection |
|
|
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 |
|
| Back to top |
|
 |
Flashware
Joined: 04 Jul 2006 Posts: 3
|
Posted: Tue Jul 04, 2006 11:39 pm Post subject: Re: Collision Detection |
|
|
| damn I wantet to edit my last post and came on the quote button.... sorry |
|
| Back to top |
|
 |
SSpeare
Joined: 23 May 2006 Posts: 63
|
Posted: Sat Jul 08, 2006 1:39 am Post subject: |
|
|
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):
| Code: | 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})
|
Then you just write a function to test a block:
| Code: | 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
|
Then test every block. Something like:
| Code: | local blockCount = #blocks
for i=1,blockCount do
if (hitBlock(blocks[i])) then
-- process it
end
end
|
|
|
| Back to top |
|
 |
Flashware
Joined: 04 Jul 2006 Posts: 3
|
Posted: Wed Jul 12, 2006 3:04 pm Post subject: |
|
|
| thank you very much. I think I explaned it wrong to you, but you understood what I meened ;). |
|
| Back to top |
|
 |
|