I've been working with Vector2's and XNA, and I've come to find that calling the Normalize() member fuction on a Zero Vector normalizes it to a vector of {NaN, NaN}. This is all well and good, but in my case I'd prefer it instead just leave them as Zero Vectors.
Adding this code to my project enabled a cute extension method:
using ExtensionMethods;
namespace ExtensionMethods
{
public static class MyExtensions
{
public static Vector2 NormalizeOrZero(this Vector2 v2)
{
if (v2 != Vector2.Zero)
v2.Normalize();
return v2;
}
}
}
Unfortunately, this method returns the normalized vector, rather than simply normalizing the vector which I use to invoke this extension method. I'd like to to instead behave as vector2Instance.Normalize() does.
Aside from making this void, how do I adjust this so that the 'v2' is modified? (Essentially, I need access to the 'this' object, or I need 'v2' to be passed by reference.)
Edit:
And yes, I have tried this:
public static void NormalizeOrZero(this Vector2 v2)
{
if (v2 != Vector2.Zero)
v2.Normalize();
}
Doesn't work, v2 is just a variable in the scope of NormalizeOrZero.