wood burning stoves 2.0*
The moose likes Applets and the fly likes Bouncing balls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Bouncing balls" Watch "Bouncing balls" New topic
Author

Bouncing balls

Tomaz Vovk
Greenhorn

Joined: May 08, 2005
Posts: 2
i am trying to do an applet with bouncing balls. each ball starts new thread.
does anyone know how can could i make balls bounce off of eachother? here is the code:

Ball class:



and here is BBalls class (main class)



and here is html code for test.html:

Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Tomaz,
I can help you in finding out whether a Ball touches another Ball(atleast its a start) but I can't help you to make them bounce off each other as I myself don't know.
Start by making a new method in the Ball class called touches(). It is to be implemented as follows:


Anyone who knows how to make them bounce off each other, PLEASE Help US!


With Best Regards,
Shyam Prasad Murarka
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8877
    
    8

Well, implement the touches method that Shyam suggests, and if two balls touch, you reverse the directions that the balls are heading in. You do know what determines the direction of a ball in the code above, right?
(of course there's a little more to it because balls won't always hit square with each other, but for the sake of argument, let's assume they do)


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Tomaz Vovk
Greenhorn

Joined: May 08, 2005
Posts: 2
So if I had an array of Ball class, then I would be able to loop throught the array and call the method touches for every ball with every ball.
for example:

US

or something like that...

and thanks
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
This is a rough idea of one way to look at collisions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bouncing balls