I want to show draw a cylinder that starts at point a and that points to I think the key is in the first glRotated, but this is my first time working with openGL a and b are btVector3
glPushMatrix();
glTranslatef(a.x(), a.y(), a.z());
glRotated(0, b.x(), b.y(), b.z());
glutSolidCylinder(.01, .10 ,20,20);
glPopMatrix();
Any suggestions ??
According to glutsolidcylinder(3) - Linux man page:
Hence, you have to prepare the transformations respectively:
The usage of
glRotatef()
seems to be mis-understood, also:This would result in:
I wrote this code out of mind (using the doc. of
btVector3
which I've never used before). Thus, please, take this with a grain of salt. (Debugging might be necessary.)So, please, keep the following in mind:
The doc. does not mention whether
btVector3::angle()
returns angle in degree or radians – I assumed radians.When writing such code, I often accidentally flip things (e.g. rotation into opposite direction). Such things, I usually fix in debugging, and this is probably necessary for the above sample code.
If (b - a) is already along positive or negative z-axis, then (b - a) × (0, 0, 1) will yield a 0-vector. Unfortunately, the doc. of
btVector3::normalize()
does not mention what happens when applied to a 0-vector. If an exception is thrown in this case, extra checks have to be added, of course.Your rotate doesn't do anything since you rotate 0 degrees.
You want the axiz z to point towards b. To do that you need to compute the angle between the z axis (0,0,1) and norm(b - a) (that is
arccos(z dot norm(b - a))
) and you need to rotate that amount around the cross product between z axis and b - a. Your vector library should have these methods(dot and cross product) already implemented.norm(x) is the normalized version of x, the one with length 1.