Dealing with light

One of the main features I always wanted to implement in this project was the ability to control your environment. I always find games that allow you to change certain elements of your surroundings attract my attention better and allow me to play uninterrupted for longer periods of time than those that don’t. Since this game can be a stealth game, which I mentioned in my first post, controlling the light in certain rooms can allow you to go undetected by guards and all that. In this post I’ll explain a little bit on how the lighting system works in my game. 

All the little lights
All the little lights

I had two options to choose from when deciding how to deal with light in my game. I could either buy/download one online and just plug it into my project or make my own. Both had their pros and cons. With buying a lighting system, I didn’t have to spend any extra time programming and could move on to the next thing but I don’t learn anything at the end. With making my own lighting system, I would learn more about GameMaker and I would learn about top down lighting even though I would have to spend an afternoon or more programming it. I thought that building my own was more beneficial than just buying someone else’s and I sat myself down to get it done.

My first lighting system was going to be extremely simplistic. All the lighting system did was draw a dark rectangle over the rooms that had their lights off and a bright rectangle over the rooms with their lights on. Although it was simplistic it did have the features I needed but in the end, the simplicity wasn’t working for me. It was too plain and didn’t help set much of an atmosphere. This is the lighting system you have been seeing in my projects so far. You can play a demo here.

easy light system
easy light system

Well, prior to this, I had never made a lighting system before. I always found them too complicated and hard to understand when you got down to reading someone else’s code. This time however, I thought I might as well just give it a go. I downloaded some old GameMaker project and some other projects I found around and got a better understanding of how the lighting should work. In GameMaker, you can use surfaces to draw onto the screen. The application surface is the main surface that GameMaker uses to draw your game onto the screen. It also allows you to create your own surfaces. If you want to read more about GameMaker surfaces read here.

What my system does is that it looks for specific points on an object that detail its corners. It then goes through a slightly complicated process in finding out how the light would hit the object based on these corners. It then draws onto a surface the outcome of this. Here’s an illustration of how it works, I got this from Nocturne’s tech blog post on yoyogames

This is how it works in theory
This is how it works in theory.

Initially, my lighting system was extremely slow for its simplicity (around 11 fps) which frustrated me. I wanted to find a workaround so I started to think on how the lights in my game would work. The lights would only switch on and off when the player chooses to. The lights and walls never move as well. Which is why when I looked at how I was handling everything, I immediately understood how I could better optimize my system better. I had no reason to keep checking updates in the light system every step (which is 60 times in a second). I just had to wait for key events where I knew the light system was going to be changed: When the player turns the light on and off. Just as a precaution though, I made it check for updates in the system only 2 times per second which allowed me to get my 60fps back. I will still be looking at other options for light although if I can make a faster solution.

Light on. Light Off.
Light on. Light Off.

So far this lighting system is doing what I need of it so it can stay for now.

I like!
I like!

So what do you think about my approach to lighting? How have you approached similar things?

Thank you for reading, leave a comment below and don’t forget to share this!

Advertisements

One thought on “Dealing with light

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s