Jump to content

  • Log In with Google      Sign In   
  • Create Account


Centering sprite


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 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 07 January 2013 - 01:08 AM

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.

 



Sponsor:

#2 NewDisplayName   Members   -  Reputation: 359

Like
0Likes
Like

Posted 07 January 2013 - 02:21 AM

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;



#3 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 07 January 2013 - 03:22 AM

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



#4 NewDisplayName   Members   -  Reputation: 359

Like
0Likes
Like

Posted 07 January 2013 - 03:27 AM

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, 07 January 2013 - 04:56 AM.


#5 GuyWithBeard   Members   -  Reputation: 804

Like
0Likes
Like

Posted 07 January 2013 - 03:45 AM

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



#6 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 07 January 2013 - 06:22 AM

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



#7 Strewya   Members   -  Reputation: 1351

Like
0Likes
Like

Posted 07 January 2013 - 06:23 AM

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, 07 January 2013 - 06:30 AM.

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


#8 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 07 January 2013 - 04:07 PM

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



#9 Strewya   Members   -  Reputation: 1351

Like
0Likes
Like

Posted 08 January 2013 - 03:14 AM

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


#10 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 08 January 2013 - 04:13 AM

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.

crosshair.png



#11 Strewya   Members   -  Reputation: 1351

Like
0Likes
Like

Posted 08 January 2013 - 04:29 PM

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


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


#12 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 10 January 2013 - 03:06 AM

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






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