Example #1
0
    private bool DisableAllCollisions(bool iDisabled)
    {
        if (MiddleVR.VRPhysicsMgr == null)
        {
            return(false);
        }

        vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine();

        if (physicsEngine == null)
        {
            return(false);
        }

        if (physicsEngine.IsStarted())
        {
            bool actionApplied = physicsEngine.EnableCollisions(!iDisabled);

            if (actionApplied)
            {
                if (iDisabled)
                {
                    MiddleVRTools.Log(2, "[ ] PhysicsDisableAllCollisions: all collisions disabled.");
                }
                else
                {
                    MiddleVRTools.Log(2, "[ ] PhysicsDisableAllCollisions: all collisions enabled.");
                }
            }

            return(actionApplied);
        }

        return(false);
    }
    protected void Update()
    {
        if (!mApplied)
        {
            vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine();

            if (physicsEngine == null)
            {
                MiddleVRTools.Log(0, "[X] PhysicsDisableAllCollisions: No PhysicsEngine found.");
                enabled = false;

                return;
            }

            if (physicsEngine.IsStarted())
            {
                bool disabled = physicsEngine.EnableCollisions(false);

                if (disabled)
                {
                    MiddleVRTools.Log(2, "[ ] PhysicsDisableAllCollisions: all collisions disabled.");
                }

                mApplied = true;
            }
        }
    }
Example #3
0
    protected void Update()
    {
        if (!m_DidFirstAttachment)
        {
            var physicsMgr = MiddleVR.VRPhysicsMgr;

            if (physicsMgr == null)
            {
                MiddleVRTools.Log(0, "[X] PhysicsBodyManipulator: No PhysicsManager found.");
                enabled = false;
                return;
            }

            vrPhysicsEngine physicsEngine = physicsMgr.GetPhysicsEngine();

            if (physicsEngine == null)
            {
                return;
            }

            if (physicsEngine.IsStarted())
            {
                AttachOrDetachBody(true);

                m_DidFirstAttachment = true;
            }
        }
    }
Example #4
0
    protected bool AddConstraint()
    {
        vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine();

        if (physicsEngine == null)
        {
            return(false);
        }

        if (m_PhysicsConstraint == null)
        {
            return(false);
        }

        bool addedToSimulation = false;

        // Cannot fail since we require this component.
        VRPhysicsBody body0 = GetComponent <VRPhysicsBody>();

        VRPhysicsBody body1 = null;

        if (m_ConnectedBody != null)
        {
            body1 = m_ConnectedBody.GetComponent <VRPhysicsBody>();
        }

        if (body0.PhysicsBody != null)
        {
            m_PhysicsConstraint.SetAxis(MiddleVRTools.FromUnity(Axis));

            m_PhysicsConstraint.SetLowerLimit(m_Limits.Min);
            m_PhysicsConstraint.SetUpperLimit(m_Limits.Max);

            m_PhysicsConstraint.SetReferencePosition(m_ZeroPosition);

            m_PhysicsConstraint.SetBody(0, body0.PhysicsBody);
            m_PhysicsConstraint.SetBody(1, body1 != null ? body1.PhysicsBody : null);

            addedToSimulation = physicsEngine.AddConstraint(m_PhysicsConstraint);

            if (addedToSimulation)
            {
                MiddleVRTools.Log(3, "[ ] The constraint '" + m_PhysicsConstraintName +
                                  "' was added to the physics simulation.");
            }
            else
            {
                MiddleVRTools.Log(3, "[X] Failed to add the constraint '" +
                                  m_PhysicsConstraintName + "' to the physics simulation.");
            }
        }
        else
        {
            MiddleVRTools.Log(0, "[X] The PhysicsBody of '" + name +
                              "' for the prismatic physics constraint '" + m_PhysicsConstraintName +
                              "' is null.");
        }

        return(addedToSimulation);
    }
    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();
        }
    }
    protected void Start()
    {
        if (MiddleVR.VRClusterMgr.IsCluster() && !MiddleVR.VRClusterMgr.IsServer())
        {
            enabled = false;
            return;
        }

        if (MiddleVR.VRPhysicsMgr == null)
        {
            MiddleVRTools.Log(0, "[X] PhysicsEnableCollisions: No PhysicsManager found.");
            enabled = false;

            return;
        }

        vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine();

        if (physicsEngine == null)
        {
            MiddleVRTools.Log(0, "[X] PhysicsEnableCollisions: No PhysicsEngine found.");
            enabled = false;

            return;
        }
    }
    protected vrPhysicsBody GetPhysicsBodyInSimulation()
    {
        if (MiddleVR.VRPhysicsMgr == null)
        {
            return(null);
        }

        vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine();

        if (physicsEngine == null)
        {
            return(null);
        }

        vrPhysicsBody physicsBody = physicsEngine.GetBody(GetComponent <VRPhysicsBody>().PhysicsBodyName);

        if (physicsBody != null && physicsBody.IsInSimulation())
        {
            return(physicsBody);
        }
        else
        {
            return(null);
        }
    }
