Skip to content

Instantly share code, notes, and snippets.

@andrusenn
Created May 11, 2020 02:59
Show Gist options
  • Save andrusenn/0666d8adabb4244bbb70c6bc06deb50b to your computer and use it in GitHub Desktop.
Save andrusenn/0666d8adabb4244bbb70c6bc06deb50b to your computer and use it in GitHub Desktop.
/*
Controles / Controls
z: Alternar retorno prticulas / Toggle particles return
x: Mantener presionado para que no borre / Keep pressed for not fade to black
s: captura imagen / image capture
Andrés Senn
andressenn.com
tw / IG -> @andrusenn
*/
// Librerias / Libraries
import com.hamoid.*;
import toxi.geom.*;
import toxi.physics2d.*;
import toxi.physics2d.behaviors.*;
// ====================
VerletPhysics2D physics;
AttractionBehavior2D mousea1, mousea2, mousea3, mousea4;
GravityBehavior2D gravbehaviour;
Vec2D mousePos1, mousePos2, mousePos3, mousePos4;
// Separacion de los puntos / Point separation
int step = 4;
//
ArrayList<Particle> ps;
int bgalph = 0;
// Modo / Mode
int MODO = 1;
// Return to initial position? Retorno a posicion inicial?
boolean initialPos = true;
VideoExport ve;
// Crear Video? // Create Video?
boolean video = false;
void settings() {
size(1080, 1080, P2D);
}
void setup() {
// Grabar video / Video Recording
if (video) {
ve = new VideoExport(this);
ve.setFrameRate(30);
ve.setMovieFileName(year()+""+month()+""+day()+""+hour()+""+minute()+""+second()+".mp4");
ve.startMovie();
}
frameRate(30);
background(0);
// INIT ================
ps = new ArrayList<Particle>();
physics = new VerletPhysics2D();
physics.setDrag(0.1f);
physics.setWorldBounds(new Rect(0, 0, width, height));
for (int x = 0; x < width; x+= step) {
for (int y = 0; y < height; y+= step) {
//float _x = x+map(noise(x*0.005, y*0.005), 0.0, 1.0, -100, 100);
//float _y = y+map(noise(x*0.005, y*0.005), 0.0, 1.0, -100, 100);
Particle p = new Particle(x, y);
p.w = 1.0 + 1.0 * noise(x*0.05, y*0.05);
//int cc = int(map(dist(x, y, width/2, height/2), 0, width/2, 255, 180));
p.setColor(color(255, 100));
ps.add(p);
physics.addParticle(p);
}
}
}
void draw() {
particulas();
if (keyPressed && key == 'm') {
translate(random(-10, 10), 0);
}
// ==================================
// Save video frame
if (video) {
ve.saveFrame();
}
}
class Particle extends VerletParticle2D {
float ix, iy;
int c;
AttractionBehavior2D backattr;
float w;
float z;
Particle(float _x, float _y) {
super(_x, _y);
ix = _x;
iy = _y;
c = color(255);
backattr = new AttractionBehavior2D(new Vec2D(ix, iy), 800, 0.3f, 0.0f);
this.addBehavior(backattr);
}
Particle(float _x, float _y, float _w) {
super(_x, _y);
ix = _x;
iy = _y;
c = color(c);
w = _w;
}
void returnPos() {
this.addBehavior(backattr);
}
void clearReturn() {
this.removeBehavior(backattr);
}
void setColor(int _c) {
c = _c;
}
public int getColor() {
return c;
}
}
void particulas() {
if (keyPressed && key == 'x') {
bgalph-=15;
if (bgalph<0)bgalph=0;
} else {
bgalph+=15;
if (bgalph>255)bgalph=255;
}
//
fill(0, bgalph);
noStroke();
rectMode(CENTER);
rect(width/2, height/2, width*2, height*2);
//
for (int i = 0; i < physics.particles.size(); i++) {
Particle p = (Particle) physics.particles.get(i);
float dis = dist(p.x, p.y, p.ix, p.iy);
if (dis > 3) {
strokeWeight(1);
stroke(p.c, 255);
beginShape(POINTS);
vertex(int(p.x), int(p.y));
//pg.ellipse(int(p.x), int(p.y), 20, 20);
endShape();
}
}
try {
physics.update();
}
catch(Exception e) {
println(e);
}
}
void mousePressed() {
mousePos1 = new Vec2D(mouseX, mouseY);
mousePos2 = new Vec2D(width-mouseX, mouseY);
mousePos3 = new Vec2D(mouseX, height-mouseY);
mousePos4 = new Vec2D(width-mouseX, height-mouseY);
float d = dist(mouseX, mouseY, width/2, height/2);
float b = map(d, 0, width/2, 200, 50);
mousea1 = new AttractionBehavior2D(mousePos1, b, 0.9f, 0.0f);
mousea2 = new AttractionBehavior2D(mousePos2, b, 0.9f, 0.0f);
mousea3 = new AttractionBehavior2D(mousePos3, b, 0.9f, 0.0f);
mousea4 = new AttractionBehavior2D(mousePos4, b, 0.9f, 0.0f);
switch (MODO) {
case 1:
physics.addBehavior(mousea1);
break;
case 2:
physics.addBehavior(mousea1);
physics.addBehavior(mousea2);
break;
case 3:
physics.addBehavior(mousea1);
physics.addBehavior(mousea2);
physics.addBehavior(mousea3);
physics.addBehavior(mousea4);
break;
}
}
void mouseReleased() {
physics.removeBehavior(mousea1);
physics.removeBehavior(mousea2);
physics.removeBehavior(mousea3);
physics.removeBehavior(mousea4);
}
void mouseDragged() {
float d = dist(mouseX, mouseY, width/2, height/2);
float b = map(d, 0, width/2, 200, 50);
mousePos1.set(mouseX, mouseY);
mousePos2.set(width-mouseX, mouseY);
mousePos3.set(mouseX, height-mouseY);
mousePos4.set((width-mouseX), height-mouseY);
mousea1.setRadius(b);
mousea2.setRadius(b);
mousea3.setRadius(b);
mousea4.setRadius(b);
}
void keyReleased() {
switch(key) {
// Un puntero de attraccion / one pointer attractor
case '1':
MODO = 1;
break;
case '2':
// Dos punteros de attraccion / two pointers attractor
MODO = 2;
break;
case '3':
// Cuatro punteros de attraccion / four pointers attractor
MODO = 3;
break;
}
// capturar actual / Capture current
if (key == 's') {
String fn = year()+""+month()+""+day()+""+hour()+""+minute()+""+second();
save("save/p"+fn+".png");
}
}
void keyPressed() {
//
if (key == 'z' || key == 'Z') {
if (initialPos) {
for (int i = 0; i < physics.particles.size(); i++) {
Particle p = (Particle) physics.particles.get(i);
p.clearReturn();
}
initialPos = false;
} else {
for (int i = 0; i < physics.particles.size(); i++) {
Particle p = (Particle) physics.particles.get(i);
p.returnPos();
}
initialPos = true;
}
}
//
if (key == 'q') {
if (video) {
ve.endMovie();
exit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment