Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (775)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
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  
  Collections.sort() explanation  (Read 1112 times)
0 Members and 1 Guest are viewing this topic.
Offline FabulousFellini
« Posted 2018-10-11 17:01:39 »

I'm making a top down game and needed a way to render objects in the correct order based on their y-axis position.  After a day of trying to hard code it, I ended up using Collections.sort() and it works perfectly, but I don't understand why it does.  The docs say it sorts by "natural ordering", but I don't understand what that actually is.  Here is the code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public static void sortAndRenderObjectsInYPositionOrder(
         ArrayList<GameObject> gameObjectList,
         SpriteBatch batch,
         ShapeRenderer shapeRenderer,
         ImageLoader imageLoader
         ) {
      Collections.sort(gameObjectList);
      for (int i = 0; i < gameObjectList.size(); i++) {
         if (playerPositionIsWithinBoundsToRenderGameObjects(
               gameObjectList.get(0).getX(),
               gameObjectList.get(0).getY(),
               gameObjectList.get(i).getX(),
               gameObjectList.get(i).getY(),
               gameObjectList.get(i).getHeight(),
               gameObjectList.get(i).getWidth()
               )) {
            gameObjectList.get(i).renderObject(batch, shapeRenderer, imageLoader);
         }
      }
   }


My question is, how is the gameObjectList actually being sorted, and since each object has an x, y, width, and height, how is it seemingly sorting by the y-position of the objects without my specifying to sort that way?

-FabulousFellini
www.fabulousfellini.com
Offline KaiHH

JGO Kernel


Medals: 636



« Reply #1 - Posted 2018-10-11 17:08:50 »

You implemented the ordering in your GameObject.compareTo(). Collections.sort(List<T>) requires T to implement Comparable.
See: https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List)
Offline FabulousFellini
« Reply #2 - Posted 2018-10-11 17:16:13 »

I guess that's another question.  The compareTo method has nothing in it.  It just returns 0.

1  
2  
3  
4  
5  
@Override
   public int compareTo(Object arg0) {
      // TODO Auto-generated method stub
      return 0;
   }


How does it know what it's comparing?  Or does it just compare all the fields within the object?   Also, thanks Smiley

-FabulousFellini
www.fabulousfellini.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KaiHH

JGO Kernel


Medals: 636



« Reply #3 - Posted 2018-10-11 17:34:03 »

If that is indeed so, then Collections.sort() will not do anything. It will not alter the ordering of the original list, because the sorting algorithm is stable (i.e. does not change the order of equal elements - with equal meaning that o1.compareTo(o2) == 0).
Offline princec

« JGO Spiffy Duke »


Medals: 1059
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #4 - Posted 2018-10-11 17:34:26 »

The short answer is, it doesn't work, you're just "lucky" and probably haven't noticed that it's broken yet.

Cas Smiley

Offline FabulousFellini
« Reply #5 - Posted 2018-10-11 17:41:57 »

Well I guess I'll be back when it breaks lol.  I'll play around with compareTo() and try to make it actually compare y values.  All I know is for now, it's all rendering in the correct order ¯¯¯\_(ツ)_/¯¯¯    Thanks all Smiley

-FabulousFellini
www.fabulousfellini.com
Offline philfrei
« Reply #6 - Posted 2018-10-11 22:29:59 »

It might be interesting to manually (optionally) swap a couple y values in  gameObjectList prior to sending it to this method, and see if you get the same results.

music and music apps: http://adonax.com
Offline FabulousFellini
« Reply #7 - Posted 2018-10-11 22:39:04 »

Ok cool, I'll see what happens and update tomorrow. 

-FabulousFellini
www.fabulousfellini.com
Offline FabulousFellini
« Reply #8 - Posted 2018-10-12 16:19:49 »

