Pong

Hello,

I want to get back into Java Midlets games using MIDP which I enjoyed at Uni because it was relatively simple, but not Flash ;D
Anyway, I’ve set up Eclipse etc, an improvement over JBuilder at any rate, and I wanted to make a simple Pong game for starters. This is actually a lot simpler than the game I made at Uni (which was a paratrooper type game), but I realised I have no idea how to make the ball bounce off at an angle, you know when it bounces of the edge of the paddle? How is that done? It doesn’t just bounce “flat” , IYSMWIM. I suppose I could just hard code angles at certain points, but I assume there is better mathematical way to do it?

Cheers,
CoffeeMonster

Calculate the distance from where the ball hits to the centre of the paddle. If you multiply this value by 2 and divide by the length of the panel you will end up with a number between -1 and 1. Then multiply this by some angle constant, say 30 degrees, then apply this angle to the ball. Then the ball angle will change by 30degrees if the ball hits the end, 0 at the centre and a smoot change in angles at other points on the paddle.

This will give you a linear change in angle with respect to the location the ball hits, which seems right to me, but you could make it quadratic by simply squaring the [-1,1] number. Then just mess around with the constant until it feels right.

Google ‘reflection vector’

Yes, check out reflection vectors. Long story short there is a calculation that translates the ball’s vector to the coordinate space of edge it is bouncing off of followed by a simple reflection at y=0. Dot products will translate to the appropriate planes and then you just flip vector from y=0 and translate back.

Of course paddles are rounded, so the type of reflection is circle/circle with velocity and circle/edge with velocity. I can’t remember how you do moving circle/circle collisions but I did write it on the forum somewhere and made code on my computer for it.

Thanks everyone, I knew you’d have good advice. I’ll have a read on reflection vectors.

Since I’m on the subject, where did you guys learn all this, is it pure trigonometry or did you learn it specifically from game writing tutorials/books?

Cheers,
CoffeeMonster

I (like most things) learned it by sketching everything on pages upon pages of paper. I studied 3d graphics so I know that [say] multiplying a point by the axis vectors will translate coordinates to be relative to the axis. I read a few articles on the net, but I always end up having to figure out formulas by myself because I do that better.

Other people (I think) tend to learn it by reading it in books or at school/college/university.

Cool, thanks. What do you think of Java3D? Is it still maintained? Last time I asked most people here prefered the Java OpenGL wrapper (LWGL or something)?

Regards,
CoffeeMonster