basic oo question

Started by
3 comments, last by peerless 14 years, 11 months ago
Hey Everyone, I have what should be a basic question regarding the design of classes. In general, a class that is involved in collision detection needs to know the location of other objects/boundaries/etc. My question is how should each object know this information? Let me use a simple example, a pong game: Say we have Ball and Paddle classes. In the Ball's move() function, which gets called each frame to update the position of the ball based on its current angle/velocity. After we move the ball to its new position we need to check to see if this new position: 1) hits one of the two paddles or 2) hits one of the wall boundaries so we can update its angle and perform some side-effects (sound effect, update score, etc). The solution I would use in this case would be to pass pointers to both paddle objects to the ball's move function and I guess just use known constants for the boundaries. That should work but I feel like this attempt will not scale well to more complicated examples. So I ask, in general what is a good way to handle this? How can moving objects be aware of other objects they might be colliding with? Also, I am just starting out with game development and I don't want to pick up a bunch of bad habits. Can anyone point me to some high level articles about class design / hierarchies. One I am using now is an article about State Machines by lazyfoo: http://lazyfoo.net/articles/article06/index.php Thats what I've been using as a general layout but I am looking for more information regarding basic design/layout etc. Thanks.
Advertisement
Well in the pong game, you could do this :

give a variable to the balls starting position. move the ball, thus increment
/decrement the variable. Check to see if the ball's variable is in bound of the
paddle variable. If so, then there is a collision.


As in general, there are some ways to handle collisions. Some are bounding
circle, bounding rectangle/ Just google them.
Our whole life is a opengl application.
no no, you missed the point of my question. I understand how to do the actual collision detection. My question(s) are design related. Not how to know if the ball hits the paddle, but how the ball knows about the paddle (or if it even should, perhaps the class that contains the ball and both paddles would manage that)
Quote:My question(s) are design related. Not how to know if the ball hits the paddle, but how the ball knows about the paddle (or if it even should, perhaps the class that contains the ball and both paddles would manage that)
I think you got it there in the last part of your post. Instead of assigning the responsibility of detecting intersections to individual objects (which is somewhat arbitrary and, as you note, doesn't scale well), assign it to a module that has knowledge of all the objects in the game. (In a simple Pong clone, this 'module' could be something fairly simple, such as an update_game() function).
Quote:Original post by jyk
]I think you got it there in the last part of your post. Instead of assigning the responsibility of detecting intersections to individual objects (which is somewhat arbitrary and, as you note, doesn't scale well), assign it to a module that has knowledge of all the objects in the game. (In a simple Pong clone, this 'module' could be something fairly simple, such as an update_game() function).



Ok, so then the "move()" function of the ball would allow it to move to an 'illegal' position and then the 'module' in charge of collisions between objects would be the one to set the balls new position? sounds good, id still be interested in articles that discuss game development from a high level, how to layout classes and what manages what, etc.

thanks

This topic is closed to new replies.

Advertisement