Hi everyone,
first of all, I don’t intend to recreate kangaroo abilities, I would just like to study the subject a little further and I thought it could be fun to code.
I browsed the K2 examples on github and read a bit the forum .If I understand it right I can relax the mesh by setting its edge length(without physic engine).
If I am not mistaken the steps are the following:
1)Get a vector for each endpoint of the edge(spring) and put it on a tree the vertexIndex as path.
2)Average the vector and move.
Maybe someone could give me a hint: Am I on the right way? Are my assumptions correct? Is it logical?
I started creating some really basic classes and tried and it seems to work somehow, the mesh seems to get a bit relaxed.
My code oviously is really amateurish and I am sure it could get optimized a lot.
A class for the vertices:
public class Vertex
{
public Point3d Position;
public bool IsAnchor = false;
public int Index = -1;
public Vertex(){}
public Vertex(Point3d position, bool isAnchor, int index)
{
Position = position;
IsAnchor = isAnchor;
Index = index;
}
}
Another one for the springs(as the k2 one)
public class Spring
{
public Vertex Start;
public Vertex End;
public double Restlength;
public double Strength;
public double Length;
public Vector3d[] Move = new Vector3d[2];
public Spring(){}
public Spring(Vertex start, Vertex end, double restlength, double strength)
{
Start = start;
End = end;
Restlength = restlength;
Strength = strength;
Length = start.Position.DistanceTo(End.Position);
}
public void ComputeVector()
{
Vector3d current = Start.Position - End.Position;
double stretchfactor = 1.0 - Restlength / Length;
Vector3d SpringMove = 0.5 * current * stretchfactor;
Move[0] = SpringMove;
Move[1] = -SpringMove;
}
}
and one for the mesh:
public class MeshSystem
{
public List<Spring> Springs = new List<Spring>();
public List<Vertex> Vertices = new List<Vertex>();
public Mesh Mesh;
public List<Vector3d> movesList = new List<Vector3d>();
public MeshSystem(){}
public MeshSystem(Mesh mesh, List<Spring> springs, List<Vertex> vertices)
{
Mesh = mesh;
Springs = springs;
Vertices = vertices;
}
public void Step()
{
var verticesMoves = new DataTree<Vector3d>();
for (int i = 0; i < Springs.Count; i++)
{
Springs[i].ComputeVector();
if(!Springs[i].Start.IsAnchor)
{verticesMoves.Add(Springs[i].Move[0], new GH_Path(Springs[i].Start.Index));}
else
{verticesMoves.Add(new Vector3d(0, 0, 0), new GH_Path(Springs[i].Start.Index));}
if(!Springs[i].End.IsAnchor)
{verticesMoves.Add(Springs[i].Move[1], new GH_Path(Springs[i].End.Index));}
else
{verticesMoves.Add(new Vector3d(0, 0, 0), new GH_Path(Springs[i].End.Index));}
}
for (int i = 0; i < verticesMoves.BranchCount; i++)
{
Vector3d averageVector = Util.average(verticesMoves.Branch(i));
movesList.Add(averageVector);
}
for (int i = 0; i < Mesh.TopologyVertices.Count; i++)
{
Mesh.TopologyVertices[i] = (Point3f) ((Point3d) Mesh.TopologyVertices[i] + movesList[i]);
}
}
}
Thanks for everybody taking the time and read!
Have a nice start into the weekend!
The file:20201210_MeshRelax.gh (17.5 KB)