# 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.

## 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 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 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

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633703
• Total Posts
3013454
×