今天目標: 碰撞偵測
0. 先把上課要用的環境設定好
0.1. LeapMotion裝起來 (Software, Leap Developer Kit)
0.2. Processing 2.1.1 新版裝起來
0.3. 用新版 Processing 來 Import Library 安裝三個library (search: colli
0.3.0. Leap Motion for Processing
0.3.1. bRigid (它另外需要 PeasyCam)
0.3.2. ComputationalGeometry
0.3.3. SuperCollider
1. 試著開啟 Examples 裡的 Leap Motion for Processing 的 e1_basic, 將很多行的程式碼,簡化成3個重點
1.1. import 並宣告
import de.voidplus.leapmotion.*;
LeapMotion lea;
1.2. 在 setup()裡面 new 好它
leap = new LeapMotion(this);
1.3. 在 draw()裡面用它
for( Hand hand : leap.getHands() ){
hand.draw();
}
import de.voidplus.leapmotion.*;
LeapMotion leap;
void setup(){
size(800, 500, P3D);
leap = new LeapMotion(this);
}
void draw(){
background(255);
for(Hand hand : leap.getHands()){
hand.draw();
}
}
2. 試著跑 bRigid 的範例 terrain
3. 試著將 Leap Motion 結合到 Terrain 範例中
3.1. 與 1.1相同, 先 import並宣告
3.2. 與 1.2相同, 在 setup()裡 new 它
3.3. 與 1.3相同, 在 draw()裡面用它
for( Hand hand : leap.getHands() ){
PVector p = hand.getPosition();
if(frameCount %2 ==0 ){
//Vector3f pos = new Vector3f(random(-90,90), -30, random(-90,90));
Vector3f pos = new Vector3f( p.x/10 -90, -30, p.z);
}
}
// This library allows the interaction of rigid bodies in 3D. Geometry/ Shapes are build with Processing PShape Class, for convinient display and export (c) 2013 Daniel Köhler, daniel@lab-eds.org
//here: build a TriangleMesh and use it as "Terrain"
import javax.vecmath.Vector3f;
import processing.core.PApplet;
import peasy.*;
import bRigid.*;
import de.voidplus.leapmotion.*;
PeasyCam cam;
BPhysics physics;
BObject rigid;
BTerrain terrain;
LeapMotion leap;
public void setup() {
size(1280, 720, P3D);
frameRate(60);
cam = new PeasyCam(this, 200);
cam.pan(0, 50);
cam.rotateX(.4f);
physics = new BPhysics();
physics.world.setGravity(new Vector3f(0, 40, 0));
float height = 1.1f;
//BTerrain(PApplet p, int tesselation, float height, int seed, Vector3f position, Vector3f scale)
terrain = new BTerrain(this, 110, height, 10, new Vector3f(), new Vector3f(2, 2, 2));
physics.addBody(terrain);
//create the first rigidBody as Sphere
rigid = new BBox(this, 5, 1.0f, 10.0f, 1.0f);
leap = new LeapMotion(this);
}
public void draw() {
background(255);
lights();
rotateY(frameCount*.002f);
for(Hand hand : leap.getHands()){
if (frameCount%2==0) {
PVector p = hand.getPosition();
//Vector3f pos = new Vector3f(random(-90, 90), -30, random(-90, 90));
Vector3f pos = new Vector3f( p.x/10 -90, -30, p.z);
//reuse the rigidBody of the sphere for performance resons
BObject r = new BObject(this, 5, rigid, pos, true);
physics.addBody(r);
}
}
physics.update();
terrain.display();
for (int i =1;i<physics.rigidBodies.size();i++) {
BObject b = (BObject) physics.rigidBodies.get(i);
b.display(50, 50, 50);
}
}
4. 什麼? 有同學上課在玩 Maya 建模? 那我們把 Maya 建出來的車子也匯出來, 在 Processing 中試著畫 3D模型吧!
4.1. Processing Import Library, OBJLoader
4.2. 跑 Example, 裡面 OBJLoader 有 OBJLoader_BoundingBox 範例可以跑
4.3. 把你的模型,放在 文件\Processing\libraries\OBJLoader\examples\OBJLoader_BoundingBox\data 中, 然後範例程式裡面的 model = new OBJLoader(.... "檔名.obj" ...) 改成你的檔名即可
import processing.opengl.*;
import saito.objloader.*;
OBJModel model;
// BoundingBox is a class within OBJModel. Check docs for all it can do.
BoundingBox bbox;
void setup()
{
size(600,400,OPENGL);
model = new OBJModel(this, "cassini.obj", "relative", TRIANGLES); //TRIANGLES obj+mtl in data
model.enableDebug();
model.scale(3);
model.translateToCenter();
bbox = new BoundingBox(this, model);
}
void draw(){
background(128);
lights();
translate(width/2, height/2, 0);
//rotateX(radians(frameCount)/2);
rotateY(radians(frameCount)/2);
for(int i = -1; i < 2; i ++){
pushMatrix();
translate(0,0,i*bbox.getWHD().z);
model.draw();
popMatrix();
}
noFill();
stroke(255,0,255);
bbox.draw();
noStroke();
}
4.4. 如果覺得大小怪怪的, 也可以用 scale 來調整。其實裡面是 OpenGL繪圖, 所以各位學過 OpenGL 就可以自己試著改改看吧



沒有留言:
張貼留言