protected void Update() { if (!m_InitialActionProcessed) { vrPhysicsBody physicsBody = GetPhysicsBodyInSimulation(); if (physicsBody != null) { m_InitialActionProcessed = true; EnableCollisions(physicsBody, false); } } }
protected void AttachOrDetachBody(bool doAttachement) { VRPhysicsBody physicsBodyComponent = GetComponent <VRPhysicsBody>(); if (physicsBodyComponent == null) { return; } vrPhysicsBody physicsBody = null; if (MiddleVR.VRPhysicsMgr != null) { vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine(); if (physicsEngine != null) { // Prefer to find the object by its name so we won't access // a dangling pointer when the body was destroyed by MiddleVR. physicsBody = physicsEngine.GetBody(m_PhysicsBodyName); } } if (physicsBody == null) { return; } var kernel = MiddleVR.VRKernel; var physicsBodyId = physicsBody.GetId(); if (doAttachement) { // SetManipulationDevice (only attachment). var attachManipDeviceToBodyPrmsValue = vrValue.CreateList(); attachManipDeviceToBodyPrmsValue.AddListItem(m_ManipulationDeviceId); attachManipDeviceToBodyPrmsValue.AddListItem(physicsBodyId); kernel.ExecuteCommand( "Haption.IPSI.AttachManipulationDeviceToBody", attachManipDeviceToBodyPrmsValue); } }
private bool OnMVRNodeDestroy(vrEvent iBaseEvt) { vrObjectEvent e = vrObjectEvent.Cast(iBaseEvt); if (e != null) { if (e.ComesFrom(m_RigidBody)) { if (e.eventType == (int)VRObjectEventEnum.VRObjectEvent_Destroy) { // The physics rigid body was killed in MiddleVR so // stop to use it in C#. m_RigidBody = null; // And even stop to use this component. enabled = false; } } } return(true); }
protected void Start() { m_ScriptStarted = true; if (MiddleVR.VRClusterMgr.IsCluster() && !MiddleVR.VRClusterMgr.IsServer()) { enabled = false; return; } vrPhysicsBody physicsBody = GetComponent <VRPhysicsBody>().PhysicsBody; if (physicsBody == null) { MiddleVRTools.Log(0, "[X] PhysicsBodyManipulator: No PhysicsBody found in '" + name + "'."); enabled = false; return; } // Fix incoherent settings. if (physicsBody.IsStatic()) { MiddleVRTools.Log(0, "[X] PhysicsBodyManipulator: The body '" + physicsBody.GetName() + "' cannot be manipulated because it is static."); // And so we stop to use this script. enabled = false; return; } m_PhysicsBodyName = physicsBody.GetName(); m_DidFirstAttachment = false; }
protected void Start() { if (MiddleVR.VRPhysicsMgr == null) { MiddleVRTools.Log(0, "[X] ApplyForceTorqueSample: No PhysicsManager found."); enabled = false; return; } VRPhysicsBody body = GetComponent <VRPhysicsBody>(); m_RigidBody = body.PhysicsBody; if (m_RigidBody == null) { MiddleVRTools.Log(0, "[X] ApplyForceTorqueSample: No rigid body given."); enabled = false; return; } m_MVREventListener = new vrEventListener(OnMVRNodeDestroy); m_RigidBody.AddEventListener(m_MVREventListener); }
protected void Start() { if (MiddleVR.VRPhysicsMgr == null) { MiddleVRTools.Log(0, "[X] ApplyForceTorqueSample: No PhysicsManager found."); enabled = false; return; } VRPhysicsBody body = GetComponent<VRPhysicsBody>(); m_RigidBody = body.PhysicsBody; if (m_RigidBody == null) { MiddleVRTools.Log(0, "[X] ApplyForceTorqueSample: No rigid body given."); enabled = false; return; } m_MVREventListener = new vrEventListener(OnMVRNodeDestroy); m_RigidBody.AddEventListener(m_MVREventListener); }
protected void Update() { var deviceMgr = MiddleVR.VRDeviceMgr; if (deviceMgr != null && deviceMgr.IsKeyPressed(MiddleVR.VRK_H) && deviceMgr.IsKeyToggled(MiddleVR.VRK_C)) { var physicsMgr = MiddleVR.VRPhysicsMgr; if (physicsMgr == null) { MiddleVRTools.Log(0, "[X] VRChangeAttachedPhysicsBodyIPSISample: No PhysicsManager found."); enabled = false; return; } vrPhysicsEngine physicsEngine = physicsMgr.GetPhysicsEngine(); if (physicsEngine == null) { return; } if (!physicsEngine.IsStarted()) { // We have to wait... return; } uint bodiesNb = physicsEngine.GetBodiesNb(); if (bodiesNb == 0) { MiddleVRTools.Log(0, "[X] VRChangeAttachedPhysicsBodyIPSISample: No physics body found!"); return; } vrPhysicsBody physicsBody = physicsEngine.GetBody(m_PhysicsBodyId); MiddleVRTools.Log(2, "[+] VRChangeAttachedPhysicsBodyIPSISample: proposed body id: " + m_PhysicsBodyId + " ('" + (physicsBody != null ? physicsBody.GetName() : "Null") + "')."); if (physicsBody != null && physicsBody.IsA("PhysicsBodyIPSI")) { var objId = physicsBody.GetId(); // As a reminder: static or frozen physics bodies cannot be manipulated. MiddleVRTools.Log(2, "[+] VRChangeAttachedPhysicsBodyIPSISample: Is the physics body '" + objId + "' static? " + (physicsBody.IsStatic() ? "Yes" : "No") + "."); MiddleVRTools.Log(2, "[+] VRChangeAttachedPhysicsBodyIPSISample: Is the physics body '" + objId + "' frozen? " + (physicsBody.IsFrozen() ? "Yes" : "No") + "."); var kernel = MiddleVR.VRKernel; // Use of "Haption.IPSI.GetManipulationDevicesNb". // Param with one vrValue: // + None, so vrValue.NULL_VALUE can be used. // Return: // A vrValue that contains a uint. // In case of problem, the vrValue is invalid. var getManipDeviceNbRetValue = kernel.ExecuteCommand( "Haption.IPSI.GetManipulationDevicesNb", vrValue.NULL_VALUE); if (getManipDeviceNbRetValue.IsNumber()) { MiddleVRTools.Log(2, "[+] VRChangeAttachedPhysicsBodyIPSISample: " + getManipDeviceNbRetValue.GetUInt() + " Haption device(s) are connected."); } else { MiddleVRTools.Log(0, "[X] VRChangeAttachedPhysicsBodyIPSISample: Failed to detect how many Haption devices are connected."); } // Use of "Haption.IPSI.GetManipulationDeviceName". // Param with one vrValue: // + the id of an Haption device (as uint). // Return: // A vrValue that contains a string (the name). // In case of problem, the vrValue is invalid. var getManipDeviceNamePrmsValue = new vrValue(m_ManipulationDeviceId); var getManipDeviceNameRetValue = kernel.ExecuteCommand( "Haption.IPSI.GetManipulationDeviceName", getManipDeviceNamePrmsValue); if (getManipDeviceNameRetValue.IsString()) { MiddleVRTools.Log(2, "[+] VRChangeAttachedPhysicsBodyIPSISample: The name of the Haption device '" + m_ManipulationDeviceId + "' is '" + getManipDeviceNameRetValue.GetString() + "'."); } else { MiddleVRTools.Log(0, "[X] VRChangeAttachedPhysicsBodyIPSISample: Failed to the find the name of the Haption device '" + m_ManipulationDeviceId + "'."); } // Use of "Haption.IPSI.AttachManipulationDeviceToBody". // Params with one vrValue: // + 1st arg: the id of an Haption device (as uint), // + 2st arg: the id of a physics body (returned by GetId() on this object). // Return: // A vrValue that contains a boolean: True if successfully attached, False otherwise. // In case of problem, the vrValue is invalid. // // If the attachment failed, the previous attached physics body // will remain attached, otherwise it will be detached. // // It is also possible to add arguments in order to select how // the manipulated object will be attached: // + 3rd arg: the type of attachment (as uint), // + 4th arg: a translation offset for an arbitrary point of attachment (as vrVec3), // + 5th arg: a rotation for offset of an arbitrary point of attachment (as vrQuat). // // The type of attachment can take the following values: // + 0: unknown attach point so no attachment will occur, // + 1: attachment at the geometric center, // + 2: attachment at the center of the axis-aligned bounding box (AABB), // + 3: attachment at an arbitrary point that is an offset in // the object coordinate frame. // // So the 4th and the 5th arguments will be used with type 3 // (i.e. arbitrary point) but ignored otherwise. // If the type 3 is used but the 4th and the 5th arguments are // not given, translation will equal to 0 and rotation to identity. // If the type 3 is used but the 5th argument is not given, // rotation will equal to identity. var attachManipDeviceToBodyPrmsValue = vrValue.CreateList(); attachManipDeviceToBodyPrmsValue.AddListItem(m_ManipulationDeviceId); attachManipDeviceToBodyPrmsValue.AddListItem(objId); // The previous manipulated physics body (if any), will be // automatically detached. var attachManipDeviceToBodyRetValue = kernel.ExecuteCommand( "Haption.IPSI.AttachManipulationDeviceToBody", attachManipDeviceToBodyPrmsValue); if (attachManipDeviceToBodyRetValue.IsBool()) { MiddleVRTools.Log(2, "[+] VRChangeAttachedPhysicsBodyIPSISample: Did attachment of the Haption device '" + m_ManipulationDeviceId + "' to the physics body '" + objId + "' succeeded? " + (attachManipDeviceToBodyRetValue.GetBool() == true ? "Yes" : "No") + "."); } else { MiddleVRTools.Log(0, "[X] VRChangeAttachedPhysicsBodyIPSISample: Failed to attach the Haption device '" + m_ManipulationDeviceId + "' to the physics body '" + objId + "'."); } // Use of "Haption.IPSI.IsManipulationDeviceAttachedToABody". // Params with one vrValue: // + the id of an Haption device (as uint). // Return: // A vrValue that contains a boolean: True means that the device is attached to a body, False otherwise. // In case of problem, the vrValue is invalid. var isManipDeviceAttachedToABodyPrmsValue = new vrValue(m_ManipulationDeviceId); var isManipDeviceAttachedToABodyRetValue = kernel.ExecuteCommand( "Haption.IPSI.IsManipulationDeviceAttachedToABody", isManipDeviceAttachedToABodyPrmsValue); if (isManipDeviceAttachedToABodyRetValue.IsBool()) { MiddleVRTools.Log(2, "[+] VRChangeAttachedPhysicsBodyIPSISample: Is the Haption device '" + m_ManipulationDeviceId + "' attached to a physics body? " + (isManipDeviceAttachedToABodyRetValue.GetBool() == true ? "Yes" : "No") + "."); } else { MiddleVRTools.Log(0, "[X] VRChangeAttachedPhysicsBodyIPSISample: Failed to check whether the Haption device '" + m_ManipulationDeviceId + "' is attached to a physics body."); } // Use of "Haption.IPSI.IsBodyAttachedToAManipulationDevice". // Params with one vrValue: // + the id of a physics body (returned by GetId() on this object). // Return: // A vrValue that contains a boolean: True means that the body is attached to a manipulation device, False otherwise. // In case of problem, the vrValue is invalid. var isBodyAttachedToAManipDevicePrmsValue = new vrValue(objId); var isBodyAttachedToAManipDeviceRetValue = kernel.ExecuteCommand( "Haption.IPSI.IsBodyAttachedToAManipulationDevice", isBodyAttachedToAManipDevicePrmsValue); if (isBodyAttachedToAManipDeviceRetValue.IsBool()) { MiddleVRTools.Log(2, "[+] VRChangeAttachedPhysicsBodyIPSISample: Is the physics body '" + objId + "' attached to a Haption device? " + (isBodyAttachedToAManipDeviceRetValue.GetBool() == true ? "Yes" : "No") + "."); } else { MiddleVRTools.Log(0, "[X] VRChangeAttachedPhysicsBodyIPSISample: Failed to check whether the physics body " + objId + " is attached to a Haption device."); } // Use of "Haption.IPSI.GetIdOfManipulationDeviceAttachedToBody". // Params with one vrValue: // + the id of a physics body (returned by GetId() on this object). // Return: // A vrValue that contains the id (a uint) of the attached manipulation device. // In case of problem or if the physics body was not attached, the vrValue is invalid. var getIdOfManipDeviceAttachedToBodyPrmsValue = new vrValue(objId); var getIdOfManipDeviceAttachedToBodyRetValue = kernel.ExecuteCommand( "Haption.IPSI.GetIdOfManipulationDeviceAttachedToBody", getIdOfManipDeviceAttachedToBodyPrmsValue); if (getIdOfManipDeviceAttachedToBodyRetValue.IsNumber()) { MiddleVRTools.Log(2, "[+] VRChangeAttachedPhysicsBodyIPSISample: The physics body '" + objId + "' is attached to the Haption device '" + getIdOfManipDeviceAttachedToBodyRetValue.GetNumber() + "'."); } else { MiddleVRTools.Log(0, "[X] VRChangeAttachedPhysicsBodyIPSISample: The physics body '" + objId + "' does not seem to be attached to a Haption device."); } // Use of "Haption.IPSI.GetIdOfBodyAttachedToManipulationDevice". // Params with one vrValue: // + the id of an Haption device (as uint). // Return: // A vrValue that contains the id (returned by vrObject.GetId()) of the attached physics body. // In case of problem or if the device was not attached, the vrValue is invalid. var getIdOfBodyAttachedToManipDevicePrmsValue = new vrValue(m_ManipulationDeviceId); var getIdOfBodyAttachedToManipDeviceRetValue = kernel.ExecuteCommand( "Haption.IPSI.GetIdOfBodyAttachedToManipulationDevice", getIdOfBodyAttachedToManipDevicePrmsValue); if (getIdOfBodyAttachedToManipDeviceRetValue.IsNumber()) { MiddleVRTools.Log(2, "[+] VRChangeAttachedPhysicsBodyIPSISample: The Haption device '" + m_ManipulationDeviceId + "' is attached to the physics body '" + getIdOfBodyAttachedToManipDeviceRetValue.GetNumber() + "'."); } else { MiddleVRTools.Log(0, "[X] VRChangeAttachedPhysicsBodyIPSISample: The Haption device '" + m_ManipulationDeviceId + "' does not seem to be attached to a physics body."); } // Use of "Haption.IPSI.DetachManipulationDevice". // Params with one vrValue: // + the id of an Haption device (as uint). // Return: // A vrValue that contains a boolean: True if successfully detached, False otherwise. // In case of problem, the vrValue is invalid. // Please uncomment the code following to try... /* * var detachManipDevicePrmsValue = new vrValue(m_ManipulationDeviceId); * * var detachManipDeviceRetValue = kernel.ExecuteCommand( * "Haption.IPSI.DetachManipulationDevice", * detachManipDevicePrmsValue); * * if (detachManipDeviceRetValue.IsBool()) * { * MiddleVRTools.Log(2, "[+] VRChangeAttachedPhysicsBodyIPSISample: Did detachment of the Haption device '" + * m_ManipulationDeviceId + "' succeeded? " + * (detachManipDeviceRetValue.GetBool() == true ? "Yes" : "No") + "."); * } * else * { * MiddleVRTools.Log(0, "[X] VRChangeAttachedPhysicsBodyIPSISample: Failed to detach the Haption device '" + * m_ManipulationDeviceId + "'."); * } */ // Use of "Haption.IPSI.DetachBodyFromAManipulationDevice". // Params with one vrValue: // + the id of a physics body (returned by GetId() on this object). // Return: // A vrValue that contains a boolean: True if successfully detached, False otherwise. // In case of problem, the vrValue is invalid. // Please uncomment the code following to try... /* * var detachBodyFromAManipDevicePrmsValue = new vrValue(objId); * * var detachBodyFromAManipDeviceRetValue = kernel.ExecuteCommand( * "Haption.IPSI.DetachBodyFromAManipulationDevice", * detachBodyFromAManipDevicePrmsValue); * * if (detachBodyFromAManipDeviceRetValue.IsBool()) * { * MiddleVRTools.Log(2, "[+] VRChangeAttachedPhysicsBodyIPSISample: Did detachment of the physics body '" + * objId + "' succeeded? " + * (detachBodyFromAManipDeviceRetValue.GetBool() == true ? "Yes" : "No") + "."); * } * else * { * MiddleVRTools.Log(0, "[X] VRChangeAttachedPhysicsBodyIPSISample: Failed to detach the physics body '" + * objId + "'."); * } */ } m_PhysicsBodyId = (m_PhysicsBodyId + 1) % (bodiesNb); } }
protected void EnableCollisions(vrPhysicsBody physicsBody0, bool iEnabled) { bool operationDone = false; vrPhysicsBody physicsBody1 = null; if (m_ConnectedBody != null) { if (MiddleVR.VRPhysicsMgr == null) { return; } vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine(); if (physicsEngine == null) { return; } physicsBody1 = physicsEngine.GetBody(m_ConnectedBody.GetComponent <VRPhysicsBody>().PhysicsBodyName); if (physicsBody1 == null) { MiddleVRTools.Log(0, "[X] PhysicsEnableCollisions: No PhysicsBody found in the connected body."); return; } operationDone = physicsBody0.EnableCollisionsWith(physicsBody1, iEnabled); } else { operationDone = physicsBody0.EnableAllCollisions(iEnabled); } if (operationDone) { string againstTxt = (physicsBody1 != null ? " against object '" + physicsBody1.GetName() + "'" : " against the world scene" ); if (iEnabled) { MiddleVRTools.Log(2, "[ ] PhysicsEnableCollisions: Enabled collisions for '" + physicsBody0.GetName() + "'" + againstTxt + "."); } else { MiddleVRTools.Log(2, "[ ] PhysicsEnableCollisions: Disabled collisions for '" + physicsBody0.GetName() + "'" + againstTxt + "."); } } else { if (iEnabled) { MiddleVRTools.Log(0, "[X] PhysicsEnableCollisions: Failed to enable collisions for '" + physicsBody0.GetName() + "'."); } else { MiddleVRTools.Log(0, "[X] PhysicsEnableCollisions: Failed to disable collisions for '" + physicsBody0.GetName() + "'."); } } }
protected void EnableCollisions(vrPhysicsBody physicsBody0, bool iEnabled) { bool operationDone = false; vrPhysicsBody physicsBody1 = null; if (m_ConnectedBody != null) { if (MiddleVR.VRPhysicsMgr == null) { return; } vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine(); if (physicsEngine == null) { return; } physicsBody1 = physicsEngine.GetBody(m_ConnectedBody.GetComponent<VRPhysicsBody>().PhysicsBodyName); if (physicsBody1 == null) { MiddleVRTools.Log(0, "[X] PhysicsEnableCollisions: No PhysicsBody found in the connected body."); return; } operationDone = physicsBody0.EnableCollisionsWith(physicsBody1, iEnabled); } else { operationDone = physicsBody0.EnableAllCollisions(iEnabled); } if (operationDone) { string againstTxt = (physicsBody1 != null ? " against object '" + physicsBody1.GetName() + "'" : " against the world scene" ); if (iEnabled) { MiddleVRTools.Log(2, "[+] PhysicsEnableCollisions: Enabled collisions for '" + physicsBody0.GetName() + "'" + againstTxt + "."); } else { MiddleVRTools.Log(2, "[+] PhysicsEnableCollisions: Disabled collisions for '" + physicsBody0.GetName() + "'" + againstTxt + "."); } } else { if (iEnabled) { MiddleVRTools.Log(0, "[X] PhysicsEnableCollisions: Failed to enable collisions for '" + physicsBody0.GetName() + "'."); } else { MiddleVRTools.Log(0, "[X] PhysicsEnableCollisions: Failed to disable collisions for '" + physicsBody0.GetName() + "'."); } } }
protected void Start() { if (MiddleVR.VRClusterMgr.IsCluster() && !MiddleVR.VRClusterMgr.IsServer()) { enabled = false; return; } if (MiddleVR.VRPhysicsMgr == null) { MiddleVRTools.Log(0, "[X] PhysicsBody: No PhysicsManager found."); enabled = false; return; } vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine(); if (physicsEngine == null) { MiddleVRTools.Log(0, "[X] PhysicsBody: Failed to access a physics engine for body '" + name + "'."); enabled = false; return; } if (m_PhysicsBody == null) { m_PhysicsBody = physicsEngine.CreateBodyWithUniqueName(name); if (m_PhysicsBody == null) { MiddleVRTools.Log(0, "[X] PhysicsBody: Failed to create a physics body for '" + name + "'."); enabled = false; return; } else { GC.SuppressFinalize(m_PhysicsBody); m_MVREventListener = new vrEventListener(OnMVRNodeDestroy); m_PhysicsBody.AddEventListener(m_MVREventListener); var nodesMapper = MVRNodesMapper.Instance; nodesMapper.AddMapping( gameObject, m_PhysicsBody, MVRNodesMapper.ENodesSyncDirection.MiddleVRToUnity, MVRNodesMapper.ENodesInitValueOrigin.FromUnity); m_PhysicsBodyName = m_PhysicsBody.GetName(); string geometryName = m_PhysicsBodyName + ".Geometry"; MiddleVRTools.Log(4, "[>] PhysicsBody: Creation of the physics geometry '" + geometryName + "'."); m_Geometry = new vrPhysicsGeometry(geometryName); GC.SuppressFinalize(m_Geometry); Mesh mesh = null; MeshCollider meshCollider = GetComponent<MeshCollider>(); if (meshCollider != null) { mesh = meshCollider.sharedMesh; if (mesh != null) { MiddleVRTools.Log(2, "[ ] PhysicsBody: the physics geometry '" + geometryName + "' uses the mesh of its MeshCollider."); } } // No mesh from collider was found so let's try from the mesh filter. if (mesh == null) { var meshFilter = GetComponent<MeshFilter>(); if (meshFilter != null) { mesh = meshFilter.sharedMesh; if (mesh != null) { MiddleVRTools.Log(2, "[ ] PhysicsBody: the physics geometry '" + geometryName + "' uses the mesh of its MeshFilter."); } } } if (mesh != null) { ConvertGeometry(mesh); MiddleVRTools.Log(4, "[ ] PhysicsBody: Physics geometry created."); } else { MiddleVRTools.Log( 0, "[X] PhysicsBody: Failed to create the physics geometry '" + geometryName + "'."); } MiddleVRTools.Log(4, "[<] PhysicsBody: Creation of the physics geometry '" + geometryName + "' ended."); m_PhysicsBody.SetGeometry(m_Geometry); m_PhysicsBody.SetStatic(m_Static); m_PhysicsBody.SetMass(m_Mass); m_PhysicsBody.SetRotationDamping(m_RotationDamping); m_PhysicsBody.SetTranslationDamping(m_TranslationDamping); m_PhysicsBody.SetMargin(m_Margin); if (physicsEngine.AddBody(m_PhysicsBody)) { MiddleVRTools.Log(3, "[ ] PhysicsBody: The physics body '" + m_PhysicsBodyName + "' was added to the physics simulation."); } else { MiddleVRTools.Log(0, "[X] PhysicsBody: Failed to add the body '" + m_PhysicsBodyName + "' to the physics simulation."); } } } }
protected void OnDestroy() { if (m_PhysicsBody != null) { if (MVRNodesMapper.HasInstance()) { var nodesMapper = MVRNodesMapper.Instance; nodesMapper.RemoveMapping(gameObject); } if (MiddleVR.VRPhysicsMgr != null) { vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine(); if (physicsEngine != null) { physicsEngine.DestroyBody(m_PhysicsBodyName); } } m_PhysicsBody.Dispose(); m_PhysicsBody = null; } m_PhysicsBodyName = ""; if (m_MVREventListener != null) { m_MVREventListener.Dispose(); } }
private bool OnMVRNodeDestroy(vrEvent iBaseEvt) { vrObjectEvent e = vrObjectEvent.Cast(iBaseEvt); if (e != null) { if (e.ComesFrom(m_RigidBody)) { if (e.eventType == (int)VRObjectEventEnum.VRObjectEvent_Destroy) { // The physics rigid body was killed in MiddleVR so // stop to use it in C#. m_RigidBody = null; // And even stop to use this component. enabled = false; } } } return true; }
protected void Start() { if (MiddleVR.VRClusterMgr.IsCluster() && !MiddleVR.VRClusterMgr.IsServer()) { enabled = false; return; } if (MiddleVR.VRPhysicsMgr == null) { MiddleVRTools.Log(0, "[X] PhysicsBody: No PhysicsManager found."); enabled = false; return; } vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine(); if (physicsEngine == null) { MiddleVRTools.Log(0, "[X] PhysicsBody: Failed to access a physics engine for body '" + name + "'."); enabled = false; return; } if (m_PhysicsBody == null) { m_PhysicsBody = physicsEngine.CreateBodyWithUniqueName(name); if (m_PhysicsBody == null) { MiddleVRTools.Log(0, "[X] PhysicsBody: Failed to create a physics body for '" + name + "'."); enabled = false; return; } else { GC.SuppressFinalize(m_PhysicsBody); m_MVREventListener = new vrEventListener(OnMVRNodeDestroy); m_PhysicsBody.AddEventListener(m_MVREventListener); var nodesMapper = MVRNodesMapper.Instance; nodesMapper.AddMapping( gameObject, m_PhysicsBody, MVRNodesMapper.ENodesSyncDirection.MiddleVRToUnity, MVRNodesMapper.ENodesInitValueOrigin.FromUnity); m_PhysicsBodyName = m_PhysicsBody.GetName(); string geometryName = m_PhysicsBodyName + ".Geometry"; MiddleVRTools.Log(4, "[>] PhysicsBody: Creation of the physics geometry '" + geometryName + "'."); m_Geometry = new vrPhysicsGeometry(geometryName); GC.SuppressFinalize(m_Geometry); Mesh mesh = null; MeshCollider meshCollider = GetComponent <MeshCollider>(); if (meshCollider != null) { mesh = meshCollider.sharedMesh; if (mesh != null) { MiddleVRTools.Log(2, "[ ] PhysicsBody: the physics geometry '" + geometryName + "' uses the mesh of its MeshCollider."); } } // No mesh from collider was found so let's try from the mesh filter. if (mesh == null) { var meshFilter = GetComponent <MeshFilter>(); if (meshFilter != null) { mesh = meshFilter.sharedMesh; if (mesh != null) { MiddleVRTools.Log(2, "[ ] PhysicsBody: the physics geometry '" + geometryName + "' uses the mesh of its MeshFilter."); } } } if (mesh != null) { ConvertGeometry(mesh); MiddleVRTools.Log(4, "[ ] PhysicsBody: Physics geometry created."); } else { MiddleVRTools.Log( 0, "[X] PhysicsBody: Failed to create the physics geometry '" + geometryName + "'."); } MiddleVRTools.Log(4, "[<] PhysicsBody: Creation of the physics geometry '" + geometryName + "' ended."); m_PhysicsBody.SetGeometry(m_Geometry); m_PhysicsBody.SetStatic(m_Static); m_PhysicsBody.SetMass(m_Mass); m_PhysicsBody.SetRotationDamping(m_RotationDamping); m_PhysicsBody.SetTranslationDamping(m_TranslationDamping); m_PhysicsBody.SetMargin(m_Margin); if (physicsEngine.AddBody(m_PhysicsBody)) { MiddleVRTools.Log(3, "[ ] PhysicsBody: The physics body '" + m_PhysicsBodyName + "' was added to the physics simulation."); } else { MiddleVRTools.Log(3, "[X] PhysicsBody: Failed to add the body '" + m_PhysicsBodyName + "' to the physics simulation."); } } } }
protected void Start() { if (MiddleVR.VRClusterMgr.IsCluster() && !MiddleVR.VRClusterMgr.IsServer()) { enabled = false; return; } if (MiddleVR.VRPhysicsMgr == null) { MiddleVRTools.Log(0, "[X] PhysicsBody: No PhysicsManager found."); enabled = false; return; } vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine(); if (physicsEngine == null) { MiddleVRTools.Log(0, "[X] PhysicsBody: Failed to access a physics engine for body '" + name + "'."); enabled = false; return; } if (m_PhysicsBody == null) { m_PhysicsBody = physicsEngine.CreateBodyWithUniqueName(name); if (m_PhysicsBody == null) { MiddleVRTools.Log(0, "[X] PhysicsBody: Failed to create a physics body for '" + name + "'."); enabled = false; return; } else { GC.SuppressFinalize(m_PhysicsBody); m_MVREventListener = new vrEventListener(OnMVRNodeDestroy); m_PhysicsBody.AddEventListener(m_MVREventListener); var nodesMapper = MVRNodesMapper.Instance; nodesMapper.AddMapping( gameObject, m_PhysicsBody, MVRNodesMapper.ENodesSyncDirection.MiddleVRToUnity, MVRNodesMapper.ENodesInitValueOrigin.FromUnity); m_PhysicsBodyName = m_PhysicsBody.GetName(); m_Geometry = CreateGeometry(m_MergeChildGeometries); if (m_Geometry != null) { GC.SuppressFinalize(m_Geometry); } m_PhysicsBody.SetGeometry(m_Geometry); m_PhysicsBody.SetStatic(m_Static); m_PhysicsBody.SetMass(m_Mass); m_PhysicsBody.SetRotationDamping(m_RotationDamping); m_PhysicsBody.SetTranslationDamping(m_TranslationDamping); m_PhysicsBody.SetMargin(m_Margin); if (physicsEngine.AddBody(m_PhysicsBody)) { MiddleVRTools.Log(3, "[ ] PhysicsBody: The physics body '" + m_PhysicsBodyName + "' was added to the physics simulation."); } else { MiddleVRTools.Log(0, "[X] PhysicsBody: Failed to add the body '" + m_PhysicsBodyName + "' to the physics simulation."); } } } }