Centering sprite

Recommended Posts

Medo Mex    891

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 on other sites

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 on other sites
Medo Mex    891

@NewDisplayName: How do I make it look normal? I don't want it to be scaled.

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 on other sites
GuyWithBeard    1890

Hmm, normally a scale of 1.0f means "no scaling".

Share on other sites
Medo Mex    891

Tried to set the scale to 1.0f and to 0.0f, both doesn't give a normal sprite with no scaling.

Share on other sites
Strewya    2798
Since you didn't show it in the code (and i'm assuming you are using the ID3DXSprite interface), are you setting the matrix transform?
sprites[i]->sprite->SetTransform(&matrix);
sprites[i]->sprite->Draw(sprite->texture, NULL, NULL, NULL, color);
Edited by Strewya

Share on other sites
Medo Mex    891

@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 on other sites
Strewya    2798

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 on other sites
Medo Mex    891

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 on other sites
Strewya    2798

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 on other sites
Medo Mex    891

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

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