Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  SOLVED - ISOMetric Sort Order Issue  (Read 4815 times)
0 Members and 1 Guest are viewing this topic.
Offline Fsig

Senior Newbie





« Posted 2014-05-09 09:13:27 »

Hi there.

I am trying to sort objects on an ISOMetric map to get the correct drawing order. I have tried using the below example which works perfect when all the objects are the same Height and Width but as soon as the Height or Width changes then this method is no longer accurate as seen in the pictures.

I have tried searching the forums and internet for how others have overcome this issue and the answers seem to be use OpenGL etc... I am sure there has to be another way to accomplish this with some math... If anyone knows of a way to correct this issue or can point me in the right direction it would be appreciated.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
package com.deadendertainment.chat.objects;

import java.util.Comparator;

import com.deadendertainment.chat.ChatGame;
import com.deadendertainment.chat.characters.Player;

/**
 *
 * @author Fsig
 *
 */

public class GameObjectComparator implements Comparator<GameObject>{

   @Override
   public int compare(GameObject e1, GameObject e2) {
      return (int) ((e1.getX() - e2.getX()) - (e1.getZ() - e2.getZ()) - (e1.getY() - e2.getY()));
   }

}


Example picture one:


Example picture two:
Online Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2014-05-09 09:32:54 »

1. first sort by X axis
2. then sort (whatever is on the same row) by Z axis
3. then sort (whatever is on the same tile) by Y axis (height)

This means that for every tile you visit, you draw the items from lowest to highest.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Fsig

Senior Newbie





« Reply #2 - Posted 2014-05-09 11:13:30 »

1. first sort by X axis
2. then sort (whatever is on the same row) by Z axis
3. then sort (whatever is on the same tile) by Y axis (height)

This means that for every tile you visit, you draw the items from lowest to highest.


Thanks, ill give this a go.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #3 - Posted 2014-05-09 11:42:57 »

Depends a bit on how you are storing/drawing your objects. If you only have a 2d array, e.g., you can simply draw them in order from lowest to highest like Riven suggests. But glancing at your comparator, simply sorting by depth (x+y+z) should work:

1  
2  
3  
4  
5  
6  
   @Override
   public int compare(GameObject e1, GameObject e2) {
      int a = e1.getX() + e1.getY() + e1.getZ();
      int b = e2.getX() + e2.getY() + e2.getZ();
      return (a < b) ? -1 : 1;
   }

Offline Fsig

Senior Newbie





« Reply #4 - Posted 2014-05-10 01:53:57 »

Depends a bit on how you are storing/drawing your objects. If you only have a 2d array, e.g., you can simply draw them in order from lowest to highest like Riven suggests. But glancing at your comparator, simply sorting by depth (x+y+z) should work:

1  
2  
3  
4  
5  
6  
   @Override
   public int compare(GameObject e1, GameObject e2) {
      int a = e1.getX() + e1.getY() + e1.getZ();
      int b = e2.getX() + e2.getY() + e2.getZ();
      return (a < b) ? -1 : 1;
   }


The above method yields the same results.
I haven't been able to work out a good way of sorting by rows and such yet.

The Objects are stored in an ArrayList and then to render I use a for loop as seen below.
1  
2  
for(GameObject go : gameObjects)
            go.render(g);
Offline Fsig

Senior Newbie





« Reply #5 - Posted 2014-05-10 04:47:29 »

Resolved by comparing the X then the Z as seen below like mentioned above. (Ignoring Y altogether)
Well this at least seems to fix the height issue, I will have to test width when I make a object for one.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
@Override
   public int compare(GameObject o1, GameObject o2) {
      if(o1.getX() < o2.getX())
         return 1;
      else if(o1.getX() > o2.getX())
         return -1;
      else
         return 0;
   }

   @Override
   public int compare(GameObject o1, GameObject o2) {
      if(o1.getZ() < o2.getZ())
         return -1;
      else if(o1.getZ() > o2.getZ())
         return 1;
      else
         return 0;
   }


And then in my update method I just call them in order.

1  
2  
         Collections.sort(gameObjects, goX);
         Collections.sort(gameObjects, goZ);


Picture one:


Picture two:



Thanks Guys Cheesy
Offline jonjava
« Reply #6 - Posted 2014-05-10 07:26:42 »

Try this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
  @Override
   public int compare(GameObject e1, GameObject e2) {
      int a = e1.getX() + e1.getZ();
      int b = e2.getX() + e2.getZ();
      if (a < b) return -1;
      if (a > b) return 1;
      // if same, order by Y value
     return e1.getY() < e2.getY() ? -1 : 1;
   }

Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (20 views)
2014-09-21 02:42:18

BurntPizza (14 views)
2014-09-21 01:30:30

moogie (14 views)
2014-09-21 00:26:15

UprightPath (25 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (42 views)
2014-09-12 09:08:26

Norakomi (73 views)
2014-09-10 13:57:51

TehJavaDev (97 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!