# Theorical question

• 11-11-2012, 02:21 PM
tnrh1
Theorical question
Hello,
I want to add to my JPanel something like 5-6 balls (could be a picture) that fall down from the sky randomly.. looks like snowing for example.
How would I do that?
I mean would it be an array of balls?with random x and y positions?when I thought about it generally i thought that it would be easy but when I start to code it I didn't knew how to manage that.
They can't touch each other fall on random time and positions.

Any help?
Thanks.
• 11-13-2012, 05:45 PM
Re: Theorical question
Quote:

would it be an array of balls?with random x and y positions?
Yes! thats exactly how that is done.

You can include all kinds of information here, this is a great project for learners - there are many programming aspects covered here.

Your most basic form of this application would involve a simple array of x,y positions which could be generated randomly if you like. You would then simply iterate through the array and draw a ball at each coordinate.

The next form of this app would be to make them bounce around. This is a great time to model things more verbosely - create a Ball class with attributes, like position, direction, color, etc... Your array would now contain a collection of balls instead of just points. You can animate the balls by incrementing their position in every loop.

Next, you could add basic collision detection by looking to see if a ball has hit a wall or not, and if so, reverse it's direction. This will give you a fun, balls bouncing all around effect.

Finally to really make a big finish, make a version which simulates gravity - start the balls at random heights and use an algorithm to make them accelerate towards the ground and rebound when they collide. You can fake this effect with a fibonacci sequence, which many simple games do rather than using real gravity formulas. Or you could just make one up.

If you really want to go balls-to-the-walls (har har), add sliders to the app that let you adjust gravity in real time, and spawn balls by clicking on the screen. You could even make the balls detect each other and bounce off each other.

I've spent a fair amount of time building this exact toy back in my early programming days. Have fun!
• 11-13-2012, 06:20 PM
tnrh1
Re: Theorical question
No one?
• 11-13-2012, 06:35 PM
Tolls
Re: Theorical question
Quote:

Originally Posted by tnrh1
No one?

-Tolls looks at quad64bit's post-
?
• 11-13-2012, 06:40 PM
JosAH
Re: Theorical question
Quote:

Originally Posted by Tolls
-Tolls looks at quad64bit's post-
?

Mwah, invisible ink; I know these things ...

kind regards,

Jos
• 11-14-2012, 03:25 PM
tnrh1
Re: Theorical question
That's very wierd.
When I replayed I couldn't see quad replay and now I do.
Thank you for your replay but something is still not clear to me.
All of the ball objcets will be created in run time or before?I mean incase that I want that each ball will be splitted into 2 balls after 10 seconds how would I do that?
I dont want to be limited.

Thanks.
• 11-14-2012, 05:09 PM