# Matrix.CreateScale() is axially aligned.

This topic is 2168 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Using the Matrix.CreateScale() method or any of it's 6 overloads I can only create uniform or axially aligned scales. I wan't to be able to scale objects along an arbitrary axis. How can I do this. I assume I will have to roll my own function but my knowledge of matrix math isn't up to that task.

So amusing I have a 3 element unit vector and a scalar how can I create a matrix that stretches an object in the direction of the vector the amount of the scalar.

##### Share on other sites

Could you be a little more specific. Matrix.CreateScale(myUnitVector * scalar) will do what it seems like you are asking but maybe I'm missing something in your question.

##### Share on other sites

In 3d, let's say you want to scale along 3 axes defined by v1, v2 and v3, scales s1, s2, s3

Let R = matrix with rows v1, v2 and v3, S = matrix with s1, s2, s3 along leading diagonal

Apply the matrix R-1SR to the model (i.e. apply inverse rotation to get the axes mapped along the x, y and z axes, scale in that reference frame, rotate back).

The axes must be linearly independent (otherwise R won't be invertible).

If that doesn't work, the transpose surely will - it depends whether you use row-major or column major matrices. So if it doesn't work with v1, v2, v3 as the rows of R, use those vectors as the columns of R instead ;)

EDIT: If the axes are mutually perpendicular then R-1 = RT (i.e. inverse( R ) = transpose( R )). The axes must be normalised (i.e. length 1) for that though.

EDIT2: You can just multiply the 3 matrices together to get a transform to apply rather than apply each matrix transform separately.

EDIT3: I see you just want to scale along 1 axis. You need to construct a mutually perpendicular axis set then for the matrix R, and scale the other 2 perpendicular axes by a factor of 1. You can do that (construct a mutually perpendicular axis set) with cross products.

• 9
• 17
• 10
• 11
• 18