New Tutorial.....

Posted by Radial543 on July 16, 2007, 10:23 p.m.

Lighting in games are a great effect. Imagine yourself in an RPG, going into a cave and everything goes black. You have to lit a torch to continue. I'll show you one way to do this, one that is easy to understand for everybody. Here is a screenshot of how the final result will look. You might recognise the squares, but don't fear, it is possible to make these squares as small as is needed for a good result. You can find an example file at the bottom of this page and you can play around with the size of…. the sprite - for each square is an object using a sprite and they all have different level of alpha. This level is calculated using a formula, which will soon be described, but first I shall present it all to you. This is how it looks, obj_char:

Create Event:

execute code:

for (iy=0; iy<room_height; iy+=obj_darkness.sprite_height) {

for (ix=0; ix<room_width; ix+=obj_darkness.sprite_width) {

instance_create(ix,iy,obj_darkness);

}

}

Step Event:

execute code:

if (keyboard_check(vk_right)) x+=4;

if (keyboard_check(vk_left)) x-=4;

if (keyboard_check(vk_up)) y-=4;

if (keyboard_check(vk_down)) y+=4;

This is the character and he moves around, that is what is in the step event. This is not intresting. The create event though is highly relevant for lighting as it is here that all the objects are created. We create just enough number of objects to cover the entire room with them. Each of that object is totally black - so as it is now nothing can be seen. We'll fix that in the step event of the black object.

image_alpha = distance_to_object(obj_char)/100;

Where 100 is the maximum distance at which the player can see anything at all. How come? distance_to_object is the distance from the individual object to the character. If this distance is 100, then image alpha becomes 1 as 100/100 = 1, and all values above 100 will result in 1.x or even x.y, an arbitary number depending solely on the distance. But if distance is smaller than 100 then you will get 0.x, which means you can see through that black block. And you can see what's underneath it. Around the player you will be able to see everything.

Depending on the size of these blocks, the more detailed lighting you get. Play around with the example…

Comments