Esempio n. 1
0
    public override void Update()
    {
        base.Update();

        processBodies();

        if (closestBody != null && !closestBody.IsTracked)
        {
            Debug.LogWarning("Force body null");
            closestBody = null;
        }

        closestBody = null;
        float dist = 20;

        foreach (KeyValuePair <ulong, Body> b in bodies)
        {
            float d = getLocalJointPos(b.Value, JointType.SpineBase).magnitude;
            if (d < dist)
            {
                closestBody = b.Value;
                dist        = d;
            }
        }

        bool  shouldSend      = false;
        float deltaUpdateTime = Time.time - lastUpdateTime;

        if (Time.time > lastUpdateTime + 1.0f / sendFreq)
        {
            lastUpdateTime = Time.time;
            shouldSend     = true;
        }

        if (closestBody != null)
        {
            headT.targetLocalPosition      = getLocalJointPos(closestBody, JointType.Head);
            bodyT.targetLocalPosition      = getLocalJointPos(closestBody, JointType.SpineBase);
            leftHandT.targetLocalPosition  = getLocalJointPos(closestBody, JointType.HandLeft);
            rightHandT.targetLocalPosition = getLocalJointPos(closestBody, JointType.HandRight);
            spineShoulder = getAbsoluteJointPos(closestBody, JointType.SpineShoulder);
            bodyTarget.transform.position = new Vector3(bodyT.transform.position.x, 0.01f, bodyT.transform.position.z);

            if (sendOSC && shouldSend)
            {
                sendFeedback();
            }

            if (bodyIsTracked && shouldSend)
            {
                bodySpeed  = Vector3.Distance(bodyT.transform.localPosition, lastBodyPos) / deltaUpdateTime;
                handSpeeds = Vector3.Distance(rightHandT.transform.localPosition, lastHandPos) / deltaUpdateTime;


                if (bodySpeed > bodySpeedThreshold)
                {
                    mood.sendMood(LampMoodController.DELIGHTED, .4f);
                }
                if (handSpeeds > handSpeedThreshold)
                {
                    mood.sendMood(LampMoodController.HAPPY, .4f);
                }
            }

            lastBodyPos = bodyT.transform.localPosition;
            lastHandPos = rightHandT.transform.localPosition;

            Vector2 groundBody   = new Vector2(bodyTarget.position.x, bodyTarget.position.z);
            Vector2 groundOrigin = new Vector2(origin.position.x + 1, origin.position.z);
            angle = Vector2.SignedAngle(groundOrigin, groundBody);
            DataFeedback.sendAngle(angle);

            bodyIsTracked = true;
        }
        else
        {
            spineShoulder = Vector3.Lerp(headT.transform.position, bodyT.transform.position, .3f);

            if (Input.GetMouseButton(0))
            {
                Ray        r = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (Physics.Raycast(r, out hit))
                {
                    bodyTarget.position = new Vector3(hit.point.x, bodyTarget.position.y, hit.point.z);
                    bodyTarget.LookAt(new Vector3(origin.position.x, bodyTarget.position.y, origin.position.z));

                    bodyT.targetLocalPosition      = transform.InverseTransformPoint(bodyTarget.TransformPoint(relTargetBody));
                    headT.targetLocalPosition      = transform.InverseTransformPoint(bodyTarget.TransformPoint(relTargetHead));
                    leftHandT.targetLocalPosition  = transform.InverseTransformPoint(bodyTarget.TransformPoint(relTargetLH));
                    rightHandT.targetLocalPosition = transform.InverseTransformPoint(bodyTarget.TransformPoint(relTargetRH));

                    if (sendOSC && bodyIsTracked && shouldSend)
                    {
                        sendFeedback();
                    }


                    Vector2 groundBody   = new Vector2(bodyTarget.position.x, bodyTarget.position.z);
                    Vector2 groundOrigin = new Vector2(origin.position.x + 1, origin.position.z);
                    angle = Vector2.SignedAngle(groundOrigin, groundBody);
                    DataFeedback.sendAngle(angle);
                }
            }

            if (bodyIsTracked && shouldSend)
            {
                bodySpeed  = Vector3.Distance(bodyT.transform.localPosition, lastBodyPos) / deltaUpdateTime;
                handSpeeds = Vector3.Distance(rightHandT.transform.localPosition, lastHandPos) / deltaUpdateTime;

                if (bodySpeed > bodySpeedThreshold)
                {
                    mood.sendMood(LampMoodController.DELIGHTED, .4f);
                }
                if (handSpeeds > handSpeedThreshold)
                {
                    mood.sendMood(LampMoodController.HAPPY, .4f);
                }
            }

            lastBodyPos = bodyT.transform.localPosition;
            lastHandPos = rightHandT.transform.localPosition;

            bodyIsTracked = simulateBodyIsTracked;
        }


        if (lastBodyIsTracked != bodyIsTracked)
        {
            lastBodyIsTracked = bodyIsTracked;
            if (bodyIsTracked)
            {
                mood.sendMood(LampMoodController.RELAXED, .5f);
            }
            else
            {
                mood.sendMood(LampMoodController.OFF, .7f);
            }
            bodyTarget.GetComponent <Renderer>().material.color = bodyIsTracked ? (simulateTouch ? Color.blue : Color.green) : Color.red;

            if (sendOSC)
            {
                sendTrackFeedback();
            }
            DataFeedback.sendTracked(bodyIsTracked);
        }

        if (!bodyIsTracked)
        {
            mood.sendMood(LampMoodController.OFF, .5f);
        }
        else
        {
            mood.sendMood(LampMoodController.RELAXED, .15f);
        }

        if (Input.GetKeyDown(KeyCode.T))
        {
            simulateTouch = !simulateTouch;
        }
        if (Input.GetKeyDown(KeyCode.B))
        {
            simulateBodyIsTracked = !simulateBodyIsTracked;
        }

        if (lastSimulateBody != simulateBodyIsTracked)
        {
            lastSimulateBody = simulateBodyIsTracked;
            bodyTarget.GetComponent <Renderer>().material.color = simulateBodyIsTracked ? (simulateTouch?Color.blue:Color.green) : Color.red;
        }

        if (lastSimulateTouch != simulateTouch)
        {
            lastSimulateTouch = simulateTouch;
            if (sendOSC)
            {
                sendTouchFeedback();
            }
            TouchHandler.instance.isTouched = simulateTouch;
            mood.sendMood(LampMoodController.DELIGHTED, .8f);
            bodyTarget.GetComponent <Renderer>().material.color = bodyIsTracked ? (simulateTouch ? Color.blue : Color.green) : Color.red;
        }

        foreach (LineRenderer r in lr)
        {
            r.SetPositions(new Vector3[] { spineShoulder, r.transform.position });
        }
    }