Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use a lot of 2D vectors in my Flash games - basically all velocities, positions etc I store in this way. My Vector2D class has lots of built in functions for rotation, dotproduct, projectOnto etc. The native Vector2 class doesn't seem to have half of this stuff, but it is used in a lot of places. The Vector3 has all this stuff, but has an extra dimension I don't need. So should I use either of the built in ones or port my AS3 class? Or should I try to extend Vector2 and add the extra functionality I need. Could that cause any problems?

share|improve this question
up vote 9 down vote accepted

The native Vector2 class doesn't seem to have half of this stuff, but it is used in a lot of places. The Vector3 has all this stuff, but has an extra dimension I don't need.

I believe you're mistaken; Vector2 and Vector3 both have extensive functionality. Note that this functionality is exposed as static methods to the Vector2/Vector3 class, rather than of the form someVector.operationOn(otherVector). So for example, for the dot product of two Vector2s, you simply write Vector2.Dot(firstVector, secondVector). In addition, the same set of methods has existed since XNA 2.0, so I'm certain we are not talking about different versions.

Here is the list of Vector2 Members and Vector3 Members from Microsoft. The pages default to the latest XNA version, but you can view prior versions by clicking on the "Other Versions" dropdown at the top of the page, right under the title.

You will see, though, that the class does not implement rotation and projection. Of course, the projection operation is simply Vector2.Multiply(vecB, Vector2.Dot(vecA, vecB)) so you can write a small function for that if you use it often. For rotation, you should make use of Vector2.Transform and feed it a rotation matrix; I definitely recommend writing a function for that.

Overall, I recommend using the built-in Vector2 class, and I think you'll find it suits most of your needs. The rest, like I said, are very short functions which you can port. But no need to duplicate the entire class, right? But if you feel the need to rewrite it, I don't see a strong reason why you shouldn't, as I can't see how XNA may somehow implement it better than you can.

share|improve this answer
I agree, I would mention that you can add on some Extension methods to the existing Vector2 class, so your code stays somewhat clean in the sense that you don't have code like MyVectorHelper.Rotate(...) – Nate Oct 28 '10 at 15:29
Very good point, I'm not very experienced in C# so I hadn't heard of Extension methods. They appear to be somewhat complicated but worth the effort. – Ricket Oct 28 '10 at 21:39
Extension methods aren't very hard at all, and are immensely useful. – ThatsGobbles Oct 29 '10 at 19:27

The XNA Vector classes also have a hell of a lot of functionality, and the Matrix functions take in Vector classes specific to XNA, so personally, I'd probably use the XNA ones. Why reinvent the wheel?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.