PDA

View Full Version : Problem with direction constraint



remedios79
04-15-2004, 03:49 AM
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)\n", 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
Remedios

endash
04-15-2004, 02:44 PM
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.

remedios79
04-15-2004, 11:46 PM
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:
file:/home/simona/personal/ScreenShots/const1.jpeg
file:/home/simona/personal/ScreenShots/const2.jpeg

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

charliejay
04-16-2004, 04:28 AM
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...

remedios79
04-16-2004, 05:02 AM
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

charliejay
04-19-2004, 02:14 AM
Sorry, my bad, didn't read carefully enough.

Yeah, I've done that before, it' s much the same as what I described above.

What you essentially need to do, is to be able to calculate the moving cube's position and orientation, in terms of the static cube's coordinate system.

Once you can do that, you then just use a standard rotation to rotate the moving cube, and then convert from the static cube's coordinate system back to world coordinates.

I can explain how to do all that, but a picture would probably be better, so if you want me to do that, let me know, and I'll post a diagram...