ãThe Nature of Codeãããããããã£ãã¯ããã£ã«ã¤ãã¦åãä¸ãã¾ããããã£ã®ã¿ã¤ããããããã£ãã¯ã«ãã¦ãç©ä½ãå¼ã£å¼µã£ã¦ã¿ã¾ããProcessingã§ããã°ã©ã ãæ¸ãã¦ãåä½ã確èªãã¾ããåä½ã確èªã§ããã¨ãããProcessingã®æ¥½ããã¨ããã§ãã
ããããã£ãã¯ããã£
ãã¦ã¹ã¸ã§ã¤ã³ãã使ã£ã¦ç©ä½ããã©ãã°ãã¦ãã¾ããããããã£ã®ã¿ã¤ããããããã£ãã¯ã«ãããã¨ã§ãå®ç¾ã§ãã¾ãã
以ä¸ã¯ããã®åèä¾ã§ãã
注æã¨ãã¦ããããã£ãã¯ããã£ã¯ãããããã£ãã¯ã¾ãã¯ã¹ã¿ãã£ãã¯ãªããã£ã¨è¡çªãã¾ãããè¡çªããããå ´åã¯ããã¦ã¹ã¸ã§ã¤ã³ããé©ãã¦ãã¾ãã
//Basic example of controlling an object with the mouse (by attaching a spring) 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.*; Box2DProcessing box2d; ArrayList<Boundary> boundaries; Box box; void setup(){ size(640, 360); box2d = new Box2DProcessing(this); box2d.createWorld(); box = new Box(width/2, height/2); //å¢ç boundaries = new ArrayList(); boundaries.add(new Boundary(width/2, height-5, width, 10, 0)); boundaries.add(new Boundary(width/2, 5, width, 10, 0)); boundaries.add(new Boundary(width-5, height/2, 10, height, 0)); boundaries.add(new Boundary(5, height/2, 10, height, 0)); } void draw(){ background(255); box2d.step(); if(box.dragged){ box.setPosition(mouseX, mouseY); } for(Boundary wall: boundaries){ wall.display(); } box.display(); } //ãã¦ã¹ãæ¼ããã¨ãã®å¦ç void mousePressed(){ if(box.contains(mouseX, mouseY)){ box.dragged = true; } } //ãã¦ã¹ãé¢ããã¨ãã®å¦ç void mouseReleased(){ box.dragged = false; }
class Boundary{ float x; float y; float w; float h; Body body; //ã³ã³ã¹ãã©ã¯ã¿ Boundary(float x_, float y_, float w_, float h_, float a){ x = x_; y = y_; w = w_; h = h_; //ããã£ã®å®ç¾© BodyDef bd = new BodyDef(); bd.type = BodyType.STATIC; bd.angle = a; bd.position.set(box2d.coordPixelsToWorld(x, y)); body = box2d.createBody(bd); //ã·ã§ã¤ãã®å®ç¾© PolygonShape sd = new PolygonShape(); float box2dW = box2d.scalarPixelsToWorld(w/2); float box2dH = box2d.scalarPixelsToWorld(h/2); sd.setAsBox(box2dW, box2dH); body.createFixture(sd,1); } //å³å½¢ã®æç» void display(){ noFill(); stroke(0); strokeWeight(1); rectMode(CENTER); float a = body.getAngle(); pushMatrix(); translate(x, y); rotate(-a); rect(0, 0, w, h); popMatrix(); } }
class Box{ Body body; float w; float h; boolean dragged = false; //ã³ã³ã¹ãã©ã¯ã¿ Box(float x_, float y_){ float x = x_; float y = y_; w = 24; h = 24; makeBody(new Vec2(x, y), w, h); } //ãã¦ã¹åº§æ¨ããªãã¸ã§ã¯ãå ã«ãããã®å¤å® boolean contains(float x, float y){ Vec2 worldPoint = box2d.coordPixelsToWorld(x, y); Fixture f = body.getFixtureList(); boolean inside = f.testPoint(worldPoint); return inside; } void setAngularVelocity(float a){ body.setAngularVelocity(a); } void setVelocity(Vec2 v){ body.setLinearVelocity(v); } void setPosition(float x, float y){ Vec2 pos = body.getWorldCenter(); Vec2 target = box2d.coordPixelsToWorld(x, y); Vec2 diff = target.sub(pos); diff.mulLocal(50); setVelocity(diff); setAngularVelocity(0); } //å³å½¢ã®æç» void display(){ Vec2 pos = box2d.getBodyPixelCoord(body); float a = body.getAngle(); rectMode(PConstants.CENTER); pushMatrix(); translate(pos.x, pos.y); rotate(-a); fill(175); stroke(0); rect(0, 0, w, h); popMatrix(); } //Box2Dã®è¨å® void makeBody(Vec2 center, float w_, float h_){ //ããã£ã®å®ç¾© BodyDef bd = new BodyDef(); bd.type = BodyType.KINEMATIC; //ããããã£ã㯠bd.position.set(box2d.coordPixelsToWorld(center)); 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.5; body.createFixture(fd); body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); body.setAngularVelocity(random(-5, 5)); } }
ã¾ã¨ã
ãThe Nature of Codeãããããããã£ãã¯ããã£ã«ã¤ãã¦åãä¸ãã¾ãããããã£ã®ã¿ã¤ããããããã£ãã¯ã«ãã¦ãç©ä½ãå¼ã£å¼µã£ã¦ã¿ã¾ãããå¼ãç¶ãããThe Nature of Codeãã®å 容ãåå¼·ãã¾ãã
åèæ¸ç±
- Nature of Code -Processingã§ã¯ãããèªç¶ç¾è±¡ã®ã·ãã¥ã¬ã¼ã·ã§ã³-
- The Nature of Code (English Edition)
â»Javaã®åå¼·ã«ããªãã®ã§ä¸ç³äºé³¥ã§ãã