Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

寿司が落ちてくるやつ

More than 5 years have passed since last update.
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();
  }
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away