Collision Detection

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
Flashware
Posts: 3
Joined: Tue Jul 04, 2006 11:28 pm

Collision Detection

Post by Flashware »

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
Flashware
Posts: 3
Joined: Tue Jul 04, 2006 11:28 pm

Re: Collision Detection

Post by Flashware »

damn I wantet to edit my last post and came on the quote button.... sorry
SSpeare
Posts: 63
Joined: Tue May 23, 2006 11:45 pm
Contact:

Post by SSpeare »

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: 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})
Then you just write a function to test a block:

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
Then test every block. Something like:

Code: Select all

local blockCount = #blocks
for i=1,blockCount do
  if &#40;hitBlock&#40;blocks&#91;i&#93;&#41;&#41; then
     -- process it
  end
end
Flashware
Posts: 3
Joined: Tue Jul 04, 2006 11:28 pm

Post by Flashware »

thank you very much. I think I explaned it wrong to you, but you understood what I meened ;).
Post Reply