Skip to content

Instantly share code, notes, and snippets.

@clemp
Last active March 24, 2020 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clemp/dfb0a3de906d7fb72786411ded667882 to your computer and use it in GitHub Desktop.
Save clemp/dfb0a3de906d7fb72786411ded667882 to your computer and use it in GitHub Desktop.
Triggering Object Interactions

Triggering Object Interactions

When objects interact, we can trigger an event or effect to show that an interaction has occurred. We'll use Processing to show how to implement object interactions in a simulation environment.

For this tutorial, we'll create Particle objects (displayed as ellipses) that move around the screen. When two particles come within a certain distance of each other, an ellipse will pulse from the center of each particle. The ellipse will grow to a specified size, then disappear.

First let's create the base Particle class with some physics to move around.

class Particle {
  PVector pos;
  PVector velocity;
  PVector acceleration;
  float maxforce;
  float maxspeed;
  float r = 15;
  
  int id;
  
  Particle(int x, int y, int _id) {
    accleration = new PVector(0, 0);
    velocity = new PVector(0, 0);
    pos = new PVector(x, y);
    maxspeed = 4.0;
    maxforce = 0.03;
    
    id = _id;
  } // end Particle constructor
  
  void applyForce(PVector force) {
    acceleration.add(force);
  }
  
  void seek(PVector target) {
    PVector desired = PVector.sub(target, pos);
    desired.normalize();
    desired.mult(maxspeed);
    PVector steer = PVector.sub(desired, velocity);
    steer.limit(maxforce);
    applyForce(steer);
  }
  
  void update() {
    velocity.add(acceleration);
    velocity.limit(maxspeed);
    pos.add(velocity);
    acceleration.mult(0);
  }
  
  void display() {
    fill(255);
    noStroke();
    ellipse(pos.x, pos.y, r, r);
  }
} // end Particle class

The main program will look like this:

void setup() {

}

void draw() {

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment