PDA

View Full Version : Matrix from vector (any math nerds?)



Dan Ritchie
04-20-2016, 01:49 PM
I am wanting to write some code that generates a rotation (or probably orientation) matrix from a given direction vector in 3D space. A good example would be creating a matrix to align an object to a surface normal.

There are many examples on-line, but it is all in GL, and I am just using plain C or C++, or a generic language with no special classes or vector math.

I would like to create a 3x3 or 4x4 matrix that makes the correct orientation, and store it in a 2-d array. Any help would be appreciated.

RebelHill
04-20-2016, 02:43 PM
You dont need a matrix tor epresent a vector... for instance, the vector <1,2,3>... as a matrix would simply be

1 0 0
0 2 0
0 0 3

Im presuming you're wanting to find the rotation amtrix between one vector in order to transform it to another...

https://www.google.co.uk/search?q=transform+between+2+vectors&ie=utf-8&oe=utf-8&gws_rd=cr&ei=_eYXV4yYIKaTgAbjiq6YBw#q=transform+between+2+ve ctors&start=0

Alternatively, for a quicker way to the full version...

Let A and B be the 2 vectors... to find the transform from A to B...

get some other variables (which will themselves be vectors), we'll call C, D and E, where...

C = normalise(A cross B)
D = C cross A
E = C cross B

then...

Matrix1 = B,C,E
Matrix2 = transpose(A,C,D)

and finally...

Rotation A>B = Matrix1 x Matrix 2

MonroePoteet
04-20-2016, 02:44 PM
Here's some C code I wrote a *long* time ago to rotate a Point around a Center by (Xradians,Yradians,Zradians) which doesn't really do what you're asking, but might be helpful. I don't remember which "handed" coordinate system I used or if it will match LW's left-handed coordinate system, so you may have to swap or negate some LW coordinates to make it work the way you expect.

Function prototype:


void rotate_one_point(
double Point[3],
double fRadians[3],
double fCenter[3]);


mTp

Dan Ritchie
04-20-2016, 02:59 PM
I want to create a rotation matrix so I can apply it to an object.
I want to use a vector to specify the direction that the object will be oriented to

The example I gave was orienting an object along a surface normal. An example of this would be orienting a tree on hillside, so it is slanted in whatever direction that the hill is sloping at that spot.

So, I want something like, in psudo code

Vector MyVector = (1,2,3)

float[4*4] MyMatrix = CreateMatrixFromVector ( MyVector )

Dan Ritchie
04-20-2016, 03:09 PM
You dont need a matrix tor epresent a vector... for instance, the vector <1,2,3>... as a matrix would simply be

1 0 0
0 2 0
0 0 3

Im presuming you're wanting to find the rotation amtrix between one vector in order to transform it to another...

https://www.google.co.uk/search?q=transform+between+2+vectors&ie=utf-8&oe=utf-8&gws_rd=cr&ei=_eYXV4yYIKaTgAbjiq6YBw#q=transform+between+2+ve ctors&start=0

Alternatively, for a quicker way to the full version...

Let A and B be the 2 vectors... to find the transform from A to B...

get some other variables (which will themselves be vectors), we'll call C, D and E, where...

C = normalise(A cross B)
D = C cross A
E = C cross B

then...

Matrix1 = B,C,E
Matrix2 = transpose(A,C,D)

and finally...

Rotation A>B = Matrix1 x Matrix 2


You lost me after "then..."

would that be (my matrix math is a little rusty)
matrix1=
[b.x,b.y,b.z]
[c.x,c.y,c.z]
[e.x,e.y,e.z]

and the use of "transpose?" is that to " turn all the rows of a given matrix into columns and vice-versa"
and are we talking column, or row centric in the first place?

RebelHill
04-20-2016, 03:31 PM
If you're wanting to align something to a normal... then you need the transform between some direction of the object, and the direction of that normal... the vector itself (or either one of them) wont be a matrix, the transforation between the 2 will.

And yes, transpose is swapping rows for columns. As for whether we're talking row or column matrices here, it makes no difference. If we were talking row... then...

Matrix1, row 1 = B
Matrix1, row 2 = C
Matrix1, row 3 = E

if column... then...

Matrix1, col1 = B
etc...

Dan Ritchie
04-20-2016, 05:21 PM
If you're wanting to align something to a normal... then you need the transform between some direction of the object, and the direction of that normal... the vector itself (or either one of them) wont be a matrix, the transforation between the 2 will.

etc...


Yes. I was assuming world space and z up, so 0,0,1 as the original orientation, so, yes....

Dan Ritchie
04-21-2016, 02:24 PM
I am trying this but I am not getting the results I expected.

My geometry is very definitely z up, but I only get correct results if I specify 0,1,0 as my source vector. Do you have any source documents I can reference to check my math?

Dan Ritchie
04-23-2016, 09:39 AM
After getting a little more cozy with matrix math, I have a better grasp on the problem.

A rotation matrix basically contains 3 vectors that represent a new coordinate system.
World space looks like 3 vectors, x,y,z, each pointing in perpendicular directions, like

x=1,0,0
y=0,1,0
z=0,0,1

in the matrix it would look like

1,0,0
0,1,0
0,0,1

When you make a rotation matrix, you are rotating a coordinate system.

Using the cross product technique to create a matrix from 2 vectors, you make a first guess at a perpendicular vector by taking the cross product of the direction with an "up" direction

This gives the first perpendicular vector. Then you use cross product with that and the direction to create the second vector, and again with the 2 vectors that were created to make a 3rd.

The problem with this technique is it fails if the vector and the up vector are the same, or perpendicular.

RebelHill
04-23-2016, 11:57 AM
Yes... having the 2 vectors initially the same is the one fail point... but Im not sure any such transform exists which accounts for that. The usual procedure Ive seen is that folk make a test against some very small epsilon (or use a dot product, which returns 1 for equal vectors [make sure to normalise both for dot testing]), and set a specific case to perform no rotation if that condition is met.

creacon
04-24-2016, 12:29 AM
I am currently using this lightweight math lib:

http://cmldev.net/

creacon

Dan Ritchie
04-26-2016, 09:59 AM
I am currently using this lightweight math lib:

http://cmldev.net/

creacon

Any support in it for Barycentric coordinates?