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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}






up vote
1
down vote

favorite













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.










share|improve this question




























    up vote
    1
    down vote

    favorite













    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.










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite












      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.










      share|improve this question














      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.







      mesh dynamic-mesh






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 3 at 8:28









      Kartik Shah

      469




      469






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          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(in.x + timeCode)
          );
          }


          My testing mesh in Blender:
          mesh



          The result in Unity (quite hypnotic):
          wave






          share|improve this answer























          • 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.
            – Kartik Shah
            2 days ago










          • @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
            – Philipp
            2 days ago












          • 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.
            – Kartik Shah
            2 days ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "53"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgamedev.stackexchange.com%2fquestions%2f165846%2fhow-can-we-achieve-earthquake-simulation-in-unity-simulation-of-p-and-s-waves%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          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(in.x + timeCode)
          );
          }


          My testing mesh in Blender:
          mesh



          The result in Unity (quite hypnotic):
          wave






          share|improve this answer























          • 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.
            – Kartik Shah
            2 days ago










          • @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
            – Philipp
            2 days ago












          • 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.
            – Kartik Shah
            2 days ago















          up vote
          3
          down vote



          accepted










          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(in.x + timeCode)
          );
          }


          My testing mesh in Blender:
          mesh



          The result in Unity (quite hypnotic):
          wave






          share|improve this answer























          • 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.
            – Kartik Shah
            2 days ago










          • @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
            – Philipp
            2 days ago












          • 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.
            – Kartik Shah
            2 days ago













          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          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(in.x + timeCode)
          );
          }


          My testing mesh in Blender:
          mesh



          The result in Unity (quite hypnotic):
          wave






          share|improve this answer














          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(in.x + timeCode)
          );
          }


          My testing mesh in Blender:
          mesh



          The result in Unity (quite hypnotic):
          wave







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 3 at 20:04

























          answered Dec 3 at 9:56









          Philipp

          76.7k19180230




          76.7k19180230












          • 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.
            – Kartik Shah
            2 days ago










          • @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
            – Philipp
            2 days ago












          • 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.
            – Kartik Shah
            2 days ago


















          • 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.
            – Kartik Shah
            2 days ago










          • @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
            – Philipp
            2 days ago












          • 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.
            – Kartik Shah
            2 days ago
















          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.
          – Kartik Shah
          2 days ago




          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.
          – Kartik Shah
          2 days ago












          @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
          – Philipp
          2 days ago






          @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
          – Philipp
          2 days ago














          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.
          – Kartik Shah
          2 days ago




          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.
          – Kartik Shah
          2 days ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Game Development Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgamedev.stackexchange.com%2fquestions%2f165846%2fhow-can-we-achieve-earthquake-simulation-in-unity-simulation-of-p-and-s-waves%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy