Search code examples
simulationwaveelapsedtime

How to scale a 2D wave simulation by elapsed time?


I have a wave simulation with values calculated like this: (pseudocode)

for(x):
  for(y):
    new_value = (current[x-1,y] + current[x+1,y] + current[x,y-1] + current[x,y+1]) / 2 - previous[x, y]
    new_value -= new_value * damp
    previous[x, y] = new_value

swap(current, previous)

Double the average of surrounding points minus previous value for that point.

Right now the change happens every frame so how fast it goes is dependent on the framerate. The problem is, I can't figure out how to scale this process by elapsed time. I tried getting the difference between old and new value and scaling it, but that doesn't work at all; probably something to do with board swapping but I'm not sure.

Can I make it work or do I need a completely different approach?


Solution

  • Ok I figured it out. My problem was based on the fact that I thought I have to run this algorithm every frame – I don't. If you just run it every few frames then obviously the resulting animation is not smooth; but I didn't consider that you can just do linear interpolation to get smooth transition from old state to new state which produces a result I'm happy with.