I am in the middle of making a rope physics simulation in c++ (spring physics). I successfully implemented basic rope movement, which is made up from several "bones" (which are just some particles [masses] with position, weight, etc). When each particle's position is calculated, I assign them each to its corresponding bone of a skinned mesh armature.
What I want is to calculate a particle's rotation so that it is rotated into its child particle (Circle is the head of the bone [the position of the particle], the black dot is the tail, which should connect to its child's head, and so on...):
I looked up some threads and came to this: Finding quaternion representing the rotation from one vector to another and tried to implement the accepted answer to my project, so I've got this:
XMVECTOR q;
XMVECTOR a = XMVector3Cross(head,tail);
XMVECTOR lh=XMVector3Length(head),lt=XMVector3Length(tail),dot=XMVector3Dot(head,tail);
q.m128_f32[0] = a.m128_f32[0]; //assigning the x coordinate
q.m128_f32[1] = a.m128_f32[1]; //assigning the y coordinate
q.m128_f32[2] = a.m128_f32[2]; //assigning the z coordinate
q.m128_f32[3] = sqrt(pow(lh.m128_f32[0],2)*pow(lt.m128_f32[0],2)) + dot.m128_f32[0]; //assigning the w coordinate
return XMQuaternionNormalize(q);
Unfortunately, it has not worked for me for some reason, so tried another which also failed:
XMVECTOR Head = XMVector3Normalize( head );
XMVECTOR Tail = XMVector3Normalize( tail );
float angle = acos(XMVector3Dot(Head,Tail).m128_f32[0]); //acos(dot(Head,Tail))
XMVECTOR axis = XMVector3Normalize(XMVector3Cross(Head,Tail));
XMVECTOR q = XMQuaternionRotationAxis(axis,angle);
I would be very grateful if someone would post me an alternate solution, and sorry for the poor paint skills.