Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
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  
  Best way to handle depth?  (Read 1121 times)
0 Members and 1 Guest are viewing this topic.
Offline Agro
« Posted 2013-02-09 01:20:48 »

Right now, in my game, to handle depth(like which entities are behind eachother) so that entities appear correctly layered behind one another, what I do is this:

1. Each entity has a depth (negative y position)
2. Each loop of the game, I run my whole ArrayList of entities through the sorting method of who has a higher depth than others etc.

That did the trick, but sometimes it says I'm violating the contract I gave to the sorting method, but at other times the depth doesn't work.

Anyone have a better solution/fix to what im doing?

Offline deepthought
« Reply #1 - Posted 2013-02-09 02:53:06 »

we can't help you if you don't give us your sorting method.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline Agro
« Reply #2 - Posted 2013-02-09 02:53:58 »

Sorry, I thought it was a problem with concept.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
Collections.sort(entities, new Comparator<Entity>() {

         @Override
         public int compare(Entity a, Entity b) {
            if(a.getDepth() > b.getDepth()) {
               return -1;
            } else if(a.getDepth() < b.getDepth()) {
               return 1;
            }
            return 0;
         }

      });

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ClickerMonkey

JGO Coder


Medals: 20


Game Engineer


« Reply #3 - Posted 2013-02-09 02:54:10 »

Have layers of entities (an array of entity lists), assign each layer a specific purpose. Render the layers from back to front.

Offline jonjava
« Reply #4 - Posted 2013-02-09 03:13:00 »

What seems to be the problem? Looks fine to me.

You might want to take a peek at this topic: http://www.java-gaming.org/topics/sorted-list/24888/msg/212173/view.html

I've used a similar method for for sorting tiles in my isometric experiment except that instead of sorting the whole list I surgically insert the tile in its correct position using Collections.binarySearch()

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
class zComparator implements Comparator<ISOObject> {
      // A Comparator class to compare objects z values
     public int compare(ISOObject obj1, ISOObject obj2) {
         // compare z value
        if (obj1.getZ() == obj2.getZ())
            return -1;
         if (obj1.getZ() < obj2.getZ())
            return -1;
         else
            return 1;
      }
   }

   // Methods
  public void add(ISOObject obj) {
      if (arrayList.size() <= 0) {
         // If there are no other objects in the list ( no need to sort )
        arrayList.add(obj);
      }
      // adds an object to the array list at its correct position
     // ( depends on z value )
     Comparator<ISOObject> c = new zComparator();
      int index;
      index = Collections.binarySearch(arrayList, obj, c);
      if (index < 0)
         index = -(index + 1);
      // index is a negative value of the position it would have taken
     // if it doesn't fit between any two other objects
     // more info in javadocs about Collections.binarySearch()
     arrayList.add(index, obj);
   }

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2013-02-09 11:15:34 »

Inserting items in a list/array is among the slowest ways to do sorting.

Dump all your values in a list/array and sort it as a final step.

I told you that in the thread you quoted...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline jonjava
« Reply #6 - Posted 2013-02-09 12:18:35 »

Yeah it was discussed quite thoroughly so I thought the old thread would be relevant.

I actually use both methods, but the latter (slower) method is used for when the world is already instantiated.

I.e:
There are two situations we have to distinguish:

1. You have to add one or a handful of items to a list, which must be ordered.
In that case, use binarySearch(...) to insert the item(s)

2. You have to add more than a handful of items to a list, which must be ordered.
In that case, add the N items to the end of the list, and sort(...) the entire list

Offline lincore

Senior Newbie


Medals: 1



« Reply #7 - Posted 2013-02-09 12:23:11 »

To avoid violating the contract make sure that
1  
(a.compareTo(b) == 0) == (a.equals(b))

(see http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html).

Populating and sorting a list every frame sounds needlessly expensive to me. You should consider updating such a list only when needed, which I guess would be far less often than every frame.
If you are using OpenGL and you don't need the list for game logic but only for rendering, I'd let the gpu handle that by using a 3d vector for vertex positions, which will automagically render everything in order. Otherwise you should organise your entities in layers as was mentioned earlier. Assuming you use integer depth values this can be as easy as having a
1  
TreeMap<Integer, List<Entity>

where each List<Entity> is a layer. To render you just iterate over the map. Of course you should not have too many different depth values.
Offline ClickerMonkey

JGO Coder


Medals: 20


Game Engineer


« Reply #8 - Posted 2013-02-09 12:40:53 »

Avoid Autoboxing like the plague! Change that to an array Entity lists :-P

Offline lincore

Senior Newbie


Medals: 1



« Reply #9 - Posted 2013-02-09 12:50:36 »

Avoid Autoboxing like the plague! Change that to an array Entity lists :-P
Why? It's not like you do complex calculations with them, you just use them as hash keys.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2013-02-09 12:54:59 »

TreeMap and TreeSet are like the LinkedList of List implementations... just use PriorityQueue.

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

Senior Newbie


Medals: 1



« Reply #11 - Posted 2013-02-09 12:58:41 »

TreeMap and TreeSet are like the LinkedList of List implementations... just use PriorityQueue.
Didn't know that one, nice :~)
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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 (18 views)
2014-09-19 03:14:18

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

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

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

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

mitcheeb (64 views)
2014-09-08 06:06:29

BurntPizza (47 views)
2014-09-07 01:13:42

Longarmx (35 views)
2014-09-07 01:12:14

Longarmx (39 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
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!