Пример #1
0
        /// <summary>
        /// Ends all collision sessions currently running.
        /// </summary>
        void EndAllCollisions(CollisionType type)
        {
            MLResult result;

            if (type.HasFlag(CollisionType.Hard))
            {
                foreach (KeyValuePair <int, ulong> collision in hardCollisionMap)
                {
                    result = MLMovement.EndCollision(sessionHandle, collision.Value);
                    if (!result.IsOk)
                    {
                        Debug.LogErrorFormat("MLMovementBehavior.EndAllCollisions failed to end hard collision session, disabling script. Reason: {0}", result);
                        enabled = false;
                        return;
                    }
                }
                hardCollisionMap.Clear();
            }

            if (type.HasFlag(CollisionType.Soft))
            {
                foreach (KeyValuePair <int, ulong> collision in softCollisionMap)
                {
                    result = MLMovement.EndCollision(sessionHandle, collision.Value);
                    if (!result.IsOk)
                    {
                        Debug.LogErrorFormat("MLMovementBehavior.EndAllCollisions failed to end soft collision session, disabling script. Reason: {0}", result);
                        enabled = false;
                        return;
                    }
                }
                softCollisionMap.Clear();
            }
        }
Пример #2
0
        /// <summary>
        /// Ends the movement session if a session was previously active.
        /// </summary>
        /// <param name="forceEnd"> Forces the movement to end and not return pending if it's true. </param>
        public void EndMovementSession(bool forceEnd = false)
        {
            if (movementSessionState == MovementSessionState.ShutDown)
            {
                Debug.LogWarning("MLMovementBehavior.EndMovementSession failed to end the movement session. Reason: Movement session was never started.");
                return;
            }

            if (forceEnd)
            {
                EndAllCollisions(CollisionType.All);
            }
            else
            {
                EndAllCollisions(CollisionType.Hard);
            }

            // Passing float.MaxValue if forceEnd is true to ensure movement ends with a Timeout result.
            MLResult result = MLMovement.End(sessionHandle, forceEnd ? float.MaxValue : Time.deltaTime, out sessionObject);

            if (result.IsOk || result.Code == MLResultCode.Pending)
            {
                if (AllowCollision)
                {
                    objectRigidbody.position        = sessionObject.ObjectPosition;
                    objectRigidbody.rotation        = sessionObject.ObjectRotation.normalized;
                    objectRigidbody.velocity        = Vector3.zero;
                    objectRigidbody.angularVelocity = Vector3.zero;
                }
                else
                {
                    transform.position = sessionObject.ObjectPosition;
                    transform.rotation = sessionObject.ObjectRotation.normalized;
                }

                if (result.IsOk)
                {
                    sessionHandle        = MagicLeapInternal.MagicLeapNativeBindings.InvalidHandle;
                    movementSessionState = MovementSessionState.ShutDown;
                }
                else
                {
                    movementSessionState = MovementSessionState.PendingShutDown;
                }
            }
            else if (result.Code == MLResultCode.Timeout)
            {
                hardCollisionMap.Clear();
                softCollisionMap.Clear();
                sessionHandle        = MagicLeapInternal.MagicLeapNativeBindings.InvalidHandle;
                movementSessionState = MovementSessionState.ShutDown;
            }
            else
            {
                Debug.LogErrorFormat("MLMovementBehavior.EndMovementSession failed to end the movement session, disabling script. Reason: {0}", result);
                enabled = false;
                return;
            }
        }
