Tarea 10 Partículas - Carolina Bustos
De Casiopea
Título | Carolina Bustos - Tarea 10 - Partículas |
---|---|
Tipo de Proyecto | Proyecto de Curso |
Palabras Clave | tarea 10 |
Asignatura | Imagen Escrita 2012, Imagen Escrita, |
Del Curso | Imagen Escrita, Imagen Escrita 2012, |
Carreras | Arquitectura |
Alumno(s) | Carolina Bustos |
Profesor | Herbert Spencer |
int MAX = 45; Objeto[] t = new Objeto[MAX]; Atractor a; Atractor2 a2; boolean showVectors = false; boolean velo = false;
void setup() {
size(800, 950); smooth(); for (int i = 0; i < t.length; i++){ PVector ac = new PVector(0.2, 0.5); PVector ve = new PVector(random(-2, 1), random(-1, 0.4)); PVector lo = new PVector(random(width), random(height)); t[i] = new Objeto(ac, ve, lo, random(5, 167));
}
a = new Atractor(new PVector(width/5, height/2.3), 30, 1.4); a2= new Atractor2 (new PVector(width/2, height/2), 10, 1);
PFont font; font = createFont("Arial", 12); textFont(font); }
color cualquiera() {
color c = color(random(255), random(255), random(255)); return c;
}
void draw() {
if (!velo)background(346); a.rollover(mouseX, mouseY); a2.rollover(mouseX,mouseY); a.go(); a2.go(); for (int i = 0; i < t.length; i++) {
PVector f = a.calcGravForce(t[i]); t[i].applyForce(f); t[i].go(); } if (keyPressed) { if (key == 'a') { a.G -= 1; } } fill(255); text("G = "+a.G+",\t la masa del atractor = "+a.mass, 21, height-25); if (velo) { fill(255, 30); noStroke(); rect(1, 4, width, height); }
}
void mousePressed() {
a.clicked(mouseX, mouseY); a2.clicked(mouseX, mouseY);
}
void mouseReleased() {
a.stopDragging(); a2.stopDragging();
}
void keyPressed() {
if (key == 's') { showVectors = !showVectors; } if (key == 'v') { velo = !velo; }
}
void drawVector(PVector v, PVector loc, float scayl) {
strokeWeight(2); if (v.mag() > 0.0) { pushMatrix(); float arrowsize = 24; translate(loc.x, loc.y); stroke(255, 200, 10, 139); rotate(v.heading2D()); float len = v.mag()*scayl; line(0, 0, len, 0); line(len, 0, len-arrowsize, +arrowsize/2); line(len, 0, len-arrowsize, -arrowsize/0.7); popMatrix(); }
}
class Atractor {
float mass; float G; PVector loc; boolean dragging = false; boolean rollover = false; PVector drag; Atractor(PVector l_,float m_, float g_) { loc = l_.get(); mass = m_; G = g_; drag = new PVector(0.2,0.5); } void go() { render(); drag(); } PVector calcGravForce(Objeto t) { PVector dir = PVector.sub(loc,t.getLoc()); float d = dir.mag(); d = constrain(d, 1.0, 250.0); dir.normalize(); float force = (G * mass * t.getMass()) / (d * d); dir.mult(force); return dir; }
void render() { strokeWeight(mass / 58); stroke(0, 139); ellipseMode(CENTER); if (dragging) fill (67); else if (rollover) fill(100); else fill(400,700); ellipse(loc.x,loc.y,mass*2,mass*2); noStroke(); }
void clicked(int mx, int my) { float d = dist(mx,my,loc.x,loc.y); if (d < mass) { dragging = true; drag.x = loc.x-mx; drag.y = loc.y-my; } } void rollover(int mx, int my) { float d = dist(mx,my,loc.x,loc.y); if (d < mass) { rollover = true; } else { rollover = false; } } void stopDragging() { dragging = false; }
void drag() { if (dragging) { loc.x = mouseX + drag.x; loc.y = mouseY + drag.y; } }
}
class Atractor2 {
float mass; float G; PVector loc; boolean dragging = false; boolean rollover = false; PVector drag; Atractor2(PVector l_,float m_, float g_) { loc = l_.get(); mass = m_; G = g_; drag = new PVector(1,0.4); } void go() { render(); drag(); } PVector calcGravForce(Objeto t) { PVector dir = PVector.sub(loc,t.getLoc()); float d = dir.mag(); d = constrain(d, 1.2, 1340.0); dir.normalize(); float force = (G * mass * t.getMass()) / (d * d); dir.mult(force); return dir; }
void render() { strokeWeight(mass / 44); stroke(0, 139); ellipseMode(CENTER); if (dragging) fill (134); else if (rollover) fill(120); else fill(400,700); ellipse(loc.x,loc.y,mass*4,mass*2); noStroke(); }
void clicked(int mx, int my) { float d = dist(mx,my,loc.x,loc.y); if (d < mass) { dragging = true; drag.x = loc.x-mx; drag.y = loc.y-my; } } void rollover(int mx, int my) { float d = dist(mx,my,loc.x,loc.y); if (d < mass) { rollover = true; } else { rollover = false; } } void stopDragging() { dragging = false; }
void drag() { if (dragging) { loc.x = mouseX + drag.x; loc.y = mouseY + drag.y; } }
}
class Objeto {
PVector loc; PVector vel; PVector acc; float mass; float max_vel; color col; Objeto(PVector a, PVector v, PVector l, float m_) { acc = a.get(); vel = v.get(); loc = l.get(); mass = m_; max_vel = 20.0; col = color(random(255, 673), random(100, 300), random(0.5), 208); } PVector getLoc() { return loc; } PVector getVel() { return vel; } float getMass() { return mass; } void applyForce(PVector force) { force.div(mass); acc.add(force); if (showVectors) { drawVector(force,loc, 200); } } void go() { update(); render(); } void update() { vel.add(acc); vel.limit(max_vel); loc.add(vel); acc.mult(0); } void render() { ellipseMode(CENTER); fill(col); ellipse(loc.x,loc.y,mass,mass); if (showVectors) { drawVector(vel,loc,32); } }
}