[Solved]Monogame Origin/SourceRectangle issues

I am currently having an issue with Monogame while drawing with a SourceRectangle and Origin. When the Origin is (0,0) everything works as expected other than, well, Rotation. When the SourceRectangle is the whole image it works as expected with correct origin. With both variables the way they should be, this happens as the Sourcerectangle “zooms” in(by subtracting width and height, so basically animation): http://gyazo.com/7fed3d2e228beb15c644d43155d226db .

To recreate the problem yourself, use the following code with a 64x64 image.

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Playground {
    public class Game1 : Game {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D _image;
        float _angle = 0f;
        Rectangle old;
        Vector2 center;
        public Game1() {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        protected override void Initialize() {
            old = new Rectangle(0, 0, _image.Width, _image.Height);
            center = new Vector2((float)_image.Width / 2, (float)_image.Height / 2);
            this.IsMouseVisible = true;
        protected override void LoadContent() {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            _image = Content.Load<Texture2D>("Collider");
        protected override void UnloadContent() {
        protected override void Update(GameTime gameTime) {
            _angle += 0.1f;
                old = new Rectangle(old.X, old.Y, old.Width - 1, old.Height - 1);
                old = new Rectangle(old.X, old.Y, old.Width + 1, old.Height + 1);
        protected override void Draw(GameTime gameTime) {
            spriteBatch.Draw(_image, new Rectangle(Mouse.GetState().Position.X, Mouse.GetState().Position.Y, 64, 64), old, Color.White, _angle, center, SpriteEffects.None, 0f);

Help greatly appreciated! :smile:

U have your rectangles wrong way round. Old should be first.

Also store your center In a vector2 for speed instead of working it out every update.

Remember to typecast your width and height to float before divideding by 2 so as to get the exact center.

Thank you for you’re response :).

I tried ordering the rectangles different however it didn’t work :(. It set the rectangle to the top left and now it was offset but the size. About the typecasting, done nothing seems different, I have also set the center variable to only be calculated once as you suggested :).

Thanks again for the response!

Being that old is the sourceRect, and the source size to copy from is being changed (not sure if this is the behavior you want) then you would need in each size change, to also update the center to equal the sourceRect’s center based on it’s new size - otherwise you’d get a strange rotation origin.

1 Like

Thank you so much! :slight_smile: . Anyway I can rep you or do something nice back? I’m new here :stuck_out_tongue:

1 Like