3DCG屋さんの活動記録

PROFILE ★★こんな人です

映像制作会社でクリエイティブの修行中。日ごろはMaya&AEを中心にいろんなCGソフトを触りながら、TD寄りの作業が好きで、インタラクティブな体験型空間演出をやりたいと模索中。面白いこと新しいこと大好きな『素直』でいつづける

2017年2月1日水曜日

Box2d for processing 使ってみた


なかなかProcessing の勉強が進まないので、、
とりあえず物理シミュレーションを「使ってみる!」ということで
Box2Dのサンプルを少し変えてみることから始めてみました。


BumpySurfaceNoiseを使いました。

目標
・box2dのプログラムを理解。落下、衝突、などの基礎部分の理解。
・ノイズの地面を、手描きにカスタマイズできるようになる

進捗
・box2d.step() で落下が始まる
・particles.size() でオブジェクト数が取得できる

やってみたこと
・今回は、初期状態では重力がなく、マウスのドラッッグで文字が書ける。
・g キーを押すと重力が発生して落下する。
・オブジェクト数を画面に表示する




マウスで文字書ける(オブジェクトは落下しない)


gキー押すと一気に落下開始!



ぐわー


ガラガラ!画面上のカウントも変化しています。






以上。
こんなペースで進めていこうと思います(汗)
今年中には何か具体的な作品を作りたいです!!!


=====BumpySurfaceNoise=========
// The Nature of Code
// <http://www.shiffman.net/teaching/nature>
// Spring 2010
// Box2DProcessing example

// An uneven surface

import shiffman.box2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.dynamics.*;

//dekapoppo
int grav = 0; //重力のON

// A reference to our box2d world
Box2DProcessing box2d;

// An ArrayList of particles that will fall on the surface
ArrayList<Particle> particles;

// An object to store information about the uneven surface
Surface surface;

void setup() {
  size(500,300);
  smooth();

  // Initialize box2d physics and create the world
  box2d = new Box2DProcessing(this);
  box2d.createWorld();
  // We are setting a custom gravity
  box2d.setGravity(0, -20);

  // Create the empty list
  particles = new ArrayList<Particle>();
  // Create the surface
  surface = new Surface();
}

void draw() {
  // If the mouse is pressed, we make new particles
  if (mousePressed) {
    float sz = random(2,6);
    particles.add(new Particle(mouseX,mouseY,sz));
  }
 
  //dekapoppo
  if(key == 'g'){
    if(grav == 1){
      grav = 0;
    }
     else if (grav ==0){
       grav = 1;
     }
  }
  // We must always step through time!
  if(grav  == 1){
    box2d.step();
  }

  background(255);

  // Draw the surface
  surface.display();

  // Draw all particles
  for (Particle p: particles) {
    p.display();
  }

  // Particles that leave the screen, we delete them
  // (note they have to be deleted from both the box2d world and our list
  for (int i = particles.size()-1; i >= 0; i--) {
    Particle p = particles.get(i);
    if (p.done()) {
      particles.remove(i);
    }
  }

  // Just drawing the framerate to see how many particles it can handle
  fill(0);
  text("framerate: " + (int)frameRate,12,16);
  text("count: "+(int)particles.size(),12,32); //パーティクル数のカウント
  text("press G key  GravityON",12,48); //はじめ重力なし。gキー押すと落下する。
}

0 コメント:

コメントを投稿