Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Effecient way to use z-index for 2d games  (Read 693 times)
0 Members and 1 Guest are viewing this topic.
Offline P0jahn

Senior Member


Projects: 3



« Posted 2014-02-20 19:11:16 »

I want to add rendering priorities with z-index for my entities in my 2D game.

For example, I have two entities: A with z-index 4, and B with z-index 5.
If they where on the same position, B would be rendered in front of A because of its higher z-index value.

Here is how my game works: I have an array list with entities. I iterate over the list in the rendering method to access their images.
1  
2  
for(GameObject go : stage.entities)
   drawObject(go);

This means the last entity added to the game will have the highest rendering priority(this is unwanted).

So I was thinking of giving the entity class a new variables called z-index and sort the list according to it, which would give the results I want.
The problem is that the list would have to be resorted every time a new entity is added.

Some ideas?
Offline trollwarrior1
« Reply #1 - Posted 2014-02-20 19:18:34 »

Make a 'layer' or 'z' int in your gameobject class. Right before rendering, sort your gameobjects list so that gameobjects with highest z value are first in the list, and gameobjects with lowest z value are at the end of the list.
Offline P0jahn

Senior Member


Projects: 3



« Reply #2 - Posted 2014-02-20 19:22:21 »

That is exactly what I wrote. Maybe I was unclear?

The problem with that is, that the sorting may affect the performance in a negative way.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #3 - Posted 2014-02-20 19:26:50 »

That is exactly what I wrote. Maybe I was unclear?

The problem with that is, that the sorting may affect the performance in a negative way.


Never worry with perforamance before there is a  problems. Modern cpu's can sort billions entries in second. Its common to sort all render objects as optimization(for less state changes)

Edit: You can also add item to middle of list so you don't need to sort but just move rest of the list one step back.
Offline trollwarrior1
« Reply #4 - Posted 2014-02-20 19:28:49 »

Anything you write into your code effects performance in a bad way.. But it is so small usually you don't even notice.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #5 - Posted 2014-02-20 19:43:17 »

Sorting is at most a few if statements and a loop, that won't affect anything. Consider all the games you have probably seen or played and their complexity. If something as trivial as sorting took up a bunch of CPU cycles then we wouldn't have these amazing games.

Offline The Lion King
« Reply #6 - Posted 2014-02-20 19:55:07 »

If you set stage.entities right off the bat you can just sort them, it won't affect your performance significantly.

If you are adding and removing GameObjects in stage.entities often, look into using a linked list instead so you can efficiently insert GameObjects in the middle of your List. This eliminates having the ArrayList have to make a new array and move all array elements from one array to another which can be very slow mattering on the number of elements.

"You have to want it more than you want to breath, then you will be successful"
Offline P0jahn

Senior Member


Projects: 3



« Reply #7 - Posted 2014-02-20 20:48:17 »

Alright. I guess I had the wrong idea about the sorting.

I assume Collections.sort use quicksort algorithm?
Offline actual

JGO Coder


Medals: 23



« Reply #8 - Posted 2014-02-20 20:49:19 »

It might be more trouble than it is worth but if you have a relatively finite number of z-index values you can have a List for each layer. The layers can then be held in an array which you iterate over. Something like:

1  
2  
3  
4  
5  
6  
7  
8  
// Loop through each layer
for (int i=0; i< layers.length; i++) {

   // For each layer render everything on that layer.
  for (Renderable renderable : layers[i]) {
      renderable.render();
   }
}


if you want your z-index to take arbitrary values or if the z-index is changing frequently it may not be the best approach.
Offline princec

JGO Kernel


Medals: 386
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #9 - Posted 2014-02-20 20:54:34 »

Collections.sort uses modified quicksort, and the latest implemention in JDK7 uses some fancy newfangled "timsort". It's fast enough.

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza
« Reply #10 - Posted 2014-02-20 21:34:14 »

Collections.sort uses modified quicksort, and the latest implemention in JDK7 uses some fancy newfangled "timsort". It's fast enough.

Cas Smiley

Indeed, Timsort is a hybrid sorting algorithm, combining all the best traits (that's the goal anyway) of several algorithms so it can be more effective on more kinds of data.
Quicksort is fast, but on mostly-sorted data it can run in O(n2) time, whereas Timsort's worst-case is listed as O(n log n), which is much better for large datasets, and the sub-algorithms keep it very quick on small sets.

Here you can see it visualized: http://www.youtube.com/watch?v=NVIjHj-lrT4

I do like my sorting algorithms  Wink
Offline philfrei
« Reply #11 - Posted 2014-02-21 01:01:53 »

Roughly how many items are being sorted?

For most situations, I vote with princec's recommendation. But if items are being handled concurrently, you can consider keeping them in a ConcurrentSkipListSet. To do so, you'll have to make a Comparator that uses the Z data.

Quote
This implementation provides expected average log(n) time cost for the contains, add, and remove operations and their variants.
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentSkipListSet.html

I'm using this to store "AudioEvent" objects (custom code I'm writing for my audio library) which can be generated from MIDI data or via game state events. So the list has to allow concurrent access since the audio processing is happening in its own thread and I don't want to create any blocks via synchronization.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
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.

Pippogeek (38 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

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

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

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

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

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