|
-
Feb 24th, 2002, 11:41 AM
#1
Thread Starter
Frenzied Member
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
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Feb 26th, 2002, 02:09 AM
#2
Member
You only have to check for collision with the tiles surrounding the object
-
Feb 26th, 2002, 10:58 AM
#3
Thread Starter
Frenzied Member
but I won't know which 'tiles' are surrounding the object without going through them all in a loop
right?
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Feb 26th, 2002, 12:36 PM
#4
Member
depends on how you store your map.
If you have your map in one big map[x][y] array then it's really simple
-
Feb 26th, 2002, 12:46 PM
#5
Thread Starter
Frenzied Member
I don't have a map. As I said, I just used a text box array
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Feb 26th, 2002, 02:49 PM
#6
Addicted Member
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.
"1 4m 4 1337 #4xz0r!'
Janus
-
Feb 26th, 2002, 07:48 PM
#7
Thread Starter
Frenzied Member
Well, I haven't done that before. But I'll try it out.
Thanks
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Feb 27th, 2002, 11:15 PM
#8
New Member
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.
Last edited by Omlesna2000; Feb 27th, 2002 at 11:32 PM.
Somewhere between doubly linked lists, binary trees and the looming threat of having to learn neural networks, you realize programming isn't fun anymore.
-
Mar 2nd, 2002, 09:07 AM
#9
That's a 2d array.............
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 3rd, 2002, 11:11 AM
#10
New Member
YEah... That's a 2D array.... What's up with that?
-
Mar 4th, 2002, 09:42 AM
#11
omlesna said 3D array but described a 2d array...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 4th, 2002, 10:36 AM
#12
transcendental analytic
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
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Mar 4th, 2002, 11:05 AM
#13
Thread Starter
Frenzied Member
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.
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Mar 4th, 2002, 11:09 AM
#14
transcendental analytic
no probs if it works for you
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|