コード例 #1
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);
    }
コード例 #2
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)
        {
            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);
    }
コード例 #3
0
    private void ConvertGeometry(Mesh mesh)
    {
        Vector3[] vertices  = mesh.vertices;
        int[]     triangles = mesh.triangles;

        MiddleVRTools.Log(3, "PhysicsBody: Number of vertices: " + vertices.Length);
        MiddleVRTools.Log(3, "PhysicsBody: Number of Triangles: " + triangles.Length);

        // We will reuse the same vectors to avoid many memory allocations.
        Vector3 vertexPos = new Vector3();
        vrVec3  vPos      = new vrVec3();

        // We compute a matrix to scale vertices according to their world
        // coordinates, so this scale depends on the scales of the GameObject
        // parents. Matrices 4x4 are used because matrices 3x3 aren't available.
        vrMatrix worldMatrix = MVRTools.RawFromUnity(transform.localToWorldMatrix);

        worldMatrix.SetCol(3, new vrVec4(0.0f, 0.0f, 0.0f, 0.0f));
        worldMatrix.SetRow(3, new vrVec4(0.0f, 0.0f, 0.0f, 0.0f));

        vrQuat invRotWorlQ = worldMatrix.GetRotation();

        invRotWorlQ = invRotWorlQ.Normalize().GetInverse();

        vrMatrix invRotWorldMatrix = new vrMatrix();

        invRotWorldMatrix.SetRotation(invRotWorlQ);
        invRotWorldMatrix.SetCol(3, new vrVec4(0.0f, 0.0f, 0.0f, 0.0f));
        invRotWorldMatrix.SetRow(3, new vrVec4(0.0f, 0.0f, 0.0f, 0.0f));

        vrMatrix  scaleWorldMatrix      = invRotWorldMatrix.Mult(worldMatrix);
        Matrix4x4 scaleWorldMatrixUnity = MiddleVRTools.RawToUnity(scaleWorldMatrix);

        foreach (Vector3 vertex in vertices)
        {
            vertexPos = scaleWorldMatrixUnity.MultiplyPoint3x4(vertex);

            MiddleVRTools.FromUnity(vertexPos, ref vPos);

            m_Geometry.AddVertex(vPos);

            MiddleVRTools.Log(6, "PhysicsBody: Adding a vertex at position (" +
                              vPos.x() + ", " + vPos.y() + ", " + vPos.z() + ").");
        }

        for (int i = 0, iEnd = triangles.Length; i < iEnd; i += 3)
        {
            uint index0 = (uint)triangles[i];
            uint index1 = (uint)triangles[i + 1];
            uint index2 = (uint)triangles[i + 2];

            m_Geometry.AddTriangle(index0, index1, index2);

            MiddleVRTools.Log(6, "PhysicsBody: Adding a triangle with vertex indexes (" +
                              index0 + ", " + index1 + ", " + index2 + ").");
        }
    }
コード例 #4
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 void ConvertGeometry(Matrix4x4 iTopMatrixWorld, Transform iTransform, Mesh iMesh, vrPhysicsGeometry ioGeometry, uint iGeometryIndex)
    {
        var vertices  = iMesh.vertices;
        var triangles = iMesh.triangles;

        MiddleVRTools.Log(3,
                          "[ ] PhysicsBody: Number of vertices in sub-geometry '" +
                          iGeometryIndex + "': " + vertices.Length);
        MiddleVRTools.Log(3,
                          "[ ] PhysicsBody: Number of triangles in sub-geometry '" +
                          iGeometryIndex + "': " + triangles.Length);

        var m = iTopMatrixWorld.inverse * iTransform.localToWorldMatrix;

        // We will reuse the same vector to avoid many memory allocations.
        vrVec3 vPos = new vrVec3();

        MiddleVRTools.Log(6, "[>] PhysicsBody: Adding vertices.");

        foreach (Vector3 vertex in vertices)
        {
            var vertexPos = m.MultiplyPoint3x4(vertex);
            MiddleVRTools.FromUnity(vertexPos, ref vPos);
            ioGeometry.AddVertex(vPos, iGeometryIndex);

            MiddleVRTools.Log(6,
                              "[ ] PhysicsBody: Adding a vertex at position (" +
                              vPos.x() + ", " + vPos.y() + ", " + vPos.z() +
                              ") to sub-geometry '" + iGeometryIndex + "'.");
        }

        MiddleVRTools.Log(6, "[<] PhysicsBody: End of adding vertices.");
        MiddleVRTools.Log(6, "[>] PhysicsBody: Adding triangles.");

        for (int i = 0, iEnd = triangles.Length; i < iEnd; i += 3)
        {
            var index0 = (uint)triangles[i + 0];
            var index1 = (uint)triangles[i + 1];
            var index2 = (uint)triangles[i + 2];

            ioGeometry.AddTriangle(index0, index1, index2, iGeometryIndex);

            MiddleVRTools.Log(6,
                              "[ ] PhysicsBody: Adding a triangle with vertex indexes (" +
                              index0 + ", " + index1 + ", " + index2 + ") to sub-geometry '" +
                              iGeometryIndex + "'.");
        }

        MiddleVRTools.Log(6, "[<] PhysicsBody: End of adding triangles.");
    }
コード例 #6
0
    protected void Update()
    {
        var deviceMgr = MiddleVR.VRDeviceMgr;

        if (deviceMgr != null &&
            deviceMgr.IsKeyPressed(MiddleVR.VRK_H))
        {
            if (deviceMgr.IsKeyToggled(MiddleVR.VRK_F))
            {
                Vector3 force = m_Force;

                if (deviceMgr.IsKeyPressed(MiddleVR.VRK_LSHIFT) ||
                    deviceMgr.IsKeyPressed(MiddleVR.VRK_RSHIFT))
                {
                    force = -force;
                }

                m_RigidBody.AddForce(MiddleVRTools.FromUnity(force));

                MiddleVRTools.Log(2, "[ ] ApplyForceTorqueSample: applied force " +
                                  force + " on '" + m_RigidBody.GetName() + "'.");
            }

            if (deviceMgr.IsKeyToggled(MiddleVR.VRK_T))
            {
                Vector3 torque = m_Torque;

                if (deviceMgr.IsKeyPressed(MiddleVR.VRK_LSHIFT) ||
                    deviceMgr.IsKeyPressed(MiddleVR.VRK_RSHIFT))
                {
                    torque = -torque;
                }

                m_RigidBody.AddTorque(MiddleVRTools.FromUnity(torque));

                MiddleVRTools.Log(2, "[ ] ApplyForceTorqueSample: applied torque " +
                                  m_Torque + " on '" + m_RigidBody.GetName() + "'.");
            }
        }
    }