• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Simple game engine OOP problem

## Recommended Posts

Zole    122
As sort of a first project, I''m working on a simple clone of Breakout/Arkanoid in C++ using DirectX and OOP. I understand the basics of C and C++ and I''ve written small but working DirectDraw and DirectInput programs before. However, I''m having a hard time taking to OOP, and here''s my current problem: I''ve got a class CBall and a class CPaddle that would seem to represent the main play objects (we''ll get to the bricks later). How would I do collision detection in this arrangement? The ball and paddle are contained in the game/world object (the world "has-a" ball and paddle) but the ball needs to ask the paddle, walls, and bricks if it is colliding so it can act accordingly. Since the game object is global, the ball could ask it that way, but that strikes me as bad OOP style. Does anyone have better ideas of how to do this? (or, is this even enough information describing the problem?) Thanks.

##### Share on other sites
I think i would have a CollisionDetection()member function in the Game/World object, and I would have member functions for the ball and paddle that would retun the position of the ball and paddle. The game object would get the positions of the objects using these functions, and would use its collision detection function to do all of the computation, results of collision, etc. there is probably a better way, but this is how I have done it in the past.

##### Share on other sites
Argus    118
It really depends on how you want to arrange things. But since the Paddle, Bricks, and Ball are part of the GameWorld object, they should probably talk to the GameWorld object when interacting with one another.

So the ball object might continually ask the GameWorld object :
"did I hit anything?"
"did I hit anything?"
"did I hit anything?"

And the GameWorld object could check whether it did hit anything, tell whatever the ball hit that it has been hit, and return the collision infomation to the ball.

##### Share on other sites
Strauss X    122
Instead of the Ball instance call the world object continuous if it has been hit, you could use a event model aproach (or callback).
If you plan to make your GameWorld class global, I sugest you use Singleton Desing Pattern.
The Singleton desing pattern ensures that will be only one instance of the GameWorld class.

Edited by - Strauss X on January 9, 2002 8:20:22 PM

##### Share on other sites
Ironside    122
Since you ball class probably has an x and y position as well as an x and y velocity. You could probably implement a member function like this cball.move() the move function adds the x and y velocites to the x and y position and then checks to see if the ball has collided with the paddle. Something like x = GameWorld.Paddle.GetBoundingBox(); and then a member function in ball DoICollide(x). Then loop through your linked list of Bricks checking each one to see if the ball collides with them.

If the ball collides with the paddle you could reverse the y velocity and add/subtract .5 of the paddles x velocity from the ball and just let the main game loop call ball.move() again, and again, and again...

##### Share on other sites
Sphet    631
You also might want to consider the fact that the ball may eventually move fast enough to ''pass through'' an object it should have it with, so when you do call move() and do collision detection for bricks or paddle, you might want to break it down into a check along the path traveled from current(x,y) to nextframe(x,y). I''m sure calculus and integration have something to do with it but i just did a brute force approach and when I know two objects might collide through the course of an objects movement, I do the collision check every 1/5 of a step through..

##### Share on other sites
TraHari    122
Heh, well, we''ve been discussing this, but I''ll pass along my take on all of this for everyone else''s edification.

GameWorld contains physics, therefore, collision detection should be mostly GameWorld''s responsibility (or, just _not_ the ball or the paddle''s responsibility).

Or, put another way, all the ball needs to know is whether or not it bounces, since all it does is move and bounce. . .and all the paddle needs to do is move.

This should be the responsibility of something other than the ball or paddle, either GameWorld, or some other object like you suggested, such as a CPhysics object inside GameWorld with a method like CollisionDetect( CGameObject*, CGameObject* ).

Anyway, my 2 OOP cents.

- Tra''Hari

##### Share on other sites
pink_daisy    200
quote:
Original post by Zole
As sort of a first project, I''m working on a simple clone of Breakout/Arkanoid in C++ using DirectX and OOP.

Deja Vu:
I''ve been a programmer for a longtime. I''ve always loved games, but just recently got enough spare time to finally write a complete C++/DX/OOP game. My choice: arkanoid.

Well about 45 days and nearly 6000 lines of code later, I am *almost* done. Make the menu a little neater, do some code cleanup, maybe a little performance tuning, then she''s off to the showcase.

My program is OOP. Only globals are WinMain and the message pump. Everything else is encapsulated. For program structure, I made a game class. Singleton, with 3 functions. A c-tor, d-tor, and a loop method.

The loop is just a game-state function. Depending on the state (i.e. playing, level editing, menu, title screen) it performs the necessary work. The playing state, for example, has two parts: for the first part I move all the game objects (aliens, balls, paddle, laser beams, powerups). For the second part I render everything to the backbuffer.

so the loop loops something like this:

  if(currentGameState == PLAYING){ // move everything ball->move(); paddle->move(); if(alien) alien->move(); // no need to move if they don''t exist if(laser) laser->move(); if(powerUp) powerUp->move(); // they are moved, now render background->render(); border->render(); bricks->render(); paddle->render(); if(alien) alien->render(); // these aren''t always present if(powerUp) powerUp->render(); if(laser) laser->render(); ball->render();}

of course i''m leaving out all the function parameters. Collision detection is performed when moving objects. Thus, when moving say the ball(s), we need to know about the walls, paddle, aliens, and the bricks.

So, when I call ball->move() I pass it pointers to the related objects. Internally the move method querys the various objects to determine whether collisions have occured.

quote:

You also might want to consider the fact that the ball may eventually move fast enough to ''pass through'' an object it should have it with, so when you do call move() and do collision detection for bricks or paddle, you might want to break it down into a check along the path traveled from current(x,y) to nextframe(x,y). I''m sure calculus and integration have something to do with it but i just did a brute force approach and when I know two objects might collide through the course of an objects movement, I do the collision check every 1/5 of a step through..

ack,
Just check the ''corners'' of the ball against the other objects, if any corner of the ball is within an object, reflect it appropriately. My ball(s) total velocity is capped. This avoids any possibility of the ball "passing through" objects. A hack you might say but everyone who has tested it so far complaimed that the ball moves too fast after awhile. BTW, in all my movement/collision detection functions I only used a float once. No calculus, no trig, just nice integer algebra.

I''m hoping to have this thing in the showcase by this weekend, if you want to see it in all its unfinished glory (i might even let ya peek at the source), drop me a note.

~don

"They that can give up essential liberty to obtain temporary safety deserve neither liberty nor safety."
- Benjamin Franklin

##### Share on other sites
Zole    122
Thanks for the suggestions everyone.

pink_daisy - could I take a look at your source? I''ve never had a chance to look at an OOP game engine, so that''d be very helpful. By that token, if anyone knows of any simple open-source OOP games, I''d like to look at those too.

##### Share on other sites
pink_daisy    200
quote:
Original post by Zole
Thanks for the suggestions everyone.

pink_daisy - could I take a look at your source? I''ve never had a chance to look at an OOP game engine, so that''d be very helpful. By that token, if anyone knows of any simple open-source OOP games, I''d like to look at those too.

YGM

~don

"They that can give up essential liberty to obtain temporary safety deserve neither liberty nor safety."
- Benjamin Franklin