Scaling Matrix Scales position of sprite also?

Started by
6 comments, last by VanillaSnake21 14 years, 10 months ago
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

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Advertisement
Hi,

Are you using D3DXMatrixTransformation2D () for that? If not, I recommend you to build your sprite's transformation matrix with this.
There's no "hard", and "the impossible" takes just a little time.
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?

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Can you post how you're building your sprite transformation matrix with code?
There's no "hard", and "the impossible" takes just a little time.
D3DXMatrixScaling(&ScaleMtrx, XScaleFactor, YScaleFactor, 1.0f);

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

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.
There's no "hard", and "the impossible" takes just a little time.
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.
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?

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

This topic is closed to new replies.

Advertisement