aspose file tools*
The moose likes Beginning Java and the fly likes Make the thing bounce! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Make the thing bounce!" Watch "Make the thing bounce!" New topic
Author

Make the thing bounce!

Paul Jones
Greenhorn

Joined: May 03, 2006
Posts: 11
Hi,

I'm a Java beginnner trying to make a simple game, I've done most things on the game, but am trying to make the ball ricochet from one wall to another.

So far my code is;


public void ricochet(int xChange, int yChange, int noOfHits)
{
int hitCount = 0;
while (hitCount < noOfHits)
{
if (this.xPos < 0 || this.xPos > 250 || this.yPos < 0 || this.yPos > 250)
{
hitCount++;
System.out.println(hitCount);
}


if (hitCount % 2 == 0)
{

this.xPos = this.xPos + xChange;
this.yPos = this.yPos + yChange;
System.out.println(yPos);
System.out.println(xPos);
this.delay(100);
this.update();


}
else if (hitCount % 2 != 0)
{
this.xPos = this.xPos - xChange;
this.yPos = this.yPos + yChange;
System.out.println(hitCount);
this.delay(100);
this.update();

}

I know it's a bit crap, but I can't really work out how to make the ball bounce from one wall to the next in a 'natrual way',

Can anyone help??
[ May 09, 2006: Message edited by: Bear Bibeault ]
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
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 ]

With Best Regards,
Shyam Prasad Murarka
Paul Jones
Greenhorn

Joined: May 03, 2006
Posts: 11
Sorry - Here's name!

Regards
Koh Khai Huat
Ranch Hand

Joined: Aug 05, 2005
Posts: 100
Hi Paul,

Can you describe more clearly wat problem u face and which part you needed help on thanks

regards
KKH


if (!WorkHard){
System.out.println("Fail!!!");
}
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
hi,

that would be my aproach:

a ball moves in x and y coordinates (at least for a 2d game). so when it moves into a certain direction, it usually will calculate its new position like this.

x += xOffset; // think of "Speed" in x direction
y += yOffset; // ... in y direction

the offset is the speed of your ball, seperate for each direction, can be positive or negative.

if you got this, the rest is a piece of cake ;-)

if the ball hits a wall at the left or right, the _increase_ or _decrease_ is _not_ influenced, while obviosly the left-right direction has to change.

how?

xOffset = (-1) * xOffset;

analog for bottom / top walls and the yOffset.

got it?

feel free to ask further questions,

jan
[ May 10, 2006: Message edited by: Jan Groth ]
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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 / Amazon Amazon UK )
Paul Jones
Greenhorn

Joined: May 03, 2006
Posts: 11
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?
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
you'll do yourself a favour if you check each wall seperately. fancy modifications can be added later.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Make the thing bounce!