Hi all.

I try to summarize the problem.

I’m triyng to draw 2 spheres connected by a cylinder.

If I only perfom x-axe rotation it works (the 2 sphere are perfectly connected). The same if I only perform the y-axe rotation.

Unfortunately if I perform 2 consequently rotation the

second sphere is not correcly aligned with the cylinder.

Here the code:

…

// draw first sphere

glPushMatrix();

glTranslatef(x1, y1, z1);

glutSolidSphere(10.0f, 15, 15);

// draw cylinder

glPushMatrix();

rx = atan2f((x2-x1), (z2-z1)) * (360.0f / (2.0f * PI));

ry = atan2f((y1-y2), (z2-z1)) * (360.0f / (2.0f * PI));

glRotatef(rx, 0.0f, 1.0f, 0.0f);

glRotatef(ry, 1.0f, 0.0f, 0.0f);

gluCylinder(cyl, 2.0, 2.0, height, 15, 15);

glPopMatrix();

glPopMatrix();

// draw second sphere

glTranslatef(x2, y2, z2);

glutSolidSphere(10.0f, 15, 15);

…

Anyone can explain me why it doesn’t work?

Thanks

S.

Well, this may be too obvious to be the reason, but from your code, it looks like you are rotating the cylinder and not the spheres. If you want the spheres to stay attached to the ends (?) of the cylinder, you need to rotate them all together…

I found the solution here: http://www.euclideanspace.com/maths/geometry/rotations/euler/index.htm

Imaging you are in a cuboid room, you on the floor of one corner looking along along the base of one wall, you want to turn to look at the top of the opposite corner. What angle do you have to turn through?

Most people would say that you would need to turn through a heading of 45° and then rotate up at an attitude of 45°. This belief is so strong that programmers will spend days rewriting and debugging their code if they don’t get this answer.

In fact we need to turn through a heading of 45° and then rotate up at an attitude of 35°. You turn through 45° and you are now looking at the base of the diagonal corner. You now want to look up to the top of the diagonal side. This forms a triangle where the adjacent side is √2 and the opposite side is 1. So the angle is arctan(0.7071)=35°.

This is a warning not to trust intuition when working with 3D angles and avoid using euler angles whenever possible. (see lookat method for other methods)