Sign in to follow this  

Rotation Matrix for difference between 2 vectors

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am lost on what I need to setup a rotation matrix, that is derived from 2 vectors. One is the lights direction and the other would be I guess vec3(0,1,0) for pointing up ward or a point light. What I am trying to do is get the math right to position a object in my map editor that represents the light in the scene and want a triangles point to point in the direction of the light if its directional...
vec3 triNormal(0.0f, 1.0f, 0.0f);
vec3 lightNormal(mapFileData.lightPosition.x, mapFileData.lightPosition.y, mapFileData.lightPosition.z);
float angle = VectorAngleFrom2Vectors(triNormal, lightNormal);


Thanks, and I am sure I am way off base... ;(

Share this post


Link to post
Share on other sites
There is no "unique" matrix that does what you want (the object can be rotated arbitrarily around the "goal" vector), but you can easily get matrix that does minimal turn.
Do you have a function that finds rotation matrix from axis and angle? Use cross product of 2 vectors as axis (cross product is orthogonal to both), and angle between them as angle (you may need to use -angle depending to handedness of coordinate system and angles). Don't forget check that cross product vector is not extremely short (if it is, just use identity. the formula for matrix from axis and angle could break with too small axis)

(If you really want it maximally neat you can even further optimize out all trigonometry from the formula using half and double angle trig identities, but it'll be pain)

Share this post


Link to post
Share on other sites
Ok I think this will work but has some bugs. OpenGL's default vector is 0,0,1. Now I am setting my vector to that also. When the user puts in their input say 1,0,0 I take that angle and rotate the object to that angle. If this makes sense at all?


float angle = AngleBetweenVectors(mapFileData.lightPosition.GetVec3(), vec3(0,0,1));
if(mapFileData.lightPosition.x)
rotateX.Rotate(DEGTORAD(angle), true, false, false);
if(mapFileData.lightPosition.y)
rotateY.Rotate(DEGTORAD(angle), false, true, false);
if(mapFileData.lightPosition.z)
rotateZ.Rotate(DEGTORAD(angle), false, false, true);

result = translate * initRot * rotateX * rotateY * rotateZ;




The problem I think is the way I go about rotating each one... I am not sure, but think that order of operations for rotations matters... So if I am correct, then how do I overcome that problem, if that is my problem.

Thanks

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this