Interactivity

edited February 2018 in Questions about Code

Hi everyone

I need help to add interactivity to a work I've done, can you help me with this?

class Particle {

  PVector location;
  PVector velocity;
  PVector acceleration;
  PVector force,friction;
  float mass,r,distance,loc;
  float maxForce, maxSpeed,strength;
  int id,val;
  color c;
  float k = 0.02;
  int age = 0;
  int spawnAge = (int)random(80,120);
  float minr = 0;
  float maxr = 5.0;
  boolean spawned = false;

  Particle(float x, float y,int _id) {
    mass = 0.1;
    id = _id;
    r = 0.1;
    maxSpeed = 1;
    maxForce = 10;
    location = new PVector(constrain(x,r,width-r), constrain(y,r,height-r));
    velocity = new PVector(0, 0);
    acceleration = new PVector(0, 0);
  }

  void applyForce(PVector force) {
    //PVector f = PVector.div(force, mass);
    acceleration.add(force);
  }

  void update() {
    velocity.add(acceleration);
    velocity.limit(maxSpeed);
    location.add(velocity);
    acceleration.mult(0);
    friction();
    borders();
    age+=1;
  }

  void friction(){
     friction = velocity.copy();
     friction.mult(-1);
     friction.normalize();
     friction.mult(k);

     applyForce(friction);
  }

  void display() {

    ellipse(location.x,location.y,r*2,r*2);
  }

  PVector attract(Particle p) {
    force = PVector.sub(p.location,location);
    distance = force.magSq(); 

      distance = constrain(distance, 1.0, 5000.0);
      force.normalize();

      strength = (g) / (distance*4*PI);
      force.mult(strength);n
      return force;


  }

  void setRadius (PImage img){

      loc = (int)location.x + (int)location.y*img.width;
      //c= img.get((int)location.x,(int)location.y);
      //c =  img.pixels[int(loc)];
      val = img.pixels[int(loc)] & 0xFF;
      r=  lerp(r,map(val,0.0,255.0,minr,maxr),0.01);

  }

  void borders() {
      if ( location.x < r || location.x > width-r){
          velocity.x = -velocity.x;
      }
      if ( location.y < r || location.y > height-r){
          velocity.y = -velocity.y;
      }

      if (location.x> width-r) location.x = width-r;
      if (location.x< r) location.x = r;
      if (location.y> height-r) location.y = height-r;
      if (location.y< r) location.y = r;

  }

}

Answers

  • How to post in the processing forum.

    Best is to hit ctrl-t in processing first before copy paste into the forum (https://forum.processing.org).

    You can edit your post (click the small gear and the 'Edit').

    How to format Code:

    empty line before and after the code section
    
    select (highlight) entire code with the mouse
    
    hit ctrl-o OR click the small C in the command bar
    
  • Answer ✓

    can you post the class Particle as well please ?

  • Still main code missing and class Grid

  • class Grid{
       ArrayList[] grid;
       int cellWidth;
       int cellHeight;
    
       int rows,cols;
    
       public Grid(int _cols, int _rows){
          grid = new ArrayList[_rows*_cols];
    
          rows = _rows;
          cols = _cols;
    
          cellWidth = width/cols;
          cellHeight = height/rows;
       }
    
       void clear(){
          for (int i = 0; i<grid.length; i++){
            grid[i] = null;
          }
       }
    
       int getParticlePosition(Particle p){
         int xpos =  floor(p.location.x/cellWidth);
         int ypos =  floor(p.location.y/cellHeight);
         return (ypos*cols) + xpos;
    
       }
    
       void insert(Particle p){
         int index = getParticlePosition(p);
    
         if (grid[index] == null){
             grid[index] = new ArrayList<Particle>();
         }
         grid[index].add(p);
       }
    
       ArrayList getCell(Particle p){
         int index = getParticlePosition(p);   
         return  grid[index];
       }
    
       ArrayList getNear(Particle p){
         ArrayList objects = new ArrayList<Particle>();
    
          int xpos =  floor(p.location.x/cellWidth);
          int ypos =  floor(p.location.y/cellHeight);  
    
          int xstart = constrain(xpos-1,0,cols);
          int ystart = constrain(ypos-1,0,rows);
          int xend = constrain(xpos+2,0,cols);
          int yend = constrain(ypos+2,0,rows);
    
          for (int x = xstart; x<xend; x++){
            for (int y= ystart; y<yend; y++){
               int index = x + y*cols;
               if (grid[index] != null) objects.addAll(grid[index]);
            }
          }
    
          return objects;
       }
    
       ArrayList getInsideCircle(Particle p, float radius){
         ArrayList<Particle> objects = getNear(p);
         for (int i = objects.size()-1; i>=0; i--){
           Particle tempP = objects.get(i);
           PVector dist = PVector.sub(p.location,tempP.location);
           if (dist.magSq() > radius*radius){
             objects.remove(i);
           }
         }
    
         return objects;
       }
    
       void drawGrid(){
         for (int i = 0; i < cols; i++){
            line (cellWidth*i,0,cellWidth*i,height);
         }
         for (int i = 0; i < rows; i++){
           line (0,i*cellHeight,width,i*cellHeight);
    
         }
       }
    
    }
    
  • Where is setup()? Where is draw()?

    POST A COMPLETE SKETCH. POST IT ALL AT ONCE. POST IT IN ONE NEW POST. MAKE SURE IT RUNS.

    Then tell us what is wrong with it! What does it do that you don't want it to do? What doesn't it do that you do want it to do?

  • edited February 2018

    While the above post of mine asks some great questions, if you want interaction, well, try adding a mousePressed() function. What do you want to happen when the mouse is pressed? Can you write code to do that thing? So where do you think you should put the code that you want to happen when the mouse is pressed?

  • In fact, I do not know much about mousePressed, so I needed to join the forum. This is all I want to accidentally add points with the mousePressed function. But I will try.

Sign In or Register to comment.