# Why updating sand simulation in grid doesn't work?

**URL:** <https://discourse.processing.org/t/why-updating-sand-simulation-in-grid-doesnt-work/31722>\
**Category:** Coding Questions\
**Created:** [August 14, 2021, 3:34am UTC](https://discourse.processing.org/t/why-updating-sand-simulation-in-grid-doesnt-work/31722 "2021-08-14T03:34:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafsing](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rafsing/32/10134_2.png) [@Rafsing](https://discourse.processing.org/u/Rafsing)\
**Post date:** [August 14, 2021, 3:34am UTC](https://discourse.processing.org/t/why-updating-sand-simulation-in-grid-doesnt-work/31722/1 "2021-08-14T03:34:45Z")

</div>

I want to make a falling sand simulation using cellular automata, but when I update it, nothing happens, and when I want to do a line of diffrent material using [lineDrawing()](https://pastebin.com/Z9avfnuN) this material appear in random cells. This is update code:

```java

void update()
{
  for (int i = verticalNumberOfCells - 1; i > 0; i--)
  {
    for (int j = 0; j < horizontalNumberOfCells; j++)
    {
      world[j][i].update(false);
    }
  }
 
  for (int y = verticalNumberOfCells - 1; y > 0; y--)
  {
    for (int x = 0; x < horizontalNumberOfCells; x++)
    {
      if (world[x][y].hasMoved) continue;
      if (world[x][y].state == 0 && world[x][y].state == 1) continue;
 
      if (canMove(world[x][y].state, x, y + 1)) 
      {
        move(x, y, x, y + 1);
      }
    }
  }
}

```

The auxiliary functions that I use to check if the contents of a cell can change and to change the contents of a cell look like this:

```java

boolean canMove(int state, int positionX, int positionY)
{
  if (positionX < 0 || positionX >= horizontalNumberOfCells || positionY < 0 || positionY >= verticalNumberOfCells) return false;

  int otherSubstance = world[positionX][positionY].state;

  if (state == 5) return (otherSubstance == 4);

  if (otherSubstance == 0) return true;

  if (state == 2 && otherSubstance == 3 && random(1f) < 0.5f) return true;

  return false;
}

void move(int fromX, int fromY, int toX, int toY) 
{
  Cells otherSubstance = world[toX][toY];

  world[toX][toY] = world[fromX][fromY];
  world[fromX][fromY] = otherSubstance;
  world[fromX][fromY].hasMoved = true;
  world[toX][toY].hasMoved = true;

  world[fromX][fromY].velocityX = 0;
  world[fromX][fromY].velocityY = 0;

  if (toX > fromX) 
  { 
    world[toX][toY].velocityX = 1;
  } else if (toX < fromX) 
  { 
    world[toX][toY].velocityX = -1;
  } else 
  { 
    world[toX][toY].velocityX = 0;
  }

  if (toY > fromY) 
  { 
    world[toX][toY].velocityY = 1;
  } else if (toY < fromY) 
  { 
    world[toX][toY].velocityY = -1;
  } else 
  { 
    world[toX][toY].velocityY = 0;
  }
}

```

I don’t know what’s wrong or why nothing works.

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [August 14, 2021, 2:08pm UTC](https://discourse.processing.org/t/why-updating-sand-simulation-in-grid-doesnt-work/31722/2 "2021-08-14T14:08:54Z")

</div>

Sounds like an interesting project, but could you post the entire code so people can try and find problems? Also you should elaborate a bit more on what you mean by “nothing happens” (does the window open / does anything show up on the canvas) and what is the expected result.

---

<div class="post-metadata">

**Author:** ![Rafsing](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rafsing/32/10134_2.png) [@Rafsing](https://discourse.processing.org/u/Rafsing)\
**Post date:** [August 15, 2021, 1:19am UTC](https://discourse.processing.org/t/why-updating-sand-simulation-in-grid-doesnt-work/31722/3 "2021-08-15T01:19:17Z")

</div>

I was able to fix this problem. The thing was, copying a cell in the move function didn’t work. Here is the wrong version of the code:

```java
  Cells otherSubstance = world[toX][toY];

  world[toX][toY] = world[fromX][fromY];
  world[fromX][fromY] = otherSubstance;

```

and here is right version of the code:

```java
  int oldState = world[toX][toY].state;

  world[toX][toY].state = world[fromX][fromY].state;
  world[fromX][fromY].state = oldState;

```
