I’ve been working with Vector2’s and XNA, and I’ve come to find that calling the Normalize() member function 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.
This doesn’t work because Vector 2 is actually a struct. This means it gets passed by value and you can’t modify the caller’s copy. I think the best you can do is the workaround specified by lomaxxx.
This illustrates why you should generally avoid using structures. See this question for more information. Vector2 violates the guideline that structs should be immutable, but it probably made sense to do so in the context of XNA.