Reach 3

Reach 3 based on code from Keith Peters.

The arm follows the position of the ball by calculating the angles with atan2().


int numSegments = 8;
float[] x = new float[numSegments];
float[] y = new float[numSegments];
float[] angle = new float[numSegments];
float segLength = 26;
float targetX, targetY;

float ballX = 50;
float ballY = 50;
int ballXDirection = 1;
int ballYDirection = -1;

void setup() {
  size(640, 360);
  strokeWeight(20.0);
  stroke(255, 100);
  noFill();
  x[x.length-1] = width/2;     // Set base x-coordinate
  y[x.length-1] = height;  // Set base y-coordinate
}

void draw() {
  background(0);
  
  strokeWeight(20);
  ballX = ballX + 1.0 * ballXDirection;
  ballY = ballY + 0.8 * ballYDirection;
  if(ballX > width-25 || ballX < 25) {
    ballXDirection *= -1; 
  }
  if(ballY > height-25 || ballY < 25) {
    ballYDirection *= -1; 
  }
  ellipse(ballX, ballY, 30, 30);
  
  reachSegment(0, ballX, ballY);
  for(int i=1; i<numSegments; i++) {
    reachSegment(i, targetX, targetY);
  }
  for(int i=x.length-1; i>=1; i--) {
    positionSegment(i, i-1);  
  } 
  for(int i=0; i<x.length; i++) {
    segment(x[i], y[i], angle[i], (i+1)*2); 
  }
}

void positionSegment(int a, int b) {
  x[b] = x[a] + cos(angle[a]) * segLength;
  y[b] = y[a] + sin(angle[a]) * segLength; 
}

void reachSegment(int i, float xin, float yin) {
  float dx = xin - x[i];
  float dy = yin - y[i];
  angle[i] = atan2(dy, dx);  
  targetX = xin - cos(angle[i]) * segLength;
  targetY = yin - sin(angle[i]) * segLength;
}

void segment(float x, float y, float a, float sw) {
  strokeWeight(sw);
  pushMatrix();
  translate(x, y);
  rotate(a);
  line(0, 0, segLength, 0);
  popMatrix();
}

Functions Used

ellipse()

Draws an ellipse (oval) to the screen

Learn More
popMatrix()

Pops the current transformation matrix off the matrix stack

Learn More
rotate()

Rotates the amount specified by the angle parameter

Learn More
for

Controls a sequence of repetitions

Learn More
size()

Defines the dimension of the display window width and height in units of pixels

Learn More
if

Allows the program to make a decision about which code to execute

Learn More
cos()

Calculates the cosine of an angle

Learn More
line()

Draws a line (a direct path between two points) to the screen

Learn More
stroke()

Sets the color used to draw lines and borders around shapes

Learn More
sin()

Calculates the sine of an angle

Learn More
setup()

The setup() function is run once, when the program starts

Learn More
strokeWeight()

Sets the width of the stroke used for lines, points, and the border around shapes

Learn More
translate()

Specifies an amount to displace objects within the display window

Learn More
noFill()

Disables filling geometry

Learn More
draw()

Called directly after setup(), the draw() function continuously executes the lines of code contained inside its block until the program is stopped or noLoop() is called

Learn More
atan2()

Calculates the angle (in radians) from a specified point to the coordinate origin as measured from the positive x-axis

Learn More
background()

The background() function sets the color used for the background of the Processing window

Learn More
pushMatrix()

Pushes the current transformation matrix onto the matrix stack

Learn More