• Advertisement
Sign in to follow this  

Centering sprite

This topic is 1838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm looking for a way to center a "cross hair" sprite.

 

I tried the following but I see that the sprite is not centered in the screen and it's scaled as well:

 

D3DXVECTOR2 position(WINDOW_WIDTH/2, WINDOW_HEIGHT/2);
D3DXVECTOR2 scale(1.0f, 1.0f);
D3DXMatrixTransformation2D(&matrix, NULL, NULL, &scale, NULL, NULL, &position);
sprites[i]->sprite->Draw(sprite->texture, NULL, NULL, NULL, color);

 

I even tried: position = position - textureSizeInPixels;

 

I am looking for a way to make the sprite appear in the center with exactly the same size as the texture.

 

Share this post


Link to post
Share on other sites
Advertisement

It is supposed to look scaled. Check this

Also, to calculate exact position offset you need to adjust both x and y coordinate according to the sprite rect.

I don't understand what you exactly meant when you said this : position = position - textureSizeInPixels;

Share this post


Link to post
Share on other sites

D3DXVECTOR2 scale(0.0f, 0.0f);

 

Taking into account GuyWithBeard's input (which is almost true because "normal" can vary from person to person) try to tweak 1.0f, 1.0f to some value that gives desired outcome.

I just remembered that 0.0f scaling would mean scaling an image to 0%, which is what you won't be looking for I believe.

Edited by NewDisplayName

Share this post


Link to post
Share on other sites

@Strewya: Yes, I'm setting the transformation matrix but the line is missed in the code that I posted.

 

Still having the same problem though.

Share this post


Link to post
Share on other sites

a few questions that pop to mind:

how are you creating the window, win32 or using some library?

if(win32) are you adjusting the rectangle of the client area? (this issue has happened to me, and would cause every sprite to be drawn incorrectly by a slight offset)

also, you should see how things change when you pass in a scaling center of (0,0), and if you pass the scale vector as 0/NULL/nullptr.

also, might want to post a screenshot and give info about the size of the crosshair texture, window size and such.

Share this post


Link to post
Share on other sites

Window size: 1600 x 900

 

Yes, I'm using Win32 API.

 

Here is a screenshot of the crosshair, it's not even centered and it have undesired scaling, the red point is the actual center of the screen.

[attachment=13121:crosshair.png]

Share this post


Link to post
Share on other sites

random suggestions:

make sure you're setting the DX device with the same width and height as the window.

and try drawing 6 lines using ID3DXLine to cover every edge plus diagonals of your screen (0,0 to w,h; 0,h to w,0; etc) and see if the window's center is where you think it is :)

Share this post


Link to post
Share on other sites

@Strewya: The DirectX device setting is the same width and height as the window.

 

Maybe you can give me a tested snapshot of code that should work to center the sprite with no scaling and I will try it to find out if the problem is the related to drawing the sprite code or something else.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement