Skip to content

Instantly share code, notes, and snippets.

@benjavides
Last active April 23, 2016 04:28
Show Gist options
  • Select an option

  • Save benjavides/b37fcf6638b557775e40dac8446520b2 to your computer and use it in GitHub Desktop.

Select an option

Save benjavides/b37fcf6638b557775e40dac8446520b2 to your computer and use it in GitHub Desktop.
Simulación física de lluvia en P5JS
"use strict"
var system;
var line_system;
var gravity;
var wind;
function setup() {
createCanvas(720, 900);
system = new ParticleSystem(createVector(width/2, -40));
line_system = new LineSystem();
gravity = createVector(0, 0.05);
wind = createVector(random(-0.05,0.05), 0);
}
function draw() {
background(51);
system.addParticle(1);
system.run();
line_system.run();
drawControls();
//print(system.particles.length);
}
class Particle {
constructor(position, velocity,splash){
this.acceleration = createVector(0, 0);
this.velocity = velocity;
this.position = position.copy();
this.lifespan = 255.0;
this.mass = random(1.,3.);
this.splash = splash;
this.dead = false;
};
run(){
this.update();
this.display();
};
update(){
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.lifespan -= 2;
};
applyForce(force){
let f = p5.Vector.div(force, this.mass);
this.acceleration.add(f);
};
kill(){
this.dead =true;
}
display(){
stroke(200, this.lifespan);
strokeWeight(this.mass);
line(this.position.x, this.position.y, this.position.x+this.velocity.x, this.position.y+this.velocity.y);
};
// Determina si está fuera de pantalla
isDead(){
if (this.position.y > height | this.dead) {
return true;
} else {
return false;
}
};
}
class ParticleSystem {
constructor(position) {
this.origin = position.copy();
this.particles = [];
};
addParticle(number) {
for (let j = 0; j<=number; j++) {
this.particles.push(new Particle(createVector(random(-width/2,1.5*width), random(this.origin.y-40,this.origin.y+40)),createVector(0, random(1,4)),false));
}
};
addSplash(number,position,velocity){
for (let j = 0; j<=number; j++) {
let s = new Particle(position, createVector(velocity.x*0.4, velocity.y*0.4),true);
s.mass = 1;
this.particles.push(s);
}
}
run() {
for (var i = this.particles.length-1; i >= 0; i--) {
var p = this.particles[i];
p.applyForce(gravity);
p.applyForce(wind);
p.run();
if (p.isDead()) {
this.particles.splice(i, 1);
}
}
};
}
class Line {
constructor() {
this.origin = createVector(mouseX, mouseY);
this.end = null;
this.ready = false;
line_system.lines.push(this);
print("nueva linea");
};
ending(){
this.end = createVector(mouseX, mouseY);
print("linea terminada");
}
display(){
if (this.end!=null){
stroke("white");
strokeWeight(3);
line(this.origin.x,this.origin.y,this.end.x,this.end.y);
this.ready = true;
}
else if (!this.ready) {
stroke("black");
strokeWeight(1);
line(this.origin.x,this.origin.y,mouseX,mouseY);
}
}
}
class LineSystem {
constructor(){
this.lines = [];
this.f_coeff = 1.5;
}
run(){
this.display();
this.collitions();
}
addLine(){
let l = new Line();
}
endLine(){
line_system.lines[this.lines.length-1].ending();
}
deleteAll(){
this.lines.splice(0, this.lines.length);
}
display(){
for (var i = 0; i<this.lines.length; i++) {
this.lines[i].display();
}
}
collitions(){
for (let i = 0; i<this.lines.length; i++) {
for (let j = 0; j<system.particles.length; j++) {
if (system.particles[j].splash==true){
system.particles[j].kill();
}
else if(this.lines[i].ready && collideLineLine(this.lines[i].origin.x,this.lines[i].origin.y,this.lines[i].end.x,this.lines[i].end.y,system.particles[j].position.x,system.particles[j].position.y,system.particles[j].position.x+system.particles[j].velocity.x,system.particles[j].position.y+system.particles[j].velocity.y)){
let friction = system.particles[j].velocity.mult(-1).normalize().mult(this.f_coeff);
system.particles[j].applyForce(friction);
//system.particles[j].velocity = p5.Vector.mult(system.particles[j].velocity, -0.5);
let splashes = random(1,3);
//system.particles[j].mass *= 1/splashes; //quita masa a la gota original
system.addSplash(splashes,system.particles[j].position,system.particles[j].velocity); //crea pequeñas gotas que salpican
}
}
}
}
}
function mousePressed() {
if (mouseButton == LEFT){
line_system.addLine();
}
}
function mouseReleased() {
if (mouseButton == LEFT){
line_system.endLine();
}
else if (mouseButton == RIGHT){
line_system.deleteAll();
}
}
function keyPressed(){
if (keyCode== CONTROL){
line_system.deleteAll();
}
}
function drawControls(){
let controls="RIGHT CLICK->Draw line CTRL ->Erase all";
textSize(11);
fill("white");
noStroke();
text(controls,20,880);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment