user3316779
user3316779

Reputation: 161

Simple rain algorithm in java

I am trying to create rain animation in android/java using canvas.

The problem is the after raindrops go out of screen, they re-appear on air instead of appearing back in cloud.

What i want is,they should appear back in cloud and the distance between each row of raindrops should remain the same.

However after they go out of screen, the distance between each row changes and they stack on each other.

How can i fix that?

counter = 0;

            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 10; j++) {
                    if(yrain[counter]<c.getHeight()){

                        yrain[counter] = 400+ yAdder[counter] +j*50;
                        yAdder[counter]+=rainSpeed;
                    }else{
                        yAdder[counter]=0;
                        yrain[counter] = 400+ yAdder[counter];
                    }


                    xrain[counter] = 300+ ((50) * i);
                    c.drawBitmap(rain[counter], xrain[counter], yrain[counter],null);

                    counter++;
                }

            }

Upvotes: 2

Views: 887

Answers (1)

Here is my suggestion - I have done it some years ago when needed to show some plot animation:

  • Double and repeat: suppose your rain starts at point Y10 and finish at Y0. Then you can generate a random matrix with drops that has to be updated in their positions moving down in relation to Y coordinate. It goes moving up to the distance (Y10-Y0)x2.

When this circle is completed, such as:

for(step=0;step<(full_cycle_steps);step++)
{    // update Y position downwards  
     perform_animation();
}

then you restart the animation that repeats itself.

  • Smooth motion: you need to apply relative DSP (digital sinal processing), so if you have Y10 to Y0, the interval is 10 integers, take this split by 100, i.e. 10x100 will give you 1000, as you are applying it with the idea of double and repeat, then you have: 10x100x2 = 2000, that means a vector with 2000 to be moving in your canvas/graph targeted coordinates, displayed in your plotting area (1000). Of course if you don't need to have the drop moving so smoothly you can make the number of your digital x analog matrix smaller.

Here you find some reference that can be insightful:

Taking from here, you should be able to easily complete your rain animation.

Upvotes: 5

Related Questions