View Full Version : Pong
02-15-2003, 08:39 AM
Oki, so I'm writing a pong game for flash. I will freely admit that I'd probably trying to re-invent the wheel here, but it's for my own edification, so bear with me. I have everything working except for 1 detail, the ball is set to move a little faster, everytime it is succesfully hit by a paddle (using hitTest), this works fine except when the ball reaches such a speed (ball._x += 5 etc)that it basically jumps the paddle. The x position in frame A + speed means the x position in frame B is beyond the paddle, so no hitTest, even if the paddle is in the right position. The only solution I've come up with so far, is to make the increments as small as possible (ie: + 0.1) and increase the frame rate to something ridiculous like 300fps. Anyone have a better suggestion, as even this will eventually not work.
02-15-2003, 09:58 PM
You could opt for an alternative hit method. Instead of using hitTest, you could for instance check the coordinates of the ball and the paddle, compare them to the previous ones and see if a hit should have occured.
02-16-2003, 04:29 AM
That's definitely the best way I can think of. To simplify the idea, pretend that the paddle itself slides up and down the y axis of a graph. it's values can then be thought of as a range from say (0+position) to (_height+position). 0 is the origin, (it's default location), position is how far up or down it currently is from where it started. Then to find the intersection, you need to remember the previous (X1,Y1) and current (X2,Y2) ball coordinates. To find the slope (M) of the balls path use M = (Y2-Y1)/(X2-X1). Given the slope, you can find the Y-intercept (B) with B = Y2-(M*X2).
To combine these, your Y-intercept is really just Y2-(((Y2-Y1)/(X2-X1))*X2).
If you remember that your paddle is ON the y-axis, all you have to do is find out if
(position) < Y2-(((Y2-Y1)/(X2-X1))*X2) < (_height+position) AND X2 < the paddles X position (or > for the case of the paddle on the right). If so, you have a collision. Make sense?
Of course, since the paddles can't be ACTUALLY positioned at the origin of the stage, you'll have to do some translation, so that the starting position of the paddle is the origin instead...
That sounded rather complex, but really it shouldn't be too difficult once you understand it.
02-16-2003, 06:01 AM
:) Easy for you to say, Esquared. Although I know its high school math, it still looks all blurry and everything ! ;) oh well, as long as xMontreal gets it ...
02-16-2003, 06:38 AM
Thanks for the advice, I'll have a hack at it and let you know how it went.
02-16-2003, 07:03 AM
vBulletin® v3.8.5, Copyright ©2000-2013, Jelsoft Enterprises Ltd.