I'm working on writing a Pong game for my graphics class final project (I choose to write this, I wasn't assigned Pong) and I've run across a problem that wasn't addressed in either the book or the class.
I'm trying to figure out how to bounce a point off of a line.
The best method I can figure out to do this with is
- Calculate the current and future position of the Ball.
- Line Segment: {Ball.location, Ball.location + Ball.direction} (Ball.location and Ball.direction use a custom vector/coordinate class)
- Calculate if the generated line segment intersects with any of the walls or paddles.
- ??? Don't know how to do this yet (Will ask in a separate question)
- At the first intersection found
- Bounce the ball off of the line
- Create a triangle formed with
- a = Ball's current position
- b = Intersection point of the line.
- c = Closest point to the Ball's current position on the line.
- Find the angle that the ball hits the line
- angle = cos(distance(b, c) / distance(a, b))
- Find the angle to rotate the ball's direction
- (90 - angle)*2
- Rotate Ball's direction and move it to it's new position
- ignoring distance traveled to hit the line for now, doesn't need to be exactly on the line
- Create a triangle formed with
- Bounce the ball off of the line
- Else if there is no intersection
- Move the ball to it's new position.
Is this an acceptable method or am I missing something? All of the code is going to be written in C++ with OpenGL and I haven't found any OpenGL functions that help with this. (The book's list is far from complete)