Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Pong problems

Tony Craven
Greenhorn
Posts: 7
Hi,

I wasn't sure whether to post in Micro Edition forum or not as this relates to my attempts to make a pong game for my phone. I'm trying to work my way through the simple games...if I can master Pong, Tetris will be next!

The problem I'm having is that I'm unsure as to how to get the ball to move at angles from a paddle once it strikes one.

Currently how I'm trying to do it is:

I've tried to get the ball to move by changing its x direction when it collides with a paddle (so dx = 1, then dx = -dx) but the ball does not move very realistically as it seems to always head back in the same direction after each collision.

I was thinking that I could check to see what section of the paddle the ball collided with and use that to pick which value to set dx as -1 / +1.

I'd be really grateful of any tips, I don't really want to go looking at other people's source code on the net.

Thanks

Tony

vanlalhmangaiha khiangte
Ranch Hand
Posts: 170
Hi ,
I have one logic which i did in flash ..
When the ball comes calculate the angle at which it came , then reflect it back at that angle to the opposite side ... This is wat i ve done in flash.

Tony Craven
Greenhorn
Posts: 7
...When the ball comes calculate the angle at which it came , then reflect it back at that angle to the opposite side...

I was starting to come to this conclusion myself, it sounds like Snell's Law in Physics. I'll give it a go!

Tony

Austin Lee
Ranch Hand
Posts: 56
if (ball & paddle collision) // reverse the ball's vertical direction dy = -dy; if (ball & wall collision) // reverse the ball's horizontal direction dx = -dx;

I think your code is ok except you need some change for the angle.
[ February 19, 2008: Message edited by: Austin Lee ]

Bob Good
Ranch Hand
Posts: 86
Keep track of the accelleration of the paddle and apply some extra dy or -dy. Kinda like adding spin to a serve in real ping pong.

Tony Craven
Greenhorn
Posts: 7
Sorry for the long delay in replying, thanks a million for your suggestions. I got it working in the end by splitting the paddle up into 5 sections and if the ball hit one of the sections, I'd set it's return vector from a predefined list.