Hey everyone! Been a while since I posted here. I was working the other day on some projects, and I have a class I’ve been using for a while. I made it myself, it’s a simple class that’s a Vector3, but it uses integers instead of floats.
I’m just wondering if anyone has any tips on optimizing it? I use this class a LOT (For pathfinding, position storage, ect) and I just need to know if there’s ways to better it. Thanks in advance!
using UnityEngine;
[System.Serializable]
public struct IntPos {
public int x, y, z;
public IntPos(int ix, int iy, int iz) {
x = ix;
y = iy;
z = iz;
}
public Vector3 toVec {
get {
return new Vector3(x, y, z);
}
}
public static IntPos zero = new IntPos(0, 0, 0);
public static IntPos up = new IntPos(0, 1, 0);
public static IntPos right = new IntPos(1, 0, 0);
public static IntPos forward = new IntPos(0, 0, 1);
public static IntPos one = new IntPos(1, 1, 1);
//IntPos - IntPos operators
public static IntPos operator +(IntPos i1, IntPos i2) {
return new IntPos(i1.x + i2.x, i1.y + i2.y, i1.z + i2.z);
}
public static IntPos operator -(IntPos i1, IntPos i2) {
return new IntPos(i1.x - i2.x, i1.y - i2.y, i1.z - i2.z);
}
public static IntPos operator *(IntPos i1, IntPos i2) {
return new IntPos(i1.x * i2.x, i1.y * i2.y, i1.z * i2.z);
}
public static IntPos operator /(IntPos i1, IntPos i2) {
return new IntPos(i1.x / i2.x, i1.y / i2.y, i1.z / i2.z);
}
public static bool operator ==(IntPos i1, IntPos i2) {
return i1.x == i2.x && i1.y == i2.y && i1.z == i2.z;
}
public static bool operator !=(IntPos i1, IntPos i2) {
return !(i1==i2);
}
public bool isNegative {
get {
return x < 0 || y < 0 || z < 0;
}
}
//IntPos - Int Operators
public static IntPos operator +(IntPos i1, int i2) {
return new IntPos(i1.x + i2, i1.y + i2, i1.z + i2);
}
public static IntPos operator -(IntPos i1, int i2) {
return new IntPos(i1.x - i2, i1.y - i2, i1.z - i2);
}
public static IntPos operator *(IntPos i1, int i2) {
return new IntPos(i1.x * i2, i1.y * i2, i1.z * i2);
}
public static IntPos operator /(IntPos i1, int i2) {
return new IntPos(Mathf.FloorToInt((float)i1.x / i2), Mathf.FloorToInt((float)i1.y / i2), Mathf.FloorToInt((float)i1.z / i2));
}
public IntPos normalized {
get {
return IntPos.Vector3ToIntPos(toVec * 1.2f);
}
}
public static float DistanceSqr(IntPos i1, IntPos i2) {
return ( i1 - i2 ).sqrMagnitude;
}
public float sqrMagnitude {
get {
return ( x * x + y * y + z * z );
}
}
public static IntPos Vector3ToIntPos(Vector3 input) {
return new IntPos(Mathf.FloorToInt(input.x), Mathf.FloorToInt(input.y), Mathf.FloorToInt(input.z));
}
public override string ToString() {
return "IntPos(" + x + "," + y + "," + z + ")";
}
public IntPos ToFlatIntPos() {
return new IntPos(x, 0, y);
}
public IntPos ToFlatZIntpos() {
return new IntPos(x, 0, z);
}
}
A few things. For one, the division operator actually converts i1’s values to floats before division. This is to prevent things from truncating to 0, which happens quite a bit. If I don’t do this, I have problems with values that are close to 0 coordinates, since they truncate.
Next, the !=/== operators. I don’t know if those should be better or if they are fine.
The normalized value could be optimized to not do the vector3 conversion… but I barely use it so I don’t see much point.