///Maps a point on the Senso to the corresponding plate
        public SensoPlateSetup GetCorrespondingDirection(Vector2 point)
        {
            List <SensoPlateSetup> candidatePlates = new List <SensoPlateSetup>();

            foreach (SensoPlateSetup plate in hardwarePlates)
            {
                if (point.x >= plate.upperLeftCorner.x && point.y >= plate.upperLeftCorner.y &&
                    point.x <= plate.lowerRightCorner.x && point.y <= plate.lowerRightCorner.y)
                {
                    candidatePlates.Add(plate);
                }
            }
            int count = candidatePlates.Count;

            if (count == 0)
            {
                return(null);
            }
            else if (candidatePlates.Count == 1)
            {
                return(candidatePlates[0]);
            }
            else
            {
                SensoPlateSetup plate   = candidatePlates[0];
                float           minDist = Vector2.Distance(point, plate.Center);
                for (int i = 1; i < count; i++)
                {
                    float dist = Vector2.Distance(point, candidatePlates[i].Center);
                    if (dist < minDist)
                    {
                        minDist = dist;
                        plate   = candidatePlates[i];
                    }
                }
                return(plate);
            }
        }
        public void InitializeWithDefaultValues()
        {
            upperLeftCorner  = new Vector2(0f, 0f);
            lowerRightCorner = new Vector2(3f, 3f);
            hardwarePlates   = new SensoPlateSetup[5];

            //center
            hardwarePlates[0]                  = new SensoPlateSetup();
            hardwarePlates[0].direction        = Direction.Center;
            hardwarePlates[0].upperLeftCorner  = new Vector2(1f, 1f);
            hardwarePlates[0].lowerRightCorner = new Vector2(2f, 2f);

            //up
            hardwarePlates[1]                  = new SensoPlateSetup();
            hardwarePlates[1].direction        = Direction.Up;
            hardwarePlates[1].upperLeftCorner  = new Vector2(0f, 0f);
            hardwarePlates[1].lowerRightCorner = new Vector2(3f, 1f);

            //right
            hardwarePlates[2]                  = new SensoPlateSetup();
            hardwarePlates[2].direction        = Direction.Right;
            hardwarePlates[2].upperLeftCorner  = new Vector2(2f, 0f);
            hardwarePlates[2].lowerRightCorner = new Vector2(3f, 3f);

            //down
            hardwarePlates[3]                  = new SensoPlateSetup();
            hardwarePlates[3].direction        = Direction.Down;
            hardwarePlates[3].upperLeftCorner  = new Vector2(0f, 2f);
            hardwarePlates[3].lowerRightCorner = new Vector2(3f, 3f);

            //left
            hardwarePlates[4]                  = new SensoPlateSetup();
            hardwarePlates[4].direction        = Direction.Left;
            hardwarePlates[4].upperLeftCorner  = new Vector2(0f, 0f);
            hardwarePlates[4].lowerRightCorner = new Vector2(1f, 3f);
        }
Beispiel #3
0
        /// Read Input from Senso Hardware and Simulated Key Input, and calculate all the updated cog, plate states, etc.
        private void ProcessSensoInput()
        {
            _lastValidCog = _cog;
            //1. UPDATE PLATE STATE
            _plates = (Plate[])Hardware.Plates.Clone();

            Direction[] dirs = (Direction[])System.Enum.GetValues(typeof(Direction));
            foreach (Direction dir in dirs)
            {
                if (GetStep(dir))
                {
                    OnStepDown?.Invoke(dir);
                }
                if (GetRelease(dir))
                {
                    OnStepReleased?.Invoke(dir);
                }
            }

            bool keyInputDetected = false;

            if (keyInputType == SimulatedKeyInputType.StepPlate)
            {
                foreach (Direction dir in dirs)
                {
                    KeyCode key = DirectionToKey(dir);
                    if (Input.GetKeyDown(key))
                    {
                        //Debug.Log("step " + dir);
                        _plates[(int)dir] = GetSimulatedPlate(dir, true, true);
                        keyInputDetected  = true;
                    }
                    else if (Input.GetKeyUp(key))
                    {
                        //Debug.Log("release " + dir);
                        _plates[(int)dir] = GetSimulatedPlate(dir, false, true);
                        keyInputDetected  = true;
                    }
                    else if (Input.GetKey(key))
                    {
                        //Debug.Log("down " + dir);
                        _plates[(int)dir] = GetSimulatedPlate(dir, true, false);
                        keyInputDetected  = true;
                    }
                }
            }
            else if (keyInputType == SimulatedKeyInputType.CenterOfGravity)
            {
                foreach (Direction dir in dirs)
                {
                    KeyCode key = DirectionToKey(dir);
                    if (Input.GetKey(key))
                    {
                        keyInputDetected = true;
                        _simulatedCog   += Time.deltaTime * centerOfGravitySpeed * DirectionToVector(dir);
                        if (_simulatedCog.x < sensoHardwareConfiguration.upperLeftCorner.x)
                        {
                            _simulatedCog.x = sensoHardwareConfiguration.upperLeftCorner.x;
                        }
                        else if (_simulatedCog.x > sensoHardwareConfiguration.lowerRightCorner.x)
                        {
                            _simulatedCog.x = sensoHardwareConfiguration.lowerRightCorner.x;
                        }
                        if (_simulatedCog.y < sensoHardwareConfiguration.upperLeftCorner.y)
                        {
                            _simulatedCog.y = sensoHardwareConfiguration.upperLeftCorner.y;
                        }
                        else if (_simulatedCog.y > sensoHardwareConfiguration.lowerRightCorner.y)
                        {
                            _simulatedCog.y = sensoHardwareConfiguration.lowerRightCorner.y;
                        }
                        SensoPlateSetup targetPlate = sensoHardwareConfiguration.GetCorrespondingDirection(_simulatedCog);
                        if (targetPlate != null)
                        {
                            _plates[(int)targetPlate.direction] = new Plate(_simulatedCog.x, _simulatedCog.y, 0.25f);
                        }
                    }
                }
            }

            //Calculate the center of gravity and the cumulated force on all plates
            Vector2 cog    = new Vector2();
            float   weight = 0f;

            for (int i = 0; i < _plates.Length; i++)
            {
                cog    += _plates[i].f * (new Vector2(_plates[i].x, _plates[i].y));
                weight += _plates[i].f;
            }
            if (Mathf.Abs(weight) > playerPresenceForceThreshold)
            {
                _cog = Vector3.Lerp(_lastValidCog, 1 / weight * cog - _sensoCenter, 1 - CenterOfGravityFilterStrength);                 //adjust so center is 0,0
            }
            else
            {
                _cog = _lastValidCog;
            }
            _totalForce = weight;
        }