# 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 - c;
dy = o - c;
dz = o - c;

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.0f;
rot = - radian * atan2(dz, dx);
rot =   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
Remedios

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.

Sure I can! I’m implementing a 3D Viewer, in fact I’m reading xsi files (exported by SoftImage) and trying to show all the things a 3D modeller can create.

I need to show two moving objects: the first one is moving along a direction and the second one is supposed to “look” at the first one, that means that the rotation applied to the second object is a function of the position of the first.

This is my situation:

I know the position (let’s say the translation) of the two cubes, and I want to calculate the rotation of the second one (that is constraint to the position of the first).

I tried with the algorithm above, but I got strange behavior when the x value of the first object became > 0, so I think the algorithm is wrong.

Can you help to find a good solution?

Thank you very much,
Remedios

Suggestion 1, and at the risk of sounding stupid, why not use gluLookAt? Also, you might need to perfom some of the calculations below anyway to provide input for that function…

Suggestion 2, more complicated, requires that you know the positions of the camera and the moving object, and the vector that defines the motion of the moving object, and lets you define the orientation of the camera purely as a function of the position of the moving object:

Calculate a vector from the camera’s position to the moving object’s position, and normalize it.

Calculate the cross product of that vector, and the motion vector of the moving object, that will give you an “up” vector, normalize that too.

Calculate the corss product of the “up” vector and the direction vector to the object, and normalize that. That gives you a “left” or a “right” vector for the camera, depending on the order in which you calculate the cross product.

Net effect, generally, of using gluLookat (gluLookAt?) should be that the camera tracks the object wherever it goes…

Ok, I need to explain better.
The only thing I need to retrieve is the rotation of the second cube.
That means that the only information I need is about the translation (or position) of the first cube and the translation (or position) of the second.

Everything else is not necessary, because it’s a geometrical problem, and you are not supposed to have a camera looking at the cubes!

I don’t want the camera to look at my moving cube, I want the second cube to rotate showing always the same face to the moving one (like if the Moon was the moving cube and the Earth was the rotating one).

I didn’t understand the thing about gluLookAt…

Thank you again,
Remedios