Agent/particle (you name it) follows points on mesh

edited December 2013 in How To...

Hi everyone,,,

after weeks of trying to combine existing codes and codes i wrote my self after combining other codes (...) i realised i am not able to do what im asking in the title all by myself so i would like to ask for HELP! if anyone can indicate me a way to determine the path of an agent/particle according to mesh points (or pregiven vectors in somehow) , please do so!

thanks and hope you respond to my dummy question

Answers

  • ArrayList<PVector> nodes = new ArrayList();
    ArrayList<PVector> edges = new ArrayList();
    Pathoid patty;
    
    void setup() {
      size(400, 400);
      smooth();
      for (int i=0;i<10;i++) {
        nodes.add( new PVector( random(10, width-10), random(10, height-10), 0 ) );
      }
      for (int i=0; i<20;i++) {
        int from = int(random(nodes.size()));
        int to = int(random(nodes.size()));
        // Make all paths two-way!
        edges.add(new PVector( from, to, 0));
        edges.add(new PVector( to, from, 0));
      }
      patty = new Pathoid();
    }
    
    void draw() {
      background(0);
      stroke(255);
      for (int i=0; i<edges.size();i++) {
        line(nodes.get(int(edges.get(i).x)).x, nodes.get(int(edges.get(i).x)).y, nodes.get(int(edges.get(i).y)).x, nodes.get(int(edges.get(i).y)).y);
      }
      noStroke();
      fill(255, 0, 0);
      for (int i=0; i<nodes.size();i++) {
        ellipse(nodes.get(i).x, nodes.get(i).y, 10, 10);
      }
      patty.draw();
    }
    
    class Pathoid {
      int cur, next;
      float ler;
      Pathoid() {
        cur = int(random(nodes.size()));
        ler = 0;
        newDest();
      }
      void newDest() {
        // Make sure that a new destination could exist from here.
        boolean ok = false;
        for (int i=0; !ok && i<edges.size();i++) {
          if (edges.get(i).x == cur ) ok = true;
        }
        if (!ok) {
          //Magic jump, in case we start on a node that is not connected to anything.
          cur = int(random(nodes.size()));
          newDest(); // Has the potential to get stuck in a loop if no edges go anywhere! Yipe!
        }
        ok = false;
        while (!ok) {
          next = int(random(nodes.size()));
          for (int i=0; !ok && i<edges.size();i++) {
            if (edges.get(i).x == cur && edges.get(i).y == next ) ok = true;
          }
        }
      }
      void draw() {
        ler+=.01;
        noStroke();
        fill(0, 255, 0);
        float xp = lerp(nodes.get(cur).x, nodes.get(next).x, ler);
        float yp = lerp(nodes.get(cur).y, nodes.get(next).y, ler);  
        ellipse(xp, yp, 10, 10);
        if (ler>=1) {
          cur = next;
          ler = 0;
          newDest();
        }
      }
    }
    
  • May the RNG be ever in your favor.

  • TfGuy44 thank you for your response! I managed to make your sketch in 3D but its now exactly what i want... i think its close.

    I would like to use agents with flocking (cohesion, separation etc) and within these functions, i m trying to make as targets the mesh points. But first things first, do you think that by importing an .stl can translate this meshes vertices and edges to the nodes and edges of your script?

    Thank you again! p.s. what is RNG?? ;)

  • Answer ✓

    Hm. If you have flocking agents, you should be able to add a function that motivates them a little bit towards mesh points (much in the same way that the seperate from each other, but in the opposite direction and towards the mesh points, not other agents). There's no need for edges if you only want them to interact with mesh points. I don't know what an .stl file is, so I'm not qualified to answer your question about that. The RNG is the Random Number Generator - the code I wrote above has the potenrial (in very, very rare cases) to cause your program to get stuck in an endless loop if the RNG generates a bad set of numbers for you. It's a pun on the famous line from the Hunger Games movie.

  • havent loged in for long since forum was down... so thnx TfGuy44 for your responses! i found a way to do it thnx to a friend of mine..

Sign In or Register to comment.