Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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 2093 times)
0 Members and 1 Guest are viewing this topic.
Offline zngga

Junior Member


Medals: 3


Current Project: Ascension (infinite RPG)


« Posted 2012-06-30 16: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 random features!
Offline tberthel
« Reply #1 - Posted 2012-06-30 16: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

JGO Knight


Medals: 37



« Reply #2 - Posted 2012-06-30 22: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

Senior Member


Medals: 9



« Reply #3 - Posted 2012-06-30 23: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 23:24:31 »

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

Offline marcuiulian13

Senior Member


Medals: 3



« Reply #5 - Posted 2012-07-01 01: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.

Check out my current project: http://cube3dev.blogspot.com/
Please comment/review/suggest.
Offline zngga

Junior Member


Medals: 3


Current Project: Ascension (infinite RPG)


« Reply #6 - Posted 2012-07-01 03: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 random features!
Offline twinflyer

Junior Newbie





« Reply #7 - Posted 2012-07-01 21: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 22: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.

Online Riven
« League of Dukes »

JGO Overlord


Medals: 439
Projects: 4


Hand over your head.


« Reply #9 - Posted 2012-07-01 22: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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 65K
« Reply #10 - Posted 2012-07-01 22:35:31 »

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

Offline marcuiulian13

Senior Member


Medals: 3



« Reply #11 - Posted 2012-07-02 07: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))

Check out my current project: http://cube3dev.blogspot.com/
Please comment/review/suggest.
Offline 65K
« Reply #12 - Posted 2012-07-02 08: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

JGO Ninja


Medals: 66



« Reply #13 - Posted 2012-07-02 12: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (147 views)
2013-05-17 21:29:12

alaslipknot (155 views)
2013-05-16 21:24:48

gouessej (186 views)
2013-05-16 00:53:38

gouessej (180 views)
2013-05-16 00:17:58

theagentd (190 views)
2013-05-15 15:01:13

theagentd (174 views)
2013-05-15 15:00:54

StreetDoggy (218 views)
2013-05-14 15:56:26

kutucuk (243 views)
2013-05-12 17:10:36

kutucuk (241 views)
2013-05-12 15:36:09

UnluckyDevil (247 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.111 seconds with 21 queries.