Arrays

This week I created an octopus that will be used as a part of my Physical Computing midterm project. The main sketch is composed by an ellipse and an array of arms, and each arm is a class that is composed by an array of segments.

The movement of the arm is set by its functions, responding to the mouse position using sin, cosin and atan to achieve smooth and fluid movements.

Main Sketch

//DECLARE Arm[] arrayArm=new Arm[8]; Arm myArm; int numSegments = 60; float[] x = new float[numSegments]; float[] y = new float[numSegments]; float[] angle = new float[numSegments]; float segLength = 7; float targetX, targetY; void setup() { //noCursor(); size(1000, 800); //INITIALIZE myArm = new Arm(); for (int t=0; t=1; i--) { myArm.positionSegment(i, i-1); } }


Arm Class

class Arm { //CONSTRUCTOR Arm() { } //FUNCTIONS void pulseBody() { fill(255, 120, 90); //need to add accelerometer sensor values to body pulsing ellipse(width/3, height/2, 50 + 10*sin(millis()/50), 50 + 10*cos(millis()/50)); } void display() { strokeWeight(20.0); stroke(255, 120, 90); //set (x,y) to zero to perform transform and then rotate x[x.length-1] = width-width; // Set base x-coordinate y[x.length-1] = height-height; // Set base y-coordinate // x[x.length-1] = width/3; // Set base x-coordinate // y[x.length-1] = height/2; // Set base y-coordinate } 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(); } }

One Comment

  1. Pingback: Angular Motion + Oscillation | SMD @ ITP

Leave a Reply