C++ Collision detection in games programming...

I am creating a scrolling game in which a player controls a submarine, i need to check whether the sub has collided with objects.

How would i acheive this?

and does anyone have any useful links to help me?

Any help is much appreciated ^_^
Last edited on
Easy way:

http://www.box2d.org/


Hard way:

Cannot be determined until we know what you're using for graphics. Sorry. :)


-Albatross
Do you mean collision with the background (which is scrolling), or foreground objects which are moving independently? These could be different tests.

I guess you are referring to foreground objects, like fish.

You basically need to break it into two tests. The first one tells you if two objects are close together, then a second test to see if a collision actually happened.

If there's only a few foreground objects, I'd be tempted just to compare (X,Y) and (X+32, Y+32) for each fish to see if there is an overlap with your sub (assuming your fish is 32 pixels in height and width). When there is an overlap, you can AND the two bitmaps together and check the result, if non-zero then collision happened at the pixel level.

If there's many objects, I'd think about dividing the screen into a coarse grid and when you move a fish into that grid space you add the fish pointer to that cell. When you move the sub into that same cell, you can get the pointer to the fish and do the AND test.

Hope that makes sense...

There's lots of other ways of doing this...as Albatross said.

Last edited on
Topic archived. No new replies allowed.