This article is about how to use depth to create a 3D feeling, but also how to randomly generate a forest, which is an accomplishment itself. Take a look at the generation code (for the controller):
while(instance_number(tree)<40) {
instance_create(random(room_width),random(room_height-sprite_height)
,tree);
}
Create object tree at random position until the number of object tree equals 40, expressed differently, while it is still below 40 create one more. I use the whole width (defined by room_width) for my forest, but I do not entirely use the whole height. This is because I don’t want the whole tree to disappear out of screen, half trees I can get anyway considering that I scale the sprite to fit the depth visually. In difference to a lot of engines I don’t use predefined sizes for my sprite. I scale it, which is more realistic. The above code was the create event of the controller, this is for the tree:
max_sizeh = sprite_height*2
min_sizeh = sprite_height/3
max_sizew = sprite_width*2
min_sizew = sprite_width/3
scaleh = y/room_height*max_sizeh;
scalew = y/room_width*max_sizew;
if (scaleh<min_sizeh) {
scaleh = min_sizeh;
}
if (scalew<min_sizew) {
scalew = min_sizew;
}
depth =-y;
I have a kind of predefined sizes, since I decide the max width and the max height, and the minimum also. This is relative to the sprite though, so any sprite will get the right, proper dimensions compared to each other (if you want to combine different kinds of tree, or maybe stones as well). The last thing I do is setting depth=-y and I do this because I want the smallest tree to be hidden by the larger, if they are covered. This is the only realistic option, and not doing so would totally ruin the 3D feeling.
I have created an example of this which contains all code. In it I have included two different tree sprites, try both of them and compare. Ask if there is anything at all that is not clear. Note that you may change the number of trees by modifying the while string.
http://gmtutorials.com/files/depthex.gm6
.
Users logged in:
Comments
Loading comments...