Java & LibGDX: sprite rendering limit range

I recently switched from using array

integers as my screen in Java to use the library. The library I am using LibGDX

and the transform is completely different for me. Most of the stuff I've already started getting, and I'm still writing a little code.

At this point I'm curious if I can limit the rendering range Sprite

and any other drawing factor, for example, if a sprite is stuck halfway out of the box, it wouldn't reflect the part that was sticking out (so :)

Example of Rendering in a Range
Is there a way to render in a certain range, and if it is partially out of range, it does not display what is out of range, or should I do it myself?


source to share

2 answers

You can do a simple "cropping" of the rectangle with the LibGDX ScissorStack .

Since OpenGL is stateful and many of the LibGDX cache APIs, be sure to "flush" or "complete" your batches within the scissor range. See libgdx ScissorStack not working as expected and libgdx Cutting an image



If I don't miss you, you are looking camera


lets you define Viewport

(size) and you only see things inside that Viewport

You can also move it around to see other parts of the world.

For example:

OrthographicCamera cam = new OrthographicCamera(80, 45);


This defines a camera

, which shows you 80 units in x and 45 units in y. By default, P (0/0) is in the middle of the screen, so this one camera

shows objects from -40 to +40 in x and -22.5 to + 22.5 in y.
You can move it so that P (0/0) is in the lower left corner:

camera.position.x = -40;
camera.position.y = -22.5;


This should move the camera 40 units left and 22.5 units down, so P (0/0) is the bottom left corner. Remember to call update()

as this recalculates the projection and view matrix.

Finally, to draw with this camera

, you need to set SptieBatch


to one of camera




Now you can use this one SpriteBatch

to paint.
You should also consider se ViewFrustum-Culling

, which means you don't draw things from camera

because they will never appear on the screen, but the draw call costs some performance.



All Articles