Recitation 10: OOP

These two videos are the particles that I have made for the oop workshop, the idea is to let the videos firstly randomly generated on the screen and then follow the cursar wherever it is and orbit around it.

I use map() function in the first video example to declare the randomly generated position of the particles, in the second example the function is used to set the opacity of the particles in terms of their distance from the cursar.

The following code is for the first video, including three files.

Particles 

ParticleSystem ps;

void setup() {
size(1920,1080);
ps = new ParticleSystem();
ps.addParticle();
}

void draw() {

background(0);

ps.run();

}

Particle1

class Particle1{

PVector curPosition;
PVector accelerationX;
PVector accelerationY;
PVector position;
PVector velocity;
int r = 2;
color c;

Particle1(PVector curPos, PVector pos, PVector v, color parc){
//acceleration = new PVector(0.05,0.05);
position = pos.get();
velocity = v.get();
curPosition = curPos.get();
accelerationX = new PVector(random(0,2),0);
accelerationY = new PVector(0,random(0,2));
c = parc;
}

// Method to update position
void update() {
if(position.x <= 0){
velocity = new PVector(velocity.x*=-1,velocity.y);
float prey = position.y;
position = new PVector(1,prey);
}
if(position.x >= width){
velocity = new PVector(velocity.x*=-1,velocity.y);
float prey = position.y;
position = new PVector(width-1,prey);
}
if(position.y <= 0){
velocity = new PVector(velocity.x,velocity.y*=-1);
float prex = position.x;
position = new PVector(prex,1);
}
if(position.y >= height){
velocity = new PVector(velocity.x,velocity.y*=-1);
float prex = position.x;
position = new PVector(prex,height-1);
}

velocity.mult(0.99);//slowing particles down

if(position.y < curPosition.y - r){
velocity.add(accelerationY);
}
else if (position.y > curPosition.y + r){
velocity.sub(accelerationY);
}else if(position.y >= curPosition.y - r && position.y <= curPosition.y){
velocity.add(accelerationY.mult(2));
}else{
velocity.sub(accelerationY.mult(2));
}
if(position.x < curPosition.x - r){
velocity.add(accelerationX);
}
else if (position.x > curPosition.x + r){
velocity.sub(accelerationX);
}else if(position.x >= curPosition.x - r && position.x <= curPosition.x){
velocity.add(accelerationX.mult(2));
}else{
velocity.sub(accelerationX.mult(2));
}
position.add(velocity);
}

void run() {
update();
push();
display();
pop();
}

void push() {
pushMatrix();
}

void pop() {
popMatrix();
}

// Method to display
void display() {
noStroke();
fill(c);
translate(position.x,position.y);
ellipse(0,0,8,8);
}

PVector getPos(){
return position;
}

PVector getV(){
return velocity;
}

}

ParticleSystem

class ParticleSystem {
ArrayList<Particle1> particles1;
PVector position;
PVector velocity;
color c;
color[] co = new color[10];
PVector[] pos = new PVector[10];
PVector[] v = new PVector[10];
int pNum = 10;

ParticleSystem() {

particles1 = new ArrayList<Particle1>();
for(int i = 0; i<pNum; i++){
position = new PVector(random(width*1/4,width*3/4), random(height/4,height*3/4));
velocity = new PVector(random(-5,5),random(-5,5));
c = color(random(0,255), random(0,255), random(0,255));
pos[i] = position;
v[i] = velocity;
co[i] = c;
}
}

void addParticle() {
for (int i = 0; i<pNum; i++){
float x = map(mouseX,0,width,500,600);
particles1.add(new Particle1(new PVector(x,mouseY), pos[i],v[i],co[i]));
}
}

void run() {
for (int i = 0; i<pNum; i++) {
Particle1 p1 = particles1.get(i);
p1 = new Particle1(new PVector(mouseX,mouseY), pos[i],v[i],co[i]);
p1.run();
pos[i] = p1.getPos();
v[i] = p1.getV();
}
//printArray(particles);
println(particles1.size());
}
}

Recitation 10 Workshops by Barry Wang

Recitation 10 Workshops

