Make an object follow a randomly generated path

I've created a functional Path class that I will include below that uses the curve function to generate a smooth random path connected by an array of points I've called Nodes. My goal is to have a shape object follow this path, but I am unsure as to how I can do this. I'm guessing there is some way to get the x and y values of the curve drawn by the curve function that I can then use to influence the movement of the shape. Any suggestions?

class Path {

  float startX, startY;
  float endX, endY;

  Path() {
    startX = 50; 
    startY = 50;
    endX = 350; 
    endY = 100;
  }

  void show(Node [] nodes) {
    stroke(0);
    noFill();
    beginShape();
    for (int i = -2; i < nodes.length + 2; i++) {
      if (i < 0) 
        curveVertex( startX, startY );
      else if (i >= nodes.length) 
        curveVertex( endX, endY );
      else
        curveVertex( nodes[i].getNodeX(), nodes[i].getNodeY() );
    }   
    endShape();
  }
}

Answers

  • Here is an example of the output. The larger circle on the left is the separate object that I'm looking to move along the curve. The smaller circles are the aforementioned Nodes that are used to create the curve.

    path

  • edited February 2017

    I found that function in between the time of my initial post and yours, and I have gotten to a point where it is mostly functional; however, I am getting a weird effect where some of the points generated by curvePoint deviate from the curve, skewing the path of the object. This image shows each point generated by the curvePoint function to demonstrate this effect:

    Path2

    I created a Step class that creates and keeps track of each point created by the curvePoint function in an array, so here is that code:

    class Steps {
    
      int steps = 20;
      int count = 0;
      float t;
      float x;
      float y;
      float [] stepX = new float[84];
      float [] stepY = new float[84];
    
      void createSteps(Node [] nodes) {
        fill(0);
        curveTightness(0);
        for (int i = 0; i < nodes.length - 3; i++) {
          for (int j = 0; j <= steps; j++) {
            t = j / float(steps);
            stepX[count] = curvePoint(nodes[i].getNodeX(), nodes[i + 1].getNodeX(), nodes[i + 2].getNodeX(), nodes[1 + 3].getNodeX(), t);
            stepY[count] = curvePoint(nodes[i].getNodeY(), nodes[i + 1].getNodeY(), nodes[i + 2].getNodeY(), nodes[1 + 3].getNodeY(), t);
            count++;
          }
        }
      }
    
      void showSteps() {
        fill(0);
        for (int i = 0; i < stepX.length; i++) {
          x = stepX[i];
          y = stepY[i];
          ellipse(x, y, 3, 3);
        }
      }
    
      int getLength()
      {return stepX.length;}
      float getStepX(int k)
      {return stepX[k];}
      float getStepY(int l)
      {return stepY[l];}
    }
    

    Another question I guess I should ask is if there is a more optimal way to create a random smooth path as well as get an object to move along it?

  • Answer ✓

    Typo on lines 17 and 18

  • Thanks! Can't believe I missed that haha.

  • Would you like to collaborate with me on this?

  • @jwmccart1101 -- you may also be interested in the Processing example sketch:

    • Examples > Topics > Curves > ArcLengthParameterization
Sign In or Register to comment.