出于某种原因,向量 x、y、z 始终等于 0

vector x, y, z for some reason always equals 0

向量

public struct Vector3        
{
    public float x;
    public float y;
    public float z;      
}

public static void rcsmode()
{
    Vector3 vPunch;
    Vector3 CurrentViewAngles;
    Vector3 NewViewAngles;
    Vector3 OldAimPunch = new Vector3() { x = 0f, y = 0f, z = 0f };

    // For some reason the value of Vector3 is not being assigned

    if (isRcs)
    {
        int player = mem.Read<int>(client_dll + Offsets.dwLocalPlayer);
        int fire = mem.Read<int>(Offsets.m_iShotsFired);
        int pShotsFired = player + fire;

        if (pShotsFired > 1)
        {
            vPunch = mem.Read<Vector3>(player + Offsets.m_aimPunchAngle);
            int Clientstate = mem.Read<int>(client_dll + Offsets.dwClientState);
            CurrentViewAngles = mem.Read<Vector3>(Clientstate + Offsets.dwClientState_ViewAngles);

            NewViewAngles.x = (CurrentViewAngles.x + OldAimPunch.x) - (vPunch.x * 2);
            NewViewAngles.y = (CurrentViewAngles.y + OldAimPunch.y) - (vPunch.y * 2);                                                      
        }
        else
        {
            OldAimPunch.x = OldAimPunch.y = OldAimPunch.z = 0;
        }

我观察了调试器,向量 x、y、z 出于某种原因总是等于 0。
有什么问题?

可变结构当然是邪恶的,因此您需要使用构造函数使 Vector3 不可变。

这是一个 Vector3 对象的示例实现,它同时在所有三个组件上作为值类型运行。

所以你可以这样写

NewViewAngles = (CurrentViewAngles + OldAimPunch) - (vPunch * 2f);

OldAimPunch = Vector3.Zero;

要定义一个 Vector3 对象,请使用以下三个选项之一

  • var v = new Vector3(x,y,z);
  • var v = Vector3.Cartesian(x,y,z);
  • var v = Vector3.Cylindrical(r,θ,z);

你可以做基本的向量代数

Vector3 a = ... , b = ..., c = ....

c = a + b
c = 2*a - b
c = Vector3.Cross(a, b);
x = Vector3.Dot(a,b);

if(a == b) { ... }

Vector3.cs

public readonly struct Vector3 : IEquatable<Vector3>
{
    public Vector3(float x, float y, float z)
    {
        X = x;
        Y = y;
        Z = z;
    }
    public static readonly Vector3 Zero = new Vector3(0f, 0f, 0f);
    public static readonly Vector3 UnitX = new Vector3(1f, 0f, 0f);
    public static readonly Vector3 UnitY = new Vector3(0f, 1f, 0f);
    public static readonly Vector3 UnitZ = new Vector3(0f, 0f, 1f);
    public static Vector3 Cartesian(float x, float y, float z)
        => new Vector3(x, y, z);
    public static Vector3 Cylindrical(float r, float θ, float z)
        => new Vector3(r * (float)Math.Cos(θ), r * (float)Math.Sin(θ), z);
    public float X { get; }
    public float Y { get; }
    public float Z { get; }
    public float SumSquares { get => X * X + Y * Y + Z * Z; }
    public float Magnitude { get => (float)Math.Sqrt(SumSquares); }
    public Vector3 Normalized()
    {
        float m2 = SumSquares;
        if (m2 > 0f)
        {
            return Scale(1 / (float)Math.Sqrt(m2), this);
        }
        return this;
    }
    public override string ToString() => $"({X},{Y},{Z})";

    #region Algebra
    public static Vector3 Add(Vector3 vector, Vector3 other) 
        => new Vector3(vector.X + other.X, vector.Y + other.Y, vector.Z + other.Z);
    public static Vector3 Subtract(Vector3 vector, Vector3 other)
        => new Vector3(vector.X - other.X, vector.Y - other.Y, vector.Z - other.Z);
    public static Vector3 Scale(float factor, Vector3 vector) => new Vector3(factor * vector.X, factor * vector.Y, factor * vector.Z);
    public static float Dot(Vector3 vector, Vector3 other) => vector.X * other.X + vector.Y * other.Y + vector.Z * other.Z;
    public static Vector3 Cross(Vector3 vector, Vector3 other)
      => new Vector3(
          vector.Y * other.Z - vector.Z * other.Y, 
          vector.Z * other.X - vector.X * other.Z,
          vector.X * other.Y - vector.Y * other.X);
    #endregion

    #region Operators
    public static Vector3 operator +(Vector3 a, Vector3 b) => Add(a, b);
    public static Vector3 operator -(Vector3 a) => Scale(-1, a);
    public static Vector3 operator -(Vector3 a, Vector3 b) => Subtract(a, b);
    public static Vector3 operator *(float x, Vector3 b) => Scale(x,b);
    public static Vector3 operator *(Vector3 a, float x) => Scale(x,a);
    public static Vector3 operator /(Vector3 a, float x) => Scale(1 / x, a);
    public static float operator *(Vector3 a, Vector3 b) => Dot(a, b);
    public static Vector3 operator ^(Vector3 a, Vector3 b) => Cross(a, b);
    #endregion

    #region IEquatable Members
    /// <summary>
    /// Equality overrides from <see cref="System.Object"/>
    /// </summary>
    /// <param name="obj">The object to compare this with</param>
    /// <returns>False if object is a different type, otherwise it calls <code>Equals(Vector3)</code></returns>
    public override bool Equals(object obj)
    {
        if (obj is Vector3 other)
        {
            return Equals(other);
        }
        return false;
    }

    public static bool operator ==(Vector3 target, Vector3 other) { return target.Equals(other); }
    public static bool operator !=(Vector3 target, Vector3 other) { return !(target == other); }


    /// <summary>
    /// Checks for equality among <see cref="Vector3"/> classes
    /// </summary>
    /// <param name="other">The other <see cref="Vector3"/> to compare it to</param>
    /// <returns>True if equal</returns>
    public bool Equals(Vector3 other)
    {
        return X.Equals(other.X)
            && Y.Equals(other.Y)
            && Z.Equals(other.Z);
    }

    /// <summary>
    /// Calculates the hash code for the <see cref="Vector3"/>
    /// </summary>
    /// <returns>The int hash value</returns>
    public override int GetHashCode()
    {
        unchecked
        {
            int hc = -1817952719;
            hc = (-1521134295) * hc + X.GetHashCode();
            hc = (-1521134295) * hc + Y.GetHashCode();
            hc = (-1521134295) * hc + Z.GetHashCode();
            return hc;
        }
    }

    #endregion
}