1
\$\begingroup\$

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.

\$\endgroup\$
  • 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 '15 at 6:58
0
\$\begingroup\$

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)
{
this.texture=texture;
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
    public int getZorder()
    {
        return zOrder;
    }
    public void setZorder(int x)
    {
        zOrder=x;
    }
public void draw(SpriteBatch batch)
    {
        if(zOrder!=0)//This condition will avoid the boxes with zOrder value zero from rendering onto screen
        batch.draw(texture,x,y,width,height);

    }
     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
    {
    ArrayList<TextureComponets>textureComponetsList;
SpriteBatch batch;

@Override
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);
BoxA.setZorder(0); 
//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
textureComponetsList.add(BoxA);

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

}

@Override
public void render(){
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.glClear(GL20_GL_COLOR_BUFFER_BIT);
    batch.begin();

   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



            textureComponets.draw(batch);

        }
    batch.end();
}

@Override
public void dispose(){
}

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

@Override
public void pause(){
}

@Override
public void resume(){
    }
}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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
}
else{
//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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I have that working, but I need to stop rendering what is behind other objects. \$\endgroup\$ – JediBurrell Jul 8 '15 at 7:29
  • \$\begingroup\$ @JediBurrell see my answer again \$\endgroup\$ – Saeed Masoumi Jul 8 '15 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\$ – JediBurrell Jul 8 '15 at 8:49
  • \$\begingroup\$ Rendering 2500 boxes is much more better than comparing them for drawing \$\endgroup\$ – Saeed Masoumi Jul 8 '15 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.