1
\$\begingroup\$

I followed the Rain Catcher tutorial from libGDX and I made some modifications to the code, allowing the rain drops to appear faster. But I cant figure out how to make the raindrops fall at random speeds.

How can I make random raindrops fall at different speeds? I also want the raindrops to accelerate when it gets closer to the bottom.

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

The first thing to do, would be to specify a velocity component for the raindrop class; which specifies how fast the raindrop falls. To apply this, try the following (assuming the velocity component is called yVelocity):

// Take this line
raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
// Replace with something like
raindrop.y -= raindrop.yVelocity * Gdx.graphics.getDeltaTime();

You can set the yVelocity in the constructor of the raindrop using the random function. (See here for an example of how to use the random function to get a value within a range).

If you want only specific random raindrops to have a random speed applied to then; use the random function to determine whether the constructed raindrop is allowed to have a random speed applied, by checking that the random value falls within a certain range.

The following example applies a random velocity to roughly 1 in 10 raindrops.

if (Math.random() >= 0.9) {
    raindrop.yVelocity = //Random velocity calculation here;
}
else {
    raindrop.yVelocity = 200;
}

To have the raindrops move faster as they travel down the screen, simply increase yVelocity by a gravitational constant in the update call for each raindrop. :)

EDIT: I would recommend that you made Raindrop a class of it's own, as using rectangle directly in that fashion severely restricts what properties you can apply to the raindrops.

\$\endgroup\$
3
  • \$\begingroup\$ I would put it in a class of its own but since I'm not that experienced it will only create problems for me. \$\endgroup\$
    – Blue
    Apr 18, 2012 at 14:13
  • \$\begingroup\$ If you really must do it all inline; create a second collection (which uses floats/doubles) that has another iterator, and have this iterate simultaneously with your main iterator for the Rectangle collection. Then you can use the first iterator for the positions etc. And the second iterator for the yVelocity as given in my examples. \$\endgroup\$
    – Jason H
    Apr 18, 2012 at 16:53
  • \$\begingroup\$ yea I got everything to work on the desktop application but not on the android emulator so i asked another question gamedev.stackexchange.com/questions/27639/… \$\endgroup\$
    – Blue
    Apr 18, 2012 at 17:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .