Search code examples
libgdxgame-engine

Draw Rectangle and see it on batch without any texture


I am making a rectangle :

rect = new Rectangle();
rect.x = 40;
rect.y = 100;
rect.setSize(100,100);

and then also drawing some texture on the position of rectangle like:

batch.draw(myTexture, rect.x , rect.y, rect.width, rect.height);

Now what i want to do is also see the shape of rectangle like i must see some boundary so that i can see my rectangle boundary as well as my texture. And i need this for debugging purposes as the texture is complex like somewhere the background is transparent so i want to remove the collision from those places and that is why i am thinking if i could see both the rectangles and the sprite it would be great.

Although i tried doing this:

shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.setColor(Color.RED);
shapeRenderer.rect(0,0,100,100);
shapeRenderer.end();

I thought the above code would help me draw the texture as well as see the rectangle but turns out i could see only the rectangle's boundary and not the texture.

Here is the output:

pass the bars: a ball game

In the image , you can see a red rectangle and that is where i was trying to debug my bar (a bar of yellow color was also supposed to be shown here) but my yellow doesn't appear as this red-boundary rectangle overlapped my bar.

To be clear, i want to see the shapeRenderer right above the texture and be able to see the both of them


Solution

  • So i found the answer,

    As you know i wanted to appear a rectangle over my texture so that i could see both - my texture and my shapeRenderer shape but instead i could see only my shape and not the texture.

    Problem was that What is was doing is i began the batch and inside that i wrote the code for drawing to the batch and also the code for drawing the shapeRenderer to the batch like this:

    batch.begin();
    batch.draw(texture,0,0,0,0);
    shapeRenderer.begin();
    shapeRenderer.rect(0,0,0,0);
    shapeRenderer.end();
    batch.end();
    

    The above code replaces my texture as it can only draw one thing at the coordinates 0,0 so first it draws the texture at 0,0 and then the shaperenderer at 0,0.

    The solution was:

    To draw the shapeRenderer separately and not within the batch.begin() and batch.end() like:

    batch.begin();
    batch.draw(texture,0,0,0,0);
    batch.end();
    
    shapeRenderer.begin();
    shapeRenderer.rect(0,0,0,0);
    shapeRenderer.end();