Results 1 to 6 of 6
- 10-12-2008, 06:40 PM #1
I'm making a pong-like applet. This is my first attempt at game programming. I would like my pong ball to ricochet off the paddle at an angle determined by its line of entry (i.e. if it hits straight on, it would fly straight off; if it hits at a 30-degree angle, it would fly off at a 60-degree angle). Imagine a horizontal line with a y value of wherever the ball hit. The angle with sides of that line and the line of entry should be reflected, or mirrored, about that imaginary line. Anyway, you probably know how trajectory works.
This is probably asking too much for now, but I would also like the speed and direction of the paddle's movement to factor in to the trajectory. Many pong-like games I've played have included this. For instance, if the paddle where moving down when the ball hit it, the speed at which the ball is moving down would increase (or the speed at which the ball is moving up would decrease).
I don't feel like I'm making myself clear. Let me know if you don't understand what I'm trying to say.
The following is a description of my program, just in case you need it:
My program, as of now, has one ball and one paddle. The paddle follows the y-position of the mouse, while if the x-position is on the left side of the window, the paddle is on the left side, and vice-versa. (That is, the paddle follows the mouse freely up and down while the paddle "snaps" to either the left side or the right side.)
The ball has two speed variables, xSpeed and ySpeed. xSpeed governs how fast the ball moves horizontally (measured in pixels per repaint) and ySpeed measures how fast the ball moves vertically (measured in pixels per repaint). A negative xSpeed denotes travel leftward, while a negative ySpeed denotes travel upward.
Every time the ball collides with a paddle, it reverses xSpeed, but ySpeed remains unchanged. (ySpeed is determined randomly among -1, 0, or 1 when the ball is first set in motion.) A hitCounter increments when the ball hits a paddle. (It is reset if the ball makes it past the paddle.) When the hitCounter reaches a multiple of 5 (5, 10, 15, ...) the xSpeed is incremented (to a maximum of 10)."Things are made of littler things that jiggle."
- 10-12-2008, 08:07 PM #2
So, what's the question? You surely don't expect someone to code it for you!
- 10-12-2008, 08:13 PM #3
Take a piece of paper and a pencil and work out the angles.
Then design and write the code.
- 10-12-2008, 08:20 PM #4
Of course I don't expect someone to code it for me. What I meant to ask is a "head start" or sorts. Right now, I'm not sure where to begin. I can't even think of an algorithm to help tackle this problem.
I have one idea, but I don't know if it would work. Obtain the trajectory of the ball (the imaginary line the ball is traveling) by storing the x- and y-positions, then have the ball move forward one step and obtain the new position. This gives me the "entry line." I then "draw" and imaginary horizontal line out from the paddle at whatever y-position the ball hit it. I then calculate the angle of the first line to the second line; this is the angle of impact. To have the ball bounce off the paddle at the correct angle, I "draw" a new imaginary line from the paddle relative to the entry line with an angle of two times this angle of impact. Then I send the ball moving along that "line."
That is the only thing I can come up with, and, although it sounds like it might work, I have no idea to go about coding it. Retrieving the angle of impact sounds doable, but I have no clue how to send the ball moving along a set line.
Just a nudge in the right direction (no pun intended) would help."Things are made of littler things that jiggle."
- 10-12-2008, 08:23 PM #5
Scratch all that. I think I know what to do."Things are made of littler things that jiggle."
- 10-13-2008, 06:02 AM #6