I'm trying to implement a direction constraint between two objects.
So, I have a series of translation vector for the object constraining the other, and I want to get the rotation along the three axis for the constrained object.
what I do now is the following:
find the projection of the center of my constraining object on the constrained object coordinates, then calculate the arctan of the variation.
Here is the code:
c is the position vector for the constraining object,
o is the position vector for the constrained object
rot is the rotation vector (I apply the three components separately)
My constraining object is moving along x direction, y and z are fixed to 0.Code :double radian = 180.0 / 3.141592654; double dizx; float dx, dy, dz; // calculate distances along axes dx = o - c; dy = o - c; dz = o - c; if ((dx == 0) && (dy == 0) && (dz == 0)) exit(printf("Distance is 0 (%d)\n", i)); // calculate the length of the projection on xz plane dizx = sqrt((dx * dx) + (dz * dz)); rot = 0.0f; rot = - radian * atan2(dz, dx); rot = radian * atan2(dy, dizx);
x is in the range [-4, +4]
Until the value of x is < 0 my algorithm works fine, then the rotation invert his way, and so the constraint is not working at all.
I'm not very expert with trigonometry, can you tell me what am I doing wrong?