1
\$\begingroup\$

I'm having trouble working out how to recalculate 2D sprite sorting / draw order based on camera rotation.

In the example below:

Screen 1 shows a non-rotated typical "top down 2D" scene, where the grey objects can be sorted based on their screen Y position, drawn from top to bottom, so in this case B, A, C.

Screen 2 shows the situation after the player/camera rotates clockwise, with the original screen up vector (green) and the new up vector (red). In this situation, sorting by object Y position will not work, because A should now be drawn after C.

What I need to do is to sort the objects not only by their Y position, but also relative to the new up vector and/or camera rotation, but I'm unsure how to handle this.

enter image description here

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

It turns out I was probably overthinking this quite a lot. The solution in the end was to just calculate the rotated position of the object by rotating their current position Vec2 by the inverse of the camera rotation. I could then sort by this rotated Y value.

\$\endgroup\$
3
  • \$\begingroup\$ If your question is solved, it would be helpful to mark this answer as the correct one, so the question won't bump in the homepage expecting more answers. \$\endgroup\$ Commented Sep 24, 2019 at 11:16
  • \$\begingroup\$ I'm aware, however it won't allow marking it as the answer until tomorrow. \$\endgroup\$
    – Runcible
    Commented Sep 24, 2019 at 12:14
  • \$\begingroup\$ Sounds good! Glad your problem was resolved :) \$\endgroup\$ Commented Sep 24, 2019 at 12:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .