# How can we achieve Earthquake Simulation in Unity - Simulation of P and S Waves

I Cannot use Water waves as P and S Waves are different than water waves. I tried to use it but not satisfied with the result.

I am trying to simulate how Earthquake takes place in real time which you cannot simulate in the real world. Here is the reference link to understand the basics of Earthquake and to understand better here is the better-simulated Example. I wanted to Simulate P and S Waves. My first attempt will be simulating with mesh instead of showing lines, I don't know how to compress mesh (P Waves) but I found out a good example to simulate wave like effect (S Waves).

The solution I am looking forward to is :

1. Any idea of how I can compress - decompress mesh in Unity (P Waves).
2. wave like effect (S waves), I have an example but it moves only Plane, in my case, i want to do more like a deformation of mesh, so it gives me an real effect like S Waves.

In order to do this you will have to write some code to modify the mesh programmatically. This is how you read the vertices of a mesh from a C# script:

Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;


And this is how you write them back:

mesh.vertices = vertices;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
mesh.RecalculateTangents();


(Note that depending on the shader your mesh is using, recalculating the normals and/or tangents might not actually be needed. If you comment it out and the light on the mesh still looks correct, you can leave it out).

Now about the complicated part: Calculating the deformation of your mesh. I think the best way to do this would be to create a copy of the original vertex array in the Start method. Your Update method should now take that original array and run each vertex through a function which takes the vertex and a timecode as input and then returns a new vertex which says where that vertex should be at that timecode.

I am not a seismologist, so I can't tell you how this function would look exactly for physically correct S-waves or P-waves. So here is an example which applies a simple sine wave to the z-axis of the mesh which travels in x direction. You should be able to edit the wave functions to do what you want it to do:

private Vector3[] originalVertices;
private Mesh mesh;

void Start() {
mesh = GetComponent<MeshFilter>().mesh;
originalVertices = mesh.vertices;
}

void Update() {

Vector3[] newVertices = new Vector3[originalVertices.Length];

for(int i = 0; i < originalVertices.Length; i++) {
newVertices[i] = WaveFunction(originalVertices[i], Time.time);
}

mesh.vertices = newVertices;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
mesh.RecalculateTangents();
}

private Vector3 WaveFunction(Vector3 origin, float timeCode) {
// sine wave with an amplitude of 1 unit and a period of 2π units,
// traveling with a speed of 1 unit per second.
// Change this to your own wave function.
return new Vector3 (
origin.x,
origin.y,
origin.z + Mathf.Sin(origin.x + timeCode)
);
}


My testing mesh in Blender:

The result in Unity (quite hypnotic):

• your code really helps me and takes me one step further to the result I wanted. I need one further help from you. check out P wave and Rayleigh wave in the link. I wanted to do the same in Mesh, but I have no idea how to make the upper part of mesh in moving and lower part of the same mesh still. Dec 4 '18 at 8:37
• @KartikShah As I wrote: We are game developers, not a seismologists. If you want to know more about the math which can be used to describe different kinds of seismic waves, you should ask on Earth Science Stack Exchange Dec 4 '18 at 8:39
• I want your help in mesh editing, that link have the GIF of how mesh movement is done in the wave although thanks for the help. Dec 4 '18 at 12:16