Centering sprite

Started by
10 comments, last by Medo Mex 11 years, 3 months ago

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.

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;

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

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.

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

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

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

devstropo.blogspot.com - Random stuff about my gamedev hobby

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

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.

devstropo.blogspot.com - Random stuff about my gamedev hobby

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]

This topic is closed to new replies.

Advertisement