Question

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
  • Else if there is no intersection
    • Move the ball to it's new position.

Is this an acceptable method or am I missing something?

Was it helpful?

Solution

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.

OTHER TIPS

If you want the authentic Pong feel, then computing the new vector is much easier than doing real reflections. Pong just reverses the sign of either dx or dy. If the ball hits a horizontal barrier, then negate dy. Vertical, then negate dx.

In your game loop, calculate the new ball position as if there were no obsticals.
Then check to see if the new position intersects with or is PAST the obstical edge. If either condition is met, then calculate a new ball position by bouncing it off the obstical.

A method that calculates a point given a starting point, angle, and distance would come in handy:

Public Function TranslatePoint(ByVal tStartPoint As Point, _
ByVal tAngle as Decimal, ByVal tDistance As Decimal) As Point

Here is what I would do:

1) assume your pong sides are at the lines x=a and x=b

2) let your balls position be (x0, y0) and traveling on the line Ax+By=C

3) from this you can calculate easily (x1, y1) the point where that line intersects the wall

4) you can get the equation of the line after the bounce by knowing that (x0, y1+y0) is the reflection after the bounce. (The ball is y1-y0 below the intersection point and hence will be y1+y0 above the intersection point.)

5) given the 2 equations you can plot your points (assuming you know the velocity).

You may be interested in the N Tutorials, which discuss collision detection and response (including bounce and friction) used in the game "N".

The tutorials may go deeper than you particularly need, but they're well-written and have interactive examples -- and simple bounce is one of the first topics.

Bouncing is a problem from the physics domain, not the graphics domain. Therefore OpenGL offers no real support for it. If you look at the hardware, you'll notice that there have been seperate physics cards similar to videocards, although the most recent trends are to integrate them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top