Say that I have this interface:
interface Drawable {
Vector2 DrawPosition { get; }
Texture2D Texture { get; }
float Rotation { get; }
Vector2 Origin { get; }
Vector2 Scale { get; }
bool FlipHorizontally { get; }
}
and in a class that extends Microsoft.Xna.Framework.Game, I override Draw(GameTime) and this code is somewhere in there:
Drawable d = ...;
spriteBatch.Begin();
spriteBatch.Draw(d.Texture, d.DrawPosition, new Rectangle(0, 0, d.Texture.Width, d.Texture.Height), Color.White, d.Rotation, d.Origin, d.Scale, d.FlipHorizontally ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0);
spriteBatch.End();
This uses the SpriteBatch.Draw(Texture2D texture, Vector2 position, Nullable sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) overload.
Say I had a set of vertices that makes a rough outline of the image that is returned by d.Texture (that is, if I open up the image in Microsoft Paint and pencil every point from the set of vertices, it would fit pretty closely). If I wanted to plot these points so that they go over the textures using GraphicsDevice.DrawUserPrimitives(), would there be a way to transform the vertices using only matrices? The key thing is that it could only use matrices, and I have no other alternatives for drawing because I actually need to use the transformed vertices for other things as well. I already tried something like
Matrix.CreateTranslation(new Vector3(-d.Origin, 0))
* Matrix.CreateScale(new Vector3(d.Scale, 0))
* Matrix.CreateRotationZ(d.Rotation)
* Matrix.CreateTranslation(new Vector3(d.DrawPosition, 0)));
but it fails pretty hard. Is there a solution to this problem?
Oh my goodness guys, I’m so sorry. I just realized the whole reason why it is wasn’t matching was because I made the vertices wrong! Well, I guess if you guys need help on doing the same thing, here’s my final version:
it works beautifully! This actually flips the origin so that it’s at the bottom left corner and also flips the y axis so that increasing values go upwards and decreasing values go downwards. The camera could be a good base and can easily be updated (say, if you want to make it follow something on the screen) so that you can pass the world coordinates to it (with the origin at the bottom left corner) and it’ll return the screen coordinates.