This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Dear Reader, Hey "Santos1204", we sure can help. But, first, you have to follow the JavaRanch Naming Policy. It clearly states that you have to have a First Name and a Last Name. And the names should be a proper name. More details on the Policy are here. [ May 09, 2006: Message edited by: Shyam Prasad Murarka ]
I can't really work out how to make the ball bounce from one wall to the next in a 'natrual way'
The basic method Jan describes would work fine.
An enhanced algorithm would largely depend on what you mean by "natural way"... In reality (unless the ball is travelling very fast so the effect is not noticeable) the ball declerates on impact with the wall, and always looses some energy before accelerating away from the wall. So a "natural" ball effect would be one which takes this into account, and perhaps even loses kinetic energy (hence the speed after impact is less than the speed before). However, for many simple 2D games this is overkill, and the ball in fact observes very loose approximations to nature (i.e. constant speed and kinetic energy). If you want to factor in acceleration or even energy loss that wouldn't be too hard, but you'd need to know some mechanics (kinematics most likely) and you'd need to think carefully about things like impact and restitution and derive workable values for the constants which pop out of the equations. The only job then is simply getting the program to use the equations - which is the easy bit!
This post may be better in the Game Development forum if you want to pursue the mathematics (and not programming) further. Have fun!
Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / AmazonAmazon UK )
Joined: May 03, 2006
Thanks for your help, sorry my description wasn't the best!
I've got a box, say 300 x 300 and I want the ball to bounce around hitting the sides and rebounding in the direction you would expect it to. I guess what I'm most after is some guidance about the best way to go about the problem, Can I write some code that will just alter the x and y movement once it reaches the limits of the box, or do I need to make it a little more complicated and code something that will check which wall it hit before and send it the appropriate direction?
Joined: Feb 03, 2004
you'll do yourself a favour if you check each wall seperately. fancy modifications can be added later.