Enable collisisons

Hi,

I currently have a script in which some pixels are bouncing all over the screen with a damping effect and I can draw lines. I would like to find a way to make the pixels bounce on the lines (the lines would become obstacles), but I don’t know how to write this as the two elements come from different parts of my script. I am using the Fisica library for the lines.

Thank you,


int cols;
int rows;
float[][] current;// = new float[cols][rows];
float[][] previous;// = new float[cols][rows];
float dampening = 1;

import fisica.*;

FWorld mundo;
FBox caja;

float x, y;
//--------------------------------------------------------------------------------

void setup() {
  size(500, 500); // ratio/size changes interactions between waves. the window represents our horizon;
  
  smooth();
  
  Fisica.init(this);
  
  mundo = new FWorld();
  mundo.setEdges();
  
  frameRate(24);
  
//--------------------------------------------------------------------------------  

  cols = width;
  rows = height;
  current = new float[cols][rows];
  previous = new float[cols][rows];
}

void mouseMoved() {
  previous[mouseX][mouseY] = 400; // changes intensity of ripple
}
  
void mouseClicked(){ // makes concentric circles
 previous[mouseX][mouseY] = 2000;

}

//--------------------------------------------------------------------------------

void draw() {
  background (0);
  
  loadPixels();
  for (int i = 1; i < cols-1; i++) {
    for (int j = 1; j < rows-1; j++) {
      current[i][j] = (
        previous[i-1][j] + 
        previous[i+1][j] +
        previous[i][j-1] + 
        previous[i][j+1]) / 2 -
        current[i][j];
      current[i][j] = current[i][j] * dampening;
      int index = i + j * cols;
      pixels[index]  = color(current[i][j]);
    }
  }
  
  updatePixels();

  float[][] temp = previous;//ripple. if disables, only creates static pixels
  previous = current;
  current = temp;

//---------------------------
 fill(0, 20);
  noStroke();
  rect(0, 0, width, height);
  
 mundo.step();
 mundo.draw(this);

//--------------print frame---------------------------
 if (keyPressed == true) { //to save images every x seconds and make a movie
    if (key == 'p'){
      saveFrame("ondes-###.png");
    }
    }
}

//-------------- make a line-------------------------
void mousePressed() {
  caja = new FBox(4, 4);
  caja.setStaticBody(true);  //true or false
  caja.setStroke(0);
  caja.setFill(0);
  caja.setRestitution(0.9);
  mundo.add(caja);
  
  x = mouseX;
  y = mouseY;
}

void mouseDragged() {
  if (caja == null) {
    return;
  }
  
  float ang = atan2(y - mouseY, x - mouseX);
  caja.setRotation(ang);
  caja.setPosition(x+(mouseX-x)/2.0, y+(mouseY-y)/2.0);
  caja.setWidth(dist(mouseX, mouseY, x, y));
  
}

I would start by doing a google search: queries like “line point collision detection” will return a ton of results. Try to get small example program working.

Shameless self-promotion: here is a guide on collision detection in Processing: