[ Video ]
[ About ]
Processing勉強会 #32 課題「記号と落下」
Symbol and falling.
[ 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 |
import shiffman.box2d.*; import org.jbox2d.collision.shapes.*; import org.jbox2d.common.*; import org.jbox2d.dynamics.*; Box2DProcessing box2d; ArrayList<Boundary> boundalyList; ArrayList<Character> characterList; ArrayList<String> mojiList; void setup() { size(720, 720); PFont font = createFont("MS Pゴシック", 30, true); textFont(font); box2d = new Box2DProcessing(this); box2d.createWorld(); box2d.setGravity(0, -25); boundalyList = new ArrayList<Boundary>(); characterList = new ArrayList<Character>(); mojiList = new ArrayList<String>(); boundalyList.add(new Boundary(width * 0.5, height - 50, 250, 25)); mojiList.add("(*•ω•*)"); mojiList.add("( ´;ω;` )"); mojiList.add("ヾ(*´∀`*)ノ"); mojiList.add("ヾ(*ΦωΦ)ノ"); mojiList.add("( • ̀ω•́ )ゞ"); } void draw() { background(239); this.box2d.step(); if (random(1) < 0.05) { Character character = new Character(random(width * 0.5 - 50, width * 0.5 + 50), 100); characterList.add(character); } for (Boundary boundary : boundalyList) { boundary.display(); } for (Character character : characterList) { character.display(); } for (int i = characterList.size() - 1; i >= 0; i -= 1) { if (characterList.get(i).death()) { characterList.remove(i); } } } |
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 |
class Boundary { Body body; float x, y, w, h; Boundary(float x, float y, float w, float h) { this.x = x; this.y = y; this.w = w; this.h = h; BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.STATIC; bodyDef.position.set(box2d.coordPixelsToWorld(this.x, this.y)); body = box2d.createBody(bodyDef); PolygonShape polygonShape = new PolygonShape(); float box2dWidth = box2d.scalarPixelsToWorld(this.w * 0.5); float box2dHeight = box2d.scalarPixelsToWorld(this.h * 0.5); polygonShape.setAsBox(box2dWidth, box2dHeight); body.createFixture(polygonShape, 1); } void display() { fill(39); stroke(239); rectMode(CENTER); rect(this.x, this.y, this.w, this.h); } } |
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 |
class Character { Body body; int mojiIndex = 0; Character(float x, float y) { this.mojiIndex = (int)random(mojiList.size()); BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DYNAMIC; bodyDef.position = box2d.coordPixelsToWorld(x, y); this.body = box2d.world.createBody(bodyDef); PolygonShape polygonShape = new PolygonShape(); polygonShape.setAsBox(box2d.scalarPixelsToWorld(textWidth(mojiList.get(this.mojiIndex)) * 0.5), box2d.scalarPixelsToWorld(16)); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = polygonShape; fixtureDef.density = 1; fixtureDef.friction = 0.3; fixtureDef.restitution = 0.5; this.body.createFixture(fixtureDef); } void display() { Vec2 position = box2d.getBodyPixelCoord(this.body); float angle = this.body.getAngle(); pushMatrix(); translate(position.x, position.y); rotate(-angle); fill(39); textAlign(CENTER, CENTER); text(mojiList.get(this.mojiIndex), 0, 0); popMatrix(); } Boolean death() { return box2d.getBodyPixelCoord(this.body).y > height + 30; } } |