Hello Guest

Author Topic: Sprite Layering  (Read 5714 times)

loofou

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Sprite Layering
« on: December 23, 2012, 02:59:30 pm »
Hi there,

I have a question about best practices for sprite layering. I'm currently working on an oldschool rpg style game (eg Zelda ALttP, or Final Fantasy 6 or less) with tilemaps. Tilemap layering is not the issue, but layering overlapping sprites in the world itself.

I want my sprites to overlap sprites that are "behind" them (from game perspective), but are overlapped from the same sprites, if they are "behind" them. I don't know how to best describe my problem, so I made a few images with prototype graphics.

Player is the green link sprite. This is correct.
https://docs.google.com/open?id=0B0m9Fxe6-AAQZ0FWdU1HbktWRVU

If the player walks behind the dark link sprite. This happens now, but is not correct.
https://docs.google.com/open?id=0B0m9Fxe6-AAQV0d3bGxsaEJaQUU

And this is the correct expected behavior.
https://docs.google.com/open?id=0B0m9Fxe6-AAQeWVQM2pTbmpKNnM

My question is: What are the best practices to get this behavior? I thought of always changing the z-positition of all sprites in regard to their y-position, but i guess thats dumb.
Another thought was to change the render queue all the time, but that would mean to create a new material every time, right?

I appreciate any help of you guys. Thanks in advance :)

Loofou

Supagoat

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Sprite Layering
« Reply #1 on: December 25, 2012, 04:27:19 pm »
Why would it be dumb to change the z position?  That's how I'd do it.

loofou

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Sprite Layering
« Reply #2 on: December 25, 2012, 09:12:37 pm »
For starters: At which z-position should the tilemap be? I don't want to move around every single object every frame. I mean the most trivial method would be to set 'z = - y' every frame. So things that go "up", go "behind". But after a few seconds going "up" or "down" your objects would move behind the map or out of the camera. This would be a problem even with 0.01-steps if the map is big enough.

I think one could just change the position of sprites that overlap, but before I implement something like that I want to know if there are any simpler methods, i haven't thought of yet.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite Layering
« Reply #3 on: December 26, 2012, 05:04:28 am »
For starters: At which z-position should the tilemap be? I don't want to move around every single object every frame. I mean the most trivial method would be to set 'z = - y' every frame. So things that go "up", go "behind". But after a few seconds going "up" or "down" your objects would move behind the map or out of the camera. This would be a problem even with 0.01-steps if the map is big enough.

I think one could just change the position of sprites that overlap, but before I implement something like that I want to know if there are any simpler methods, i haven't thought of yet.
[/quote

First, set your camera z range to something large. Also move your camera back so the far end of the camera is where the tilemap is.

The tilemap should be behind everything else, so lets set z = 0.
The camera should see the tilemap, so lets set the camera position to -1000. The Far clipping plane should be at least 1000. I'd set it to 1001.

Now you should have a lot of range to set z = -y * 0.01f;

You can certainly move the camera back further should you want/need to.

loofou

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Sprite Layering
« Reply #4 on: December 26, 2012, 12:38:08 pm »
Hm, well, ok. I thought there were a more elegant way to solve this. A colleague implemented something like this by changing the render queue, but it was very slow with many sprites.

And yep. I didn't thought of moving the camera farther away ::)

Thank you for your help :)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite Layering
« Reply #5 on: December 26, 2012, 03:28:58 pm »
Changing renderqueue will massively increase drawcalls, this is a much nicer way to do it.