Simulating Heat Diffusion in Rust
While exploring physics beyond mechanics, I wanted to see how heat actually moves through materials — not as equations on paper, but visually.
That curiosity led me to build a 2D heat diffusion simulator in Rust using Macroquad, based on the real heat equation used in thermodynamics.
The Physics Behind It
Heat spreads from hot regions to cold ones.
In continuous form, that’s expressed by the heat equation:
Here:
- : temperature at each point,
- : thermal diffusivity — how quickly heat spreads.
To simulate it on a computer, I discretized the space into a grid and replaced derivatives with finite differences:
That one line drives the entire simulation.
From Theory to Code
I started by making a 2D grid in Rust — a simple Vec<Vec<f32>> where each element is a temperature.
Then, on every frame, I applied the diffusion formula above to compute the next grid.
To make sure it looked alive, I visualized each cell as a colored rectangle using macroquad.
[dependencies]
macroquad = "0.4"
Once I saw a red spot fading into blue, I knew it worked.
Adding Interaction
Next, I made it interactive. By checking the mouse position each frame, I could inject heat wherever I clicked — the longer I held, the hotter it got. The code looked like this:
if is_mouse_button_down(MouseButton::Left) {
let (mx, my) = mouse_position();
let gx = (mx / SCALE) as i32;
let gy = (my / SCALE) as i32;
grid[gy as usize][gx as usize] += 0.02;
}
That tiny block transformed the simulation from a passive demo into a playground.
Cooling and Realistic Physics
In reality, heat doesn’t just spread — it also fades into the environment. To capture that, I added a simple cooling term:
I used real thermal properties of steel:
- Thermal diffusivity ( )
- Grid spacing ( )
- Time step ( )
and set
This gave physically stable and realistic heat flow. Every frame now represented half a second of real time.
Making It Beautiful
I wanted it to look like real glowing metal. Cold steel has a bluish tint, while hot steel glows red, orange, then white. So I mapped temperature values to those hues and added a metallic gray background. Suddenly, the screen looked like a steel plate heating up under a torch.
Here’s the result in motion: drag your cursor across the plate, and watch the glow spread and fade like molten iron cooling.
Full Source Code
You can find the complete code here: 👉 2d-heat-diffusion