Results 1 to 14 of 14

Thread: detect overlap (collision) for 2D game

  1. #1

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    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

  2. #2
    You only have to check for collision with the tiles surrounding the object

  3. #3

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    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

  4. #4
    depends on how you store your map.

    If you have your map in one big map[x][y] array then it's really simple

  5. #5

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    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

  6. #6
    Addicted Member Janus's Avatar
    Join Date
    Aug 2001
    Location
    California
    Posts
    221
    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

  7. #7

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    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

  8. #8
    New Member
    Join Date
    Feb 2002
    Location
    Shizzowitch, NF.
    Posts
    5

    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.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  10. #10
    New Member
    Join Date
    Feb 2002
    Location
    Indiana
    Posts
    12
    YEah... That's a 2D array.... What's up with that?
    -Jose

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  13. #13

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    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

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
  •  



Click Here to Expand Forum to Full Width