I’m working on writing a Pong game and I’ve run across a problem.
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?
You just need to check if the center of the ball is within its radius of the paddle to tell whether or not its time to bounce. There was an older question asked that has several answers on calculating bounce angle.