How can we achieve Earthquake Simulation in Unity - Simulation of P and S Waves
$begingroup$
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 :
- Any idea of how I can compress - decompress mesh in Unity (P Waves).
- 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
$endgroup$
add a comment |
$begingroup$
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 :
- Any idea of how I can compress - decompress mesh in Unity (P Waves).
- 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
$endgroup$
add a comment |
$begingroup$
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 :
- Any idea of how I can compress - decompress mesh in Unity (P Waves).
- 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
$endgroup$
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 :
- Any idea of how I can compress - decompress mesh in Unity (P Waves).
- 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
mesh dynamic-mesh
asked Dec 3 '18 at 8:28
Kartik ShahKartik Shah
469
469
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
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:
The result in Unity (quite hypnotic):
$endgroup$
$begingroup$
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.
$endgroup$
– Kartik Shah
Dec 4 '18 at 8:37
$begingroup$
@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
$endgroup$
– Philipp
Dec 4 '18 at 8:39
$begingroup$
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.
$endgroup$
– Kartik Shah
Dec 4 '18 at 12:16
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$begingroup$
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:
The result in Unity (quite hypnotic):
$endgroup$
$begingroup$
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.
$endgroup$
– Kartik Shah
Dec 4 '18 at 8:37
$begingroup$
@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
$endgroup$
– Philipp
Dec 4 '18 at 8:39
$begingroup$
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.
$endgroup$
– Kartik Shah
Dec 4 '18 at 12:16
add a comment |
$begingroup$
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:
The result in Unity (quite hypnotic):
$endgroup$
$begingroup$
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.
$endgroup$
– Kartik Shah
Dec 4 '18 at 8:37
$begingroup$
@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
$endgroup$
– Philipp
Dec 4 '18 at 8:39
$begingroup$
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.
$endgroup$
– Kartik Shah
Dec 4 '18 at 12:16
add a comment |
$begingroup$
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:
The result in Unity (quite hypnotic):
$endgroup$
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:
The result in Unity (quite hypnotic):
edited Dec 3 '18 at 20:04
answered Dec 3 '18 at 9:56
PhilippPhilipp
78.9k19182234
78.9k19182234
$begingroup$
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.
$endgroup$
– Kartik Shah
Dec 4 '18 at 8:37
$begingroup$
@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
$endgroup$
– Philipp
Dec 4 '18 at 8:39
$begingroup$
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.
$endgroup$
– Kartik Shah
Dec 4 '18 at 12:16
add a comment |
$begingroup$
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.
$endgroup$
– Kartik Shah
Dec 4 '18 at 8:37
$begingroup$
@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
$endgroup$
– Philipp
Dec 4 '18 at 8:39
$begingroup$
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.
$endgroup$
– Kartik Shah
Dec 4 '18 at 12:16
$begingroup$
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.
$endgroup$
– Kartik Shah
Dec 4 '18 at 8:37
$begingroup$
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.
$endgroup$
– Kartik Shah
Dec 4 '18 at 8:37
$begingroup$
@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
$endgroup$
– Philipp
Dec 4 '18 at 8:39
$begingroup$
@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
$endgroup$
– Philipp
Dec 4 '18 at 8:39
$begingroup$
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.
$endgroup$
– Kartik Shah
Dec 4 '18 at 12:16
$begingroup$
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.
$endgroup$
– Kartik Shah
Dec 4 '18 at 12:16
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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