开发者

XNA - Why does it use Vector2 and not Point?

I'm used to WinForms graphics, but I've been dabbling in XNA, and one thing I've noticed is that the Point object isn't very useful, and doesn't seem to be used very much. For positioning, the various SpriteBatch Draw methods use either a Rectangle or a Vector2. And Vector2 has a lot of useful static and instance methods, whereas Point has basically nothing except the X and Y properties.

Why does XNA use a Vector2 to represent position instead of a Point? I realize they both have an X and a Y, 开发者_高级运维but semantically and logically, using a Vector2 instead of a Point to represent location makes no sense to me. (For instance, if you normalize the Vector2, suddenly you have a different location!)

Is this as strange as it seems, or am I missing something?


Even assuming Point were using float values, what meaning would you give to "adding 2 points together"? Now adding vectors to update positions is a well understood and widely used concept, so vectors map better to describing positions and speeds in a virtual world.


Semantically there's little difference between a vector and a point; they both represent a displacement from an origin in some Cartesian coordinate system. The advantage of vectors is that they have more structure (scaling, addition, etc.) than points.

For example, you might want to find the displacement of one entity from another: this is simply the vector difference between their respective position vectors. They're also more versatile in representing other quantities, such as velocity. For example, you might want to calculate the position of an entity with position r and velocity v after a time interval t; using vectors, this could be calculated simply by r + t * v.

Your example of normalization could be useful if you want the direction to the entity from the origin (or some other point).


Thought I'd share a little extension magic, since I love extensions. I created a Point extension method for easily converting to a Vector2 for like SpriteBatch.Draw()

public static class PointExt
{
    public static Vector2 ToVector2(this Point point)
    {
        return new Vector2(point.X, point.Y);
    }
}

then simply call it with myPoint.ToVector2().

Not the most meaningful use of an extension, but it makes my life slightly easier.


At a certain level, you just want a tuple of numbers. Higher up you may want to apply some abstractions and structure over those tuples. However, as a high performance math library, XNAMath leaves that work up to you.


Main reason... Vector2 uses floats and fields instead of properties. This makes it more accurate and more efficient than Point, which uses integers and properties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