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);
    }
Beispiel #2
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);
        }
    }
    private vrPhysicsGeometry CreateGeometry(bool iAreChildGeometriesMerged)
    {
        string geometryName = m_PhysicsBodyName + ".Geometry";

        MiddleVRTools.Log(4,
                          "[>] PhysicsBody: Creation of the physics geometry '" +
                          geometryName + "'.");

        var rootMatrixWorld = transform.localToWorldMatrix;

        vrPhysicsGeometry ret = new vrPhysicsGeometry(geometryName);

        uint geometryIndex = 0;

        var queue = new Queue <GameObject>();

        queue.Enqueue(gameObject);

        while (queue.Count > 0)
        {
            var go = queue.Dequeue();

            Mesh mesh = null;

            var meshCollider = go.GetComponent <MeshCollider>();

            if (meshCollider != null && meshCollider.enabled)
            {
                mesh = meshCollider.sharedMesh;

                if (mesh != null)
                {
                    MiddleVRTools.Log(2,
                                      "[ ] PhysicsBody: the GO '" + go.name +
                                      "' will furnish a physics geometry for '" + geometryName +
                                      "' from its MeshCollider.");
                }
            }

            // No mesh from collider was found so let's try from the mesh filter.
            if (mesh == null)
            {
                var meshFilter = go.GetComponent <MeshFilter>();

                if (meshFilter != null)
                {
                    mesh = meshFilter.sharedMesh;

                    if (mesh != null)
                    {
                        MiddleVRTools.Log(2,
                                          "[ ] PhysicsBody: the GO '" + go.name +
                                          "' will furnish a physics geometry for '" + geometryName +
                                          "' from its MeshFilter.");
                    }
                }
            }

            if (mesh != null)
            {
                // The top geometry keep its name, others will get the word "sub".
                if (geometryIndex != 0)
                {
                    ret.SetSubGeometryName(geometryIndex, geometryName + ".sub." + go.name);
                }

                ConvertGeometry(rootMatrixWorld, go.transform, mesh, ret, geometryIndex);

                ++geometryIndex;

                MiddleVRTools.Log(4,
                                  "[ ] PhysicsBody: Physics geometry created from GO '" +
                                  go.name + "'.");
            }

            if (iAreChildGeometriesMerged)
            {
                foreach (Transform child in go.transform)
                {
                    if (child.gameObject.activeSelf)
                    {
                        queue.Enqueue(child.gameObject);
                    }
                }
            }
        }

        var scaleShearMatrix = MVRTools.ComputeScaleShearMatrixWorld(transform);

        ret.TransformStoredVertices(MVRTools.FromUnity(scaleShearMatrix));

        MiddleVRTools.Log(4,
                          "[<] PhysicsBody: Creation of the physics geometry '" +
                          geometryName + "' ended.");

        return(ret);
    }