// BPSGR Sample3 // @author reco // Copyrits 2009 reco // // BPSGRライブラリを使ったサンプルプログラム1 // // 主な内容 // 物理空間に複数オブジェクトの配置 // マウスクリックに応じて物体のthrowing // 物体の動作に合わせた動的なカメラ操作 // // ソースは自由に使ってくださって結構です。 // import processing.opengl.*; import javax.media.opengl.*; import javax.media.opengl.glu.*; import javax.vecmath.Vector3f; import com.bulletphysics.dynamics.RigidBody; import com.bulletphysics.linearmath.Transform; import BPSGR.*; PFont font; int n_viewpoint = 0; BPSGR demo; RigidBody rigid; //視点5用 RigidBody rigid2 = null; //視点6用 void setup() { size(800, 600, OPENGL); font=createFont("FFScala", 32); initWorld(); } void initWorld() { PGraphicsOpenGL pgl = (PGraphicsOpenGL) g; // g may change GL gl = pgl.beginGL(); // always use the GL object returned by beginGL demo = new BPSGR(this, gl); pgl.endGL(); demo.initPhysics(); // // 中央に箱集合を設置 // demo.putBoxArray( 集合体の座標最下段中央底面の中心座標(0,0,0), 箱の数:縦横奥行(3,20,3), 箱の重さ(0.1kg), 箱の大きさ(1辺の長さ)2; demo.putBoxArray( 0, 0, 0, 3, 5, 3, 0.5 ,5); demo.putBoxArray( 0, 25, 0, 3, 5, 3, 0.4 ,3); demo.putBoxArray( 0, 40, 0, 3, 5, 3, 0.3 ,2); demo.putBoxArray( 0, 50, 0, 3, 20, 3, 0.2 ,2); // 中央箱集合の頭上に空間に箱を1個設置(重力で下に落ちます) // demo.AddBox(箱の重さ 25kg, 箱の置く位置(0,150,0) 回転ベクトル(このベクトルを中心に物体を回転),回転量(0), 箱の3辺の長さ); rigid = demo.AddBox(25f, new Vector3f(0, 150, 0), new Vector3f(1, 1, 0), radians(45), new Vector3f(10, 10, 10)); } float[] pms = new float[10]; int n_pms = 0; Vector3f cameraPosition = new Vector3f(0f, 100f, 150f); Vector3f cameraTargetPosition = new Vector3f(0f, 0f, 0f); // look at void draw() { background(208); GLU glu = new GLU(); float ms = millis(); if (demo.dynamicsWorld != null) { demo.dynamicsWorld.stepSimulation(ms / 1000000f); // demo.dynamicsWorld.stepSimulation(ms / 1f); } PGraphicsOpenGL pgl = (PGraphicsOpenGL) g; // g may change GL gl = pgl.beginGL(); // always use the GL object returned by beginGL BPSGR.setPerspective(gl, width, height); Transform m = new Transform();; Transform m2 = new Transform();; switch(n_viewpoint) { case 0: break; case 1: rigid.getMotionState().getWorldTransform(m); cameraTargetPosition = m.origin; break; case 2: if(rigid2 != null) { //カメラ位置は、物体(rigid2)と原点を結ぶベクトルの延長線上に置く // rigid2の位置の取得 rigid2.getMotionState().getWorldTransform(m); rigid2.getMotionState().getWorldTransform(m2); // 原点から見た物体の方向を求める Vector3f tmpvec = new Vector3f(0,0,0); tmpvec.sub(m.origin); tmpvec.normalize(); tmpvec.scale(-30f); //----------- //カメラは物体(rigid2)に向ける cameraTargetPosition = m.origin;//new Vector3f(0,0,0); //カメラ位置は物体の位置から上記で求めたベクトル分だけずらして配置 cameraPosition = m2.origin; cameraPosition.add(tmpvec); // cameraTargetPosition = m.origin;//new Vector3f(0,0,0); } break; } glu.gluLookAt(cameraPosition.x,cameraPosition.y,cameraPosition.z , cameraTargetPosition.x, cameraTargetPosition.y, cameraTargetPosition.z , 0, 1, 0); gl.glPushMatrix(); demo.render(); gl.glPopMatrix(); pgl.endGL(); //--------------display fps --------- // println(millis()); textFont(font,30.0); stroke(1,0,0); fill(200,0,0); int i; if(n_pms==10) { for(i=0; i