Modify a code with wave effects

edited December 2014 in Questions about Code

Processing is new to me, so I don't really know how to use it. I wanted to try to make wave effect with it, so I used the Water Simulation I found here. I wanted to change it a bit and try to add first a white background (a white image), so first we see nothing, and then, when we hover the mouse over the white background, you can see a second image (I changed it, but it's the blue one for the link I gave, it's the only image in it) appearing with the waves. So the white is disappearing and now we can only see the second image.

I did it, but the problem is that the wave effect changed completely and seems like it's always calculating again the pixels of the images. I don't really know why is it doing this, but I would be thankful if someone here knew why it is doing this, and how we could change the code so the water disturbance is working again ?

Here is the code I changed :

public static int CIRCLE_WIDTH = 100;
public static int CIRCLE_HEIGHT = 100;

PImage img;

int size;
int hwidth,hheight;
int riprad;

int ripplemap[];
int ripple[];
int texture[];

int oldind,newind, mapind;

int i,a,b; 

void setup(){
  img = loadImage("background.jpeg");
  //width = img.width;
  //height = img.height;
  //size(width, height);
  size(300, 300);
  frameRate(40);

  // Add an initial set of boid into the system
  for (int i = 0; i < 150; i++) {
  hwidth = width>>1;
  hheight = height>>1;
  riprad=3; //test with 3 (initially 5)

  size = width * (height+2) * 2;

  ripplemap = new int[size];
  ripple = new int[width*height];
  texture = new int[width*height];

  oldind = width;
  newind = width * (height+3);

  //image background
 /* image(img, 0, 0); 
 * loadPixels();
 *
 * smooth();
  */


//for an image loaded at the beginning
 /*  loadPixels();
  *smooth();
  *
  *img = loadImage("background.jpeg");
  *img.loadPixels();
  *img.mask(img);
  */

  }
  //here is the background added to see it first, before the second image appears
  background(255);
}

void draw() {
//hid the second image to not see it first
  //image(img, 0, 0);
  copy(img, 
              constrain(mouseX-CIRCLE_WIDTH/2,0,width), 
              constrain(mouseY-CIRCLE_HEIGHT/2,0,height), 
              CIRCLE_WIDTH,CIRCLE_HEIGHT, 
              constrain(mouseX-CIRCLE_WIDTH/2,0,width), 
              constrain(mouseY-CIRCLE_HEIGHT/2,0,height), 
              CIRCLE_WIDTH,CIRCLE_HEIGHT);

  loadPixels();
  texture = pixels;

  newframe();

  for (int i = 0; i < pixels.length; i++) {
    pixels[i] = ripple[i];
  }

  updatePixels();
}




public void disturb(int dx, int dy) {
  for (int j=dy-riprad;j<dy+riprad;j++) {
    for (int k=dx-riprad;k<dx+riprad;k++) {
      if (j>=0 && j<height && k>=0 && k<width) {
      ripplemap[oldind+(j*width)+k] += 500;   //test with 512         
      } 
    }
  }
}

void newframe() {
  //Toggle maps each frame
  i=oldind;
  oldind=newind;
  newind=i;

  i=0;
  mapind=oldind;
  for (int y=0;y<height;y++) {
    for (int x=0;x<width;x++) {
    short data = (short)((ripplemap[mapind-width]+ripplemap[mapind+width]+ripplemap[mapind-1]+ripplemap[mapind+1])>>1);
      data -= ripplemap[newind+i];
      data -= data >> 5;
      ripplemap[newind+i]=data;

    //where data=0 then still, where data>0 then wave
    data = (short)(1024-data);

      //offsets
    a=((x-hwidth)*data/1024)+hwidth;
      b=((y-hheight)*data/1024)+hheight;

     //bounds check
      if (a>=width) a=width-1;
      if (a<0) a=0;
      if (b>=height) b=height-1;
      if (b<0) b=0;

      ripple[i]=texture[a+(b*width)];
      mapind++;
    i++;
    }
  }
}

//Try switching between using the disturb method in mousePressed or mouseMoved
void mousePressed()
{
  //disturb(mouseX, mouseY);
}

void mouseMoved()
{
  disturb(mouseX, mouseY);
}

void mouseReleased()
{
}

Answers

  • You made a duplicate message, deleted it.
    If you want to bring attention to an unnoticed message, reply to it, instead of creating a new one. You can also edit the first message to improve the subject or other things.
    And don't forget to choose a category.

    Note on your code: don't forget that size() should be the first thing called in setup().

Sign In or Register to comment.