Пример #3
0
        /// <summary>
        /// Updates an existing hard collision session with new info.
        /// </summary>
        /// <param name="collision">The latest collision data.</param>
        void UpdateHardCollision(ref Collision collision)
        {
            int objId = collision.gameObject.GetInstanceID();

            MLResult result = MLMovement.UpdateHardCollision(sessionHandle, hardCollisionMap[objId], collision.contacts[0].normal);

            if (!result.IsOk)
            {
                Debug.LogErrorFormat("MLMovementBehavior.UpdateHardCollision failed to update a hard collision, disabling script. Reason: {0}", result);
                enabled = false;
                return;
            }
        }
        void Awake()
        {
            if (UseDefaultSettings)
            {
                MLResult result = MLMovement.GetDefaultSettings(out Settings);

                if (!result.IsOk)
                {
                    Debug.LogErrorFormat("MLMovementSeetingsManager.Awake failed to initialize settings to default settings, disabling script. Reason: {0}", result);
                    enabled = false;
                    return;
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Ends the collision matching the id and handle passed.
        /// </summary>
        /// <param name="collisionId">The id of the collider object.</param>
        /// <param name="collisionHandle">The handle for the collision session.</param>
        void EndCollision(CollisionType type, int collisionId, ulong collisionHandle)
        {
            MLResult result = MLMovement.EndCollision(sessionHandle, collisionHandle);

            if (type == CollisionType.Hard)
            {
                hardCollisionMap.Remove(collisionId);
            }
            else if (type == CollisionType.Soft)
            {
                softCollisionMap.Remove(collisionId);
            }

            if (!result.IsOk)
            {
                Debug.LogErrorFormat("MLMovementBehavior.EndCollision failed to end collision session, disabling script. Reason: {0}", result);
                enabled = false;
                return;
            }
        }
Пример #6
0
        /// <summary>
        /// Starts a new soft collision session given some collider data.
        /// </summary>
        /// <param name="other">The collider data.</param>
        void StartSoftCollision(ref Collider other)
        {
            ulong collisionHandle = MagicLeapInternal.MagicLeapNativeBindings.InvalidHandle;

            Vector3 thisCenter       = objectCollider.bounds.center;
            Vector3 otherCenter      = other.bounds.center;
            float   maxDistance      = Vector3.Distance(thisCenter, otherCenter);
            float   closesetDistance = maxDistance * (other.gameObject.GetComponent <MLMovementCollider>().MaxDepth / 100.0f);

            MLResult result = MLMovement.StartSoftCollision(sessionHandle, otherCenter, closesetDistance, maxDistance, out collisionHandle);

            if (!result.IsOk)
            {
                Debug.LogErrorFormat("MLMovementBehavior.StartSoftCollision failed to start a soft collision, disabling script. Reason: {0}", result);
                enabled = false;
                return;
            }

            softCollisionMap.Add(other.gameObject.GetInstanceID(), collisionHandle);
        }
Пример #7
0
        /// <summary>
        /// Provides the string value for any MLResult.Code.
        /// </summary>
        /// <param name="resultCode">The code to convert into a string value.</param>
        /// <returns>The string value of the given MLResult.Code.</returns>
        public static string CodeToString(MLResult.Code resultCode)
        {
            string codeString = string.Empty;

            switch ((CodePrefix)((int)resultCode >> 16))
            {
            case CodePrefix.MLResultGlobal:
            case CodePrefix.MLSnapshotResult:
                codeString = Marshal.PtrToStringAnsi(MagicLeapNativeBindings.MLSnapshotGetResultString(resultCode));
                break;

            case CodePrefix.MLAudioResult:
                codeString = Marshal.PtrToStringAnsi(MLAudio.GetResultString(resultCode));
                break;

            case CodePrefix.MLMediaDRMResult:
            case CodePrefix.MLMediaGenericResult:
            case CodePrefix.MLMediaPlayerResult:
            case CodePrefix.MLMediaResult:
                codeString = Marshal.PtrToStringAnsi(MLMediaPlayer.GetResultString(resultCode));
                break;

            case CodePrefix.MLDispatchResult:
                codeString = Marshal.PtrToStringAnsi(MLDispatch.GetResultString(resultCode));
                break;

            case CodePrefix.MLIdentityResult:
                codeString = Marshal.PtrToStringAnsi(MLIdentity.GetResultString(resultCode));
                break;

            case CodePrefix.MLPassableWorldResult:
                codeString = Marshal.PtrToStringAnsi(MLPersistentCoordinateFrames.GetResultString(resultCode));
                break;

            case CodePrefix.MLTokenAgentResult:
                codeString = Marshal.PtrToStringAnsi(MLTokenAgent.GetResultString(resultCode));
                break;

            case CodePrefix.MLPrivilegesResult:
                codeString = Marshal.PtrToStringAnsi(MLPrivileges.GetResultString(resultCode));
                break;

            case CodePrefix.MLContactsResult:
                codeString = Marshal.PtrToStringAnsi(MLContacts.GetResultString(resultCode));
                break;

            case CodePrefix.MLLocationResult:
                codeString = Marshal.PtrToStringAnsi(MLLocation.GetResultString(resultCode));
                break;

            case CodePrefix.MLNetworkingResult:
                codeString = Marshal.PtrToStringAnsi(MLNetworkingNativeBindings.MLNetworkingGetResultString(resultCode));
                break;

            case CodePrefix.MLMovementResult:
                codeString = Marshal.PtrToStringAnsi(MLMovement.GetResultString(resultCode));
                break;

            case CodePrefix.MLConnectionsResult:
                codeString = Marshal.PtrToStringAnsi(MLConnections.GetResultString(resultCode));
                break;

            case CodePrefix.MLSecureStorageResult:
                codeString = Marshal.PtrToStringAnsi(MLSecureStorageNativeBindings.MLSecureStorageGetResultString(resultCode));
                break;

            case CodePrefix.MLAppConnect:
                codeString = Marshal.PtrToStringAnsi(MLAppConnectNativeBindings.MLAppConnectGetResultString(resultCode));
                break;

            case CodePrefix.MLWebRTC:
                codeString = Marshal.PtrToStringAnsi(MLWebRTC.NativeBindings.MLWebRTCGetResultString(resultCode));
                break;

            case CodePrefix.MLBluetoothGattResult:
                codeString = Marshal.PtrToStringAnsi(MLBluetoothLE.NativeBindings.MLBluetoothGattGetResultString(resultCode));
                break;

            default:
                // This will catch any unknown/invalid return values.
                codeString = MagicLeapNativeBindings.MLGetResultString(resultCode);
                break;
            }

            return(codeString);
        }
Пример #8
0
        /// <summary>
        /// Updates object and movement session with latest data and applies depth and rotation changes
        /// to the object.
        /// </summary>
        void Update()
        {
            if (movementSessionState == MovementSessionState.Running)
            {
                MLResult result;

                if (UseTouchForRotation &&
                    ControllerHandler.ConnectedController != null &&
                    ControllerHandler.ConnectedController.TouchpadGesture.Type == MLInputControllerTouchpadGestureType.RadialScroll)
                {
                    float   deltaRadians = 0.0f;
                    Vector2 newPos       = new Vector2(ControllerHandler.ConnectedController.Touch1PosAndForce.x, ControllerHandler.ConnectedController.Touch1PosAndForce.y);

                    if (ControllerHandler.ConnectedController.TouchpadGesture.Direction == MLInputControllerTouchpadGestureDirection.Clockwise)
                    {
                        deltaRadians = Mathf.Min(Vector2.SignedAngle(previousTouchPosition, newPos), MaxRotationDelta) * Mathf.Deg2Rad;
                    }
                    else
                    {
                        deltaRadians = Mathf.Max(Vector2.SignedAngle(previousTouchPosition, newPos), -MaxRotationDelta) * Mathf.Deg2Rad;
                    }

                    previousTouchPosition = newPos;

                    result = MLMovement.ChangeRotation(sessionHandle, deltaRadians);
                    if (!result.IsOk)
                    {
                        Debug.LogErrorFormat("MLMovementBehavior.Update failed to change the object rotation in movement session, disabling script. Reason: {0}", result);
                        enabled = false;
                        return;
                    }
                }

                else if (UseTouchForDepth &&
                         ControllerHandler.ConnectedController != null &&
                         ControllerHandler.ConnectedController.Touch1Active &&
                         Mathf.Abs(ControllerHandler.ConnectedController.Touch1PosAndForce.y) > Mathf.Abs(ControllerHandler.ConnectedController.Touch1PosAndForce.x))
                {
                    float unsignedDepthDelta = MaxDepthDelta * Mathf.Pow(ControllerHandler.ConnectedController.Touch1PosAndForce.z, 3.0f);

                    result = MLMovement.ChangeDepth(sessionHandle, (ControllerHandler.ConnectedController.Touch1PosAndForce.y > 0.0f) ? unsignedDepthDelta : -unsignedDepthDelta);
                    if (!result.IsOk)
                    {
                        Debug.LogErrorFormat("MLMovementBehavior.Update failed to change the object depth in movement session, disabling script. Reason: {0}", result);
                        enabled = false;
                        return;
                    }
                }

                switch (sessionInteractionMode)
                {
                case MovementInteractionMode.ThreeDof:
                {
                    session3DofControls.HeadposePosition = mainCamera.transform.position;
                    if (sessionInputDriverType == MovementInputDriverType.Controller)
                    {
                        session3DofControls.ControlRotation = (ControllerHandler.ConnectedController != null) ? ControllerHandler.ConnectedController.Orientation.normalized : Quaternion.identity;
                    }
                    else
                    {
                        session3DofControls.ControlRotation = mainCamera.transform.rotation.normalized;
                    }

                    result = MLMovement.Update3Dof(sessionHandle, in session3DofControls, Time.deltaTime, ref sessionObject);
                    if (!result.IsOk)
                    {
                        Debug.LogErrorFormat("MLMovementBehavior.Update failed to update the current 3dof movement session, disabling script. Reason: {0}", result);
                        enabled = false;
                        return;
                    }

                    break;
                }

                case MovementInteractionMode.SixDof:
                {
                    session6DofControls.HeadposePosition = mainCamera.transform.position;
                    session6DofControls.HeadposeRotation = mainCamera.transform.rotation.normalized;
                    if (sessionInputDriverType == MovementInputDriverType.Controller)
                    {
                        session6DofControls.ControlPosition = (ControllerHandler.ConnectedController != null) ? ControllerHandler.ConnectedController.Position : Vector3.zero;
                        session6DofControls.ControlRotation = (ControllerHandler.ConnectedController != null) ? ControllerHandler.ConnectedController.Orientation.normalized : Quaternion.identity;
                    }
                    else
                    {
                        session6DofControls.ControlPosition = mainCamera.transform.position;
                        session6DofControls.ControlRotation = mainCamera.transform.rotation.normalized;
                    }

                    result = MLMovement.Update6Dof(sessionHandle, in session6DofControls, Time.deltaTime, ref sessionObject);
                    if (!result.IsOk)
                    {
                        Debug.LogErrorFormat("MLMovementBehavior.Update failed to update the current 6dof movement session, disabling script. Reason: {0}", result);
                        enabled = false;
                        return;
                    }

                    break;
                }

                default:
                {
                    Debug.LogError("MLMovementBehavior.Update failed to update the movement session, disabling script. Reason: Invalid InteractionMode parameter.");
                    enabled = false;
                    return;
                }
                }

                if (AllowCollision)
                {
                    objectRigidbody.position        = sessionObject.ObjectPosition;
                    objectRigidbody.rotation        = sessionObject.ObjectRotation.normalized;
                    objectRigidbody.velocity        = Vector3.zero;
                    objectRigidbody.angularVelocity = Vector3.zero;
                }
                else
                {
                    transform.position = sessionObject.ObjectPosition;
                    transform.rotation = sessionObject.ObjectRotation.normalized;
                }
            }

            else if (movementSessionState == MovementSessionState.PendingShutDown)
            {
                EndMovementSession();
            }
        }