Beispiel #1
0
    NormalizedGesture Normalize(List <Vector2> gesture)
    {
        NormalizedGesture normalized     = new NormalizedGesture();
        List <Vector2>    normalizedList = new List <Vector2>(gesture);

        //sacamos centro de puntos y normalizamos posición
        //Vector2 center = Vector2.zero;
        //foreach(Vector2 v in gesture) {
        //    center += v;
        //}
        //center = center / ((float)gesture.Count);
        //m_gesturePosition = center;
        //normalized.originalCenter = center;
        Vector2 c = normalizedList[0];

        for (int i = 0; i < normalizedList.Count; i++)
        {
            normalizedList[i] = normalizedList[i] - c;
        }

        //sacamos angulo y normalizamos rotación;
        float angle = Vector2.Angle(gesture[1] - gesture[0], Vector2.up);

        if ((gesture[1] - gesture[0]).x < 0)
        {
            angle = 360 - angle;
        }
        for (int i = 0; i < normalizedList.Count; i++)
        {
            normalizedList[i] = Rotate(normalizedList[i], angle);
        }
        normalized.rotation = angle;

        //normalized inversion;
        if ((normalizedList[2] - normalizedList[1]).x < 0)
        {
            for (int i = 0; i < normalizedList.Count; i++)
            {
                normalizedList[i] = new Vector2(-normalizedList[i].x, normalizedList[i].y);
            }
            normalized.inverted = true;
        }
        else
        {
            normalized.inverted = false;
        }

        normalized.gesture = normalizedList;
        return(normalized);
    }
Beispiel #2
0
    //string gestureName = "Triangulo";
    void Update()
    {
        Vector2 thisDelta = ((Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - lastMousePos) / Time.deltaTime;

        if (Input.GetMouseButtonDown(0))
        {
            currentGesture = new List <Vector2>();
        }
        if (Input.GetMouseButton(0))
        {
            Vector3 instpos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z - Camera.main.transform.position.z));
            RecordPosition(instpos, thisDelta, lastDelta, currentGesture);
            //RecordPosition(Camera.main.ScreenToViewportPoint(Input.mousePosition) , thisDelta, lastDelta, currentGesture);
            //RecordPosition(Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z - Camera.main.transform.position.z)));
        }
        if (Input.GetMouseButtonUp(0))
        {
            currentGesture = OptimizeGesture(currentGesture);
            GetGestureMesurements(currentGesture);
            currentNormalized = Normalize(currentGesture);
            finalGesture      = Simplification(currentNormalized.gesture);
            //scr_magicManager.magicName = Recognition(finalGesture); //Se clicka despues
            m_gestureName = Recognition(finalGesture);
            scr_magicManager.SpawnMagic(m_gestureName, m_gesturePosition, m_gestureAngle, m_gestureScale); //Aparece en un punto segun dibujas
        }
        lastDelta = thisDelta;
        //lastMousePos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
        lastMousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z - Camera.main.transform.position.z));


        if (currentGesture != null)
        {
            Vector2 vP = currentGesture[0];
            for (int i = 1; i < currentGesture.Count; i++)
            {
                if (vP != null)
                {
                    Debug.DrawLine(vP, currentGesture[i], Color.red);
                }
                vP = currentGesture[i];
            }
        }

        if (currentNormalized.gesture != null)
        {
            Vector2 vP = currentNormalized.gesture[0];
            for (int i = 1; i < currentNormalized.gesture.Count; i++)
            {
                if (vP != null)
                {
                    Debug.DrawLine(vP, currentNormalized.gesture[i], Color.green);
                }
                vP = currentNormalized.gesture[i];
            }
        }

        if (finalGesture != null)
        {
            Vector2 vP = new Vector2(0, 0);
            for (int i = 0; i < finalGesture.Count; i++)
            {
                if (vP != null)
                {
                    Debug.DrawLine(vP, finalGesture[i] + vP, Color.blue);
                }

                vP = vP + finalGesture[i];
            }
        }
    }