1
\$\begingroup\$

I'm new to programming, and I finally delved into XNA/MonoGame for wanting to practice coding game logic.

I'd like to know what the difference is between a Position (Vector2) and a Destination Rectangle (Rectangle), when it comes to positioning a sprite.

\$\endgroup\$
4
\$\begingroup\$

The simple answer is: the Vector2 positions the sprite at the X,Y coordinates, while Destination rectangle fills the sprite to fit that rectangle.

More advanced answer:

Sprites are in reality quads made up of two triangles, since underneath all this graphics stuff 3D routines are used. So both Draw methods transfer the sprite to a quad. This quad is basically a rectangle.

So what the Vector2 variant does is create a DestinationRectangle internally like this when calling SpriteBatch.Draw():

destrect = new Rectangle((int)V2.X, (int)V2.Y, Texture.Width, Texture.Height);
\$\endgroup\$
  • \$\begingroup\$ Thanks for the info. I noticed that the 8th overload in SpriteBatch.Draw() includes both a Vector2 and a DestinationRectangle for the same purpose--to display the position. Does that mean I must set either one of them to null? \$\endgroup\$ – Alexandre Laframboise Feb 19 '17 at 10:55
  • 1
    \$\begingroup\$ In the overload that takes both vector2 and rectangle, the rectangle is the source rectangle. It means the sprite will be draw only using that section from the Texture. \$\endgroup\$ – Felsir Feb 19 '17 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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