# Maths - Rotation Matrices - forum

By: nobody ( Nobody/Anonymous )
Rorating Around A Vector
2003-05-19 03:03
I have used the following page:
https://www.euclideanspace.com/maths/algebra/matrix/orthogonal/rotation/index.htm

for the algorythm to enable rotation around a Vector. I am currently aware that my Matrix maths base functions as I can rotate around each individual axis, and scale specific axis without issues. However, with the vector rotation, there seems to be a massive scaling factor, as the object baloons to a rediculous size.
I am assuming that If i draw the Vertex that I am using as the axis, it will lie through the origin of the triangle / matrix. My algorythm is as follows (And I know all the sub methods work).

static public Matrix VectorRotate(Matrix M, Vector V, double A) {
double x,y,z;
x = ((Double)V.get(0)).doubleValue();
y = ((Double)V.get(1)).doubleValue();
z = ((Double)V.get(2)).doubleValue();

Matrix Rotate = new Matrix(new double[]
{ (1+(1-Math.cos(A))*(x*x-1)), (-z*Math.sin(A)+(1-Math.cos(A))*x*y), (y*Math.sin(A)+(1-Math.cos(A))*x*z),
(z*Math.sin(A)+(1-Math.cos(A))*x*y), (1 + (1-Math.cos(A))*(y*y-1)), (-x*Math.sin(A)+(1-Math.cos(A))*y*z),
(-y*Math.sin(A)+(1-Math.cos(A))*x*z), (x*Math.sin(A)+(1-Math.cos(A))*y*z), (1 + (1-Math.cos(A))*(z*z-1))});

return Multiply(M,Rotate);
}

By: nobody ( Nobody/Anonymous )
RE: Rorating Around A Vector
2003-05-19 03:47
Neatened Version:

(((1-Math.cos(A))*(x*x)) + Math.cos(A)),
(((1-Math.cos(A))*(x*y)) - (z*Math.sin(A))),
(((1-Math.cos(A))*(x*z)) + (y*Math.sin(A))),
(((1-Math.cos(A))*(x*y)) + (z*Math.sin(A))),
(((1-Math.cos(A))*(y*y)) + Math.cos(A)),
(((1-Math.cos(A))*(y*z)) - (x*Math.sin(A))),
(((1-Math.cos(A))*(x*z)) - (y*Math.sin(A))),
(((1-Math.cos(A))*(y*z)) + (x*Math.sin(A))),
(((1-Math.cos(A))*(z*z)) + Math.cos(A))});

I am using a 3x3 matrix for this process.

By: martinbaker ( Martin Baker )
RE: Rorating Around A Vector
2003-05-19 03:47
The algorithm that you are using assumes that Vector V is unit length.

If it is possible that this is not unit length then you should normalise it first.

Martin

By: nobody ( Nobody/Anonymous )
RE: Rorating Around A Vector
2003-05-19 03:54
Neater Version:

(((1-Math.cos(A))*(x*x)) + Math.cos(A)),
(((1-Math.cos(A))*(x*y)) - (z*Math.sin(A))),
(((1-Math.cos(A))*(x*z)) + (y*Math.sin(A))),
(((1-Math.cos(A))*(x*y)) + (z*Math.sin(A))),
(((1-Math.cos(A))*(y*y)) + Math.cos(A)),
(((1-Math.cos(A))*(y*z)) - (x*Math.sin(A))),
(((1-Math.cos(A))*(x*z)) - (y*Math.sin(A))),
(((1-Math.cos(A))*(y*z)) + (x*Math.sin(A))),
(((1-Math.cos(A))*(z*z)) + Math.cos(A))});

I do not understand why this causes scaling..
If i input a vector of 0,0,0. The matrix shrinks slowly. Any value above 1.00X causes the matrix to explode by a few hundred % per second.
While it does rotate around the desired axis, the scaling is unrequired, and I don't know why?

Should I be using a 4x4 matrix for this?

By: nobody ( Nobody/Anonymous )
RE: Rorating Around A Vector
2003-05-19 04:32
Normalise the polygon? or the Vector?
Gona experiment with the Vector 1st :)

Cheers

By: martinbaker ( Martin Baker )
RE: Rorating Around A Vector
2003-05-19 09:02
> Normalise the polygon? or the Vector?
> Gona experiment with the Vector 1st

Normalise the vector which is input to this method, ie you could start your method like this:

static public Matrix VectorRotate(Matrix M, Vector V, double A) {
double x,y,z;
x = ((Double)V.get(0)).doubleValue();
y = ((Double)V.get(1)).doubleValue();
z = ((Double)V.get(2)).doubleValue();
double n = Math.sqrt(x*x + y*y + z*z);
x /= n;
y /= n;
z /= n;

The reason I did not do this is that I assumed for axis-angle the axis would already be normalised.

Thank you to the person who sent the neatened version, Ill include that on the website if that’s alright?

On the other question, a 4x4 matrix is only required if you want to include a linier translation as well as a rotation (such as when you are rotating about a point other than the origin (see message from Tristan).

Cheers

Martin