Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Isometric Draw Order  (Read 2296 times)

0 Members and 1 Guest are viewing this topic.

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Isometric Draw Order
« on: May 23, 2017, 07:59:44 pm »
Hey guys,

I'm making a tile-based isometric game (Diablo style) and I'm trying to tackle draw order.

At first I accomplished this by giving all entities a Z value depending on how many tiles there were between them and the player on the Y axis. With the game screen height being 1080p and my tile height being 60p, this resulted my dividing the screen into 18 "rows" or layers to be drawn from top to bottom. I then needed to repeat the following code 18 times with each entity:

if (random_entity.get_Z() == 18)
window.draw(random_entity);

This was crude and unmaintainable, so I looked around and found this: https://github.com/SFML/SFML/wiki/Tutorial:-Drawable-Group. What I did then was instead create 18 of these Group classes from the tutorial, add a "m_drawables.clear();" function to it and I constantly clear and push_back entities into these groups.

I then finally realized that I wouldn't have to use 18 different groups if I could somehow sort the entities according to their Y positions since whatever's higher on the screen should get drawn first anyway. Unfortunately I have no idea how to even begin implementing this.

From what I understand, the Group class uses a reference_wrapper to store the addresses of sf::drawables; and you use the push_back function to add drawables to it. I know that if it were a simple vector of ints I could just do:

std::sort(m_drawables.begin(), m_drawables.end());

... but it's a vector of objects and I have no idea how to sort the Y or Z values of these sf::drawables.

I hate asking others for code, but in this case at least we could add it to the tutorial. I'm a self-taught c++ enthusiast and I'm learning SFML as I make this game, so I really appreciate any help you can give me.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Isometric Draw Order
« Reply #1 on: May 23, 2017, 09:26:39 pm »
There is another version of std::sort that takes in a 3rd parameter. The third parameter is a function you can pass in to describe how your objects should be ordered.

http://www.cplusplus.com/reference/algorithm/sort/

The examples on that page should be helpful. Especially make note of the example that uses "myfunction"

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Isometric Draw Order
« Reply #2 on: May 24, 2017, 01:41:36 am »
Note that sorting by absolute y position only works if everything is grounded or at least has its origin (the value's y that is tested) on the ground. If an entity was to be "raised" off the ground, the y is artificially higher and can be above an entity of which it should be in front.
The solution could be to always ground the position or origin.

However, if your entity's position is based on positive x for down and right and positive y for down and left (common isometric space), you can calculate comparative depth by just summing x and y. The higher values are, or course, in front of lower values.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything