I currently am hiding objects that are not in the frustums view. However, some objects are still being rendered that are in the frustums view, but are hidden. My goal is to not render objects that won't be seen at all rather than not render objects that are in the POV but are hidden.

Let me better explain.

There are three boxes, Box A, Box B, and Box C. They are all aligned in the x axis, However, Box C has a greater y value than the rest of the boxes. Well if I'm looking straight towards Box C, I will only see Box C. But Box A and Box B are still being rendered.

I want to know if there is a method that I can use to not render objects that are hidden behind other objects.

Thanks in advanced, Jedi.

  • 1
    \$\begingroup\$ Have you profiled it and verified that rendering the occluded boxes is an issue? You could obviously calculate whether or not one of the boxes is obscured by another (and for boxes this is relatively simple), but to do this for any type of shape at any transform (rotation, scale) the calculation becomes expensive and you might be better off just rendering them. \$\endgroup\$
    – bornander
    Jul 8, 2015 at 6:58

2 Answers 2


zOrdering can be used to prevent the sprites from being rendered on to the screen,Since LibGdx does not support zOrdering you can write your own custom zOrdering the advatage of using zOrdering is you can logically decide which sprite to be rendered on or sprites that must not be rendered on the screen

Here is an example : textureComponets class is used to draw sprite on the screen and it can setZorder for the box to decide which box to be drawn first,second or third or boxes that are never to be drawn on the screen it can also set postions width and height for boxes .We create an array list called textureComponetList to hold three boxes I have created arraylist in MyGdxGame class .

Class textureComponents

public class TextureComponets {

public TextureComponets(Texture texture,float x,float y,float width,float height)
    public int getZorder()
        return zOrder;
    public void setZorder(int x)
public void draw(SpriteBatch batch)
        if(zOrder!=0)//This condition will avoid the boxes with zOrder value zero from rendering onto screen

     public static Comparator<TextureComponets> zOrderComperator = new Comparator<TextureComponets>() {

            public int compare(TextureComponets s1, TextureComponets s2) {

               int zOrder1 = s1.getZorder();
               int zOrder2 = s2.getZorder();



class MyGdxGame

public class MyGdxGame implements ApplicationListener
SpriteBatch batch;

public void create(){
Texture box = new Texture(Gdx.files.internal("box.png"));
batch = new SpriteBatch();
textureComponetsList=new ArrayList<TextureComponets>();

TextureComponets BoxA=new TextureComponets(box,10,10,30,30);
//when you setZorder to zero your sprites wont be rendered on the screen
//The maximum zOrder value it will be drawn in the top most layer or that will be drawn last in the screen

TextureComponets BoxB=new TextureComponets(box,10,20,30,30);
TextureComponets BoxC=new TextureComponets(box,10,100,30,30);
//BoxC will be rendered on the screen since its zOrder value greater than zero


public void render(){
    Gdx.gl.glClearColor(1, 1, 1, 1);

   Collections.sort(textureComponetsList, TextureComponets.zOrderComperator);

//collections.sort function will sort all the components based on the  
//z order set for each box and sort them in the arraylist TextureComponetList 
  // you can also logically setZorder value in program 
//like if(BoxC.y>100){BoxC.setZorder(1)}  or like if(BoxC.x<100){BoxC.setZorder(0)} thus preventing BoxC from rendering on to screen 

        for(TextureComponets textureComponets:textureComponetsList)
//This loop draws the sorted list on to the screen 
//if you look above code we have set zOrder for BoxA and BoxB as zero so they
//wont be rendered on the screen but BoxC will be rendered on screen since it have zOrder value greater than zero



public void dispose(){

//I want to zoom in to the map here
public void resize(int width, int height){

public void pause(){

public void resume(){

Check whether it's possible to draw your objects or not when they are out of screen.

if(yourSprite.x>screenWidth || yourSprite.x+yourSprite.width<0 || yourSprite.y>screeenHeight || yourSprite.y+yourSprite.Height<0){
//Don't draw
//Draw your sprite

And easily can find whether your sprite is behind of another object or not

if(boxA.x>=boxB.x && boxA.x<=boxB.x+boxB.width &&
   boxA.y>=boxB.y && boxA.y<=boxB.y+boxB.height){
//don't draw

But it's easy only for rectangle shapes, if you want to check this for polygons it's very expansive and better to draw your textures.

  • \$\begingroup\$ I have that working, but I need to stop rendering what is behind other objects. \$\endgroup\$ Jul 8, 2015 at 7:29
  • \$\begingroup\$ @JediBurrell see my answer again \$\endgroup\$ Jul 8, 2015 at 7:53
  • \$\begingroup\$ seems like it would work, but I need to do it relative to the camera. I have 2500 boxes so I need to compare them to all of them, but I am calling this function each time the render() function is called. Using a for loop would make it run too slow for playing. Is there another way of doing it, or possibly comparing it to all of the variables at once or am I just going to have to render it? \$\endgroup\$ Jul 8, 2015 at 8:49
  • \$\begingroup\$ Rendering 2500 boxes is much more better than comparing them for drawing \$\endgroup\$ Jul 8, 2015 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .