File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Game Development and the fly likes Pong problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Pong problems" Watch "Pong problems" New topic
Author

Pong problems

Tony Craven
Greenhorn

Joined: Aug 14, 2007
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


Never trust a computer you can't throw out a window - Steve Wozniak
vanlalhmangaiha khiangte
Ranch Hand

Joined: Sep 11, 2006
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

Joined: Aug 14, 2007
Posts: 7
...When the ball comes calculate the angle at which it came , then reflect it back at that angle to the opposite side...


Thanks for the reply.

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

Joined: Feb 18, 2008
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

Joined: Jan 09, 2008
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

Joined: Aug 14, 2007
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pong problems