C#运算符'/'不能应用于'Vector3'和'float'类型的操作数

时间:2015-11-24 20:11:09

标签: c#

我正在平滑Vector 3角度,但是现在我采用了我的光滑矢量3角度。现在,平滑因子是0-1,1表示不平滑。但是我希望平滑因子为0-100,而100则不平滑。所以我想出了这个希望它会起作用。

        public static Vector3 SmoothAngle(this Vector3 src, Vector3 dest, float smoothAmount)
    {
        Vector3 SmoothedAngle;
        SmoothedAngle = dest - src;
        SmoothedAngle = ClampAngle(SmoothedAngle);
        SmoothedAngle = src + SmoothedAngle / 100f * (100f - smoothAmount);
        Console.WriteLine(SmoothedAngle);
        return ClampAngle(SmoothedAngle);
    }

但我收到了标题中所述的错误。 这是我的矢量3类。

public struct Vector3
{
    #region VARIABLES
    public float X;
    public float Y;
    public float Z;
    #endregion

    #region PROPERTIES
    public static Vector3 Zero => new Vector3(0, 0, 0);
    public static Vector3 UnitX => new Vector3(1, 0, 0);
    public static Vector3 UnitY => new Vector3(0, 1, 0);
    public static Vector3 UnitZ => new Vector3(0, 0, 1);

    #endregion

    #region CONSTRUCTOR
    public Vector3(float x, float y, float z)
    {
        X = x;
        Y = y;
        Z = z;
    }
    public Vector3(Vector3 vec) : this(vec.X, vec.Y, vec.Z) { }
    public Vector3(float[] values) : this(values[0], values[1], values[2]) { }
    #endregion

    #region METHODS
    public float Length()
    {
        return (float)Math.Abs(Math.Sqrt(Math.Pow(X, 2) + Math.Pow(Y, 2) + Math.Pow(Z, 2)));
    }
    public float DistanceTo(Vector3 other)
    {
        return (this - other).Length();
    }

    public override bool Equals(object obj)
    {
        Vector3 vec = (Vector3)obj;
        return GetHashCode() == vec.GetHashCode();
    }

    public override int GetHashCode()
    {
        return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode();
    }

    public override string ToString()
    {
        return $"[X={X.ToString()}, Y={Y.ToString()}, Z={Z.ToString()}]";
    }
    #endregion

    #region OPERATORS
    public static Vector3 operator +(Vector3 v1, Vector3 v2)
    {
        return new Vector3(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z);
    }
    public static Vector3 operator -(Vector3 v1, Vector3 v2)
    {
        return new Vector3(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z);
    }
    public static Vector3 operator *(Vector3 v1, float scalar)
    {
        return new Vector3(v1.X * scalar, v1.Y * scalar, v1.Z * scalar);
    }
    public static bool operator ==(Vector3 v1, Vector3 v2)
    {
        return v1.X == v2.X && v1.Y == v2.Y && v1.Z == v2.Z;
    }
    public static bool operator !=(Vector3 v1, Vector3 v2)
    {
        return !(v1 == v2);
    }
    public float this[int i]
    {
        get
        {
            switch (i)
            {
                case 0:
                    return X;
                case 1:
                    return Y;
                case 2:
                    return Z;
                default:
                    throw new IndexOutOfRangeException();
            }
        }
        set
        {
            switch (i)
            {
                case 0:
                    X = value;
                    break;
                case 1:
                    Y = value;
                    break;
                case 2:
                    Z = value;
                    break;
                default:
                    throw new IndexOutOfRangeException();
            }
        }
    }
    #endregion
}

1 个答案:

答案 0 :(得分:4)

您已实施*运算符,但未实现/运算符。

public static Vector3 operator /(Vector3 v1, float scalar)
{
    return new Vector3(v1.X / scalar, v1.Y / scalar, v1.Z / scalar);
}