Search code examples
c++collision-detectionpong

Pong game in c++


Now I'm making a pong game in c++, and by now I've made a paddle and ball which both inherits from entity that holds the coordinates and the bounding box(can be circle or polygon) of the entity, also I've implement a function entity.isColliding(entity) which easily telling if the entities are colliding for any two entities.

Now I want to make the Ball move and when it will collide with an entity it will reverse the right speed coordinate(x or y). I thought of making a function canMove() which will move and check collision and if there is move back, but I have no idea how to check if I need to reverse the x or the y speed coordinates all I know is that there was or will be a collision.

Any suggestion?


Solution

  • Hm, if I understood your problem correctly... Reversing the right one of the speed coordinates makes your ball resume it's travels the way it should, while reversing the other one makes it just resume to go into the wall (but from the different angle).

    I don't know how fast your canMove() function is, but if it's fast enough maybe you could try it this way:
    - reverse the x coordinate, if it works, that's the one
    - if it doesn't work, then reverse the y coordinate
    - if both of these don't work then you're in a special case situation (e.g. corner) -- reverse both :D