Created
January 17, 2018 16:58
-
-
Save mao-test-h/4d6abda804fb0af223d92457d413f13c to your computer and use it in GitHub Desktop.
Processingで布っぽいのを実装(Nature of Code : Chapter5参照)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Nature of Code : Chapter5 参照 | |
import toxi.physics2d.behaviors.*; | |
import toxi.physics2d.*; | |
import toxi.geom.*; | |
import toxi.math.*; | |
VerletPhysics2D physics; | |
Blanket blanket; | |
void setup() | |
{ | |
size(640, 360); | |
physics = new VerletPhysics2D(); | |
physics.addBehavior(new GravityBehavior2D(new Vec2D(0, 0.1))); | |
blanket = new Blanket(); | |
} | |
void draw() | |
{ | |
background(255); | |
physics.update(); | |
blanket.update(mouseX, mouseY); | |
blanket.display(); | |
} | |
void mousePressed() | |
{ | |
blanket.pressed(mouseX, mouseY); | |
} | |
void mouseReleased() | |
{ | |
blanket.released(); | |
} | |
// ---------------------------------------------------- | |
// 粒子部 | |
class Particle extends VerletParticle2D | |
{ | |
float radius = 8; | |
boolean dragged = false; | |
PVector offset = new PVector(); | |
Particle(Vec2D loc) | |
{ | |
super(loc); | |
} | |
void display() | |
{ | |
fill(175); | |
stroke(0); | |
ellipse(x, y, radius, radius); | |
} | |
void pressed(int mouseX_, int mouseY_) | |
{ | |
float d = dist(mouseX_, mouseY_, x, y); | |
if(d >= radius){ return; } | |
offset.x = x - mouseX_; | |
offset.y = y - mouseY_; | |
lock(); | |
dragged = true; | |
} | |
void released() | |
{ | |
if(!dragged){ return; } | |
unlock(); | |
dragged = false; | |
} | |
void update(int mouseX_, int mouseY_) | |
{ | |
if(!dragged){ return; } | |
set(mouseX_ + offset.x, mouseY_ + offset.y); | |
} | |
} | |
// ---------------------------------------------------- | |
// 接続部 | |
class Connection extends VerletSpring2D | |
{ | |
Connection(Particle p1, Particle p2, float len, float strength) | |
{ | |
super(p1, p2, len, strength); | |
} | |
void display() | |
{ | |
stroke(0); | |
line(a.x, a.y, b.x, b.y); | |
} | |
} | |
// ---------------------------------------------------- | |
// 布 | |
class Blanket | |
{ | |
ArrayList<Particle> particles; | |
ArrayList<Connection> springs; | |
Blanket() | |
{ | |
particles = new ArrayList<Particle>(); | |
springs = new ArrayList<Connection>(); | |
// 布の設定 | |
int w = 51; | |
int h = 20; | |
float len = 10; | |
float strength = 0.125; | |
for(int y = 0; y < h; y++) | |
{ | |
for(int x = 0; x < w; x++) | |
{ | |
Particle p = new Particle(new Vec2D(width/2 + x * len - w * len/2, len)); | |
physics.addParticle(p); | |
particles.add(p); | |
if(x > 0) | |
{ | |
Particle previous = particles.get(particles.size() - 2); | |
Connection c = new Connection(p, previous, len, strength); | |
physics.addSpring(c); | |
springs.add(c); | |
} | |
if(y > 0) | |
{ | |
Particle above = particles.get(particles.size() - w - 1); | |
Connection c = new Connection(p, above, len, strength); | |
physics.addSpring(c); | |
springs.add(c); | |
} | |
} | |
} | |
for(int i = 0; i < w; i += 2) | |
{ | |
Particle target = particles.get(i); | |
target.lock(); | |
} | |
} | |
void pressed(int mouseX_, int mouseY_) | |
{ | |
for(Particle p : particles) | |
{ | |
p.pressed(mouseX_, mouseY_); | |
} | |
} | |
void released() | |
{ | |
for(Particle p : particles) | |
{ | |
p.released(); | |
} | |
} | |
void update(int mouseX_, int mouseY_) | |
{ | |
for(Particle p : particles) | |
{ | |
p.update(mouseX_, mouseY_); | |
} | |
} | |
void display() | |
{ | |
for(Connection c : springs) | |
{ | |
c.display(); | |
} | |
for(Particle p : particles) | |
{ | |
// コメントアウト解除で粒子部表示(確認用) | |
//p.display(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment