Thursday, September 8, 2016

Where to start creating a 2d rain simulation with OpenGL ES 1.1?


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?



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...