Skip to main content
gamedev.net
🔒 Locked

Calculating

Started by Node 2 replies 1k views
Original Post
Node
Node
I have to spheres in my opengl window (S and T), and I want to draw a cylinder (C) between them. The sphere is connected to S via a translation, however I am not sure how to to calculate the required rotations to make the other end of C connect to T. My code currently looks like this: dx = Tx - Sx; dy = Ty - Sy; dz = Tz - Sz; len = sqrt((dx * dx) + (dy * dy) + (dz * dz)); angle = atan2(dz, dx); angle = (180 * angle) / PI; glRotatef(angle, 0.0f, 1.0f, 0.0f); angle = atan2(dy, dz); angle = (180 * angle) / PI; glRotatef(angle, 1.0f, 0.0f, 0.0f); angle = atan2(dy, dx); angle = (180 * angle) / PI; glRotatef(angle, 0.0f, 0.0f, 1.0f);
SuperNerd
SuperNerd
glRotatef(0.0f, dx / len, dy / len, dz / len);

I havn't used opengl but from what I just looked up the documetation that should work. Depending on the orientation of your cylinder you may have to switch some of the x, y, and z parameters in the function to get it to work right.
Vorpy
Vorpy
This sounds similiar to this recent thread except that you're using opengl, while that thread was asking for a solution for quaternions.

I'm guessing right now the cylinder is aligned to one of the axes, and you want a rotation to align it with the direction from one sphere to the other.

Let v1 be a unit vector in the direction of the axis the cylinder is aligned with, and let v2 be (dx,dy,dz) in your code, the vector from the center of one sphere to the center of the other.

The axis of rotation is normalize(cross(v1,v2)).

The angle of rotation is acos(dot(v1,normalize(v2))).

I'm sure that like in the other thread there's some clever and more direct way of calculating the rotation you want, but I don't feel like trying to come up with it right now and this method is probably good enough.
Zakwayda
Zakwayda
I think this thread was an accidental double post; the original thread is here.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.