In this week’s recitation, I attended two workshops, which are map() function workshop and OOP workshop

For the map() function, it is pretty straightforward.

map(Variable name, lower bound of domain, upper bound of domain, lower bound of codomain, upper bound of codomain), which accept a variable name and 4 float data as range.

For the OOP part, a definition of class starts with

class (class name){

}

Then we define local variables within the class, and write an initialize funtion within the class, whose name is same as the class itself. It goes like:

class (test){

float x = 0;

float y = 0;

test(parameters){

    ……        

}

}

When creating a new instance of class, we use 

test instance = new test(parameters);

In my final project, I wrote a class of bullets in my game since all the bullets are similar objects and can be put together.

class Balls {
  float x_pos;
  float y_pos;
  float vy = 10;
  PImage bullet;
  Balls (int n,int m,int p,float x,float y){
	x_pos = (x-50*width/height) + ((m+1)*100*width/height)/(n+1);
    y_pos = y;
	switch (n){
		case 1:
			switch(p){
			case 1:
			bullet = loadImage("bullet1.png");
			break;
			case 2:
			bullet = loadImage("bullet3.png");
			break;
			}
			break;
		case 2:
			switch(p){
			case 1:
			bullet = loadImage("bullet2.png");
			break;
			case 2:
			bullet = loadImage("bullet4.png");
			break;
			}
			break;
		case 3:
			switch(p){
			case 1:
			bullet = loadImage("bullet2.png");
			break;
			case 2:
			bullet = loadImage("bullet4.png");
			break;
			};			
		default:
			bullet = loadImage("bullet4.png");
			break;			
	}
  }
  void update(){
    //fill(255);
    //ellipse(x_pos,y_pos,8*width/1000,8*width/1000);	
	image(bullet,x_pos,y_pos,25*width/1000,25*width/1000);
    y_pos -= vy * 0.9;
  }
}
        

Recitation 10: Workshops – Ariana Alvarez

For this week’s recitation, after the map() function workshop, I chose to attend the media manipulation workshop, as it was what aligned the most with my project. What I wanted to work on in this workshop, was to learn how to manipulate pixels in webcam. 

Initially, I attempted to change directly the RGB colors in the webcam, as during the workshop I was told that there may not be the possibility of adding a filter to it (similar with an image). As this process wasn’t being effective in creating a negative image effect,  I did some research and it was possible to add filters to webcam with cam.filter() function. 

After adding an inverse black and white filter effect on the webcam, I also attempted to make the image brighter and darker by manipulating the HSB values of the pixels. It was quite challenging, however this media manipulation workshop provided me with a better head-start towards my project and allowed me to explore further ways in which pixels could be manipulated in webcam through processing. 

The code was the following:

//int r = 50;
//int g = 50;
//int b = 50;

import processing.video.*; 
Capture cam;

//color invertColor( int r, int g, int b) {

//  return color(255 - r, 255 - g, 255 - b);
//}

void setup() {  
  size(640, 480); 
  colorMode(HSB);
  cam = new Capture(this, 640, 480);
  cam.start(); 
} 
 void draw() { 
   
   
   
  if (cam.available()) { 
   cam.read(); 
   image(cam, 0, 0); 
   cam.filter(GRAY);
   cam.filter(INVERT);
      //background(invertColor(r,g,b));
  } 

  cam.loadPixels();
       
//Pixels, code with Arduino Distance Sensor
  noStroke();
  int rectSize = 10;
  int w = cam.width;
  int h = cam.height;
  
  for (int y = 0; y < h; y+=rectSize) {
    for (int x = 0; x < w; x+=rectSize) {
      int i =  x + y * w;
      
      fill( cam.pixels[i] );     
      rect(x, y, rectSize, rectSize);
      
      
    //for (int y = 0; y < h; y++) {
    //  for (int x = 0; x < w; x++) {
    //    int i =  x + y*w; // *** IMPORTANT ***
    
        float b = brightness(cam.pixels[i]); 
        float s = saturation(cam.pixels[i]);
        float u = hue(cam.pixels[i]);
        float ch = map(mouseX, 0, 255, height, width);
        cam.pixels[i] = color(u, s, b+ch); 
     
      }
   
    cam.updatePixels();
  }
  }
 //   }
 //}

