Creating obstacles in an existing script

Hi,

I am currently working on the following script studying the effects of ‘‘waves’’. I would like to add controls that could modify the waves’ behaviour. Two things I would like to do:

  1. Find a way to update the screen’s ratio and size using the keyboard (ex: pressing the left key/right key change the width of the display and upper/lower keys change the height) without losing the geometries that are already on it

  2. create lines by dragging the mouse that would create obstacles for the waves (as right now the waves are bouncing on the screen’s boundaries)

I also don’t understand why I can’t change the background color nor create other geometries within that code, it seems like it makes any additions impossible - but I am very new to coding so there might be a way.

Thank you very much,

Florence


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

float dampening = 1;

void setup() {

  size(400, 400); // ratio/size changes interactions between waves. the window represents our horizon; what happens beyond is impossible to describe but it is shown as the waves come back + direct bounce of neg waves on boundaries 
  cols = width;
  rows = height;
  current = new float[cols][rows];
  previous = new float[cols][rows];
}

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

}

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;
}```

Here is the code with an attempt to draw lines. When I deactivate the rest of the code, i can see the lines, but otherwise they don’t show:

final static int NUM = 0300, NEWEST = NUM - 1;
final int[] x = new int[NUM], y = new int[NUM];

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

float dampening = 1;

void setup() {
  
  colorMode(RGB, NEWEST);
  frameRate(60);
  smooth(4);
  strokeWeight(1);
  
  size(400, 400); // ratio/size changes interactions between waves. the window represents our horizon; what happens beyond is impossible to describe but it is shown as the waves come back + direct bounce of neg waves on boundaries 
  cols = width;
  rows = height;
  current = new float[cols][rows];
  previous = new float[cols][rows];
}

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

}

void draw() {
  background(0);  
  
    for ( int i = 0; i != NEWEST;) {
    stroke(0, i, 0);
    line(x[i], y[i], x[i] = x[i + 1], y[i] = y[++i]);
    }
  
  x[NEWEST] = mouseX;
  y[NEWEST] = mouseY;
  
  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;
}```
loadPixels()

pixels[index] = color(current[i][j]);

updatePixels();

overwrite the canvas content, no matter what was drawn prior.


try to draw something
AFTER that code might work. ( move the line part down )

Thank you, this worked and the line now displays. But right now it only appears as a horizontal line ending at the tip of my mouse when I click; as soon as I release the button it disappears. + the waves do not interact (bounce) on it. I would like to create lines in all directions (starting when I click, ending where I drag my mouse and release) that would stay permanently as obstacles.

Is this too complicated if I’m a beginner?

Thank you,

1 Like

I’ll have to review more closely later on a larger screen.

But as you draw the line you are modifying your x and y arrays via assignment. Why aren’t you just incrementing i?

I think you’re effectively erasing the line by losing the previous segment each iteration. And since you’re clearing the screen each time it isn’t even there from the last loop.