More efficient method to draw thousands of particles (Java / Android)

So, I’m writing some kind of particle simulator, like a “sand game”, if you know what it is, and now I’ve kind of hit a roadblock. The way I do this is a particle object, which essentially now has a position (int x, int y) and that it is. The way I draw / move them is the thread and the onDraw event for the android panel. Every time onDraw is called, I go through all the particles, move them one pixel if they do not fall to the bottom, and then draw them, it is pretty smooth until I get to 200 particles, and then decreases significantly. I know that this is a very difficult calculation, how I do it, there is no debate about this, but is there a way to do this to allow a lot more particles to be drawn and with less delay?

Thanks in advance.

+6
source share
5 answers

I understand that you use the function of drawing individual pixels for this? It really will be slow.

I see a couple of ways to improve it. First, you need to put the pixels in the bitmap in memory, and then draw the entire bitmap at the same time. Secondly, since particles always just go down one pixel, you can scroll through part of the bitmap instead of relaying everything. If Android does not have a scroll, just draw a bitmap one pixel and start a new bitmap for particles above the scroll. You will need to fix the particles below, but there are fewer.

+2
source

You might want to learn about OpenGL ES hardware acceleration and renderscript . This does not give you a more efficient solution code (see Other answers for this). However, it opens up much more processing power. You can even run the entire simulator on the GPU (you may not know the implementation details).

Edit
Also, if you still decide to process in java, you should see Profiling Methods in DDMS . This will help you understand where your performance bottlenecks are.

+2
source

I had never done such things before, but I did some complex cellular automata. Sorry if this is too vague.

The main idea is to mark all particles that should “continue to fall” or “not move” and exclude from complex processing (with a special short / fast processor for the “falling” list - all you have to do is reset each pixel).

  • The acceleration for non-moving particles - static particles (I will call them S-particles) - is that they do not move. Mark it for all non-moving areas (for example, the gravitational-immune "wall" or "bowl" that the user can make. Mark the particles above it S if they are stable, for example, for a liquid if it has S-particles, and on both sides, it doesn’t move.For something like sand, which forms piles, if it has S in each of the three spots under it, it creates a bunch, you will get nice 45-degree piles like this, I’m sure that you can change it to make some things steeper or less cool piles. e S from bottom to top.
  • The acceleration of particles without a particle below them decreases - F-particles. Particles with an F particle below them are also F particles. Mark also from bottom to top.
  • Particles without the F or S sign are complex , they can start to fall, stop falling or rolling, use the slow processor that you already have to deal with them, there should not be much.

In the end, you will have many many fast particles. Those in the heap / lakes and those who go down. The remaining particles are those located on the edge of the slopes, on top of lakes or in other difficult positions. There should not be nearly as many as fast particles.

Visually mark each kind of particle with a certain color, complex particles are bright red. Find cases where it is still slow, and see what other kinds of fast processors you should do. For example, you may find that creating a large number of piles of sand creates many red areas along the slopes, you may want to invest in accelerating “rolling zones” along the slopes of the piles.

Hope this makes sense. Remember to come back and edit as soon as you come up with something!

+2
source

If you blur the image a little, then you can simply move half the figure at a time, perhaps only one quarter and print them all .. to reduce the calculation, and the user will not see it, having felt all those present move.

But no matter what you choose, I think you should set a strong limit, not all users have powerful Android devices.

Regards, Stéphane

0
source

I think that if the particles are close to each other, you can create objects that represent 3 or more particles.

When several particles are displayed on the screen, sets of grains may not be visible.

0
source

Source: https://habr.com/ru/post/890622/


All Articles