1
\$\begingroup\$

I would like to create a game similar to powder game.

I've got a 2d array that stores different types of powder such as sand, water, gas, and fire. I've managed to get these to all move around successfully.

I would now like the powders to react to wind. For example if there is fire it should push air upwards. If the air is moving upwards it will move other bits of powder around.

The way I've been trying is to have two more 2d byte arrays. One for the wind x direction and another for the wind y direction. The fire will then add an amount to the cells and each update will add the wind x and y to the piece of powder that is on that cell. This almost works but pushes the powder much to fast.

The difficulty in doing this is that the powder is on a grid while the wind could be going a variety of directions.

What is the most efficient way of doing this?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Hint: air is a fluid. Wind is a current. Particles flow along with the stream. Techniques used for fluid simulation, like the Navier-Stokes equations, can be applied here. \$\endgroup\$
    – DMGregory
    Dec 28, 2019 at 20:58

1 Answer 1

1
\$\begingroup\$

I believe the problem you are experiencing is that while wind can blow in any direction from 0 to 360 degrees, your particles can only move aligned to the grid you've established. To remedy this, I would give each particle a real position, but snap each particle to your grid. That way, diagonal movements are still accounted for, but they don't cause particles to misalign with other particles.

For example, let's say a particle is resting at the bottom of your level. We'll say its worldPosition is (14, 0). We'll say that each particle is 3 pixels wide, and so the particles snap to the grid in increments of 3, putting our particle's gridPosition at (4, 0).

Then, a force acts on the particle, translating it by (2, 2). Our particle's new worldPosition is (16, 2), and its new gridPosition moves to (5, 0).

Over time, gravity would act and bring the particle back down to rest on the floor at worldPosition (16, 0). However, its gridPosition remains at (5 , 0).

Basically, you're storing and operating on all the relevant physics information within the worldPosition, then organizing and displaying it to the player at gridPosition. That way you can operate "between" cells without your game falling apart.

\$\endgroup\$
1
  • \$\begingroup\$ You can also solve this problem probabilistically by assigning a y/(y+x) chance of moving vertically and a x/(y+x) chance of moving horizontally where x and y are the horizontal and vertical components (well, absolute value of the components) \$\endgroup\$
    – rtpax
    Jan 29, 2020 at 20:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .