Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
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  
  Rendering in order of Z-value, not array order  (Read 3055 times)
0 Members and 1 Guest are viewing this topic.
Offline zngga
« Posted 2012-06-30 14:26:15 »

I am currently working on a 2D RTS game. I made a custom Entity Manager which stores all the game entities in a single ArrayList. What I have now renders these entities in the order they appear in the array, but I want to render them in order of a z-value instead. I am fairly sure I know how I am going to accomplish this, but wanted to see if anyone had any thoughts before I get too deep into implementation. So any ideas guys?

My code never has bugs... it just develops unexpected features!
Offline tberthel
« Reply #1 - Posted 2012-06-30 14:34:52 »

If the order never changes then you can just insert each new gameobject in the correct z order.

If the order changes during game play then you must update the List when the order changes.

Offline Danny02
« Reply #2 - Posted 2012-06-30 20:09:04 »

The only way to do this is to sort all the things you want to render by their z-value each time you want to render them.

Because this problem is very common stuff like OpenGL uses Depthbuffers(a buffer which stores the depthvalue of each screen pixel)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Z-Man
« Reply #3 - Posted 2012-06-30 21:09:40 »

Just a thought. You could write your own system that is a sort of "depth buffer" (I have no idea how OpenGL's depth buffer works). My idea of this would be implemented either as your render system or along side it and you'd simply call db.drawImage(image, x, y, z) (or whatever draw call). Under the hood you could maintain an ArrayList, or whatever data structure would be fastest, of images representing each buffer "level". When a call is made to a draw method, draw to the specified "level" based on the z value. If there isn't an image for that level then create one and add it to the list at the correct location. Then have a method to draw the depth buffer to the screen, compressing all the layers in the correct order. This is just something I came up with while reading through the thread, I wouldn't suggest actually using it x) It seems rather roundabout and inefficient.

EDIT: The method of drawing to the screen could be as simple as g.drawImage(db.getImage(), 0, 0, null);
Offline Cero
« Reply #4 - Posted 2012-06-30 21:24:31 »

how you determine z depends on the game, but yeah, sort by z, render in order

Offline marcuiulian13

Senior Member


Medals: 5
Exp: 3 years



« Reply #5 - Posted 2012-06-30 23:54:03 »

My method is like this:

1  
2  
3  
4  
5  
for(each row of pixels in the screen) {
     for(each entity on screen)
          if(entity.y == y)
               render entity
}


I'm sure this isn't the best method to use, but is the simplest.

Getting a project done is by far the most hard thing in game development.
Offline zngga
« Reply #6 - Posted 2012-07-01 01:13:26 »

What I ended up doing is implementing a simple bubble sort, which works fine for now. Thanks for all the suggestions though!

My code never has bugs... it just develops unexpected features!
Offline twinflyer

Senior Newbie


Exp: 2 years



« Reply #7 - Posted 2012-07-01 19:55:25 »

We had the same Problem in our 2D RPG.
We solved it like this:

We have an ArrayList which holds every Static object. This List is sorted by the Y Value of the objects (mid of where they "stand", not the Image Y Value).
Before rendering, every static Object is copied into a RenderList. After that every nonstatic Object is sorted in that List.

After that we can render this List from 0 = ArrayList.size()-1 and everything is in the right order.


(yes our Player is a Smily....and the tree is stolen from Pokemon...)

I hope this was what you'te looking for :S
Offline 65K
« Reply #8 - Posted 2012-07-01 20:08:43 »

What I ended up doing is implementing a simple bubble sort, which works fine for now. Thanks for all the suggestions though!
java.util.Arrays.sort() offers a pimped merge sort for free.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2012-07-01 20:21:29 »

What I ended up doing is implementing a simple bubble sort, which works fine for now. Thanks for all the suggestions though!
java.util.Arrays.sort() offers a pimped merge sort for free.
IIRC, since java 6u24 it uses a dual pivot quick-sort.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 65K
« Reply #10 - Posted 2012-07-01 20:35:31 »

I see now, for primitive types its quick sort, for objects its a merge sort variation.

Offline marcuiulian13

Senior Member


Medals: 5
Exp: 3 years



« Reply #11 - Posted 2012-07-02 05:23:22 »

I am sorry to post in your topic, but is there any way to sort an ArrayList firstly by y-coordinate and then by layer? (layer in Tiled, i'm using Slick2D).

Let's say we have an array like this: {[0, 0], [0, 1], [3, 2], [2, 1], [4, 6]} and it should be sorted like: {[0, 0], [0, 1], [2, 1], [3, 2], [4, 6]} (first is the Y and second is the layer (bigger = upper))

Getting a project done is by far the most hard thing in game development.
Offline 65K
« Reply #12 - Posted 2012-07-02 06:22:28 »

I am sorry to post in your topic, but is there any way to sort an ArrayList firstly by y-coordinate and then by layer? (layer in Tiled, i'm using Slick2D).
You can do that by implementing your own Comparator and call Arrays.sort() with it.

Offline Roquen
« Reply #13 - Posted 2012-07-02 10:15:28 »

Another option is to bit pack the two values.
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.

Nickropheliac (14 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (28 views)
2014-08-22 19:31:30

atombrot (40 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32
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!