# Creating obstacles in an existing script

**URL:** https://discourse.processing.org/t/creating-obstacles-in-an-existing-script/17190
**Category:** Coding Questions
**Created:** [January 19, 2020, 10:43pm UTC](https://discourse.processing.org/t/creating-obstacles-in-an-existing-script/17190 "2020-01-19T22:43:26Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![floflopm](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@floflopm](https://discourse.processing.org/u/floflopm)
#### Post date: [January 19, 2020, 10:43pm UTC](https://discourse.processing.org/t/creating-obstacles-in-an-existing-script/17190/1 "2020-01-19T22:43:26Z")

</div>

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

````auto

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

---

<div class="post-metadata">

### Author: ![floflopm](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@floflopm](https://discourse.processing.org/u/floflopm)
#### Post date: [January 19, 2020, 10:49pm UTC](https://discourse.processing.org/t/creating-obstacles-in-an-existing-script/17190/2 "2020-01-19T22:49:31Z")

</div>

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:

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

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 20, 2020, 1:20am UTC](https://discourse.processing.org/t/creating-obstacles-in-an-existing-script/17190/3 "2020-01-20T01:20:05Z")

</div>

```auto
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 )

---

<div class="post-metadata">

### Author: ![floflopm](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@floflopm](https://discourse.processing.org/u/floflopm)
#### Post date: [January 20, 2020, 1:44am UTC](https://discourse.processing.org/t/creating-obstacles-in-an-existing-script/17190/4 "2020-01-20T01:44:23Z")

</div>

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,

---

<div class="post-metadata">

### Author: ![Nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nathan](https://discourse.processing.org/u/Nathan)
#### Post date: [January 20, 2020, 5:30pm UTC](https://discourse.processing.org/t/creating-obstacles-in-an-existing-script/17190/5 "2020-01-20T17:30:24Z")

</div>

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.
