Source Code. Projects. Nerd Stuff. Art Stuff.

Underwater


Posted on October 16th, by amoeboar in classes, icm. No Comments

A sketch that puts you underwater!

 

[gn_spoiler title="underwater source code:" open="0" style="1"]

[gn_tabs style="3"]

[gn_tab title="underwater"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/********************************
 / John Capogna
 / underwater.pde
 / ITP, 2012
 ********************************/

import processing.video.*;

Capture cam;
PImage displayImg;
//int greenX = 0;
//int greenY = 0;
//float mostGreenPixel = 10000;  // start green value very high

ArrayList images;

void setup() {
  size(640, 480);

  cam = new Capture(this, 640, 480);
  cam.start();

  displayImg = new PImage(640, 480);

  // Initialize the ArrayList
  images = new ArrayList();

  // Make bubbles and fishes
  for (int i = 0; i < 25; i++) {
    if (i < 20) {
      Bubble tempBubble = new Bubble(random(width), random(height), random(-1, 1), random(-1, 1));
      images.add(tempBubble);
    }
    else {
      Fish tempFish = new Fish(random(width), random(height), random(-1, 1), random(-1, 1));
      images.add(tempFish);
    }
  }
}  



void draw() {
  if (cam.available()) {
    cam.read();

    for (int x = 0; x < cam.width; x++) {
      for (int y = 0; y < cam.height; y++) {
        // (width - x - 1) refers to a reversed image
        int dIndex = x + y * cam.width;  // display index
        int cIndex = (width - x - 1) + y * cam.width;  // camera index

        color currentPixel = cam.pixels[cIndex];

        displayImg.pixels[dIndex] = cam.pixels[cIndex];
      }
    }

    displayImg.updatePixels();

    image(displayImg, 0, 0);
  }


  // We go through the images ArrayLIst
  for (int i = 0; i < images.size(); i++) {
    // We cast every object we pull out as a Bubble.
    Image clone = (Image)images.get(i);
    // When we call bubble.draw, it calls the Bubble class's draw function
    clone.draw();
  }
}
[/gn_tab] [gn_tab title="Image"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Image {

  PVector position;
  PVector speed;

  // Constructor: make a spot in memory, set up objects
  Image(float pX, float pY, float sX, float sY) {
    position = new PVector(pX, pY);
    speed = new PVector(sX, sY);
  }

  // Image behavior
  void draw() {
    position.add(speed);

    // "Bouncing Shape" check
    if (position.x < 0 || position.x > width) {
      speed.x *= -1;
    }

    if (position.y < 0 || position.y > height) {
      speed.y *= -1;
    }
  }
}
[/gn_tab] [gn_tab title="Bubble"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Bubble extends Image {

  PImage bubbleImg = loadImage("bubble.png");

  // Constructor: make a spot in memory, set up objects
  Bubble(float pX, float pY, float sX, float sY) {
    super(pX, pY, sX, sY);
  }

  // draw bubble image
  void draw() {
    super.draw();
    image(bubbleImg, position.x, position.y);
  }
}
[/gn_tab] [gn_tab title="Fish"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Fish extends Image {

  PImage fishImg = loadImage("fish.png");
//  boolean hitWall = false;

  // Constructor: make a spot in memory, set up objects
  Fish(float pX, float pY, float sX, float sY) {
    super(pX, pY, sX, sY);
  }
 
  // draw fish image
  void draw() {
    super.draw();
    image(fishImg, position.x, position.y);      

//    // Reverse fish direction
//    if (position.y < 0 || position.y > height) {
//      hitWall = true;
//    }
//    
//    while (hitWall = true) {
//      pushMatrix();
//      scale(-1.0, 1.0);
//      image(fishImg, -fishImg.width, 0);      
//      popMatrix();
//    }
  }
}
[/gn_tab] [/gn_tabs] [/gn_spoiler]

 

You’ll need the images to use the code. You can unzip them here.





Leave a Reply