unity – How to create transfer function for temperature in 2D space

First of all, I am making a couple assumptions which are not explicitly written in the question but I consider very likely to be true (because that’s how it works in Rimworld). When any of these are incorrect, please drop a comment and also update the question.

  • Your game world is tile-based. So you don’t need temperature at arbitrary points, just on a tile-based resolution.
  • The game world is a rectangle with a limited width and length.
  • Heat spreads slowly across tiles, not instantly.
  • You already know how to detect clicks on tiles and derive the tile coordinates in your click event.

Under these premises I would actually not rely too much on what Unity can do for me and rather roll my own solution in form of a cellular automaton.

I would create one new gameObject “TemperatureSystem” with a script of the same name. That script has a private two-dimensional array of float’s with the same size as the game tilemap. This array represents the temperature of each tile.

The script would also offer a public method which allows other scripts to read the heat at a specific tile. This method should be used by other game mechanics which take heat into account. I would also add another public method to increase or decrease the heat value of a specific tile. This is for objects which change heat, like heaters or air conditioners. Such an object would in most cases only change the heat of the one tile it is on and then rely on the TemperatureSystem to dissipate the change to the surrounding.

The Update-method of my TemperatureSystem would then derives the new heat value of each cell from the old value of itself and the old value of its neighbors.

  1. Create a copy of your heat values (the new values).

  2. Iterate over the old heat values and:

    1. Calculate the heat difference between this tile and its right neighbor by subtracting the neighbor heat value from its own heat value (use the old values, not the new ones)
    2. Determine the amount of heat being transferred between the tiles by multiplying that value with your desired rate of heat spread and Time.deltaTime. You might modify the heat spread multiplier depending on the materials of the two tiles. So some materials might work as heat insulators while others might work as heat conductors. But remember that the heat taken from a tile should be equal to the heat given to another. Otherwise you violate conservation of energy.
    3. Change the heat values in the new array by subtracting that amount from the current tile and adding it to the neighbor tile
    4. Repeat steps 1-3 for the lower neighbor
  3. Replace the old heat values with the new ones.

By the way: There are a lot of other applications for cellular automatons in tile-based or block-based games. You can, for example, use them for:

  • Air pressure in a space vessel
  • Flow of liquids
  • Spreading of fire (where you usually work with spread probability instead of constant spread rate)
  • Plant growth