Jump to content
  • Advertisement
Sign in to follow this  
Jason624

Depth (sprite layering) in a 2D world

This topic is 3606 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
...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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Quote:
Original post by Jason624
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?
You specified a 2D world, therefore such things do not exist ;)

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.

Share this post


Link to post
Share on other sites
2D does not mean everything has to look like a grid.

I think I am just going to split my sprite into mutiple spirtes. Each just a little higher than the other. It will give the illustion of a continuous wall.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!