Recitation 10 media manipulation workshop (Katie)

In this recitation, I chose to attend the media manipulation workshop.  The thing I want to work out in this workshop is how to switch scenes. For example, to switch from the webcam to the video. I want to add a keypress function to achieve but the problem is that the scene only changes when I press the key. After I release the key, it turns back to the webcam. To solve this problem, I add a boolean to trigger the video.

Recitation 10: Workshops Documentation by Eleanor Wade

Serial Communication Workshop:

Using a button on the Arduino as a sensor to control the sketch on processing.  We used multiple value Arduino to Processing sample code.  

This recitation was very helpful in learning some necessary techniques in serial communication, with sensors that are beyond that of a potentiometer.  This will definitely be useful when moving forward with my final project as I will  definitely be using a color sensor to translate colored tags into specific factual information and images on the screen.  In this way, it was particularly beneficial to me to be able review the information that we previously had learned regarding arduino to processing.  

Processing Code: 

// IMA NYU Shanghai
// Interaction Lab
// For receiving multiple values from Arduino to Processing

/*
 * Based on the readStringUntil() example by Tom Igoe
 * https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html
 */

import processing.serial.*;

String myString = null;
Serial myPort;


int NUM_OF_VALUES = 2;   /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int[] sensorValues;      /** this array stores values from Arduino **/


void setup() {
  size(500, 500);
  background(0);
  setupSerial();
}


void draw() {
  updateSerial();
  printArray(sensorValues);

  // use the values like this!
  // sensorValues[0] : pot 0 - 1023
  // sensorValues[1] : button switch 0, 1

  // add your code
  float posX = map(sensorValues[0], 0, 1023, 0, 500); 
 int size;
 if (sensorValue[1] == 0) {
   size = 50;
 } else { 
   size = 200;
 }
 
  
  
ellipse (300, 300, size, size);
  //
}



void setupSerial() {
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[ 1 ], 9600);
  // WARNING!
  // You will definitely get an error here.
  // Change the PORT_INDEX to 0 and try running it again.
  // And then, check the list of the ports,
  // find the port "/dev/cu.usbmodem----" or "/dev/tty.usbmodem----" 
  // and replace PORT_INDEX above with the index number of the port.

  myPort.clear();
  // Throw out the first reading,
  // in case we started reading in the middle of a string from the sender.
  myString = myPort.readStringUntil( 10 );  // 10 = '\n'  Linefeed in ASCII
  myString = null;

  sensorValues = new int[NUM_OF_VALUES];
}



void updateSerial() {
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil( 10 ); // 10 = '\n'  Linefeed in ASCII
    if (myString != null) {
      String[] serialInArray = split(trim(myString), ",");
      if (serialInArray.length == NUM_OF_VALUES) {
        for (int i=0; i<serialInArray.length; i++) {
          sensorValues[i] = int(serialInArray[i]);
        }
      }
    }
  }
}

One value Processing to Arduino Serial Communication: 

Arduino Code:

// IMA NYU Shanghai
// Interaction Lab
// This code receives one value from Processing to Arduino

#include <Servo.h>
Servo myservo;
int val;

char valueFromProcessing;
//int ledPin = 13;

void setup() {
Serial.begin(9600);
myservo.attach(9);
}

void loop() {
// to receive a value from Processing
while (Serial.available()) {
valueFromProcessing = Serial.read();
}

val = valueFromProcessing;
val = map(val, 0, 500, 0, 180);
myservo.write(val);
delay(15);

// if (valueFromProcessing == ‘H’) {
// digitalWrite(ledPin, HIGH);
// } else if (valueFromProcessing == ‘L’) {
// digitalWrite(ledPin, LOW);
// } else {
// something esle
// }

// too fast communication might cause some latency in Processing
// this delay resolves the issue.
delay(10);
}

In using the map function to map the values of canvas size, we specifically changed the code to this:

val = map(val, o, 500, 0, 180);