ãThe Nature of Codeãããè¡çªã¤ãã³ããå¥ã¯ã©ã¹ã«åãåãããã¨ã«ã¤ãã¦åãä¸ãã¾ããä»ã¾ã§ã¯ãå®è¡ç¨ã®ã¯ã©ã¹ã«è¡çªã¤ãã³ãã®å¦çãæ¸ãã¦ãã¾ãããããã§ã¯ãè¡çªã¤ãã³ããå¥ã¯ã©ã¹ã«åãåãã¦ããã¾ããProcessingã§ããã°ã©ã ãæ¸ãã¦ãåä½ã確èªãã¾ããåä½ã確èªã§ããã¨ãããProcessingã®æ¥½ããã¨ããã§ãã
è¡çªã¤ãã³ããå¥ã¯ã©ã¹ã«åãåãã
CustomListenerã¯ã©ã¹ã«è¡çªã¤ãã³ããåãåãã¾ãã以ä¸ã¯ããã®åèä¾ã§ãã
//ãªãã¸ã§ã¯ããã³ã³ããã¼ã«ããåºæ¬ä¾ //ã©ã®ãªãã¸ã§ã¯ããããããããç¥ãæ¹æ³ 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; Box box; ArrayList<Particle> particles; Spring spring; //ãã¼ãªã³ãã¤ãºã®å¤ float xoff = 0; float yoff = 1000; void setup(){ size(400, 300); box2d = new Box2DProcessing(this); box2d.createWorld(); //è¡çªããªãã¹ã³ãããªã¹ãã¼ box2d.world.setContactListener(new CustomListener()); box = new Box(width/2, height/2); spring = new Spring(); spring.bind(width/2, height/2, box); particles = new ArrayList<Particle>(); } void draw(){ background(255); box2d.step(); //ãã¼ãã£ã¯ã«ã®çæã»è¿½å if(random(1) < 0.2){ float sz = random(4, 8); particles.add(new Particle(width/2, -20, sz)); } //ãã¼ãªã³ãã¤ãºãã座æ¨ãä½ã float x = noise(xoff)*width; float y = noise(yoff)*height; xoff += 0.01; yoff += 0.01; if(mousePressed){ spring.update(mouseX, mouseY); spring.display(); }else{ spring.update(x, y); } //ãã¼ãã£ã¯ã«ã«å¯¾ããå¦ç for(int i = particles.size()-1; i >= 0; i--){ Particle p = particles.get(i); p.display(); if(p.done()){ particles.remove(i); } } box.display(); }
class Box{ Body body; float w; float h; //ã³ã³ã¹ãã©ã¯ã¿ Box(float x_, float y_){ float x = x_; float y = y_; w = 24; h = 24; makeBody(new Vec2(x, y), w, h); body.setUserData(this); } //å³å½¢ã®æç» 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(); } void makeBody(Vec2 center, float w_, float h_){ //ããã£ã®å®ç¾© BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; 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)); } }
class Particle{ Body body; float r; color col; //ã³ã³ã¹ãã©ã¯ã¿ Particle(float x, float y, float r_){ r = r_; makeBody(x, y, r); body.setUserData(this); col = color(127); } //è¡çªãããè²ãå¤æ´ãã 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){ 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)); } }
class Spring{ MouseJoint mouseJoint; //ã³ã³ã¹ãã©ã¯ã¿ Spring(){ mouseJoint = null; } //座æ¨ã®æ´æ° void update(float x, float y){ if(mouseJoint != null){ Vec2 mouseWorld = box2d.coordPixelsToWorld(x, y); mouseJoint.setTarget(mouseWorld); } } //å³å½¢ã®æç» void display(){ if(mouseJoint != null){ //Vec2 v1 = null; Vec2 v1 = new Vec2(0, 0); mouseJoint.getAnchorA(v1); //Vec2 v2 = null; Vec2 v2 = new Vec2(0, 0); mouseJoint.getAnchorB(v2); v1 = box2d.coordWorldToPixels(v1); v2 = box2d.coordWorldToPixels(v2); stroke(0); strokeWeight(1); line(v1.x, v1.y, v2.x, v2.y); } } //ç»é¢ã®åº§æ¨ã¨Boxãªãã¸ã§ã¯ãã®ç´ä»ã void bind(float x, float y, Box box){ //ãã¦ã¹ã¸ã§ã¤ã³ãã®å®ç¾© MouseJointDef md = new MouseJointDef(); md.bodyA = box2d.getGroundBody(); md.bodyB = box.body; Vec2 mp = box2d.coordPixelsToWorld(x, y); md.target.set(mp); md.maxForce = 1000.0f * box.body.m_mass; md.frequencyHz = 5.0f; md.dampingRatio = 0.9f; mouseJoint = (MouseJoint)box2d.world.createJoint(md); } //ãã¦ã¹ã¸ã§ã¤ã³ããåãé¤ã void destroy(){ if(mouseJoint != null){ box2d.world.destroyJoint(mouseJoint); mouseJoint = null; } } }
import org.jbox2d.callbacks.ContactImpulse; import org.jbox2d.callbacks.ContactListener; import org.jbox2d.collision.Manifold; import org.jbox2d.dynamics.contacts.Contact; class CustomListener implements ContactListener{ //ã³ã³ã¹ãã©ã¯ã¿ CustomListener(){ } //è¡çªããã¨ãã®å¦ç 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() == Box.class){ Particle p = (Particle)o2; p.change(); }else if(o2.getClass() == Box.class){ Particle p = (Particle)o1; p.change(); } } void endContact(Contact contact){ } void preSolve(Contact contact, Manifold oldManifold){ } void postSolve(Contact contact, ContactImpulse impulse){ } }
ã¾ã¨ã
ãThe Nature of Codeãããè¡çªã¤ãã³ããå¥ã¯ã©ã¹ã«åãåãããã¨ã«ã¤ãã¦åãä¸ãã¾ãããå¼ãç¶ãããThe Nature of Codeãã®å 容ãåå¼·ãã¾ãã
åèæ¸ç±
- Nature of Code -Processingã§ã¯ãããèªç¶ç¾è±¡ã®ã·ãã¥ã¬ã¼ã·ã§ã³-
- The Nature of Code (English Edition)
â»Javaã®åå¼·ã«ããªãã®ã§ä¸ç³äºé³¥ã§ãã