XukeLho
September 18, 2015, 9:57am
1
Before everything, please refer to the image attached to help you follow up my explanation.
I have a box that I created procedurally (actually, its more complex then a box, but lets assume its a box for simplification purposes), and I want to make it look that it was ‘cut’ at an angle. In order to do that, I must move the mesh vertices along the Z axys, forward or backwards, depending on the angle given.
This is the code that I use to do so:
Mesh mesh = GetComponent<MeshFilter>().mesh;
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] verts = mesh.vertices;
for(int i = 0; i < verts.Length; i++)
{
float newZ = center.z + (verts[i].z - center.z) * Mathf.Cos(angle) - (verts[i].y - center.y) * Mathf.Sin(angle);
}
verts[i].Set(verts[i].x, verts[i].y, newZ);[/INDENT]
mesh.vertices = verts;
The problem with this code is that it clamps the slicing angle between 45 to 135 (a 90º angle). So, if I want to slice it at a 30º, it will slice it at 45º, and so on. You get the point.
Any ideias?
hpjohn
September 18, 2015, 11:01am
2
2 things
You Shouldn’t compare floats with ==. Use Mathf.Approximately [Float precision errors]
You create a newX variable, and never assign it to anything
also, aren’t you calculating a new z value for the verts?
I don’t really understand the brief
XukeLho
September 18, 2015, 1:32pm
3
You are right, I was missing a line of code. Changed it a litle so it avoids confusing more people. Please take another look.
XukeLho
September 21, 2015, 8:21am
4
I’ve uploaded my project file, along with a PDF explaining a litle better what I need it to do. Please take a look at both of them.
https://dl.dropboxusercontent.com/u/20794300/Problem.rar
Simo
September 21, 2015, 12:22pm
5
it will not work as you expected with only two vertices of one edge!! you need a third one, depend on which vertex you are dragging!!
you need to find the median at this vertex, let says you are moving V0 which is connected to V1 and V3
Vmedian = (V0V1 + V0V3).normalized
once you find the median, you need to change the vertices of the egde V0V1 and V0V3 at this corner through the median direction
let me know if you need more help
XukeLho
September 21, 2015, 1:58pm
6
Solved!
The code wasn’t
float newZ = center.z + (verts[i].z - center.z) * Mathf.Cos(angle) - (verts[i].y - center.y) * Mathf.Sin(angle)
But
float newZ = (Mathf.Abs(verts[i].y) / Mathf.Tan(local_angle * Mathf.Deg2Rad));
I was using Sin as Cos when what I should have been using Tan