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());
}
}

Leave a Reply