• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Capoeirista

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 :

 

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!

 

 

 

0

Share this post


Link to post
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 smile.png

1

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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)

 

WM6lJh3.png

 

Any ideas?

0

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0