#Help with y sorting

1 messages · Page 1 of 1 (latest)

cloud oak
#

Hi everyone,

I'm working on implementing Y-sorting in C++ for rendering a 2D game map. I need some guidance regarding the structure of my Tile class and how to manage rendering different objects like the player, tiles, and monsters in the correct order.

Here’s what I have so far:

I was planning to add a bool ySortable member to the Tile class to indicate whether a tile should be Y-sorted.

In the map rendering function, I would collect all such tiles in an array, sort them by their Y coordinate, and then render them.

However, I'm running into a design issue:

If I define the array as holding Tile objects, I won't be able to include the player, monsters, or other game entities that also need to be Y-sorted.

This is making me unsure about the best class design or data structure to allow consistent Y-sorting across different object types.

Soo

What kind of class hierarchy or structure should I use so that I can Y-sort tiles, the player, and other entities together in one array? Should I go for a common base class with a virtual getY() and render() method, or is there a better design approach?

real pineBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

cloud oak
#

(sorry for geeting this generated by gpt😭 😭 🙏 🙏 I suck at english😭 )

brazen spear
#

Have you considered using the depth buffer instead of manually sorting?

cloud oak
#

Hmm sorry I didn't mention
I use sdl2

#

What exactly is that?

#

I am new to sdl

brazen spear
#

This is what 3D games use instead of sorting the objects

#

But you can use it in 2D too. But I don't think SDL2 lets you do this unless you use OpenGL with it

brazen spear
#

Also why are you using SDL2 when SDL3 exists?

cloud oak
#

Hmm

cloud oak
real pineBOT
#

@cloud oak Has your question been resolved? If so, type !solved :)

cloud oak
#

!solved