Depth (sprite layering) in a 2D world
I have a 2D world complete with sprites and my playable character. If my character is in front of a sprite (say a wall) and they overlap, I would like my hero to be on top so it looks like he is standing in front of the object. If my hero's is behind a sprite, I would like my hero to be drawn under my sprite so it looks like he is behind the object creting depth in my 2D game.
In my OnPaint method I can draw my hero first and always have my hero on top, or I can draw my sprites first and always have my hero on bottom. I am cluelss as to how to get the right layering or draw order to make the depth look correct.
Can someone help me acheve this. I am using C# and just paiting to a UserControl. I am not using XNA or DirectX.
Many 2D games store the sprites in several layers, typically a background layer (rendered behind the characters), an 'active' layer (in which the characters are displayed), and a foreground layer, rendered in front of the characters. Collision detection is also only performed on the active layer.
...and typically walls, trees and other people would be sprites in the "active" layer. You should sort all your sprites on the active layer by their y coord before drawing them so you draw from the top of your screen downwards (which would mean that your hero sprite would be drawn before the wall sprite).
You'll want to use the lowest y coord for each sprite for sorting. So for a person that usually means the position of their feet, for a tree it'll be the bottom of the trunk, for a wall where it meets the ground, etc.
You'll want to use the lowest y coord for each sprite for sorting. So for a person that usually means the position of their feet, for a tree it'll be the bottom of the trunk, for a wall where it meets the ground, etc.
Thanks for the replay. That sounds like a good idea, but that assumes all sprites will be parallel with the X and Y axes. What if the sprite is a wall that is at an angle to look like it is going back into the distance?
Quote:Original post by Jason624You specified a 2D world, therefore such things do not exist ;)
Thanks for the replay. That sounds like a good idea, but that assumes all sprites will be parallel with the X and Y axes. What if the sprite is a wall that is at an angle to look like it is going back into the distance?
Realistically, you would split the sprite into 2 sprites, one containing the pixels that should be rendered behind the player, and the other the pixels to be rendered in front.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement