Description
This is a 2D wave simulation based on a differential equation describing classical waves in two
dimensions. The simulation is based on approximations on the current and previous states thus
allowing real-time interaction with adding waves by clicking and adding walls for the waves to
reflect off of. The application is creating in C++ using Raylib. The web online demo is compiled using Emscripten. The desktop application uses a
thread pool to take full advantage of the hardware to allow for faster calculations. You can
continue reading to understand the math behind the simulation.
The Wave Equation
This is the two-dimensional wave equation which describes wave propagation.
Starting on the left , this is the second time derivative of the wave function which represents the acceleration of the wave in time. The right side of the equation is the second spatial derivative of the wave function for each dimension. This represents the "curvature" of the wave in space. This whole equation thus shows that the acceleration of the wave in time is governed by the curvature of the wave in space multiplied by some constant wave speed .
- represents the wave function . It can represent various quantities, such as pressure in a sound wave or the height of water waves given a particular position and time.
- is time.
- and are the spatial coordinates.
- is the speed of wave propagation.
Starting on the left , this is the second time derivative of the wave function which represents the acceleration of the wave in time. The right side of the equation is the second spatial derivative of the wave function for each dimension. This represents the "curvature" of the wave in space. This whole equation thus shows that the acceleration of the wave in time is governed by the curvature of the wave in space multiplied by some constant wave speed .
Time Derivative
Since we are dealing with discrete values for both space (a grid) and time (individual
frames/time steps), we need a way to approximate these second derivatives. Essentially, given a
current grid state and previous states, we want to determine what the next state/frame should be
in time. For this, we will use the finite difference method which allows us to approximate
derivatives given a certain number of states in either time or space.
The second order finite difference formula for one dimension is
Since we want to find the derivative of time and not space, we can adjust this formula. Instead of looking at discrete distances left and right of a position, we can look forward and backward in time. Now represents the delta timestep between states rather than between spatial grid points. To represent this better, we can say is the state in the future , is the current state , and is the state in the past . We can also say is the delta time between states or .
Now we get
However, this assumes we have one spatial dimension , so we can substitute in the functions for .
Our goal is to determine the state of the function in the future at a specific coordinate. This means we need to solve for
Going back to our original wave propagation equation, is represented as in our equation. They are the same, just difference notation and also a partial derivative because the wave function is a function of both space and time and thus taking the derivative of time makes it a partial derivative instead of a complete derivative.
In our simulation, we can store the present and past states of our simulation in separate buffers and also keep track of the timestep , however, in order to fully calculate the future state, we need the second partial derivative of the wave function with respect to time . Using the original wave propagation function, we can substitute the right side of the equation in.
The second order finite difference formula for one dimension is
- is the position in space.
- is the distance between discrete points in space.
Since we want to find the derivative of time and not space, we can adjust this formula. Instead of looking at discrete distances left and right of a position, we can look forward and backward in time. Now represents the delta timestep between states rather than between spatial grid points. To represent this better, we can say is the state in the future , is the current state , and is the state in the past . We can also say is the delta time between states or .
Now we get
- is the position in space.
- is the distance between discrete points in space.
However, this assumes we have one spatial dimension , so we can substitute in the functions for .
Our goal is to determine the state of the function in the future at a specific coordinate. This means we need to solve for
Going back to our original wave propagation equation, is represented as in our equation. They are the same, just difference notation and also a partial derivative because the wave function is a function of both space and time and thus taking the derivative of time makes it a partial derivative instead of a complete derivative.
In our simulation, we can store the present and past states of our simulation in separate buffers and also keep track of the timestep , however, in order to fully calculate the future state, we need the second partial derivative of the wave function with respect to time . Using the original wave propagation function, we can substitute the right side of the equation in.
Spatial Derivative
We now need to approximate the second spatial derivative of the wave function . We can use the same method we used for time; the finite difference method. However, unlike
time where there is a single dimension, we have two spatial dimensions thus requiring us to use
the two-dimensional second-order finite difference formula.
Even though this equation looks pretty complicated, it can actually be visualized nicely through a stencil which can be imagined as an overlay applied on top of a grid where the number is a multiplier.
We can substitute this into our equation for predicting the future.
Now we have everything necessary to solve for . We simply need to provide neighboring grid cells surrounding , a past value of and some constants such as wave speed , timestep , and grid spacing .
- and are coordinates in space.
- is the distance between grid points.
Even though this equation looks pretty complicated, it can actually be visualized nicely through a stencil which can be imagined as an overlay applied on top of a grid where the number is a multiplier.
We can substitute this into our equation for predicting the future.
Now we have everything necessary to solve for . We simply need to provide neighboring grid cells surrounding , a past value of and some constants such as wave speed , timestep , and grid spacing .
Embed Demo
<embed width="100%" height="100%" src="https://static.pixeled.site/wave-simulation/index.html"></embed>