Skip to content

Instantly share code, notes, and snippets.

@AlcaDesign
Created January 7, 2017 23:40
Embed
What would you like to do?
[Processing] Particle thing
class Entity {
PVector size = new PVector(entitySize, entitySize);
PVector pos = new PVector(0, 0);
PVector vel = new PVector(0, 0);
PVector acc = new PVector(0, 0);
color c;
boolean bounced = false;
int life = 0;
int lifeSpan = 200;
Entity() {
reset();
}
void reset() {
bounced = false;
life = 0;
vel.set(0, 0);
pos.set(width / 2, 200);
float circleRate = frameCount / 30.0 + HALF_PI;
pos.add(
cos(circleRate) * (width * 0.35),
sin(circleRate) * (height * 0.1)
);
/*PVector v = PVector.random2D()
.mult(
random(
30,
60 + cos(frameCount / 8.0) * 30
)
);*/
PVector v = PVector.fromAngle(random(0, QUARTER_PI));
v.mult(
random(
30,
60 + cos(frameCount / 8.0) * 30
)
);
v.x = abs(v.x);
v.y = abs(v.y);
v.rotate(frameCount / 20.0 + EIGHTH_PI + QUARTER_PI);
applyForce(v);
}
void update() {
life++;
if(canRender()) {
float distFromTractor = pos.dist(tractor);
if(distFromTractor < tractorRadius) {
PVector p = pos.copy();
p.sub(tractor);
/*p.normalize();
p.mult(tractorMag);*/
p.mult(tractorDirection ? -0.1 : 0.1);
applyForce(p);
}
}
vel.add(acc);
acc.mult(0);
pos.add(vel);
vel.mult(0.985);
if(pos.y + size.y > height) {
if(vel.y > 3) {
applyForce(0, -vel.y * 1.875);
}
else {
pos.y = height - size.y;
}
bounced = true;
}
//wrap();
}
void render() {
if(canRender()) {
//fill(c, map(life, 0, lifeSpan, 255, 0));
fill(c);
rect(pos.x, pos.y, size.x, size.y);
}
}
boolean canRender() {
return bounced || life > 40;
}
void applyForce(PVector force) {
acc.add(force);
}
void applyForce(float x, float y) {
acc.add(x, y);
}
void wrap() {
if(pos.x < 0) {
pos.x = width + pos.x;
}
else if(pos.x + size.x > width) {
pos.x = pos.x - width;
}
}
}
float hueVariation = 100;
float hueOffset = 120;
int entityCount = 40000;
float entitySize = 2;
Entity[] ents = new Entity[entityCount];
float EIGHTH_PI = PI / 8;
PVector g = new PVector(0, 0.99);
PVector tractor;
float tractorRadius = 100;
float tractorMag = 10;
boolean tractorDirection = true;
void setup() {
size(1920, 1080);
tractor = new PVector(width / 2, height * 0.75);
colorMode(HSB);
for(int i = 0; i < ents.length; i++) {
Entity e = new Entity();
ents[i] = e;
e.c = color(
map(
i,
0, (float)ents.length,
0, hueVariation
) + hueOffset,
255, 255
);
}
}
void mousePressed() {
tractorDirection ^= true;
}
void mouseWheel(MouseEvent event) {
tractorRadius -= event.getCount() * 10;
if(tractorRadius < 0) {
tractorRadius = 0;
}
}
void draw() {
background(0);
tractor.set(mouseX, mouseY);
noStroke();
for(Entity e : ents) {
e.render();
e.applyForce(g);
e.update();
if(
e.life > e.lifeSpan * 0.1 &&
e.vel.mag() < 3.5 &&
e.pos.y > height - e.size.y - 100
) {
e.reset();
}
}
/*noFill();
stroke(0, 255, 255);
point(tractor.x, tractor.y);
ellipse(tractor.x, tractor.y, tractorRadius * 2, tractorRadius * 2);*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment