Hello everyone, I’m a beginner developer writing different games and applications in java (I practice, I gain skills). I have been haunted by the problem of drawing a large number of objects. All of them greatly inhibit the gameplay, even if they are not updated, I certainly understand that the redrawing method also plays the role of updating, but if you call the rendering method only when creating an object, it immediately overlaps with the background of the application, which erases all residual tinsel. But after all, people do it somehow - below is an example. In this example, the objects are not just lying on the field, but also have their own hit-box, apparently, since in a collision with them, the player leaves a mark. I would like to achieve about this result, and I ask everyone who is familiar with this problem to share the material or their experiences. Perhaps my problem lies in the rendering algorithm itself, but since I am a beginner, I don’t know how to do it better yet. I would be very grateful! At the moment I try to understand libGDX, in order to find more optimization, but apparently the problem lies in me ... 
- Are you sprites batch? github.com/libgdx/libgdx/wiki/… - Suvitruf ♦ 2:07 pm
- No, I did not come across this. And I don’t quite understand the essence of the sprites' batting, if not difficult, please explain on fingers. - It happens
- Okay, I got a little into it. But what is the difference between sprites and graphic primitives, especially if the object is simple to the impossibility and more convenient to use the second. * Of course in terms of optimization - It happens
- I gave this as an example. Until you know exactly where the drawdown is, it is difficult to substantively speak. What is there in profiler? - Suvitruf ♦
- A huge amount of RAM is being eaten, as if some AAA project has been launched. Again, most likely the reason is in my code. I use an array for all game objects, and for example, such simple objects in the form of effects or tiles on the floor also go there, and processing thousands of java objects is not a thrill. Just how else to do to me does not reach, I do not argue, you can not update the objects beyond the player’s field of vision, but if they are all at one point, then what to do? - It happens
1 answer
How to draw a lot of objects without losing performance?
General optimization techniques
When a frame is drawn, the call to the function of drawing takes a lot of time. Therefore, the first thing I do for accelerating the graphics engines is baching. Baching is the process of grouping objects that can be drawn into one approach. For example, in a particle system instead of drawing hundreds of squares one at a time, they create one grid of polygons and draw all at once in one approach. Baching of all objects, as a rule, is not possible. The easiest batching can be done like this: sort all objects by depth, group them by material (shader + textures), calculate the final position of all grids in the current camera, connect all grids into one. This is a dynamic baching. There is still static when, for example, in the 3D scene, the details of the landscape are combined into one large grid. In general, I am surprised every time how much faster everything works when you turn on baching.
To improve baching, you need to use atlases - one large texture with many small textures. This will allow you to group more objects. There are cases when a shader is generated which simultaneously uses several atlases dynamically choosing the right one for the right object.
The second technique for rendering optimization is rejection. There are several elements here. Screening surfaces that are not directed into the camera (mainly for 3D) - back face culling. Rejection of objects outside the pyramid of the review - view frustum culling. And to help her - tiling (tessellation) of objects that are partially in the pyramid.
For effective texture rendering, it is necessary that the texture pixels maximally coincide with the pixels of the buffer in which they will be drawn (1: 1 scale). It also improves the quality of the final image.
Optimization in particular cases
The first thing to do when optimizing is to understand what exactly needs to be optimized. To do this, use a profiler. Profiler will show how long each piece of code takes. After that, you can do code optimization. It is also very important to understand where the problem is implementation, and where is the algorithm. About the optimization of each case is better to ask separately.