views:

472

answers:

6

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
  • 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)

+1  A: 

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
Boo
I forgot to mention that I'm doing this in C++ with OpenGL.
epochwolf
It was an example.
Boo
+3  A: 

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.

Bill the Lizard
You are forgetting bouncing off one of the side walls or going out of bounds.
epochwolf
It's the same calculation. If the ball gets within its radius of an obstacle, its direction should change. Just check to see if it goes beyond a certain X coordinate to tell if it's out of bounds.
Bill the Lizard
What do you mean by "within it's radius". I rejected a bounce when near method because it would be possible for the ball to travel beyond the nearness value in a single step. (There is a command to accelerate or freeze the ball for x steps)
epochwolf
If it's possible for the ball to travel beyond the nearness value in a single step then your update rate is too slow.
Bill the Lizard
I'll look at doing this method then. I'll get back to you on how well it works :)
epochwolf
I implemented this method and it worked quite well. Sorry for taking so long to get back to you on this.
epochwolf
Cool, thanks for the feedback. Do you have any code short enough to post? :)
Bill the Lizard
A: 

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).

No One in Particular
+2  A: 

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.

mtnygard
That would make things alot easier. I was trying to make a more flexible model but I'm not really doing anything fancy. I might just go for this.
epochwolf
I would mark this as accepted as well if I could. I implemented this instead of going for a generic formula which saved me enough time to implement some shiny lighting which probably helped my grade more than anything else I was trying to do.
epochwolf
A: 

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.

leander
A: 

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.

MSalters