Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Rotating a matrix4 with a normalised vector.

Author
Message
kaedroho
17
Years of Service
User Offline
Joined: 21st Aug 2007
Location: Oxford,UK
Posted: 7th Aug 2009 21:15
How can this be done? Currently I can only see using loads of trigonometry to find the angle then using the rotate (xyz) matrix4 commands. But there must be a better way.

Thanks in advance for any help!

Math89
21
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 7th Aug 2009 23:53
I think you need to calculate 2 other vectors using the one you have. Those 3 vectors need to be orthogonal (just like the xyz axis). The Z vector is the one you already have. The Y vector can be set to 0,1,0 as long as Z is not vertical, and X would then be the cross product of Y and Z. Since the Y axis is completely wrong (because I arbitrarily chose 0,1,0), it needs to be recalculated from X and Z using the cross product.
Now you have the 3 vectors, and can write them in the matrix: each vector is a row (or a column, can't remember ). Don't forget to set the lower right number to 1.

I know it's not that clear, and I'm not even sure that my algorithm is working, but it's probably better than nothing.
kaedroho
17
Years of Service
User Offline
Joined: 21st Aug 2007
Location: Oxford,UK
Posted: 8th Aug 2009 00:23 Edited at: 8th Aug 2009 00:26
right here goes, someone check this to see that i got this right

Set Z to my normalised vector

set Y to 0,1,0

Cross product Y and Z to make X

Cross product X and Z to make Y

write them into a matrix like this

[X1,Y1,Z1,0]
[X2,Y2,Z2,0]
[X3,Y3,Z3,0]
[X4,Y4,Z4,1]

Or

[X1,X2,X3,X4]
[Y1,Y2,Y3,Y4]
[Z1,Z2,Z3,Z4]
[0 ,0 ,0 ,1 ]

Note: X1 would be found by x vector4(xvector), x2 would be found by y vector4(xvector), etc

Also, would I need to put the 4th value of the vector in? I'm using vector4s so I'm guessing that i should.

Math89
21
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 8th Aug 2009 11:42
It should be something like that. But keep in mind that if your direction is vertical, you need another Y vector. And since all you want is the rotation matrix, you don't need the W component: the last column (or row?) is for the translation.

Login to post a reply

Server time is: 2025-06-08 04:23:11
Your offset time is: 2025-06-08 04:23:11