[ Video ]
[ About ]
Processing勉強会の課題「ランダム and 音」
Random and sound.
[ Source ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
import java.util.*; import shiffman.box2d.*; import org.jbox2d.common.*; import org.jbox2d.dynamics.joints.*; import org.jbox2d.collision.shapes.*; import org.jbox2d.collision.shapes.Shape; import org.jbox2d.common.*; import org.jbox2d.dynamics.*; import org.jbox2d.dynamics.contacts.*; import ddf.minim.*; Box2DProcessing box2d; ArrayList<Particle> particles; Obstacle[] obstacles; ArrayList<Wave> waves; Minim minim; AudioSample[] pianos; void setup() { size(720, 720); frameRate(30); colorMode(HSB); box2d = new Box2DProcessing(this); box2d.createWorld(); box2d.setGravity(0, -20); box2d.listenForCollisions(); particles = new ArrayList<Particle>(); obstacles = new Obstacle[3]; obstacles[0] = new Obstacle(width / 2, height / 2); obstacles[1] = new Obstacle(width / 6 * 1, height / 6 * 4); obstacles[2] = new Obstacle(width / 6 * 5, height / 6 * 4); minim = new Minim(this); pianos = new AudioSample[8]; pianos[0] = minim.loadSample("pianoA.mp3"); pianos[1] = minim.loadSample("pianoB.mp3"); pianos[2] = minim.loadSample("pianoC.mp3"); pianos[3] = minim.loadSample("pianoC2.mp3"); pianos[4] = minim.loadSample("pianoD.mp3"); pianos[5] = minim.loadSample("pianoE.mp3"); pianos[6] = minim.loadSample("pianoF.mp3"); pianos[7] = minim.loadSample("pianoG.mp3"); } void draw() { box2d.step(); background(239); for(Obstacle obs : obstacles) { obs.display(); } if(frameCount % 60 == 1) { Particle p = new Particle(random(width * 0.2, width * 0.8), 50, 25); particles.add(p); } Iterator<Particle> it_particles = particles.iterator(); while(it_particles.hasNext()) { Particle p = it_particles.next(); p.display(); if(p.isDead()) { it_particles.remove(); } } Iterator<Wave> it_waves = waves.iterator(); while(it_waves.hasNext()) { Wave w = it_waves.next(); w.run(); if(w.isDead()) { it_waves.remove(); } } } void stop() { minim.stop(); for(int i = 0; i < pianos.length; i++){ pianos[i].close(); } } void beginContact(Contact cp) { Fixture f1 = cp.getFixtureA(); Fixture f2 = cp.getFixtureB(); Body b1 = f1.getBody(); Body b2 = f2.getBody(); Object o1 = b1.getUserData(); Object o2 = b2.getUserData(); if(o1.getClass() == Particle.class) { Particle p = (Particle)o1; p.lifespan -= 30; } if(o2.getClass() == Particle.class) { Particle p = (Particle)o2; p.lifespan -= 30; } if(o1.getClass() == Obstacle.class) { Obstacle o = (Obstacle)o1; waves.add(new Wave(o.x, o.y, o.size)); pianos[int(random(8))].trigger(); return; } else if(o2.getClass() == Obstacle.class) { Obstacle o = (Obstacle)o2; waves.add(new Wave(o.x, o.y, o.size)); pianos[int(random(8))].trigger(); return; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
class Obstacle { Body body; float x, y, size; Obstacle(float x_, float y_) { x = x_; y = y_; size = 150; makeBody(new Vec2(x, y)); waves = new ArrayList<Wave>(); } void makeBody(Vec2 center) { CircleShape cs = new CircleShape(); float box2dSize = box2d.scalarPixelsToWorld(size / 2); cs.setRadius(box2dSize); BodyDef bd = new BodyDef(); bd.type = BodyType.STATIC; bd.position.set(box2d.coordPixelsToWorld(center)); body = box2d.createBody(bd); body.createFixture(cs, 1); body.setUserData(this); } void display() { Vec2 pos = box2d.getBodyPixelCoord(body); pushMatrix(); translate(pos.x, pos.y); fill(39); noStroke(); ellipse(0, 0, size, size); popMatrix(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
class Particle { Body body; float size; float lifespan; boolean countDown; Particle(float x, float y, float s) { size = s; lifespan = 255; makeBody(new Vec2(x, y)); } void makeBody(Vec2 center) { CircleShape cs = new CircleShape(); float box2dSize = box2d.scalarPixelsToWorld(size); cs.setRadius(box2dSize / 2); FixtureDef fd = new FixtureDef(); fd.shape = cs; fd.density = 1; fd.friction = 1; fd.restitution = 0.5; BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; bd.position.set(box2d.coordPixelsToWorld(center)); body = box2d.createBody(bd); body.createFixture(fd); body.setUserData(this); } boolean isDead() { if(lifespan < 0){ box2d.destroyBody(body); return true; }else{ return false; } } void display() { Vec2 pos = box2d.getBodyPixelCoord(body); pushMatrix(); translate(pos.x, pos.y); fill(39, lifespan); stroke(39); strokeWeight(2); ellipse(0, 0, size, size); popMatrix(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
class Wave { PVector center; float radius; float lifespan; Wave(float x, float y, float r) { center = new PVector(x, y); radius = r; lifespan = 125; } void run() { update(); display(); } void update() { radius += 3; lifespan -= 1; } void display() { pushMatrix(); translate(center.x, center.y); noFill(); stroke(39, lifespan); strokeWeight(3); ellipse(0, 0, radius, radius); popMatrix(); } boolean isDead() { if(lifespan < 0) { return true; } return false; } } |