Jump to content
  • Advertisement
Sign in to follow this  

Rotation Matrix for difference between 2 vectors

This topic is 3623 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));
rotateX.Rotate(DEGTORAD(angle), true, false, false);
rotateY.Rotate(DEGTORAD(angle), false, true, false);
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.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!