Vectors and Forces: Part 2

To continue on from my posts Vectors and Forces and Processing:Object Orientated Programming and understand the methods behind what we were doing better I decided to create objects with vector forces and create arrays of these objects.

In the example below the objects are attracted to the position of the mouse. Using methods outlined in The Nature of Code This is done by giving the objects three vectors, location, acceleration and velocity. As the object gets updated each frame a positional vector is taken from the mouse position and subtracted from the object’s location. After being normalized (reducing it’s length to one), and scaled by 0.5 so it is not as strong. This is then fed into the acceleration which is then added to the velocity. Which is then in turn added to the location vector which moves the object. In this I have also used PVector.limit to limit the velocity to 4, so the objects do not move too fast.

The objects can then be called in the main sketch by using an array to create a lot of them at one time and by using for loops they can all be updated and displayed at once.

//MAIN TAB

Mover[] movers = new Mover[20];

void setup() {
  size(600, 600);
  
  for (int i = 0; i < movers.length; i++){
  movers[i] = new Mover();
  }
  
}

void draw(){
  background(222);
  
  for (int i = 0; i < movers.length; i++){
  movers[i].update();
  movers[i].checkEdges();
  movers[i].display();
  }
  
}

//MOVER TAB

class Mover{
  
  PVector location;
  PVector velocity;
  PVector acceleration;
  float topspeed;
  
 Mover(){
   location = new PVector(random(width), random(height));
   velocity = new PVector(0,0);
   topspeed= 4;
  
 }
 
  void update(){
    PVector mouse = new PVector(mouseX, mouseY);
    PVector dir = PVector.sub(mouse, location);
    
    dir.normalize();
    dir.mult(0.5);
    acceleration = dir;
    
    
    velocity.add(acceleration);
    velocity.limit(topspeed);
    location.add(velocity);
  }
  
  void display(){
    stroke(0);
    fill(175);
    ellipse(location.x, location.y, 16 ,16);
  }
  
 void checkEdges(){
   
   if (location.x>width){
     location.x = 0;
   } else if (location.x < 0){
     location.x = width;
   }
 
   if (location.y>height){
     location.y = 0;
   } else if (location.y < 0){
     location.y = height;
   }
   
 }
  
  
}

Another advantage in using objects in code is that more functionality can always be given to objects by expanding the code and adding more methods. Here I added the force of gravity to the objects when the mouse is pressed by adding an if conditional to the update method and a new method called applyForce which adds a force to the acceleration. The force gravity is a simply a vector (0, 2) which adds two to the y position, making the object move down.

However there are probably more realistic and accurate ways to simulate movement using more complex mathematics and physics equations. I will look into these as more research into my project to get objects moving in my sketches more realistically.

//MOVER TAB

class Mover{
  
  PVector location;
  PVector velocity;
  PVector acceleration;
  PVector gravity;
  float topspeed;
  
 Mover(){
   location = new PVector(random(width), random(height));
   velocity = new PVector(0,0);
   gravity = new PVector (0, 2);
   topspeed= 4;
  
 }
 
  void update(){
   
    PVector mouse = new PVector(mouseX, mouseY);
    PVector dir = PVector.sub(mouse, location);
    
    dir.normalize();
    dir.mult(0.5);
    acceleration = dir;
    
    if (mousePressed){
    applyForce(gravity);
  }
    
    
    velocity.add(acceleration);
    velocity.limit(topspeed);
    location.add(velocity);
   acceleration.mult(0);
  }
  
  void display(){
    stroke(0);
    fill(175);
    ellipse(location.x, location.y, 16 ,16);
  }
  
 void checkEdges(){
   
   if (location.x>width){
     location.x = 0;
   } else if (location.x < 0){
     location.x = width;
   }
 
   if (location.y>height){
     location.y = 0;
   } else if (location.y < 0){
     location.y = height;
   }
   
 }
 
 void applyForce(PVector force){
   acceleration.add(force);
 }
  
  
}
//MAIN TAB

Mover[] movers = new Mover[20];

void setup() {
  size(600, 600);
  
  for (int i = 0; i < movers.length; i++){
  movers[i] = new Mover();
  }
  
}

void draw(){
  background(222);
  
  for (int i = 0; i < movers.length; i++){
  
  movers[i].update();
  movers[i].checkEdges();
  movers[i].display();
  
  }
  
}

Press the mouse button in the sketch to create “gravity”

Shiffman, D., 2012. The Nature of Code [online] Mountain View: Creative Commons

Leave a Comment