# Scaling Matrix Scales position of sprite also?

## 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 on other sites
programci_84    336
Hi,

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

##### Share on other sites
Quote:
 Original post by programci_84Hi,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 on other sites
programci_84    336
Can you post how you're building your sprite transformation matrix with code?

##### Share on other sites
D3DXMatrixScaling(&ScaleMtrx, XScaleFactor, YScaleFactor, 1.0f);

##### Share on other sites
programci_84    336
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 translatingD3DXVECTOR2 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 on other sites
simonloach    130
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.

##### 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?