Author Topic: Vector to matrix  (Read 2661 times)

0 Members and 1 Guest are viewing this topic.

Bryco

  • Water Moccasin
  • Posts: 1883
Vector to matrix
« on: August 20, 2006, 12:33:27 PM »
If I have a vector how do I convert that to a rotational matrix?

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Vector to matrix
« Reply #1 on: August 20, 2006, 06:06:31 PM »
I don't think you can without having 2 other vectors that produce the given vector but if you had those 2 you could just use the angle between them and perform a normal rotation around a given axis.
You can use the vector as the rotation axis though.
The problem with a vector is it only gives direction and length, not position, unless it's used as a point which still only one part of the puzzle.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Vector to matrix
« Reply #2 on: August 20, 2006, 07:20:03 PM »
Hi Mick.
I'm using a vector as an example as I can work out the translation later.
I just can't seem to figure out the individual rotation matrix in the x,y and z axis or which order to multiply them.
This site (http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToMatrix/index.htm) has a great function for rotating around a vector, in vba
Code: [Select]
Function RotateAroundVector(V As Variant, Angle As Double) As Variant
    Dim M(3, 3)
    Dim X As Double, Y As Double, Z As Double
   
    X = V(0): Y = V(1): Z = V(2)
    M(0, 0) = 1 + (1 - Cos(Angle)) * (X * X - 1)
    M(0, 1) = -Z * Sin(Angle) + (1 - Cos(Angle)) * X * Y
    M(0, 2) = Y * Sin(Angle) + (1 - Cos(Angle)) * X * Z
    M(0, 3) = 0
       
    M(1, 0) = Z * Sin(Angle) + (1 - Cos(Angle)) * X * Y
    M(1, 1) = 1 + (1 - Cos(Angle)) * (Y * Y - 1)
    M(1, 2) = -X * Sin(Angle) + (1 - Cos(Angle)) * Y * Z
    M(1, 3) = 0
   
    M(2, 0) = -Y * Sin(Angle) + (1 - Cos(Angle)) * X * Z
    M(2, 1) = X * Sin(Angle) + (1 - Cos(Angle)) * Y * Z
    M(2, 2) = 1 + (1 - Cos(Angle)) * (Z * Z - 1)
    M(2, 3) = 0
   
    M(3, 0) = 0
    M(3, 1) = 0
    M(3, 2) = 0
    M(3, 3) = 1
   
    RotateAroundVector = M
   
End Function

This gives me the ability to somewhat copy the cad commandline spherical coordinates Line:0,0,0   @1<45<45 which in effect is rotate around the z using the angle of the the vector then rotate from the xy plane using the matrix and an angle.
So far it's working as I haven't tried to many variations. I still have no idea how to work out the 3 axis as it seems they would work if the 3 could be multiplied at one time, whereas once you multiply x*y the resulting matrix is wrong for multiplying z , maybe, perhaps.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Vector to matrix
« Reply #3 on: August 20, 2006, 07:38:29 PM »
The way I would approach it is break it up into seperate matrices to transform the current coordinate system so the new z axis is equal to the vector then build a matrix to do the rotation around the z axis which is standard operation. It doesn't really mater which way the x and y are pointing for this exercise.
You can then multiply them together to get your final matrix which with out looking too hard is probably what you have there but if you look at both seperately it will probably make more sense.
hth.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Vector to matrix
« Reply #4 on: August 20, 2006, 08:01:57 PM »
That sounds like the GO Mick. Like making a ucs.
Thanks heaps,I'll give that a go.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Vector to matrix
« Reply #5 on: August 20, 2006, 08:21:30 PM »
Exactly, you could even use a temp line object (from the vector) to get its normal for an x/y axis to xproduct for the other axis. Or you could use a current x/y axis for the same purpose but they will need an extra xproduct to make the new ucs orthoganal.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien