Particle Pile Algorithm

I am creating a game in which there are about 3,000 particles that fall into a heap. The particles are a pixel, and I just use boolean [] [] to set and check which pixel is clear. I'm using this code right now

if (!isFalling(m)) { if (isClear(getX() + 1, getY()) && isClear(getX() + 1, getY() - 1)) setX(getX() + 1); else if (isClear(getX() - 1, getY()) && isClear(getX() - 1, getY() - 1) setX(getX() - 1); } 

the problem is that this code gives me a very strict pyramid shape that doesn't look very natural. I want it to look like salt if you poured it into a heap. My question is, does anyone know about an algorithm or the best way to simulate particle piles? Any help would be greatly appreciated.

Solution: I found a good article here

+4
source share
2 answers

You took on a big task :) It's not just how to calculate and draw particles, it's physics, which describes how they should move.

Take a look at this for starter:

http://www.daniweb.com/software-development/java/threads/426676/sand-game-problem-with-graphics

When using Google, try the β€œparticle generator" or the "particle emitter".

Also see this question:

How do these java sand games track so many particles?

The main thing that you are missing is to add randomness, some entropy, to your system.

+3
source

So your function looks at the particles that are seated on the bottom and asks if they can slide on the sides, and then affects this movement if they can.

Do you have this in a loop? If so, you can consider particles from the bottom up, while sliding motion can take place at any point on the pile. You can try to shuffle the particle list before the loop or re-select random items from the list until you get a low level of resolution.

In the case of a 2D array, you can try to cycle from different directions or find other ways to mix it - again, randomly selecting pixels may be the wrong choice. Simulated annealing comes to mind.

Or you can add a random check to the rolling state. Particles should be semi-stable in their original positions, so perhaps there is only a 50% chance of them sliding down.

Hope this helps!

+2
source

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


All Articles