2013年6月22日 星期六

[ Processing for Android ] fisica 2D 物理系統

import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import fisica.*; 

import java.util.*; 
import java.io.*;  

public class Pipe extends PApplet {

FWorld world;

public void setup() {
 
  smooth();

  Fisica.init(this);

  world = new FWorld();
  world.setGravity(0, -300);


  FPoly l = new FPoly();
  l.vertex(width/4, 0);
  l.vertex(0, 0);
  l.vertex(0, height);

  l.vertex(width/4, height);
  l.vertex(width/4, 7*height/10);
  l.vertex(width/2, 7*height/10);
  l.vertex(width/2, 3*height/10);
  l.vertex(width/4, 3*height/10);
  l.setStatic(true);
  l.setFill(0);
  l.setFriction(0);
  world.add(l);

  FPoly r = new FPoly();
  r.vertex(width/4+60, 0);
  r.vertex(width, 0);
  r.vertex(width, height);

  r.vertex(width/4+60, height);
  r.vertex(width/4+60, 7*height/10+60);
  r.vertex(width/2+60, 7*height/10+60);
  r.vertex(width/2+60, 3*height/10-60);
  r.vertex(width/4+60, 3*height/10-60);
  r.setStatic(true);
  r.setFill(0);
  r.setFriction(0);
  world.add(r);
}

public void draw() {
  background(80, 120, 200);

  if ((frameCount % 40) == 1) {
    FBlob b = new FBlob();
    float s = random(30, 40);
    b.setAsCircle(width/4+20, height-random(100), s, 20);
    
    b.setStroke(0);
    b.setStrokeWeight(2);
    b.setFill(255);
    b.setFriction(0);
    world.add(b);
  }

  world.step();
  world.draw();
}

  public int sketchWidth() { return 400; }
  public int sketchHeight() { return 400; }
}

沒有留言:

張貼留言