Example #8
0
    protected void Update()
    {
        if (MiddleVR.VRDeviceMgr != null &&
            MiddleVR.VRDeviceMgr.IsKeyPressed(MiddleVR.VRK_H) &&
            MiddleVR.VRDeviceMgr.IsKeyToggled(MiddleVR.VRK_C))
        {
            if (MiddleVR.VRPhysicsMgr == null)
            {
                MiddleVRTools.Log(0, "[X] VRChangeManipulationDeviceIPSISample: No PhysicsManager found.");
                enabled = false;
                return;
            }

            vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine();

            if (physicsEngine == null)
            {
                return;
            }

            uint bodiesNb = physicsEngine.GetBodiesNb();
            m_PhysicsBodyId = (m_PhysicsBodyId + 1) % ((int)bodiesNb);

            MiddleVRTools.Log(0, "[+] VRChangeManipulationDeviceIPSISample: proposed body id: " +
                              m_PhysicsBodyId + ".");

            vrPhysicsBody physicsBody = physicsEngine.GetBody((uint)m_PhysicsBodyId);
            if (physicsBody != null && physicsBody.IsA("PhysicsBodyIPSI"))
            {
                var kernel = MiddleVR.VRKernel;

                var objId = physicsBody.GetId();

                // SetAttachedToAManipDevice (do an attachment).
                var setAttachedToAManipDeviceValues = vrValue.CreateList();
                setAttachedToAManipDeviceValues.AddListItem(objId);
                setAttachedToAManipDeviceValues.AddListItem(true);

                kernel.ExecuteCommand(
                    "Haption.IPSI.SetAttachedToAManipulationDevice",
                    setAttachedToAManipDeviceValues);

                // The previous manipulated physics body (if any), will be
                // automatically marked as not-manipulated.
                // SetManipulationDevice (only attachment).
                var setManipDeviceValues = vrValue.CreateList();
                setManipDeviceValues.AddListItem(objId);
                setManipDeviceValues.AddListItem(m_ManipulationDeviceId);

                kernel.ExecuteCommand(
                    "Haption.IPSI.SetManipulationDeviceId",
                    setManipDeviceValues);

                MiddleVRTools.Log(0, "[+] VRChangeManipulationDeviceIPSISample: attached '" +
                                  physicsBody.GetName() + "' to the manipulation device '" +
                                  m_ManipulationDeviceId + "'.");
            }
        }
    }
    protected bool AddConstraint()
    {
        vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine();

        if (physicsEngine == null)
        {
            return(false);
        }

        if (m_PhysicsConstraint == null)
        {
            return(false);
        }

        bool addedToSimulation = false;

        // Cannot fail since we require this component.
        VRPhysicsBody body0 = GetComponent <VRPhysicsBody>();

        VRPhysicsBody body1 = null;

        if (m_ConnectedBody != null)
        {
            body1 = m_ConnectedBody.GetComponent <VRPhysicsBody>();
        }

        if (body0.PhysicsBody != null)
        {
            var scaleShearMatrix = MVRTools.ComputeScaleShearMatrixWorld(transform);
            m_PhysicsConstraint.SetPosition(MiddleVRTools.FromUnity(scaleShearMatrix * Anchor));
            m_PhysicsConstraint.SetAxis0(MiddleVRTools.FromUnity(Axis0));
            m_PhysicsConstraint.SetAxis1(MiddleVRTools.FromUnity(Axis1));

            m_PhysicsConstraint.SetBody(0, body0.PhysicsBody);
            m_PhysicsConstraint.SetBody(1, body1 != null ? body1.PhysicsBody : null);

            addedToSimulation = physicsEngine.AddConstraint(m_PhysicsConstraint);

            if (addedToSimulation)
            {
                MiddleVRTools.Log(3, "[ ] The constraint '" + m_PhysicsConstraintName +
                                  "' was added to the physics simulation.");
            }
            else
            {
                MiddleVRTools.Log(0, "[X] Failed to add the constraint '" +
                                  m_PhysicsConstraintName + "' to the physics simulation.");
            }
        }
        else
        {
            MiddleVRTools.Log(0, "[X] The PhysicsBody of '" + name +
                              "' for the U-joint physics constraint '" + m_PhysicsConstraintName +
                              "' is null.");
        }

        return(addedToSimulation);
    }
Example #10
0
    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)
        {
            // Do "+1" because "0" means "unknown attach point type".
            uint attachPointType = (uint)m_AttachPointType + 1;

            var scaleShearMatrix = MVRTools.ComputeScaleShearMatrixWorld(transform);
            var offsetTrans      = scaleShearMatrix * m_OffsetTranslation;
            var offsetRot        = Quaternion.Euler(m_OffsetRotation);

            var attachManipDeviceToBodyPrmsValue = vrValue.CreateList();
            attachManipDeviceToBodyPrmsValue.AddListItem(m_ManipulationDeviceId);
            attachManipDeviceToBodyPrmsValue.AddListItem(physicsBodyId);
            attachManipDeviceToBodyPrmsValue.AddListItem(attachPointType);
            attachManipDeviceToBodyPrmsValue.AddListItem(
                MiddleVRTools.FromUnity(offsetTrans));
            attachManipDeviceToBodyPrmsValue.AddListItem(
                MiddleVRTools.FromUnity(offsetRot));

            kernel.ExecuteCommand(
                "Haption.IPSI.AttachManipulationDeviceToBody",
                attachManipDeviceToBodyPrmsValue);
        }
    }
