The Old World: Dynamic Lighting Explained & New Dungeon Music

Today I'll be explaining a dynamic lighting implementation and I bring gifts of musical theme!

Posted by on

## Introduction

Heyo everyone!

In the previous news post I talked a lot about how the new map system would work, and am happy to say it is implemented and fully working.

I however forgot to mention that I've designed a new way of representing height in The Old World and now it looks a heck of a lot better than those nasty semi-transparent black borders. If you'd like to see how the new height transitions look like currently, feel free to browse the latest images.

## Creepy Dungeon Music

Our dear friend Citizen who is the main composer on our team has been a bit busy as of lately so this time the music has been composed by cYsmix and it turned out pretty well and pretty creepy, Enjoy!

## Lighting System Explained

Right, lets get on to the tasty part. So you're probably wondering by now, Dynamic Lighting System inside a 2D top-down game? Is that possible? Yes, yes it is.

Due to the nature of the new map system, the game environment is actually a 3D world but represented in a 2D top-down way which allows me to do light calculations in 3D space and then represent shadows in a 2D manner.

That sounds confusing even to me, so let me explain, I've split up the way of implementing dynamic light into roughly 3-4 easily digestible parts. These are by no means the final way things will work, but it gives you a good idea on how it'll essentially work.

Just keep in mind, this is a concept image, but represents how it'll essentially look later on inside of the game.

Lets analyse this scene and break it into its fundamental parts.

### Terrain Ambient Light

(For making all of this easier and quicker to explain and less maths, lets say the Sun is roughly in the north somewhere and the light from the sun is hitting everything in the scene at roughly 45 degrees.)

Lets start off with the ambient light, this is just a fancier term for Sun light.
To make the terrain heights show some more difference in height, lets say that everything on a higher altitude gets "more" sunlight than the lower height levels.

As you can see in the scene below, it gives a nice visible change.
This also includes inside the cave, because light cannot quite reach inside of it, we turn the general brightness of those tiles further down than the rest of the scene.

Lets move on to some more fun stuff, dynamic shadows of the terrain! Now for this example because the Sun's light is hitting the scene at 45 degrees it should give a nice 1:1 shadow to height correlation.

Which is pretty easy, just throw in the shadows and you're done, however, what you need to also do is darken the sides that are not facing the sun.

What I did in this scene was to decrease the brightness by about.. 30%, I think, per height value which gives a nice smooth shadow effect.

Saving the best for last!
Sure dynamic shadows on terrain that moves depending on where the sun is fun and all, but what doth that matter inside a dark, damp and creepy cave you're exploring? Nothing at all!
Of course it's all about the torches.. or an oil lamp, whichever floats your boat.

Lets talk about the Doodads' shadows first, what I've essentially done and am planning to do is just take the sprite of the Doodad, turn the whole thing black and voilĂˇ, you have a shadow texture for the Doodad you can use.

Only thing you have to do now is determine how much the transparency should be depending on the intensity of the light and a very important detail is to Skew the shadow.

Skewing the shadow will give the impression that light is hitting said Doodad at a certain direction and also tricks you into believing that you're seeing a 3D object when it's actually just a 2D sprite.

It's quite a nice and simple effect but adds so much to the scene as a whole.

Lets just quickly brush over the Local Light Source.

Essentially there'll be a few different types, omni-directional and directional lights. These lights will also have a height an intensity value in order to calculate all that sweet sweet linear function vector maths.

Because the lights are just as much a light source as the sun is, Doodads will throw shadows if there are local lights nearby as you can see in the image above.

However light emitting from a local light source will actually be able to be blocked by terrain or Doodads.

## Conclusion

Just to quickly explain why I used the Sun position in the north and why the light was hitting the scene at a 45 degree angle was just to be able to quickly explain everything and show off how such a scene would look like.

The calculations will work for any type of position or angle, the maths just becomes a bit tedious to explain. So by using these pre-sets, I don't even have to explain maths, yay!
(Pssst, light at 45 degrees creates a perfect triangle if an object is 90/90 at the base, which causes the shadow to be equally as big as the object itself)

Anyway, I hope you enjoyed the music and the explanation, and I'm currently working on implementing all this so expect to see an update video showing all of this fancy new stuff off.

Laters.

Seriously, this is going beyond the very limits of awesomeness!