|
-
Oct 26th, 2006, 10:35 AM
#1
Thread Starter
Lively Member
Collision Detection Ideas
I need some ideas on what is the best way to detect collisions in my game. Heres the basic idea of the game:
There is a cannon (a rectangle) in the centre of the screen that can rotate from -90 to 90 degrees (0 degrees is straight up)from the bottom of the screen. Then "ships" fall from the top of the screen and you have to shoot them down. The ships can also move side to side randomly. I just need some ideas on how to detect if a ship hits the cannon.
Here is what I was thinking of doing. Find the distance from the bottom of the cannon for each ship, and if its greater than the length of the cannon, do nothing. Other wise find the angle that the ship is at relative to the centre of the cannon. then if the angle is with like a few degrees, then a collision.
The only part that making this hard for me is if the ships moving side to side randomly. Any other ideas?
-
Oct 26th, 2006, 11:39 AM
#2
Hyperactive Member
Re: Collision Detection Ideas
 Originally Posted by fartman_900
Here is what I was thinking of doing. Find the distance from the bottom of the cannon for each ship, and if its greater than the length of the cannon, do nothing. Other wise find the angle that the ship is at relative to the centre of the cannon. then if the angle is with like a few degrees, then a collision.
The only part that making this hard for me is if the ships moving side to side randomly. Any other ideas?
You don't need to know C#, but it helps to be able to moderately grasp its collision detection code since it is the only similar project we have in the codebank: Space Invaders
How are you storing the data? How is the cannon arranged? Is it a set square or some kind of exotic polygon that is making the process more difficult? Also, if the ships have 2D movement ( Up/Down & Left/Right ), you will need to check the Left and Right sides of that cannon.
-
Oct 26th, 2006, 06:55 PM
#3
Thread Starter
Lively Member
Re: Collision Detection Ideas
The cannon is just a rectangle at the centre of the screen, and it only rotates. The ships are help in a structure, which has the x,y, and speed.
I guess the real only problem that I'm having is checking when the cannon is rotated and checking for the collision. A bounding box wont be good since there is lots of empty space around the cannon if I use that. Another way that I know of is what I said before. Or should I have like 5 small boxes that follow the cannon when it rotates and just check against all those?
-
Oct 26th, 2006, 11:35 PM
#4
Hyperactive Member
Re: Collision Detection Ideas
 Originally Posted by fartman_900
I guess the real only problem that I'm having is checking when the cannon is rotated and checking for the collision. A bounding box wont be good since there is lots of empty space around the cannon if I use that. Another way that I know of is what I said before. Or should I have like 5 small boxes that follow the cannon when it rotates and just check against all those?
Links For Basic/Practical Collision Detection:
Collision Detection
Collision Detection 2
Taken from an online post:
"Really, pixel based collision is about the following steps:
: Do bounding box check
: If they intersect, make a note of where the intersection occurs like so:
o--------o
| |
| o--+------o
| |XX| |
o-----+--o |
| |
o---------o
: Then check every pixel (or every other pixel if you think that suffices) in both sprites inside your collision rect (the one marked by XX in the inadequate graphic above), and see if both alphas are full-on (255, 1, or whatever treshold you find suitable). If so, you've got a collision.
Usually, this type of intersection rect is very small, unless you are dealing with fast-moving sprites or very low framerate."
( Taken from this post )
To put it another way:
If you are using BitBlt or something similar, you will probably have a "neutral" or "transparent" color for masking. You could find where the overlap occurs and use the GetPixel API and compare each overlapping pixel. If both of your images are overlapping on a non-neutral color, then you have a collision.
However, we may have missed a step. How are you handling your graphics? Bitmaps and BitBlt or PictureBoxes?
-
Oct 27th, 2006, 12:52 PM
#5
Thread Starter
Lively Member
Re: Collision Detection Ideas
The graphics are done with OpenGL, no bitmaps or pictures (just a rectangle for the cannon and squares for the ships). I will search around for some OpenGL functions and stuff to see if I can get the pixel info and all that.
-
Oct 27th, 2006, 01:44 PM
#6
Hyperactive Member
Re: Collision Detection Ideas
 Originally Posted by fartman_900
The graphics are done with OpenGL, no bitmaps or pictures (just a rectangle for the cannon and squares for the ships).
Maybe I need another ChemicalNova intervention, but using OpenGL means you are using some kind of paint or blitting function to put something on the screen, right?
Could you post some sample code demonstrating how you are putting the pictures on the screen?
-
Oct 28th, 2006, 01:13 AM
#7
Re: Collision Detection Ideas
 Originally Posted by Fedhax
Maybe I need another ChemicalNova intervention, but using OpenGL means you are using some kind of paint or blitting function to put something on the screen, right?
Could you post some sample code demonstrating how you are putting the pictures on the screen?
You're getting awfully good at making me blush 
I haven't tested this theory, but it would most likely work.
Since you have to have a DC for OpenGL to blit to, a GetPixel type collision detection would work. As FedHax said above, you could use GetPixel, or, if you really absolutely truely wanted the most speed out of it, you could use GetDIBits.
If you require an example of using GetDIBits I'm sure I could grab a sample from my Photoshop Clone, assuming you're using VB6.
How have you set up the gameloop so far? One of the ways I performed collision detection in my old crappy point and click game was to have a "Peak" function for each object. The peak grabbed the position it would most likely be at in the next frame, and if it collides with another object, a boolean flag was set, then the next frame the speed of the object would be reset to 0.
If you're wanting to use completely OpenGL, I did find this:
http://msdn.microsoft.com/library/de...unc03_8m7n.asp
It takes a bit of work, but it could do what you want (and, from what I read, is pretty much GetDIBits wrapped into OpenGL).
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Oct 28th, 2006, 11:07 AM
#8
Thread Starter
Lively Member
Re: Collision Detection Ideas
I'm actually using C & linux for this. But the game loop is setup using the glutTimerFunc function and and each time it draws the game and then checks collision.
The code to just draw stuff on the screen is something like this FedHax:
Code:
/*for the ships*/
glRecti(shipList[i].x-5,shipList[i].y-5,shipList[i].x+5,shipList[i].y+5);
/*for the cannon*/
glTranslatef(350,0,0);
glRotatef(angle,0,0,1);
glColor3f(0,0,1);
glRecti(-5,0,5,50);
/*for the cannon balls*/
glVertex2i(ballList[i].x,ballList[i].y);
Then theres a whole bunch of stuff that moves the balls and ships around before I draw them.
I tried my way with the 5 bounding spheres around the cannon and it works pretty good. I'll look into the link you gave me chem and maybe later on next week I will try it out.
Thanks for the help.
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
|