views:

72

answers:

3

Hi guys, I'm writing a pong game and I have a ball class that has velocity x, y, positions and all that stuff that every frame is updated by calling @ball.update, this moves the ball forward by its x_vel and Y_vel etc. My question is Should my collision code be in the loop or in the update method of the ball class? Or should it all be in the loop and the ball should not have any control of its position?

+2  A: 

The collision detection should not be done in you ball class because it would require the knowledge of all other objects in your game.

Imagine a shooter with many objects and think about what happens if each object would try to calculate the collision on it's own.

There should be a dedicated class that cares about collision detection. This class is notified if any of its supervised objects changed its position. Then it checks for collision and notifies all abjects (not only 2 :) that have a collsion.

Have fun... :)

TottiW
+1  A: 

For pong simple matrix operations should be sufficient. A ball class is not needed if there is only one and you use it only for holding a tuple.

vurte