Well yep I'm an idiot!  The players are only 1 x 1, and every tile is also 1 x 1, so when the player was "in front" of the objects it looked like it was rendering correctly since they didn't overlap.  So I messed around with the compareTo() method but couldn't really get it working correctly.  I tried variations of this:  (dont mind it being commented out, just did that to get the game back to the original state):

1  
2  
3  
4  
5  
6  
7  
8  
@Override
   public int compareTo(GameObject object) {
      /*
      if (this.getY() < object.getY()) {
         return 1;
      }*/

      return 0;
   }


Making the comparator > produces the same results, kinda like it's rendering everything in wrong order.  But I'm gonna mess around some more with it and see what I can do.  I have a hunch, but it's pure speculation, that is has to do with this code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
if (playerPositionIsWithinBoundsToRenderGameObjects(
               gameObjectList.get(0).getX(),
               gameObjectList.get(0).getY(),
               gameObjectList.get(i).getX(),
               gameObjectList.get(i).getY(),
               gameObjectList.get(i).getHeight(),
               gameObjectList.get(i).getWidth()
               )) {
            gameObjectList.get(i).renderObject(batch, shapeRenderer, imageLoader);
         }


And that method is here:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
// This method determines whether the game object is close enough to the player to draw it to the screen.
private static boolean playerPositionIsWithinBoundsToRenderGameObjects(
         float playerX,
         float playerY,
         float gameObjectXPosition,
         float gameObjectYPosition,
         float gameObjectHeight,
         float gameObjectWidth
         ) {
      if (
            playerY > gameObjectYPosition - drawDistance &&
            playerY < gameObjectYPosition - gameObjectHeight + drawDistance &&
            playerX > gameObjectXPosition - drawDistance &&
            playerX < gameObjectXPosition + gameObjectWidth + drawDistance
            ) {
         return true;
      }
      return false;
   }


But I'm not sure as I haven't tested it yet.  I'll keep ya posted.

-FabulousFellini
www.fabulousfellini.com
Offline Longarmx
« Reply #9 - Posted 2018-10-12 17:13:47 »

Why not (assuming coordinates are doubles)
1  
2  
3  
4  
@Override
public int compareTo(GameObject object) {
    return Double.compare(getY(), object.getY());
}

This will sort them in increasing order of y-values.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline FabulousFellini
« Reply #10 - Posted 2018-10-12 17:17:08 »

Why not (assuming coordinates are doubles)
1  
2  
3  
4  
@Override
public int compareTo(GameObject object) {
    return Double.compare(getY(), object.getY());
}

This will sort them in increasing order of y-values.

I tried something similar, admittedly not sure if it was the same as that, but I shall try when I get home, or on my lunch break Smiley

-FabulousFellini
www.fabulousfellini.com
Offline FabulousFellini
« Reply #11 - Posted 2018-10-13 15:54:13 »

Why not (assuming coordinates are doubles)
1  
2  
3  
4  
@Override
public int compareTo(GameObject object) {
    return Double.compare(getY(), object.getY());
}

This will sort them in increasing order of y-values.

This works perfectly!  Thanks for helping me out guys, sort() and compareTo() make a lot more sense to me now.  Hopefully this can help someone else in the future.     Cool

-FabulousFellini
www.fabulousfellini.com
Offline princec

« JGO Spiffy Duke »


Medals: 1059
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #12 - Posted 2018-10-13 21:19:07 »

Try simply returning getY() - object.getY() instead.

Cas Smiley

Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (45 views)
2018-11-16 13:46:03

hadezbladez (50 views)
2018-11-16 13:41:33

hadezbladez (29 views)
2018-11-16 13:35:35

hadezbladez (20 views)
2018-11-16 13:32:03

EgonOlsen (1900 views)
2018-06-10 19:43:48

EgonOlsen (1936 views)
2018-06-10 19:43:44

EgonOlsen (1286 views)
2018-06-10 19:43:20

DesertCoockie (1718 views)
2018-05-13 18:23:11

nelsongames (1412 views)
2018-04-24 18:15:36

nelsongames (2043 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!