# Introduction

I am making a 3D Isometric game. I have a `Tower`, filled with `Block`'s in a 3D `Tower` map, which is made of nested `ArrayList`'s, And `Entity`'s which roam free in my game Not bound to a tile

I have no tile system in my game. To create my isometric projection, I use `Graphics2D` `g2d.rotate() && g2d.scale()`.

I have a function with sorts my game objects (`Block`'s and `Entity`'s) draw order. What I mean by this is the order in which they are drawn in my draw function. This is so I draw objects that are behind other objects, before the object they are behind. In order to stop images glitching through each other and the like.

# Explanation

I have a function which is called at 60Hz, This function makes a new `ArrayList` and inserts my `Entity`'s and `Block`'s into this `ArrayList`.

My function then enters a for loop of the size of the `ArrayList` `-1`

In this loop, The function then proceeds to sort this `ArrayList` based on this expression

`fx>=sx+sw || fx+fw<=sx-sz-sh || fy>=sy+sl || fy+fl<=sy-sz-sh || fz>=sz+sh`

This expression simply says if the first object should be drawn infront of the second object, then swap these two objects around in the list. So that when it comes to drawing the objects, the first object is drawn after the second object.

Finally, the for loop runs its course, I then update the global `drawList` field so that my draw function can use it.

# Question

What can I do optimise this code? More specifically, the expression and sorting of the draw list. Is there a better way to do this? A more concise and accurate expression? Mabye a non-reccursive method?

I call this function regually In my master update function so optimising this would greatly increase the overall preformance of my game.

# Code

````public void sortRenderOrder(){     ArrayList<Object> drawObjects = new ArrayList<Object>();     for (int e=0; e<entities.size();e++){         Entity entity = entities.get(e);         drawObjects.add(entity);     }      for (int h=0;h<tower.map.size();h++){         for (int r=0;r<tower.map.get(h).size();r++){             for (int c=0; c<tower.map.get(h).get(r).size();c++){                 Block block = tower.map.get(h).get(r).get(c);                    if (!(block.material.type.equals("air"))){                     drawObjects.add(block);                 }             }                }     }        for (int i=0;i<drawObjects.size()-1;i++){         int fx=0;int fy=0;int fz=0;int fw=0;int fl=0;int fh=0;String ft="";         int sx=0;int sy=0;int sz=0;int sw=0;int sl=0;int sh=0;String st="";         if (drawObjects.get(i) instanceof Entity){             Entity e = (Entity)drawObjects.get(i);             fx=e.d.x;fy=e.d.y;fz=e.d.z;fw=e.d.w;fl=e.d.h;fh=e.d.d;ft="entity";         } else if (drawObjects.get(i) instanceof Block){             Block b = (Block)drawObjects.get(i);             fx=b.d.x;fy=b.d.y;fz=b.d.z;fw=b.d.w;fl=b.d.h;fh=b.d.d;ft="block";         }         if (drawObjects.get(i+1)instanceof Entity){             Entity e = (Entity)drawObjects.get(i+1);             sx=e.d.x;sy=e.d.y;sz=e.d.z;sw=e.d.w;sl=e.d.h;sh=e.d.d;st="entity";         } else if (drawObjects.get(i+1) instanceof Block){             Block b = (Block)drawObjects.get(i+1);             sx=b.d.x;sy=b.d.y;sz=b.d.z;sw=b.d.w;sl=b.d.h;sh=b.d.d;st="block";         }         //         if (!(st.equals("block") && ft.equals("block")) && (fx>=sx+sw || fx+fw<=sx-sz-sh || fy>=sy+sl || fy+fl<=sy-sz-sh || fz>=sz+sh)){ // thank fuck i solved this THANK YOU WORLD YAY NEVER FORGET THIS NEIN OF CODE             Object temp = drawObjects.get(i);             drawObjects.set(i, drawObjects.get(i+1));              drawObjects.set(i+1, temp);          }     }     drawList = drawObjects; } `
```

Thank you so much.

Replay

Category: java Time: 2016-07-28 Views: 1