ãThe Nature of Codeããã風ã§å¼ãå¯ããããåã«ã¤ãã¦åãä¸ãã¾ããç©ä½ã«é¢¨ã®åãé©ç¨ãã¾ããProcessingã§ããã°ã©ã ãæ¸ãã¦ãåä½ã確èªãã¾ããåä½ã確èªã§ããã¨ãããProcessingã®æ¥½ããã¨ããã§ãã
風ã§å¼ãå¯ããããå
Box2Dã®applyForce()ã¡ã½ãããå©ç¨ãã¦ã風ã®åãé©ç¨ãã¾ãã以ä¸ã¯ããã®åèä¾ã§ãã
//Basic example of falling rectangles //ApplyForceSimpleWind import shiffman.box2d.*; import org.jbox2d.common.*; import org.jbox2d.collision.shapes.*; import org.jbox2d.dynamics.*; Box2DProcessing box2d; ArrayList<Boundary> boundaries; ArrayList<Box> boxes; void setup(){ size(200, 200); box2d = new Box2DProcessing(this); box2d.createWorld(); box2d.setGravity(0, -20); //空ã®ArrayListãä½ã boxes = new ArrayList<Box>(); boundaries = new ArrayList<Boundary>(); boundaries = new ArrayList(); boundaries.add(new Boundary(width/4,height-5,width/2-50,10)); boundaries.add(new Boundary(3*width/4,height-5,width/2-50,10)); boundaries.add(new Boundary(width-5,height/2,10,height)); boundaries.add(new Boundary(5,height/2,10,height)); } void draw(){ background(255); box2d.step(); //ããã¯ã¹ã®è¿½å if(random(1) < 0.1){ Box p = new Box(random(width), 10); boxes.add(p); } //ãã¦ã¹ãã¯ãªãã¯ãããã¨ãã®å¦ç if(mousePressed){ for(Box b: boxes){ Vec2 wind = new Vec2(20, 0); b.applyForce(wind); } } //å¢çã®è¡¨ç¤º for(Boundary wall: boundaries){ wall.display(); } //ããã¯ã¹ã®è¡¨ç¤º for(Box b: boxes){ b.display(); } for(int i = boxes.size()-1; i >= 0; i--){ Box b = boxes.get(i); if(b.done()){ boxes.remove(i); } } fill(0); text("Click mouse to attract boxes", 20, 20); }
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); } //å³å½¢ã®æç» void display(){ fill(0); stroke(0); rectMode(CENTER); rect(x, y, w, h); } }
class Box{ Body body; float w; float h; //ã³ã³ã¹ãã©ã¯ã¿ Box(float x, float y){ w = random(8, 16); h = w; makeBody(new Vec2(x, y), w, h); } //ãªãã¸ã§ã¯ãã®åé¤ void killBody(){ box2d.destroyBody(body); } //ã¦ã£ã³ãã¦ããå¤ãããã®å¤å® boolean done(){ Vec2 pos = box2d.getBodyPixelCoord(body); if(pos.y > height+w*h){ killBody(); return true; } return false; } //風åã®é©ç¨ void applyForce(Vec2 force){ Vec2 pos = body.getWorldCenter(); body.applyForce(force, pos); } //å³å½¢ã®æç» void display(){ Vec2 pos = box2d.getBodyPixelCoord(body); float a = body.getAngle(); rectMode(CENTER); pushMatrix(); translate(pos.x, pos.y); rotate(-a); stroke(0); fill(127); //strokeWeight(2); rect(0, 0, w, h); popMatrix(); } void makeBody(Vec2 center, float w_, float h_){ //ããã£ã®å®ç¾© BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; bd.position.set(box2d.coordPixelsToWorld(center)); bd.angle = random(TWO_PI); body = box2d.createBody(bd); //ããªã´ã³ã®å®ç¾© PolygonShape sd = new PolygonShape(); float box2dW = box2d.scalarPixelsToWorld(w_/2); float box2dH = box2d.scalarPixelsToWorld(h_/2); sd.setAsBox(box2dW, box2dH); FixtureDef fd = new FixtureDef(); fd.shape = sd; fd.density = 1; fd.friction = 0.3; fd.restitution = 0.2; body.createFixture(fd); } }
ã¾ã¨ã
ãThe Nature of Codeããã風ã§å¼ãå¯ããããåã«ã¤ãã¦åãä¸ãã¾ãããç©ä½ã«é¢¨ã®åãé©ç¨ãã¾ãããå¼ãç¶ãããThe Nature of Codeãã®å 容ãåå¼·ãã¾ãã
åèæ¸ç±
- Nature of Code -Processingã§ã¯ãããèªç¶ç¾è±¡ã®ã·ãã¥ã¬ã¼ã·ã§ã³-
- The Nature of Code (English Edition)
â»Javaã®åå¼·ã«ããªãã®ã§ä¸ç³äºé³¥ã§ãã