Moving grass

Hi there! I am working on a program in which a type of mass-spring-damper grassfield will move in the wind. Now I have code that makes the grass move in a sinusoidal type of way, but I want to include some kind of PVector for the wind, but I can't figure out how to do this.

My code now looks like this:

Tree[] trees= new Tree[20];


void setup() {
  size(800, 800);
  for (int i = 0; i<trees.length; i++) {
    trees[i] = new Tree(i*15+width/2-150, height);
  }
}

void draw() { 
  background(255);

  //tree.draw();
  for (int i = 0; i<trees.length; i++) {
      trees[i].update();
    trees[i].display();
  }
}


class Tree {
  PVector location;
  PVector origin; 
  float plantLength;
  float angle;
  float velocity;
  float acceleration;
  float damping; 
  float parts; //Plant is divided in subparts
  float x, y; //initial translation
  float bowing=1.005;

  Tree(float X, float Y) { //misschien extra constructordingetje voor begin hoek?
    //constructor for variables used in this class
    //initial values, will get changed in update
    origin = new PVector(0,0);
    location = new PVector(0,0); 
    angle = bowing*PI; //angle, whill will decide how much the plant bows
    damping = 1; //damping speed
    y = Y;
    x = X;
    parts = 10; //smoothness
    plantLength = 200/parts; 
  }

  void update() {
    float friction = 0.01; //determines speed of plant moving
    acceleration = (friction / plantLength) * sin(angle); //formulas for MSD systems
    velocity += acceleration;
    angle += velocity;
    velocity *= damping; 
  }

  void display() {
    pushMatrix();
    translate(x, y);
    //location.add(origin);
    location.set(plantLength*sin(angle), plantLength*cos(angle), 0);
   // strokeWeight(1);
    for (int i=0; i < parts; i++) {
      stroke(51,100,0);
      strokeWeight(10);
      line(origin.x, origin.y, location.x, location.y);       
      translate(location.x, location.y);
      rotate(angle+PI);
    }
    popMatrix();
  }

}

I want to combine it with a program that works like this:

int num = 1; 
Spring[] springs = new Spring[num]; 
  int size = 200; 

void setup() {
  size(640, 360);
  noStroke(); 
  springs[0] = new Spring(width/2, height-size, 40, 0.98, 8.0, 0.1, springs, 0); 
//  springs[1] = new Spring(width, height, 120, 0.95, 9.0, 0.1, springs, 1); 
  //springs[2] = new Spring(width, height, 200, 0.90, 9.9, 0.1, springs, 2);
}

void draw() {
  background(51); 

  for (int i = 0; i<springs.length; i++) { 
    springs[i].update(); 
    springs[i].display();
  }
}


class Spring { 
 PVector pos = new PVector (0,0);
  PVector temppos = new PVector(0,0);
 PVector origin = new PVector(0,0);
   // Spring simulation constants 
  float mass;       // Mass 
  float k = 0.2;    // Spring constant 
  float damp;       // Damping 
  float rest_posx;  // Rest position X 
  float rest_posy;  // Rest position Y 

  float velx = 0.0;   // X Velocity 
  float vely = 0.0;   // Y Velocity 
  float accel = 0;    // Acceleration 
  float force = 0;    // Force 

  Spring[] friends;
  int me;

  // Constructor
  Spring(float x, float y, int s, float d, float m, 
    float k_in, Spring[] others, int id) { 
    pos.x = temppos.x = x; 
    pos.x = temppos.y = y;
    rest_posx = x;
    rest_posy = y;
    size = s;
    damp = d; 
    mass = m; 
    k = k_in;
    friends = others;
    me = id;
  } 

  void update() { 
    rest_posy = height-200; 
    rest_posx = 100;

    force = -k * (temppos.y - rest_posy);  // f=-ky 
    accel = force / mass;                 // Set the acceleration, f=ma == a=f/m 
    vely = damp * (vely + accel);         // Set the velocity 
    temppos.y = temppos.y + vely;           // Updated position 

    force = -k * (temppos.x - rest_posx);  // f=-ky 
    accel = force / mass;                 // Set the acceleration, f=ma == a=f/m 
    velx = damp * (velx + accel);         // Set the velocity 
    temppos.x = temppos.x + velx;           // Updated position
  } 



  void display() { 
    strokeWeight(10);
    stroke(255);
    line(rest_posx ,height , temppos.x, temppos.y);


  }}
Tagged:
Sign In or Register to comment.