Problem with direction constraint

Hi all,

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)

double radian = 180.0 / 3.141592654;
double dizx;
float  dx, dy, dz;

// calculate distances along axes
dx = o[0] - c[0];
dy = o[1] - c[1];
dz = o[2] - c[2];

if ((dx == 0) && (dy == 0) && (dz == 0))
    exit(printf("Distance is 0 (%d)
", i));

// calculate the length of the projection on xz plane
dizx = sqrt((dx * dx) + (dz * dz));

rot[0] = 0.0f;
rot[1] = - radian * atan2(dz, dx);
rot[2] =   radian * atan2(dy, dizx);

My constraining object is moving along x direction, y and z are fixed to 0.
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?

Thank you

Can you explain the problem at a very high level? You lost me in the first sentence, but I’m sure I can understand the problem.