detect overlap (collision) for 2D game
the game is simple:
object moving through a maze of walls by direction keys
this is 2D like the old 'Nibbles' game
The walls are made up of a textbox array (shaped in small squares) and my object moves around the screen in increments equal to the height and width of one piece of the wall.
So, it simplifies whether or not the object has collided since i loop through like:
for t = 1 to 10
if object.top = txtWall(t).Top then
if object.Left = txtWall(t).Left then
(collision)
exit for
end if
end if
next t
and that works. problem. as the map gets more intricate (more than say 50 pieces of wall squares) the game slows to a crawl.
Faster way?
Thanks
Wengang
Uuuuuuuummmmmmmmmm..........
I would stop using a textbox array immediately.
Your first step is to declare a simple 3D Array. Make this array's dimensions the size of your map.
Dim 3DArrayMap ( mapWidth , mapHeight ) as Integer
If you want to check to see if your graphic is touching an area with a wall you could just:
If 3DArrayMap ( object.X / widthOfOneTile , object.Y / heightOfOneTile ) = 1 (where 1 is a wall, and 0 is nothing) Then
'// Now you do what you would do if you were on a wall
End If
object.X / widthOfOneTile - this will end up giving you an x coordinate if object.X (for example:60) / widthOfOneTile (example:20) divide out evenly. In this example, they would come out with an x coord of 3.
(I'm not too sure about what will happen if you try to go to coordinate 3dArray ( 1.433 , 84.43 ) in terms of errors, but it wouldn't be to hard to fix.)
If you need me to clarify (assuming you understand/care at all) what I said, I'll be more than happy to make more sense.