Jump to content
  • Advertisement
Sign in to follow this  
VanillaSnake21

Scaling Matrix Scales position of sprite also?

This topic is 3315 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

Hi, in this situation I'm trying to apply a scaling matrix to sprite in the following order. Compute Scale Matrix Sprite->Begin Sprite->SetTransfor(Matrix) Sprote->Draw Sprite->End But the problem is that when I set the position of the sprite in the Draw call and apply the matrix, the position vector gets scaled also. For example: I have a matrix that does X axis * 4, Y axis * 0.5, Z axis * 1. And lets say I pass in a position as x = 100, y = 200. The sprite wouldend up at position 400, 50. How can I prevent that from happening? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Hi,

Are you using D3DXMatrixTransformation2D () for that? If not, I recommend you to build your sprite's transformation matrix with this.

Share this post


Link to post
Share on other sites
Quote:
Original post by programci_84
Hi,

Are you using D3DXMatrixTransformation2D () for that? If not, I recommend you to build your sprite's transformation matrix with this.


Ok, I tried it, but similar results. Any other ideas?


P.S Well technically I solved it by changing the position so that when it's scaled would give me the actual position I want. So lets say I want X to be 100 and my scaling factor is 1.5, I would make X 50, which would make it appear as thought its 100 after scaling. But are there any other less crude methods?

Share this post


Link to post
Share on other sites
Hi,

If you multiply your original matrix with a scale matrix that's been built up with D3DXMatrixScaling(), position data of the result matrix will be shifted as well.

So, I recommend you to use D3DXMatrixTransformation2D(). Change your code like this:
D3DXMATRIX spriteTransformation;
//assume XScaleFactor, YScaleFactor, XPos and YPos are defined for scaling and translating
D3DXVECTOR2 myScaleFactor (XScaleFactor, YScaleFactor);
D3DXVECTOR2 myTranslation (XPos, YPos);
D3DXMatrixTransformation2D (&spriteTransformation, NULL, 0, &myScaleFactor, NULL, 0, &myTranslation);

//...
Sprite->Begin(...);
Sprite->SetTransform (&spriteTransformation);
Sprite->Draw (...);
Sprite->End ();



Hope this helps.

Share this post


Link to post
Share on other sites
Just to expand on programci_84. The order of operations is commutative (the result differs for different orderings) so a translation followed by a scaling will not provide the correct results where a scaling followed by a translation will.

If you had an object with a matrix (so it already contains position, orientation, etc..) that you wanted to apply scaling to you would first need to translate it to the origin, apply the scaling matrix and translate back to its original position to make sure you don't move it when scaling.

To learn more you might want to read about linear transformations.

Share this post


Link to post
Share on other sites
Thanks guys it actually worked :) So what's the point of the Draw call having a Position varibale if it gets thrown off by any previous transformation?

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!