LoginSignup
15
13

More than 5 years have passed since last update.

寿司が落ちてくるやつ

Last updated at Posted at 2012-01-26
sushi.pde
import org.jbox2d.p5.*;
import org.jbox2d.common.*;
import org.jbox2d.collision.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.joints.*;
import org.jbox2d.dynamics.contacts.*;

Physics physics;
Body circle;
class UserData {
  PImage img;
}
UserData data;
PImage toro_img, engawa_img, kappa_img, anago_img;

void setup() {
  size(800, 500);
  imageMode(CENTER);
  toro_img = loadImage("sushi2.png");
  engawa_img = loadImage("engawas.png");
  kappa_img = loadImage("kappa.png");
  anago_img = loadImage("anago.png");
  frameRate(60);
  InitScene();
}
void draw() {
  background(255);
  if(frameCount < 300) {
  if (frameCount % 30 == 0) {
    createSushi(300, 50, toro_img);
    createSushi(370, 50, kappa_img);
    createSushi(450, 50, engawa_img);
  } else if (frameCount % 20 == 0) {
    createSushi(310, 50, anago_img);
  } else if (frameCount % 10 == 0) {
    createSushi(320, 50, toro_img);
    createSushi(450, 50, engawa_img);
  } else if (frameCount % 5 == 0) {
    createSushi(320, 50, toro_img);
  }
  }
  physics.setRestitution(random(0,0.9)); 
}

void mousePressed() {
  createSushi(mouseX, mouseY, toro_img);
}
void createSushi(int X, int Y, PImage img) {
  circle=physics.createCircle(X, Y,20);
  data = new UserData();
  data.img = img;
  circle.setUserData(data);
  circle.setAngularVelocity(random(-10,10));
}

void keyPressed() {
  physics.destroy();
  physics = null;
  InitScene();
}



void InitScene() {
  float gravX = 0.0;                 
  float gravY = -100.0;
  float AABBWidth = 2*width;   
  float AABBHeight = 2*height;
  float borderBoxWidth = width;      
  float borderBoxHeight = height;
  float pixelsPerMeter = 30;
  physics = new Physics(this, width, height,
                      gravX, gravY,
                  AABBWidth, AABBHeight,
                  borderBoxWidth, borderBoxHeight,
                  pixelsPerMeter);

  //physics = new Physics(this, 800, 500);

  physics.setDensity(1.0f);
  physics.setCustomRenderingMethod(this, "custom");

}

void custom (World w) {
  smooth();
  for (Body circle = physics.getWorld().getBodyList(); circle != null; circle = circle.getNext())
  {
    org.jbox2d.collision.Shape shape;
    for (shape = circle.getShapeList(); shape != null; shape = shape.getNext())
    {
      if (shape.getType() == ShapeType.CIRCLE_SHAPE)
      {
        fill(0,0,0,0);
        stroke(0,0,0,0);
        sushiDraw(circle, shape);
      }
    }
  }
}

void sushiDraw(Body body, org.jbox2d.collision.Shape shape) {
  CircleShape circle = (CircleShape) shape;
  Vec2 center = circle.getLocalPosition();
  Vec2 wpoint = physics.worldToScreen(body.getWorldPoint(center));
  float radius = physics.worldToScreen(circle.getRadius());
  ellipse(wpoint.x,wpoint.y,radius*2,radius*2);
  if(radius == 20)
  {
    pushMatrix();
    translate(wpoint.x+1,wpoint.y+1);
    //rotate(random(-10,10));
    UserData data = (UserData) body.getUserData();
    image(data.img,0,0);
    popMatrix();
  }
}
15
13
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
13