Okaaay, Can anyone tell me...

Started by
10 comments, last by Moogle 22 years, 3 months ago
Thanx for all the great input!

But what I mean is things like collision detection and AI.
I can read up on A* and bounding boxes but how the hell do I put it in a game?
-=Moogle=-
Advertisement
There are several ways you can do collision detection in your game loop. One is to organize your game objects into a collection and have each object check all of the other members of the collection for a collision using a simple group of if statements. This collision checking can be programmed into your objects in the code where they are displayed to start off with, but when the game becomes more complex you''ll want to put the code in its own member function; if you put it in the display code, checking will occur each time the bitmap corresponding to your game object is redrawn. You could put them directly in the code of the game loop, but I prefer the uncluttered approach to the game loop. After you have determined that your object has collided with another object, set a flag on that object like:

object[1]->state = HAS_COLLIDED. Then in the part of your object that displays your bitmap on-screen, check the state of your object and have it show the picture(s) corresponding to whatever state the object is in.

You can give an object multiple states simultaneously by using a bitfield with an OR (|) statement. Ex. Object[1]->state = HAS_COLLIDED|IS_FIRING;

Obviously you don''t want to actually "set" two states at once, but you do want to be able to allow your object to maintain more than one state at a time. What if, for example, your ship was firing at the same time it got hit with shields on? What if it was firing, using boosters, and got hit with shields on? If you use an integer, you can have thousands of states at the same time, if you so desire.

As for AI or pathfinding, you just check for mouse clicks instead of keypresses. For example, if you are doing a game like Warcraft 2 you want a unit to move to where you clicked on the screen. You would first check for a mouse click event in your game loop. Then you would locate in your game world (if your game world is bigger than a single screen) where you clicked. You would do this by getting your mouse x and y coordinates and adding them to your position in the world buffer. Then you add a member function to your Peon object that will update the Peon''s location member variables based on where your A* code suggests to move it to. The member function that shows your sprite will then be called in the game loop and the sprite''s position will be updated accordingly.

So basically, you''re just trapping mouse buttons and figuring out where the pointer was at the time in the game world and passing that information off to your A* algo, which will update your character''s location variables, which in turn will be redrawn automagically in the game loop...

It''s all a matter of putting the code where it belongs. For a more structured approach, you can make member functions to do AI, blitting, pathfinding, etc, and make one function in your object called Update(), which will be called from the game loop, which in turn will call all the other functions necessary for the upkeep of the object.

Later
Anesthesia"If you like heaven so much, GO THERE! Leave me the hell alone!"

This topic is closed to new replies.

Advertisement