- Home /
mesh deformation: howto get a coherent move into x and y?
Hello, My question concerns a direction vector that I want to alter along its X and Y only.
I'm working on a mesh deformation script (c#) based on physics collision. I get useful inputs from the colliding objects, such as relative orientation, relative speed, and collision.relativeVelocity in order to obtain a final Vector3 "moveDir" used to move the vertices along.
It looks like this :
float dist = 1.0f-Mathf.Clamp01(vertToCol.sqrMagnitude/sqrDemRange);
if( vertToCol.sqrMagnitude < sqrDemRange ) {
Vector3 moveDir = Vector3.Slerp(vertToMe, flatVertToCenterDir, impactDirManipulator).normalized * moveDelta;
verts[i] += Quaternion.Inverse(transform.rotation)*moveDir;
}
You may have recognized variables names coming from "carDamage2.cs" ;°) All the variables aren't listed there, the script is rather long, so don't take the script as it is presented above.
float dist is easy to understand, goes from 0 to 1. Vector3 moveDir is a mix of the colliding object direction and the direction of collision.point toward my collider center. impactDirManipulator offers to tune the latter ratio, currently 0.5f.
All this working well and fast, but I'd like to add variation along Y and X moveDir axis to mimic the way a piece of metal shrinks. It is currently only pushing the verts along Z, simply squeezing the mesh. I'd like the moveDir to behave like a ripple wave and eventually to fade it over distance.
I guess that a sin wave could make the job: not physically accurate, but artistically ok. I've searched in the forum for examples using Mathf.Sin, but they always use time reference to apply a sin wave. My scripts rather uses distance and returns the deformation instantly. I assume that my understanding of Sin or Cos is really bad... so thanks to anyone who can help me on this.
One may have a better approach by calculating the actual Z length between two vertices as to get the X & Y expected move... I fear to run into too heavy calculations.
Thanks for your attention...
As an alternative, I'm also looking at $$anonymous$$athf.PerlinNoise, to get some randomn alteration...
Answer by whydoidoit · Oct 23, 2013 at 09:04 AM
Well "time" is a just a concept - replace time with distance and you will get the same result. Consider a graph of a Sine wave, time is measured along the bottom - so the graph is just replacing time with distance itself!
Just bear in mind that a value of 1 is going to be a complete Sin cycle, so depending on how far away from the impact point something is you will need to divide that distance to get a value between 0 and 1 for every wave. You may wish to vary that period or just vary the magnitude of the wave as distance from impact increases.
Thank you for your explanation. I also had this in $$anonymous$$d, even if I can't solve it yet. I tried something like this:
testFloat = moveDir.z*$$anonymous$$athf.Sin (dist/10.0f);
Vector3 wave$$anonymous$$ove = Vector3.Slerp (moveDir, new Vector3(moveDir.x, testFloat, moveDir.z), dist);
Where the sin is fading with the final lerp based on distance. But my Sin (here applied to Y direction) always returns only one wave...
I've never been interessed in maths, until I stepped in Unity and scripting... I should probably go back to school ;°)
Your answer
![](https://koobas.hobune.stream/wayback/20220613120422im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Creating a bouncing game without physics - Question on Mathf.Sin() 0 Answers
Jump character using Sine Curve 1 Answer
How to move prefabs array in sine wave? 2 Answers
Mathf.Sin used to move object left and right but when value changed in game it teleports around 1 Answer
Game lags if there are many objects with this script, how to fix? 1 Answer