Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7605275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:07:38+00:00 2026-05-31T00:07:38+00:00

Say that I have this interface: interface Drawable { Vector2 DrawPosition { get; }

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:07:40+00:00Added an answer on May 31, 2026 at 12:07 am

    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:

    abstract class Drawable
    {
        public abstract Vector2 DrawPosition { get; }
        public abstract Texture2D Texture { get; }
        public abstract float Rotation { get; }
        public abstract Vector2 Origin { get; }
        public abstract Vector2 Scale { get; }
        public abstract bool FlipHorizontally { get; }
    
        public abstract Vector2[] Vertices { get; }
    
        public Matrix TransformationMatrix
        {
            get
            {
                return Matrix.CreateTranslation(-new Vector3(Texture.Width * Scale.X / 2, 0, 0))
                    * Matrix.CreateScale(new Vector3(FlipHorizontally ? -1 : 1, 1, 1))
                    * Matrix.CreateTranslation(new Vector3(Texture.Width * Scale.X / 2, 0, 0))
                    * Matrix.CreateTranslation(-new Vector3(Origin, 0))
                    * Matrix.CreateScale(new Vector3(Scale, 0))
                    * Matrix.CreateRotationZ(Rotation)
                    * Matrix.CreateTranslation(new Vector3(DrawPosition, 0));
            }
        }
    }
    
    class Camera
    {
        private readonly Viewport viewport;
    
        public Matrix GetViewMatrix()
        {
            return Matrix.CreateScale(1, -1, 1) * Matrix.CreateTranslation(0, viewport.Height, 0);
        }
    
        public Vector2 MouseToWorld(int x, int y)
        {
            return Vector2.Transform(new Vector2(x, y), Matrix.CreateScale(1, -1, 1) * Matrix.CreateTranslation(0, viewport.Height, 0));
        }
    }
    
    class Game1 : Microsoft.Xna.Framework.Game
    {
        private Drawable avatar;
        private Camera camera;
        ...
        protected override void Initialize() {
            avatar = ...;
            camera = new Camera(graphics.GraphicsDevice.Viewport);
            basicEffect = new BasicEffect(graphics.GraphicsDevice);
            basicEffect.VertexColorEnabled = true;
            basicEffect.Projection = Matrix.CreateOrthographicOffCenter(0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height, 0, 0, 1);
    
            base.Initialize();
        }
        ...
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
    
            spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.GetViewMatrix());
            spriteBatch.Draw(avatar.Texture, avatar.DrawPosition, new Rectangle(0, 0, avatar.Texture.Width, avatar.Texture.Height), Color.White, avatar.Rotation, avatar.Origin, avatar.Scale, avatar.FlipHorizontally ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0);
            spriteBatch.End();
    
            basicEffect.CurrentTechnique.Passes[0].Apply();
            VertexPositionColor[] vertices = new VertexPositionColor[avatar.Vertices.Length + 1];
            Matrix m = MakeAffineTransform(avatar);
            for (int i = 0; i < avatar.Vertices.Length; i++)
            {
                vertices[i] = new VertexPositionColor(Vector3.Transform(new Vector3(Vector2.Transform(avatar.Vertices[i], m), 0), camera.GetViewMatrix()), Color.Black);
                Console.WriteLine(vertices[i]);
            }
            vertices[vertices.Length - 1] = vertices[0];
            graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, vertices, 0, vertices.Length - 1);
    
            base.Draw(gameTime);
        }
        ...
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a class that implements the IDisposable interface. Something like this:
say I have Class1 that implements interface named Class1interface . This interface is autowired
Let's say that I have this string: s = '<p>Hello!</p>' When I pass this
I have this table: id feed_id ... Let's say that I have 500 rows
Say that i have a generic dictionary with data like this (I hope the
Let say that we have the following query: SELECT DISTINCT COUNT(`users_id`) FROM `users_table`; this
Let's say that I have a string 5a . This is the hex representation
In PHP, say that you have some code like this: $infrastructure = mt_rand(0,100); if
Ok say you have this: <input id=test value= /> Value of that input is
Say I have a xml document that looks like this <foo> <bar id=9 />

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.