Should we go for our Vector2Int/Vector3Int implementation or just use built-in float based vectors?

So in one of our projects we're using Vector2/Vector3's a lot but we do only use integer/byte values mostly. So up to the point we've implemented our Vector2Int, Vector3Int, Vector2Byte and so structures;

``````public struct Vector2Int
{
public int X;
public int Z;

public Vector2Int(int x, int z)
{
this.X = x;
this.Z = z;
}

public override bool Equals(object obj)
{
if (obj is Vector2Int) return this.Equals((Vector2Int)obj);
else return false;
}

public bool Equals(Vector2Int other)
{
return ((this.X == other.X) && (this.Z == other.Z));
}

public static bool operator ==(Vector2Int value1, Vector2Int value2)
{
return ((value1.X == value2.X) && (value1.Z == value2.Z));
}

public static bool operator !=(Vector2Int value1, Vector2Int value2)
{
if (value1.X == value2.X) return value1.Z != value2.Z;
return true;
}

public override int GetHashCode()
{
return (this.X.GetHashCode() + this.Z.GetHashCode());
}

public override string ToString()
{
return string.Format("{{X:{0} Z:{1}}}", this.X, this.Z);
}
}
``````

But i don't know if we're on right track? Should we just better use XNA's float based vectors because of performance-wise concerns?

On the other hand a Vector2Byte or Vector3Bytes consumes less space which is also quite important when you use so much vectors.

I've searched for tests / information on XNA's built-in vector performance but couldn't find a bit even on it.

I'm happy to hear your experiences.

-
This might be a better question for stackoverflow.com – Ricket Apr 12 '11 at 14:11
Should we move it? – HuseyinUslu Apr 12 '11 at 15:21
@Ricket I disagree, being that the OP is specifically looking for a comparison vs XNA's vectors. If it were more generalized, sure... – octal9 Apr 12 '11 at 15:47
@octal9, when I see this question, I think it generalizes to something like "If I rewrite this C# class with the same functionality, assuming my code is optimal C#, will it be somehow slower than the native class?" I don't see how this would be an XNA-specific question, other than the class in question is a part of XNA, so I think the people in Stack Overflow with deep understanding of the inner workings of C# would be helpful regardless of their XNA experience. I don't feel strongly enough about this to move it to SO, though. – Ricket Apr 12 '11 at 18:54
I just wonder how you end up using so many vectors that this makes a noticeable impact on memory consumption? – aaaaaaaaaaaa Apr 12 '11 at 21:15

This comes down to use the appropriate data structure for the task.

If you are using this for internal functionality key to your game and such then your own data types that hold the values in a format you are using them would make sense.

If however this is dealing with rendering and graphics hardware then you will want to use floats as that is what the hardware is generally built to use and accelerated for.

Hope this helps.

-
+1 for the note about graphics APIs. – Josh Petrie Apr 12 '11 at 17:47

I use integer vectors when my concern is in "which bucket" something occupies or "from which bucket onward" something occupies, and float vectors when my concern is "where in space" something "is."

Integers are much better than floats at bucketing things, and floats are much better at saying "where" something "is" in space.

-

I would be more concerned that there are operations (divisions, square roots, et cetera) done on or to components of vectors that would cause integer-based representations to become inaccurate. Beyond that I'm not sure it's worth switching implementations mid-project.

There are minor potential performance nitpicks here and there, but none of them are likely to be serious. I would suggest using the XNA versions simply for uniformity and scalability, but probably only if you're about to start a new project. I don't see any compelling reasons to change everything up mid-stream.

-