XNA/Monogame - Drawing around position

Started by
10 comments, last by Capoeirista 11 years ago

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 :

2Cl4Qx5.png

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

Y3weg80.png

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

Any ideas what I'm doing wrong?

Thanks for your help!

Advertisement

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 smile.png

VladR My 3rd person action RPG on GreenLight: http://steamcommunity.com/sharedfiles/filedetails/?id=92951596

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).

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).

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)

WM6lJh3.png

Any ideas?

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.

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?

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.

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

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


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

This topic is closed to new replies.

Advertisement