ãThe Nature of Codeãããè¡çªã¤ãã³ãã¨ç©ä½ã®åé¤ã«ã¤ãã¦åãä¸ãã¾ããç©ä½ãè¡çªããããã®ç©ä½ãæ¶ãã¾ããProcessingã§ããã°ã©ã ãæ¸ãã¦ãåä½ã確èªãã¾ããåä½ã確èªã§ããã¨ãããProcessingã®æ¥½ããã¨ããã§ãã
è¡çªã¤ãã³ãã¨ç©ä½ã®åé¤
ãã¼ãã£ã¯ã«ãå°é¢ã«å½ãã£ããããã¼ãã£ã¯ã«ã®è²ãå¤æ´ãã¾ãããã¼ãã£ã¯ã«å士ãå½ãã£ãããæ¶ãã¾ãã以ä¸ã¯ããã®åèä¾ã§ãã
//CollisionListeningDeletionExercise 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.dynamics.*; import org.jbox2d.dynamics.contacts.*; Box2DProcessing box2d; ArrayList<Particle> particles; Boundary wall; void setup(){ size(200, 200); box2d = new Box2DProcessing(this); box2d.createWorld(); //collision listening box2d.listenForCollisions(); particles = new ArrayList<Particle>(); wall = new Boundary(width/2, height-5, width, 10); } void draw(){ background(255); box2d.step(); //ãã¼ãã£ã¯ã«ã®çæã»è¿½å if(random(1) < 0.1){ float sz = random(4, 8); particles.add(new Particle(random(width), 20, sz)); } //ãã¹ã¦ã®ãã¼ãã£ã¯ã«ãèµ°æ»ãã for(int i = particles.size()-1; i >= 0; i--){ Particle p = particles.get(i); p.display(); //ãã¼ãã£ã¯ã«ã®æç» if(p.done()){ particles.remove(i); //ãã¼ãã£ã¯ã«ã®åé¤ } } wall.display(); } //è¡çªã¤ãã³ãã®é¢æ° 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 && o2.getClass() == Particle.class){ Particle p1 = (Particle)o1; p1.delete(); Particle p2 = (Particle)o2; p2.delete(); } //å¢çã¨è¡çªããã¨ãã®å¦ç if(o1.getClass() == Boundary.class){ Particle p = (Particle)o2; p.change(); } //å¢çã¨è¡çªããã¨ãã®å¦ç if(o2.getClass() == Boundary.class){ Particle p = (Particle)o1; p.change(); } } void endContact(Contact cp){ }
class Boundary{ float x; float y; float w; float h; Body b; //ã³ã³ã¹ãã©ã¯ã¿ Boundary(float x_, float y_, float w_, float h_){ x = x_; y = y_; w = w_; h = h_; //ããã£ã®å®ç¾© BodyDef bd = new BodyDef(); bd.type = BodyType.STATIC; bd.position.set(box2d.coordPixelsToWorld(x, y)); b = box2d.createBody(bd); //ã·ã§ã¤ãã®å®ç¾© PolygonShape sd = new PolygonShape(); float box2dW = box2d.scalarPixelsToWorld(w/2); float box2dH = box2d.scalarPixelsToWorld(h/2); sd.setAsBox(box2dW, box2dH); b.createFixture(sd, 1); b.setUserData(this); } //å³å½¢ã®æç» void display(){ fill(0); stroke(0); rectMode(CENTER); rect(x, y, w, h); } }
class Particle{ Body body; float r; color col; boolean delete = false; //ã³ã³ã¹ãã©ã¯ã¿ Particle(float x, float y, float r_){ r = r_; makeBody(x, y, r); body.setUserData(this); col = color(127); } void delete(){ delete = true; } //è²ãå¤æ´ãã void change(){ col = color(255, 0, 0); } void killBody(){ box2d.destroyBody(body); } boolean done(){ Vec2 pos = box2d.getBodyPixelCoord(body); if(pos.y > height+r*2 || delete){ killBody(); return true; } return false; } //å³å½¢ã®æç» void display(){ Vec2 pos = box2d.getBodyPixelCoord(body); float a = body.getAngle(); pushMatrix(); translate(pos.x, pos.y); rotate(-a); fill(col); stroke(0); strokeWeight(2); ellipse(0, 0, r*2, r*2); line(0, 0, r, 0); popMatrix(); } void makeBody(float x, float y, float r){ //ããã£ã®å®ç¾© BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; bd.position = box2d.coordPixelsToWorld(x, y); body = box2d.createBody(bd); //ã·ã§ã¤ãã®å®ç¾© CircleShape cs = new CircleShape(); cs.m_radius = box2d.scalarPixelsToWorld(r); //ãã£ã¯ã¹ãã£ã®å®ç¾© FixtureDef fd = new FixtureDef(); fd.shape = cs; fd.density = 1; fd.friction = 0.01; fd.restitution = 0.3; body.createFixture(fd); body.setAngularVelocity(random(-10, 10)); } }
ã¾ã¨ã
ãThe Nature of Codeãããè¡çªã¤ãã³ãã¨ç©ä½ã®åé¤ã«ã¤ãã¦åãä¸ãã¾ãããç©ä½ãè¡çªããããã®ç©ä½ãæ¶ããã¨ãã§ãã¾ãããå¼ãç¶ãããThe Nature of Codeãã®å 容ãåå¼·ãã¾ãã
åèæ¸ç±
- Nature of Code -Processingã§ã¯ãããèªç¶ç¾è±¡ã®ã·ãã¥ã¬ã¼ã·ã§ã³-
- The Nature of Code (English Edition)
â»Javaã®åå¼·ã«ããªãã®ã§ä¸ç³äºé³¥ã§ãã