Teen Programmers Unite  
 

 

Return to forum top

OpenGL/Drawing collision...?

Posted by 142857 [send private reply] at June 24, 2003, 04:17:59 PM

I just wrote a 2-D tetris game with OpenGL, and I used a grid to store all of the shapes that were already on the bottom. But what if I don't want to use a grid and read from it and update it every time it redraws? How, logically, can you test for collisions, like if a bullet hits a ship, or a shape hits another one, etc.? Thanks

Posted by CodeX [send private reply] at June 24, 2003, 06:04:17 PM

Well, since your program has to keep track of the shapes and their sizes. You could always check to see if a given shape is in the space of another given shape. This seems as if it would take forever, but really, it winds down towards the end. Say you have five objects named a,b,c,d,e. You check for collisions with a. Once that's finished, you never have to check for any other shape to be colliding with a and by the time you get to e, you know what's collided with it and don't have to check any possibilities. From there, you would know which shapes collided and could do whataver was necessary at that time.

Posted by 142857 [send private reply] at June 25, 2003, 05:06:18 PM

okay, so it's just:
the number of shapes - 1 +
the number of shapes - 2 +
the number of shapes - 3 ... until it gets to 0
that many tests. But what if you shoot a bullet to get to a ship? I guess you could just see if the bullet has the coordinates of something that would fit on the grid of a ship and then offset the ship's grid with its upper-left (x,y) position...that's what I did for the tetris shapes. I guess I was looking for some magic approach that is widely used but I haven't thought of yet?...Maybe not.

Posted by 01011010 [send private reply] at June 25, 2003, 09:58:58 PM

The method of checking that CodeX suggested doesn't happen to have that many tests. If you have n objects, the total times of checking will be n*(n-1)/2. In this case we don't have that many objects, so I guess it will not be a problem.
If n = 100, we have to check 4950 times, which is OK, I guess :)

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.