Home > front end >  vector x, y, z for some reason always equals 0
vector x, y, z for some reason always equals 0

Time:02-04

Vector

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;
        }

I watched the debugger, the vector x, y, z for some reason always equals 0.
What is the problem?

CodePudding user response:

Mutable structs are evil of course, so you need to make Vector3 immutable, with a constructor.

Here is a sample implementation of a Vector3 object that operates as a value type on all three components at the same time.

So you can write

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

and

OldAimPunch = Vector3.Zero;

To define a Vector3 object use one of the following three options

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

and the you can do basic vector algebra

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
}
  •  Tags:  
  • Related