static void test_tostring() { test_name("ToString"); Vec3d v3d = new Vec3d(0.1, 0.2, 0.3); //string s = v3d.ToString(); /**/ put("ToString", "(0.1, 0.2, 0.3)", v3d.ToString()); /**/ System.Console.WriteLine("ToString: implicit ToString call : " + v3d); PHSceneDesc descScene = new PHSceneDesc(); PHSolidDesc descSolid = new PHSolidDesc(); PHSdkIf phSdk = PHSdkIf.CreateSdk(); PHSceneIf phScene = phSdk.CreateScene(descScene); PHSolidIf phSolid = phScene.CreateSolid(descSolid); phSolid.SetPose(new Posed(1, 0, 0, 0, 0, 2, 0)); put_title("ToString: phSolid"); put_indent(2, phSolid.ToString()); FWWinBaseDesc descWinBase = new FWWinBaseDesc(); FWSdkIf fwSdk = FWSdkIf.CreateSdk(); put_title("ToString: fwSdk.ToString"); put_indent(2, fwSdk.ToString()); }
// Springhead剛体とGameObjectの間での位置姿勢の同期: 更新順を制御するためPHSceneからまとめて呼び出す public void UpdatePose() { if (sprObject != null) { PHSolidIf so = sprObject as PHSolidIf; if (fixedSolid) { // Fixedな剛体はHandleの位置をSpringheadに反映 so.SetPose(new Posed(fixedSolidPosition.ToVec3d(), fixedSolidRotation.ToQuaterniond())); gameObject.transform.position = fixedSolidPosition; gameObject.transform.rotation = fixedSolidRotation; } else { // Fixedでない剛体の場合 if (!so.IsDynamical()) { // Dynamicalでない剛体はUnityの位置をSpringheadに反映(操作可能) so.SetPose(gameObject.transform.ToPosed()); } else { // Dynamicalな剛体はSpringheadのシミュレーション結果をUnityに反映 gameObject.transform.FromPosed(so.GetPose()); } fixedSolidPosition = gameObject.transform.position; fixedSolidRotation = gameObject.transform.rotation; } } }
// -- 全てのBuildが完了した後に行う処理を書く。オブジェクト同士をリンクするなど public override void Link() { if (disableCollision) { PHSolidIf soSock = socket.GetComponent <PHSolidBehaviour>().sprObject as PHSolidIf; PHSolidIf soPlug = plug.GetComponent <PHSolidBehaviour>().sprObject as PHSolidIf; phScene.SetContactMode(soSock, soPlug, PHSceneDesc.ContactMode.MODE_NONE); } }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // SprBehaviourの派生クラスで実装するメソッド // -- Sprオブジェクトの構築を行う public override ObjectIf Build() { if (!socket) { socket = gameObject.transform.parent.GetComponentInParent <PHSolidBehaviour>().gameObject; } if (!plug) { plug = gameObject.GetComponentInParent <PHSolidBehaviour>().gameObject; } if (socket == null) { throw new ObjectNotFoundException("Socket object did not found for Joint", gameObject); } if (plug == null) { throw new ObjectNotFoundException("Plug object did not found for Joint", gameObject); } PHSolidIf soSock = socket.GetComponent <PHSolidBehaviour>().sprObject as PHSolidIf; PHSolidIf soPlug = plug.GetComponent <PHSolidBehaviour>().sprObject as PHSolidIf; PHJointIf jo = CreateJoint(soSock, soPlug); jo.SetName("jo:" + gameObject.name); if (autoSetSockPlugPose) { // priority jointObject > jointPosition/Orientation > gameObject Posed jointPose = new Posed(); if (jointObject == null) { jointObject = gameObject; jointPose = jointObject.transform.ToPosed() * new Posed(jointPosition.ToVec3d(), jointOrientation.ToQuaterniond()); } else { jointPose = jointObject.transform.ToPosed(); } jo.SetSocketPose(soSock.GetPose().Inv() * jointPose); jo.SetPlugPose(soPlug.GetPose().Inv() * jointPose); } return(jo); }
// Springhead剛体とGameObjectの間での位置姿勢の同期: 更新順を制御するためPHSceneからまとめて呼び出す public void UpdatePose() { if (sprObject != null) { PHSolidIf so = sprObject as PHSolidIf; if (!so.IsDynamical() && !fixedSolid) { // Dynamicalでない(かつ、fixedでもない)剛体はUnityの位置をSpringheadに反映(操作可能) so.SetPose(gameObject.transform.ToPosed()); } else { // Dynamicalな(もしくはfixedな)剛体はSpringheadのシミュレーション結果をUnityに反映 gameObject.transform.FromPosed(so.GetPose()); } } }
// -- Sprオブジェクトの構築を行う public override ObjectIf Build() { PHSolidIf so = phScene.CreateSolid(desc); so.SetName("so:" + gameObject.name); so.SetPose(gameObject.transform.ToPosed()); // Scene Hierarchyでの深さを取得した上でPHSceneBehaviourに登録 var t = transform; while (t.parent != null) { treeDepth++; t = t.parent; } phSceneBehaviour.RegisterPHSolidBehaviour(this); UpdateCenterOfMass(); return(so); }
static void test_simulation() { test_name("physical simulation"); PHSceneDesc descScene = new PHSceneDesc(); PHSolidDesc descSolid = new PHSolidDesc(); CDBoxDesc descBox = new CDBoxDesc(); PHSdkIf phSdk = PHSdkIf.CreateSdk(); PHSceneIf phScene = phSdk.CreateScene(descScene); PHSolidIf phSolid = phScene.CreateSolid(descSolid); phSolid.AddShape(phSdk.CreateShape(CDBoxIf.GetIfInfoStatic(), descBox)); phSolid.SetPose(new Posed(1, 0, 0, 0, 0, 2, 0)); PHSolidIf phFloor = phScene.CreateSolid(descSolid); phFloor.SetDynamical(false); descBox.boxsize = new Vec3f(10, 10, 10); phFloor.AddShape(phSdk.CreateShape(CDBoxIf.GetIfInfoStatic(), descBox)); phFloor.SetPose(new Posed(1, 0, 0, 0, 0, -5, 0)); PHBallJointDesc descJoint = new PHBallJointDesc(); PHBallJointIf j = phScene.CreateJoint(phFloor, phSolid, PHBallJointIf.GetIfInfoStatic(), descJoint).Cast(); System.Console.WriteLine(j.GetName()); PHIKBallActuatorDescStruct s = new PHIKBallActuatorDesc(); put("bias", "something", s.bias); /**/ for (int i = 0; i < 200; i++) { phScene.Step(); //System.Console.WriteLine(i.ToString() + " : " + phSolid.GetPose()); System.Console.WriteLine(String.Format("{0, 3}", i) + " : " + phSolid.GetPose()); } /**/ }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // PHJointBehaviourの派生クラスで実装するメソッド // -- 関節を作成する public override PHJointIf CreateJoint(PHSolidIf soSock, PHSolidIf soPlug) { return(phScene.CreateJoint(soSock, soPlug, PHSliderJointIf.GetIfInfoStatic(), (PHSliderJointDesc)desc)); }
static void test_func_return() { test_name("function return"); int memoryLeakTest = 0; PHSceneDesc descScene = new PHSceneDesc(); PHSolidDesc descSolid = new PHSolidDesc(); if (memoryLeakTest == 1) { return; } PHSdkIf phSdk = PHSdkIf.CreateSdk(); // ここでメモリリークする if (memoryLeakTest == 2) { return; } PHSceneIf phScene = phSdk.CreateScene(descScene); descSolid.mass = 2.0; descSolid.inertia = new Matrix3d(2.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 2.0); PHSolidIf phSolid = phScene.CreateSolid(descSolid); descSolid.mass = 1e20f; descSolid.inertia = new Matrix3d(1e20f, 0.0, 0.0, 0.0, 1e20f, 0.0, 0.0, 0.0, 1e20f); PHSolidIf solid1 = phScene.CreateSolid(descSolid); PHHapticPointerDesc descHaptic = new PHHapticPointerDesc(); PHHapticPointerIf phHaptic = phScene.CreateHapticPointer(); //HISdkDesc descHi = new HISdkDesc(); //HISdkIf hiSdk = HISdkIf.CreateSdk(); put("ret int ", "3 ", phScene.NSolids()); for (int i = 0; i < 20; i++) { phScene.Step(); } put("ret Uint ", "20 ", phScene.GetCount()); phHaptic.SetLocalRange(2.345f); put("ret float ", "2.345", phHaptic.GetLocalRange()); put("ret double", "0.005", phScene.GetTimeStep()); phScene.SetMaxVelocity(1.23); put("ret double", "0.123", phScene.GetMaxVelocity()); phScene.EnableContactDetection(false); put("ret bool ", "False", phScene.IsContactDetectionEnabled()); phScene.EnableContactDetection(true); put("ret bool ", "True ", phScene.IsContactDetectionEnabled()); put("ret size_t", "152? ", phScene.GetDescSize()); put("ret Vec3d ", "(0.0, -9.8, 0.0)", phScene.GetGravity()); phScene.SetGravity(new Vec3d(0.1, -9.9, 0.2)); put("ret Vec3d ", "(0.1, -9.9, 0.2)", phScene.GetGravity()); // function returns array by using pointer CDConvexMeshDesc descMesh = new CDConvexMeshDesc(); for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { for (int z = 0; z < 10; z++) { Vec3f v3f = new Vec3f(x, y, z); descMesh.vertices.push_back(new Vec3f(x, y, z)); } } } //PHSdkIf phSdk = PHSdkIf.CreateSdk(); CDConvexMeshIf mesh = phSdk.CreateShape(CDConvexMeshIf.GetIfInfoStatic(), descMesh) as CDConvexMeshIf; mesh.GetVertices(); arraywrapper_Vec3f vertices = mesh.GetVertices(); Vec3f[] v3fr = new Vec3f[8]; v3fr[0] = new Vec3f(0, 0, 0); v3fr[1] = new Vec3f(0, 0, 9); v3fr[2] = new Vec3f(0, 9, 0); v3fr[3] = new Vec3f(0, 9, 9); v3fr[4] = new Vec3f(9, 0, 0); v3fr[5] = new Vec3f(9, 0, 9); v3fr[6] = new Vec3f(9, 9, 0); v3fr[7] = new Vec3f(9, 9, 9); for (int i = 0; i < 8; i++) { put("ret_array", edit_vector(v3fr[i]), vertices[i]); } }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // 派生クラスで実装するメソッド // -- 関節を作成する public abstract PHJointIf CreateJoint(PHSolidIf soSock, PHSolidIf soPlug);