Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


XNA/Monogame - Drawing around position


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Orangeatang   Members   -  Reputation: 1552

Like
0Likes
Like

Posted 11 April 2013 - 08:15 AM

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!

 

 

 



Sponsor:

#2 VladR   Members   -  Reputation: 722

Like
1Likes
Like

Posted 11 April 2013 - 08:32 AM

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

 


#3 Orangeatang   Members   -  Reputation: 1552

Like
0Likes
Like

Posted 11 April 2013 - 08:38 AM

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



#4 Dave Hunt   Crossbones+   -  Reputation: 2468

Like
1Likes
Like

Posted 11 April 2013 - 08:49 AM

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



#5 Orangeatang   Members   -  Reputation: 1552

Like
0Likes
Like

Posted 11 April 2013 - 09:23 AM

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?



#6 Dave Hunt   Crossbones+   -  Reputation: 2468

Like
1Likes
Like

Posted 11 April 2013 - 10:40 AM

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.



#7 Orangeatang   Members   -  Reputation: 1552

Like
0Likes
Like

Posted 11 April 2013 - 12:43 PM

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?



#8 Dave Hunt   Crossbones+   -  Reputation: 2468

Like
1Likes
Like

Posted 11 April 2013 - 01:04 PM

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.



#9 Orangeatang   Members   -  Reputation: 1552

Like
0Likes
Like

Posted 11 April 2013 - 01:25 PM

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


Edited by Orangeatang, 11 April 2013 - 01:26 PM.


#10 Orangeatang   Members   -  Reputation: 1552

Like
0Likes
Like

Posted 11 April 2013 - 01:49 PM

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

Edited by Orangeatang, 11 April 2013 - 01:49 PM.


#11 Dave Hunt   Crossbones+   -  Reputation: 2468

Like
1Likes
Like

Posted 11 April 2013 - 01:55 PM

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

 

That should solve that part of your problem nicely.



#12 Orangeatang   Members   -  Reputation: 1552

Like
0Likes
Like

Posted 11 April 2013 - 02:31 PM

Thanks for all your help!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS