Пример #1
0
    /// <summary>
    /// Calculates motor speed values using deviation as direction(speed balance between motor A and B) and touch field movement distance (motor power multiplier)
    /// <returns>A normalized(0-100, int) 2 dimensional vector containing the calculated speed on both motors ('x' for motor A and 'y' for motor B)</returns>
    /// </summary>
    public Vector2Int GetMotorSpeedValues()
    {
        float   power       = Mathf.Clamp01(PointerMovementDistanceNormalized); // Treat normalized distance (between touch start and touch end) as a motor power multiplier
        Vector2 motorValues = new Vector2(power, power);

        Vector2 deviation = PointerDeviation; // Get pointer relation between the touch field's center position

        if (deviation.x < 0f)
        {
            motorValues.x *= deviation.y;                  // Pointer is on the left side
        }
        else if (deviation.x > 0f)
        {
            motorValues.y *= deviation.y; // Pointer is on the right side
        }
        if (deviation.y < 0f)             // Pointer is below the center
        {
            // Negate the motor power to make them run backwards
            motorValues.x = MathTools.Neg(motorValues.x);
            motorValues.y = MathTools.Neg(motorValues.y);
        }

        // Clamp motor values to an interval
        if (m_ClampToIntervalToggle.isOn && m_MotorSpeedClampInterval > 0f)
        {
            float msci = m_MotorSpeedClampInterval / 100f; // 'Convert' from percentage to decimal form
            motorValues.x = Mathf.Round(motorValues.x / msci) * msci;
            motorValues.y = Mathf.Round(motorValues.y / msci) * msci;
        }

        // Clamp motor values to each other, if they are close enough
        if (m_ClampMotorValuesToggle.isOn && m_MotorSpeedClampDelta > 0f)
        {
            float mscd = m_MotorSpeedClampDelta / 100f; // 'Convert' from percentage to decimal form
            // Clamp them to zero, if near center
            if (MathTools.Approximately(motorValues.x, 0f, mscd) && MathTools.Approximately(motorValues.y, 0f, mscd))
            {
                motorValues.y = motorValues.x = 0f;
            }
            else if (MathTools.Approximately(motorValues.x, motorValues.y, mscd))
            {
                if (deviation.y > 0)
                {
                    // Clamp to maximum value if going forwards
                    motorValues.y = motorValues.x = Mathf.Max(motorValues.x, motorValues.y);
                }
                else if (deviation.y < 0f)
                {
                    // Clamp to minumum value if going backwards
                    motorValues.y = motorValues.x = Mathf.Min(motorValues.x, motorValues.y);
                }
            }
        }

        motorValues *= 100f;                                                                                            // 'Convert' decimal to percent form
        return(new Vector2Int((int)Mathf.Clamp(motorValues.x, -100, 100), (int)Mathf.Clamp(motorValues.y, -100, 100))); // Clamp values to min/max and return them as int version of Vector2
    }