Click to See Complete Forum and Search --> : detect overlap (collision) for 2D game
wengang
Feb 24th, 2002, 10:41 AM
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
Usako
Feb 26th, 2002, 01:09 AM
You only have to check for collision with the tiles surrounding the object :)
wengang
Feb 26th, 2002, 09:58 AM
but I won't know which 'tiles' are surrounding the object without going through them all in a loop
right?
Usako
Feb 26th, 2002, 11:36 AM
depends on how you store your map.
If you have your map in one big map[x][y] array then it's really simple
wengang
Feb 26th, 2002, 11:46 AM
I don't have a map. As I said, I just used a text box array
Janus
Feb 26th, 2002, 01:49 PM
Try creating an array of Rects to represent your walls, looping through them will be faster. Also, use the With / End With statements to speed up the comparisons.
wengang
Feb 26th, 2002, 06:48 PM
Well, I haven't done that before. But I'll try it out.
Thanks
Omlesna2000
Feb 27th, 2002, 10:15 PM
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.
CornedBee
Mar 2nd, 2002, 08:07 AM
That's a 2d array.............
josegajefe
Mar 3rd, 2002, 10:11 AM
YEah... That's a 2D array.... What's up with that?
CornedBee
Mar 4th, 2002, 08:42 AM
omlesna said 3D array but described a 2d array...
kedaman
Mar 4th, 2002, 09:36 AM
textbox controls consume a ridiculous amount of resources, accessing COM objects is slow. Your nearest alternatives is the built in graphical functions line. Collisions with walls should best be detected on a raster of wall blocks, at the head only
wengang
Mar 4th, 2002, 10:05 AM
on a piece of code I got from Megatron, getting the pixel color from the dc of the desktop window, I was able to do all collision detection by simply checking which color was on the screen at the location of the head of the snake. To make sure, I sent the snake to back so that the other objects would be in front of it.
Works very well.
If anybody else does the same type project in this fashion, one other piece of advice. Don't have all the boxes/objects moving in order by index number. Just move the last box in order to the second/first position and record the moves by manipulating a string variable that holds the position order of the boxes. This solved the biggest problem of all, the game was just crawling.
I have posted this game on my new website, where I'll be putting up some of my past projects.
http://www.geesoftsolutions.com
Kedaman, I'm not disregarding your advice. I'm aware that what you're saying is right. It's just that when the problem as it appears is solved, I don't feel the need to start making changes.
Thanks all.
kedaman
Mar 4th, 2002, 10:09 AM
no probs if it works for you :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.