Followers 0

XNA/Monogame - Drawing around position

11 posts in this topic

Hey folks,

I'm hitting my head against a brick wall here, trying to draw a texture around an object's position in my engine. I've been modifying the code to supply an origin to the SpriteBatch so I can get rotation to work properly... but am not having much luck.

So given the following draw code :

Vector2 objectCenter    = aComponentObject.Position;

float halfWidth         = aComponentObject.Size.X / 2.0f;
float halfHeight        = aComponentObject.Size.Y / 2.0f;

Rectangle objectBounds  = new Rectangle( (int)(objectCenter.X - halfWidth), (int)(objectCenter.Y - halfHeight), (int)aComponentObject.Size.X, (int)aComponentObject.Size.Y );

aSpriteBatch.Draw( aTextureManager.GetColourMap(graphicComponent.ColourMapID), objectBounds, null, drawColour, aComponentObject.Rotation, objectCenter, SpriteEffects.None, 0.0f );


I would have thought that the 'objectCenter' parameter passed in to the draw call would properly offset my object's rendering (given I have an object at (100,100) with a size of (200,200)... instead I get the following :

Only half of the object is being rendered, where what I'm actually after (moving the camera up and left) is more like this :

Here the mouse is position in the dead center of the object.

Any ideas what I'm doing wrong?

0

Share on other sites

Your description of what happens can be interpreted in several ways. Are you 100% sure it is not culled by the viewport ? Your first image looks like it displays only the bottom-right corner of the red bitmap, hinting it got culled by the topleft corner of the screen...

Check that the same bitmap is not drawn afterwards with some other coords - sometimes you will get surprised when you put a breakpoint and the thing gets executed actually one additional time in the same frame

1

Share on other sites

I'm pretty sure it's getting culled by the viewport, yeah - I just don't know why that's happening :) I would have thought that the drawing would center around the position of the object (100,00) and draw a rectangle around said point (200,200) - so stretching from (0,0) to (200,200)... instead it's being drawn behind the origin (top left of screen).

0

Share on other sites

The origin parameter to SpriteBatch.Draw is relative to the upper left corner of the image rectangle being drawn. To draw a sprite centered on a point, set the origin value to the center of the object being drawn (e.g. a 32x32 image would use an origin of 16,16) and, for the target position, specify the position where you want the image centered (e.g. the center of the target object).

1

Share on other sites

The origin parameter to SpriteBatch.Draw is relative to the upper left corner of the image rectangle being drawn. To draw a sprite centered on a point, set the origin value to the center of the object being drawn (e.g. a 32x32 image would use an origin of 16,16) and, for the target position, specify the position where you want the image centered (e.g. the center of the target object).

Ah that makes much more sense, thanks!

I've modified my code to reflect this, I calculate the center to be the size of the object / 2 :

Vector2 objectCenter = new Vector2( aComponentObject.Size.X / 2.0f, aComponentObject.Size.Y / 2.0f );


Then create the rectangle for the object based on it's position and size :

Rectangle objectBounds = new Rectangle( (int)aComponentObject.Position.X, (int)aComponentObject.Position.Y, (int)aComponentObject.Size.X, (int)aComponentObject.Size.Y );


And finally draw the sprite with the calculated bounds and position :

aSpriteBatch.Draw( aTextureManager.GetColourMap(graphicComponent.ColourMapID), objectBounds, null, drawColour, aComponentObject.Rotation, objectCenter, SpriteEffects.None, 0.0f );


But I'm still getting a (slightly different) offset to the rendering. The object center here is (100,100) and the objectBounds (100,100, 200, 200). This should be drawing the square from (0,0) to (200,200) (I would think)... but it's offset by just over 20 pixels on each axis (the world transform I'm applying to the sprite batch Begin is simply an identity matrix at the moment)

Any ideas?

0

Share on other sites

Instead of using a destination rectangle to specify the position, try using a Vector2. The origin parameter makes more sense when dealing with a position, than it does with a destination rectangle. With a Vector2 position, you can be sure that your object will be centered over the position, assuming you have specified the origin correctly.

I have never had any success specifying an origin with a destination rectangle. If you need to draw the image larger than its actual source image, then you'll need to play with the scale transform. If so, be aware that the scale transform has to be accounted for in your position values and, if I remember correctly, your origin value as well. To be safe, start with leaving the origin as is and, if that doesn't work, try inverse-scaling the origin. If that doesn't work, try scaling the origin.

1

Share on other sites

If you need to draw the image larger than its actual source image, then you'll need to play with the scale transform

Thanks!

I'll definitely need to modify the scale transform for the objects if I can't get the destination rectangle working correctly. The editor I'm building uses a 2D camera, which can zoom in and out so I've got most of what I need set up already. The only worry I would have is that I'll need to call SpriteBatch.Begin() for each object that I'm rendering... assuming each object has it's own unique scale. Is SpriteBatch.Begin() particularly expensive? It doesn't flush the render pipeline does it?

0

Share on other sites

Lots of SpriteBatch.Begin/End pairs will definitely have an impact on performance. SpriteBatch.End flushes the current sprite batch, which will likely flush the render pipeline.

If you have many objects with different scales, you would be better off using the Draw overload that accepts a separate scale parameter. Unless you are scaling x and y by different amounts, this will allow you to get your scaling without flushing the pipeline.

1

Share on other sites

Excellent, thanks! I will definitely have to scale by different amounts, but I'll try and figure something out :)

Edited by Orangeatang
0

Share on other sites

Just found this (I'm using the MonoGame implementation of XNA), which will hopefully work out

Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float depth);

Edited by Orangeatang
0

Share on other sites

Hmm. Yes. XNA has that too. I missed it when I was scanning the docs.

That should solve that part of your problem nicely.

1

Create an account

Register a new account