Ejemplo n.º 1
0
    public void Update()
    {
        // Play nearest node :
        GameObject playingNode     = null;
        float      minDistance     = 0F;
        float      currentDistance = 0F;

        foreach (GameObject node in graphController.getVisibleNode())
        {
            currentDistance = Vector3.Distance(camera.transform.position, node.transform.position);

            // First iteration
            if (minDistance == 0F)
            {
                minDistance = currentDistance;
            }

            else
            {
                if (currentDistance < minDistance)
                {
                    playingNode = node;
                    minDistance = currentDistance;
                }
            }
        }
        if (playingNode)
        {
            playingNode.GetComponent <NodeController> ().PlayMusic(askForValidation);
        }



        Frame frame = controller.Frame();

        HandList hands = frame.Hands;

        if (hands.Count == 1)
        {
            foreach (Hand hand in hands)
            {
                if (hand.GrabStrength == 0 || hand.GrabStrength == 1)
                {
                    if (!gestureDetected)
                    {
                        gestureDetected  = true;
                        startGestureTime = Time.time;
                    }
                    else if (Time.time - startGestureTime > 1)
                    {
                        // Driving enabled : handling user control

                        if (hand.GrabStrength == 0 && !askForValidation)
                        {
                            // Open Hand : driving
                            if (!driving)
                            {
                                drivingOriginPosition  = hand.PalmPosition;
                                drivingOriginDirection = hand.Direction;
                                startDrivingMode(hand);
                            }

                            // Transform commands :
                            float deltaX = drivingOriginPosition.x - hand.PalmPosition.x;
                            float deltaY = drivingOriginPosition.y - hand.PalmPosition.y;
                            float deltaZ = drivingOriginPosition.z - hand.PalmPosition.z;

                            if (Math.Abs(deltaX) > drivingDistance)
                            {
                                transform.position -= transform.right * drivingVelocity * (deltaX / drivingDistance);
                            }
                            if (Math.Abs(deltaY) > drivingDistance)
                            {
                                transform.position -= transform.up * drivingVelocity * (deltaY / drivingDistance);
                            }
                            if (Math.Abs(deltaZ) > drivingDistance)
                            {
                                transform.position += transform.forward * drivingVelocity * (deltaZ / drivingDistance);
                            }


                            // Rotation commands :
                            if (Math.Abs(hand.Direction.Pitch) > drivingAngle)
                            {
                                float sign = hand.Direction.Pitch / Math.Abs(hand.Direction.Pitch);
                                transform.Rotate(-1 * sign * Math.Abs(hand.Direction.Pitch) / drivingAngle, 0, 0);
                            }
                            if (Math.Abs(hand.Direction.Yaw) > drivingAngle)
                            {
                                float sign = hand.Direction.Yaw / Math.Abs(hand.Direction.Yaw);
                                transform.Rotate(0, 1 * sign * Math.Abs(hand.Direction.Yaw) / drivingAngle, 0);
                            }
                        }
                        else if (hand.GrabStrength == 1 && !grabbing)
                        {
                            grabbing = true;
                            if (!askForValidation)
                            {
                                askForValidation = true;
                            }
                            else
                            {
                                askForValidation = false;
                            }
                        }


                        // TODO
                    }
                }
                else
                {
                    startGestureTime = 0;
                    gestureDetected  = false;
                    stopDrivingMode();
                    grabbing = false;
                    zooming  = false;
                }
            }
        }
        else if (hands.Count == 2)
        {
            if (hands[0].PinchStrength == 1 || hands[1].PinchStrength == 1)
            {
                if (!gestureDetected)
                {
                    gestureDetected  = true;
                    startGestureTime = Time.time;
                }
                else if (Time.time - startGestureTime > 1)
                {
                    zooming = true;

                    // Driving enabled : handling user control

                    // TODO
                }
            }
            else
            {
                startGestureTime = 0;
                gestureDetected  = false;
                stopDrivingMode();
                grabbing = false;
                zooming  = false;
            }
        }
        else
        {
            startGestureTime = 0;
            gestureDetected  = false;
            stopDrivingMode();
            grabbing = false;
            zooming  = false;
        }
    }