Example #11
0
    protected void Update()
    {
        if (MiddleVR.VRPhysicsMgr == null)
        {
            return;
        }

        vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine();

        if (physicsEngine == null)
        {
            return;
        }

        for (int i = 0; i < m_MaxContactsNb; i++)
        {
            GameObject go = m_ContactsToShow[i];

            if (go != null)
            {
                go.SetActive(false);
            }
        }

        // We will reuse the same vectors to avoid many memory allocations.
        Vector3 contactPosition = new Vector3();
        Vector3 contactNormal   = new Vector3();

        for (uint i = 0, iEnd = physicsEngine.GetContactInfosNb(); i < iEnd && i < m_MaxContactsNb; i++)
        {
            vrPhysicsContactInfo contactInfo = physicsEngine.GetContactInfo(i);

            MiddleVRTools.ToUnity(contactInfo.GetPositionOnBody0(), ref contactPosition);
            MiddleVRTools.ToUnity(contactInfo.GetNormalOnBody0(), ref contactNormal);

            Quaternion contactQ = Quaternion.FromToRotation(Vector3.up, contactNormal);

            Vector3 p = m_Translation + contactPosition;

            GameObject go = m_ContactsToShow[i];

            if (go != null)
            {
                go.transform.position = p;
                go.transform.rotation = m_RotationAsQuat * contactQ;

                go.SetActive(true);
            }

            if (m_RayDebug)
            {
                Debug.DrawRay(p, m_RotationAsQuat * contactNormal, Color.green);
            }
        }
    }
Example #12
0
    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 setManipDeviceValues = vrValue.CreateList();
            setManipDeviceValues.AddListItem(physicsBodyId);
            setManipDeviceValues.AddListItem(m_ManipulationDeviceId);

            kernel.ExecuteCommand(
                "Haption.IPSI.SetManipulationDeviceId",
                setManipDeviceValues);
        }

        // SetAttachedToAManipDevice (attachment or detachment).
        var setAttachedToAManipDeviceValues = vrValue.CreateList();

        setAttachedToAManipDeviceValues.AddListItem(physicsBodyId);
        setAttachedToAManipDeviceValues.AddListItem(doAttachement);

        kernel.ExecuteCommand(
            "Haption.IPSI.SetAttachedToAManipulationDevice",
            setAttachedToAManipDeviceValues);
    }
Example #13
0
    protected void Start()
    {
        if (MiddleVR.VRClusterMgr.IsCluster() && !MiddleVR.VRClusterMgr.IsServer())
        {
            enabled = false;
            return;
        }

        if (MiddleVR.VRPhysicsMgr == null)
        {
            MiddleVRTools.Log(0, "[X] No PhysicsManager found when creating a prismatic constraint.");
            enabled = false;
            return;
        }

        vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine();

        if (physicsEngine == null)
        {
            return;
        }

        if (m_PhysicsConstraint == null)
        {
            m_PhysicsConstraint = physicsEngine.CreateConstraintPrismaticWithUniqueName(name);

            if (m_PhysicsConstraint == null)
            {
                MiddleVRTools.Log(0, "[X] Could not create a prismatic physics constraint for '"
                                  + name + "'.");
            }
            else
            {
                GC.SuppressFinalize(m_PhysicsConstraint);

                m_MVREventListener = new vrEventListener(OnMVRNodeDestroy);
                m_PhysicsConstraint.AddEventListener(m_MVREventListener);

                m_PhysicsConstraintName = m_PhysicsConstraint.GetName();

                AddConstraint();
            }
        }
    }
    protected void Start()
    {
        if (MiddleVR.VRPhysicsMgr == null)
        {
            MiddleVRTools.Log(0, "[X] PhysicsDeactivateAllContacts: No PhysicsManager found.");
            enabled = false;

            return;
        }

        vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine();

        if (physicsEngine == null)
        {
            MiddleVRTools.Log(0, "[X] PhysicsDeactivateAllContacts: No PhysicsEngine found.");
            enabled = false;

            return;
        }

        physicsEngine.SetActivateContactInfos(false);

        MiddleVRTools.Log(2, "[ ] PhysicsDisableAllContacts: all contacts disabled.");
    }
    protected void OnDestroy()
    {
        if (m_PhysicsConstraint == null)
        {
            return;
        }

        if (MiddleVR.VRPhysicsMgr == null)
        {
            return;
        }

        vrPhysicsEngine physicsEngine = MiddleVR.VRPhysicsMgr.GetPhysicsEngine();

        if (physicsEngine == null)
        {
            return;
        }

        physicsEngine.DestroyConstraint(m_PhysicsConstraintName);

        m_PhysicsConstraint     = null;
        m_PhysicsConstraintName = "";
    }
    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.");
                }
            }
        }
    }
Example #17
0
    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 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() + "'.");
            }
        }
    }