How To: Make a 2D wave simulation


Introduction

Wanna learn how to make this?

...or this?

...or even this?

In this tutorial (well not really a tutorial, it's more like how I do stuff), I'm going to show you how to make a wave simulation generator from scratch. And when I say from scratch, I mean from the physics themselves! So if you're not familiar with maths, well first of all, what are you doing here, and second of all, I will explain everything in the best way that I can so you can understand the concepts and what the maths really mean.


The maths!

This article specifically talks about wave simulations in the 2D eucledean space. Below is the equation for 2D waves:

d2z      d2z   d2z
___ = c2 ___ + ___
dt2      dx2   dy2

SOOOO MANY SYMBOLS!!! What the heck do they mean???
Let's start the notation df/dt. This is called the derivative of f with respect to t. The derivative essentially is nothing more than a difference of values of the function f(t) for neighbouring values of t. df is really just f(t+dt)-f(t), where dt is just a small offset. Then it is normalised by dividing with that offset dt. Now how small is that offset? Let me introduce you to...

...Discrete Maths!!!

In conventional calculus, there is no number right next to another! Which is why there is this concept called the limit. The limit is a tool used in maths to determine what happens to a function when a variable approaches a value, when it gets closer and closer to that value but never equal.
In discrete calculus, however, the variables of a function get integer values. The value of t in f(t) can only get values like 0, 1, 2, etc. Why even bother with discrete maths?
The computer works in discrete maths!!!
There is no such thing as a "real number" when it comes to computer arithmetic! You cannot store a 0,5 value in a register of a CPU. The float and double datatypes are really just methods of representation of decimal numbers using natural numbers! Discrete values!

and why is discrete maths helpful here?

Because the smallest value a difference between two neighbouring values for a variable of a function can get is 1. So dt, dx, dy, and dwhatever is equal to 1 everywhere in the discrete version of the equation!!!


The meaning behind the equation

Why use the derivative? What does it mean?
When it comes to studying the behaviour of a phenomenon that changes over time, we need to know how the phenomenon changes in one unit of time. Ergo, find the difference between the present and the past value.
Let z(x, y, t) be the height of a point of a 2D plane at position (x, y), at the specific monent of time t. To see how fast the point moves up and down, we need the difference of the height of the point at time t and the height of it at time t+dt. Normalise over dt and you have (f(t+dt)-f(t))/dt or equivalently df/dt.
Now when it comes to waves and oscillations, we do not only care about the velocity, but also the derivative of the velocity, the acceleration. So the derivative of the derivative is d2f/dt2. In fact, the nth derivative of a function is dnf/dtn. For an oscillation to happen, the velocity has to change so the point of the surface at (x,y) has to return back to its original position, and repeatedly, well, oscillate around its original position.

To keep this tutorial short, the full equation emerges directly from Newton's law, where ΣF = m*d2f/dt2, the sum of forces upon an object equals the mass times the acceleration. For a point, the mass approaches zero, so it's a tiny dm. The density of the 2D surface is dm/dxdy. The sum of the forces on the surface is the tension T that keeps the surface "flat" on the edges essentially. You can find the full solution of the equation online, I just wanted to point out that yes, the acceleration of the point of a plane at (x,y) depends on the differences with the height of the neighbouring points in each direction, the second derivatives for x and y! The constant c2, is the speed of the wave, how fast the wave "moves" across the surface, and is equal to T/ρ, ρ being the density of the surface!

In the next page, I'm going to tell you how to translate this equation into a language that the computer can understand and simulate for you!


Back Next page