hippyman
Member
GM Version: GMS 2.3+
Target Platform: All
Download: https://www.dropbox.com/s/hl5weo10e94j3k2/EasyDepthSolution.yyz?dl=0
Summary:
I'm sure you've heard about the z-tilting technique. If you haven't you can find it here. While that technique is solid, I'd like to share my much simpler solution to get depth sorting like the old days. By only setting two flags at the start of your game and drawing with matrices. Don't worry if you're not comfortable with matrices. It's literally as simple as it could possibly be. No shaders. No camera setup. Five lines of code.
Tutorial:
Step 1: Enable z testing and alpha testing. These MUST be called FIRST in order for the next step to work. But you only ever have to call it one time at the very start of your game.
Step 2: Any object that needs to be drawn needs to have the following code in their own draw event.
That's it. Now you've got the classic "depth = -y" sorting. You can now replace the z value of the built matrix with whatever depth value you want. Lower values are drawn in front.
Explanation: (I'm not a professor or anything but I'll try my best.)
Basically GMS uses a 3D position in their default shader. But the z value is ignored unless you enable z-testing. So you turn on z-testing and draw the sprites and nothing changes. That's because the z value is always zero by default when the sprite gets drawn. So you have to supply the matrix_world matrix with a new matrix using a z value (that was a mouthful). I'm not going to get into the MVP matrix stuff. This is a good page to learn about that.
You can pop the first step's code into an object that initializes global stuff at the very start of your game.
You can put the code from the second step into a parent object and then any object that needs the same depth sorting can inherit from that parent object.
The nice thing about this system is nothing changes except for being able to sort by depth on the same layer regardless of instance order.
Since you're using the instance's built-in variables (x,y,image_angle,etc), all collision checking, scaling, rotating, etc. should work as expected.
Target Platform: All
Download: https://www.dropbox.com/s/hl5weo10e94j3k2/EasyDepthSolution.yyz?dl=0
Summary:
I'm sure you've heard about the z-tilting technique. If you haven't you can find it here. While that technique is solid, I'd like to share my much simpler solution to get depth sorting like the old days. By only setting two flags at the start of your game and drawing with matrices. Don't worry if you're not comfortable with matrices. It's literally as simple as it could possibly be. No shaders. No camera setup. Five lines of code.
Tutorial:
Step 1: Enable z testing and alpha testing. These MUST be called FIRST in order for the next step to work. But you only ever have to call it one time at the very start of your game.
GML:
gpu_set_ztestenable(true);
gpu_set_alphatestenable(true);
Step 2: Any object that needs to be drawn needs to have the following code in their own draw event.
GML:
matrix_set(matrix_world, matrix_build(x,y,-y,0,0,image_angle,image_xscale,image_yscale,1));
draw_sprite_ext(sprite_index,image_index,0,0,1,1,0,image_blend,image_alpha);
matrix_set(matrix_world, matrix_build_identity());
Explanation: (I'm not a professor or anything but I'll try my best.)
Basically GMS uses a 3D position in their default shader. But the z value is ignored unless you enable z-testing. So you turn on z-testing and draw the sprites and nothing changes. That's because the z value is always zero by default when the sprite gets drawn. So you have to supply the matrix_world matrix with a new matrix using a z value (that was a mouthful). I'm not going to get into the MVP matrix stuff. This is a good page to learn about that.
You can pop the first step's code into an object that initializes global stuff at the very start of your game.
You can put the code from the second step into a parent object and then any object that needs the same depth sorting can inherit from that parent object.
The nice thing about this system is nothing changes except for being able to sort by depth on the same layer regardless of instance order.
Since you're using the instance's built-in variables (x,y,image_angle,etc), all collision checking, scaling, rotating, etc. should work as expected.
Last edited: