Hello Guest

Author Topic: Draw order with moving sprites  (Read 3997 times)

wubak

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 16
    • View Profile
Draw order with moving sprites
« on: September 03, 2012, 07:34:11 am »
Hi,

I am new to TK2D and am making a game with moving character sprites where the bottom of the screen should be perceived as nearer than the top of the screen.

Therefore, if the trunk of a tree is at y = 5, a character at y = 3 should pass in front of it, and a character at y = 7 should pass behind it.

I could move sprites around on the z axis as they move up and down in y, which would then employ the depth buffer to draw in the proper order. This could have other potentially undesirable effects, though. So, is there a better way to make it work?

Thanks for the help!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Draw order with moving sprites
« Reply #1 on: September 03, 2012, 07:38:58 pm »
You should just order the entities in z by the order in which they should render.
So the tree trunk should have the largest z value, followed by the character at y=7, followed by y=3.
I can't think of any issues from moving the characters in z together with y - what potential issues were you worried about?


wubak

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Draw order with moving sprites
« Reply #2 on: September 03, 2012, 07:58:55 pm »
I suppose I was concerned that this could have unintentional physics implications. I suppose I can just give every sprite colliders that are large in the z direction to compensate.

This also adds in some coding overhead, where I'll need add correlated z movement to every sprite.

I guess I was just hoping there was a simple way to make the buffer draw based on the y axis instead.

gabriel_ca

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Draw order with moving sprites
« Reply #3 on: September 07, 2012, 06:49:03 pm »
I had a similar requirement, I solved it pretty much the way unikron described.  In my case all my actor sprites are at -2 z, and I added a fractional component to the z based on y, so I didn't need a huge dynamic range for my colliders/triggers.

float newZ = -2.0f - ((playfieldHeight - newY) / 1000.0f);

this works for me with my range of Y values.

You do have to make sure that anything that repositions actors goes through your actor's "move" interfaces and don't change the transform directly.
« Last Edit: September 07, 2012, 06:54:53 pm by gabriel_ca »