Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm using opengl 1.1 and i'm creating a 2d side scrolling game.How can i render a 2d rain simulation?What is logic for that?

share|improve this question
1  
Use a particle system for it. make the particle system spawn raindrops all over the map, for every raindrop spawned apply some physics, and check them until they hit something. If it hit something, delete it. That should do. –  Gustavo Maciel May 17 '12 at 23:19
    
@GustavoMaciel Okey but probably i will need a physic engine or can i do it without a physic engine? Any suggestions for a physic engine is that can be useful for this job? –  droidmachine May 18 '12 at 9:53
1  
Actually it can be simulated very easily. Just get a random number between some reasonably values, and make this be the drop weight. A symbolic value. By this drop weight you calculate the raindrop imagezie and also the y speed. –  Gustavo Maciel May 18 '12 at 22:42

1 Answer 1

up vote 3 down vote accepted

Assuming that you're using OpenGL ES because you're on a mobile platform, I'd say that any rendering technique you use should avoid trying to use alpha, for performance reasons. On a system where fill-rate isn't the limiting factor, you can get some lovely looking effects by using alpha planes and animating textures. With that in mind, you're best off making long, thin opaque raindrop polygons (either lines or elongated droplets). You can probably make them alpha if you want, as they'll only be a couple of pixels wide and so won't cost that much fill-rate. But you're going to need a fair amount of them.

The best performance would be to simply have one drop poly and just render it multiple times, but that might look a bit weird. You'd probably get better results having a few droplet polys of differing lengths.

So each droplet is a vertical streak placed somewhere in your scene, and you want to animate it downwards at a relatively constant velocity (maybe < 20% variation in speeds between droplets), starting from offscreen at the top (so you don't see it pop into existence), and moving along its long axis downwards until it hits something at the bottom (the floor, an object bounding box, etc.). Doesn't have to be straight down, you can angle the rain as well to simulate wind.

I'm also going to assume that although you say '2D side scroller', what you actually mean is some sort of faux-3D effect (think Streetfighter or Double Dragon), as I don't think I've ever seen a 'pure' 2D side scroller. That means you've got some sense of perspective on your scene: even though you can't move in 3D, the scene you draw has some sense of back and front, with the player somewhere in the middle.

With that in mind, you probably already have 'layers' in your rendering (first you draw a backdrop (far away), then you draw things which are nearer but still 'behind' the player, then you draw the player, then you draw things which are in front of the player. Each of those layers will have some notional Z distance, but more importantly, they will each have a distance up the screen where the floor is (sort of a perspective effect). So items in the front-most layer have a floor at the very bottom of the screen, but the layer containing the player has a floor some distance up from the bottom of the screen.

So every so many frames, a new rain droplet will be spawned into a layer, at a random horizontal position, and the droplets should be randomly distributed between the layers. You track the droplet by where its lower edge is, and when it hits the floor, you want to start clipping the lower edge of the droplet (so the top of the droplet continues to move downwards, and the drop itself gets shorter and shorter until it disappears). Once the droplet is completely gone (the top edge has also hit the floor), you remove the droplet from the simulation.

You don't really need a full fledged physics engine for this, because the raindrops probably don't conform to natural physics anyway (they just spawn, move in one direction, then die once they hit something). Detecting when they hit something should be straightforward if you are following normal conventions of making a 2D side scroller.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.