Kangaroo2 Custom GoalObject logic

Hi,

I’m trying to make a custom goal that duplicates a mesh then makes it move a certain distance (why? to understand the K2/c#/rhino mesh logic) and I’m having trouble with the K2/c#/rhino logic. My understanding w the code below is that it creates 5 new meshes, adds them to a list, then calls the first mesh, then with each vertex, changes the position.

It doesn’t display any errors when I run it but I don’t see anything happening. I’m also wondering how I can access those new meshes via baking because it does not output anything.

    public override void Calculate(List<KangarooSolver.Particle> p)
    {

      var MeshList = new List<Mesh>();

      for(int i = 0;i < 5;i++)
      {
        MeshList.Add(mesh_to_dup);
      }
      
      
      for(int j = 0;j < 5;j++)
      {
        Mesh dupmesh = MeshList[j];
        for (int i = 0; i < dupmesh.Vertices.Count; i++)
        {
          var vertex = dupmesh.Vertices[i];
          var move = new Vector3d(1e-6, 1e-6, 0.0);
          dupmesh.Vertices.SetVertex(i, vertex.X + move.X, vertex.Y + move.Y, vertex.Z + move.Z);
        }
      }

Hi @SalvadorIII

The Calculate method in a custom Goal is there to set the vectors in the Move array (note the case sensitivity). For each particle that Goal acts on, these vectors tell it where to go.
It is only by setting those Move vectors that the Goal has any effect on the simulation. The Move vectors from all the Goals are combined by the solver at each iteration.

Your script looks more like something to use on its own, not inside a Kangaroo Goal.

Hi Daniel,

Thanks for the reply! Yes, it does seem like it can be done outside Kangaroo though my long term plan would be to combine it with other K2 goals like collision.

Aside from ‘Move’, are there any other words that need attention to?

How are PPos and List<KangarooSolver.Particle> p related? Are they just equal to each other?

            for (int i = 0; i < mPoints.Length; i++)
            {
                PPos[i] = mPoints[i];
                Weighting[i] = K;
            }

PPos is the initial locations of the points a Goal acts on. It is only used once, in the initialisation (where coincident points are combined into single particles and indexed with PIndex) and is not updated as the simulation iterates.

The list of KangarooSolver.Particle instead gives the particles as they are at the current iteration. A Goal needs access to this so that it can update its Move vectors based on the new positions.

Thanks Daniel,

Very helpful info! I’m out of questions for now :slight_smile:

Hi Daniel,
I got some questions about the" AddParticle" method. What does the second argument
“double m” mean? I assumed it is tolerance just like what in “AssignPIndex” method, but I’m not sure and I can’t find the source code. Is there any other notification when customizing K2 physical system and operating particles in K2?
Thanks!

Hi @肝試大會长
The ‘GrowingLine’ example here uses the AddParticle method:

The m argument is the mass, and for most purposes you can just set this to 1.

1 Like

Thanks! I Have another problem now. I’m trying to write some code to do mesh grow, but i found when particles get close to each other they collapse into one, which is not good because it would lead to non manifold edges in mesh, what should I do to avoid that?

ohhhh I found the problemmm I didn’t set the collision in a right radius… Now it workd well:) so the only question is how to make particles not collapse