Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-12-2008, 08:40 PM
JDCAce's Avatar
Member
 
Join Date: Oct 2008
Location: Columbus, GA, US
Posts: 23
JDCAce is on a distinguished road
Send a message via AIM to JDCAce Send a message via MSN to JDCAce Send a message via Yahoo to JDCAce Send a message via Skype™ to JDCAce
Trajectory
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."
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-12-2008, 10:07 PM
Senior Member
 
Join Date: Sep 2008
Posts: 208
Darryl.Burke is on a distinguished road
So, what's the question? You surely don't expect someone to code it for you!

db
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-12-2008, 10:13 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Gone to Costa Rica
Posts: 2,223
Norm is on a distinguished road
Take a piece of paper and a pencil and work out the angles.
Then design and write the code.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-12-2008, 10:20 PM
JDCAce's Avatar
Member
 
Join Date: Oct 2008
Location: Columbus, GA, US
Posts: 23
JDCAce is on a distinguished road
Send a message via AIM to JDCAce Send a message via MSN to JDCAce Send a message via Yahoo to JDCAce Send a message via Skype™ to JDCAce
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."
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-12-2008, 10:23 PM
JDCAce's Avatar
Member
 
Join Date: Oct 2008
Location: Columbus, GA, US
Posts: 23
JDCAce is on a distinguished road
Send a message via AIM to JDCAce Send a message via MSN to JDCAce Send a message via Yahoo to JDCAce Send a message via Skype™ to JDCAce
Scratch all that. I think I know what to do.
__________________
"Things are made of littler things that jiggle."
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-13-2008, 08:02 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 462
fishtoprecords is on a distinguished road
For this problem, you are much better off storing Velocity and Direction (an angle) along with current position. When you hit a paddle, you can just keep the velocity and reflect the direction.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 04:37 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org