Friday, December 5, 2008

Game Engines Part 5

Effects

I mentioned before that 3D and 2D programming (with engines) only differ slightly in their approach to the problem, which is to create an illusion of realistic space. This ceases to hold true when it comes to effects. Almost everything I'm about to discuss is done entirely different in 3D than 2D, so I'll set out those differences for each one.


Particles
For those who don't know, particle animation means drawing a lot of tiny little images or sparkles onto the screen to simulate less dense materials than meshes. Smoke, fire, rain, fireworks and magic are all done using particles. Because of this need, most libraries have something built in specifically for doing particles.

In 3D, the movement and shape of a particle cloud differs depending on how the player is looking at it. Recent advances have allowed the use of particles to do almost anything, like shading and distorting meshes. One of the best open engines for the newer techniques is called Horde3D. I recommend looking it up on youtube for an example of particles gone wild. For an example of how it looks in 2D, play Baldur's Gate.


Shadows
In 2D, shadows are pure illusions. They're fast, low-memory effects that basically paste a dark shape over the ground under a sprite. Sprites can also have the shadows drawn directly on as part of the player's image. Now, there have been 2D games where shadows are actually cast, but without some invisible 3D model this almost gets more advanced than simply doing it in 3D$. For example, how does it determine that a shadow should stop at a corner of the floor and start moving up a wall, when 2D has no concept of "up"?

In 3D, Shadows are easy. Most engines use some advanced, recursive math, but all you have do is place your lights and define what sort of approach the engine is supposed to take. In a program like Ogre3D, you can choose what method of shadows you want, such as additive, modulative, stencil and texture (in combination with one another ). You can also use other shadow libraries alongside Ogre3D if you're not satisfied.

I recently discovered that additive stencilling causes problems on some older ATIs, namely mine, which is why Irrlicht shadows flickered wildly when I attempted to use that engine. Using Ogre3D's modulative stencil shadows, I was able to have beautiful shadow effects without the flickers. Another cause of flickering with shadows is a wide "near to far" ratio, which causes an ugly problem called ZFighting. In simple terms, if you have the game processing a whole lot of shadows at a time in a very large world, it will have trouble figuring out what the proper drawing order is. The defaults are usually pretty safe, but eventually you'll need to learn how to control that.


WATER
In 2D, water is an animation. You're pretty much stuck with that. Sometimes that can be a generated animation, but its always just an animation. In 3D, you have a lot of choices. You can make one flat rectangular polygon, animate water on top of it, and be done with it. You can also go so far as to give it generate polygons for wave effects ( Ogre3D has something for this ). You also get to mess with the way water distorts objects moving inside it, the way it picks up light, and so forth. There's even a lighting effect for drawing wavelike movements on nearby walls, like real water does when light hits it. Again, I'm going to promote Ogre3D and say to check out its Fresnel and Water demos, if you can.


MAPPING
Environment mapping has been done for a long time. It really only works in 3D, since it's based on the angle that the user's camera is facing it. Environment mapping is a reflective effect that makes an object seem mirror-like while cylindrical. It uses a snapshot of its environment, which it maps across its polygons depending on the facing of the camera. Since it uses a given snapshot, it has the problem of showing the wrong location when used in a tile set sometimes. For example, in Neverwinter Nights a player's helmet is mapped to show the sky, so if you're indoors and zoom in, your helmet still reflects clouds.


The gist of all this is this: since 3D adheres to the way physical space really works for human perception, effects can be approached with the real world as a model. 2D, on the other hand, has to be approached the way you'd approach a painting.

No comments: