gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:16:27 26 4
gpt4 key购买 nike

我正在对 Vector 3 角进行平滑处理,但现在我正在对一个 Vector 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
}

最佳答案

您已经实现了 * 运算符,但没有实现 / 运算符。

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

关于C# 运算符 '/' 不能应用于类型 'Vector3' 和 'float' 的操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33902915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com