Public Group

# cylinder

This topic is 4932 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Does anyone know the easiest way to draw a cylinder on a line segment? The segment can be oriented any way and can be any length. Basically, it will be a segment with a width of r except the endpoints won't be rounded. Mike C. http://www.coolgroups.com/zoomer

##### Share on other sites
You need to be more clear about what you mean by drawing the cylinder "on" the line segment. Will the segment form the axis of the cylinder, or a diameter of one of the endcaps, or just what exactly?

Also, you need to tell us what graphics library you're using, unless you're just looking for an algorithmic view of the problem.

##### Share on other sites
It forms the axis, and I'm using OpenGL.

Mike C.
http://www.coolgroups.com/zoomer

##### Share on other sites
Constructing your own cylinder is pretty easy. However, I think OpenGL (or the gl toolkit or something) has built-in functionality for drawing a cylinder. Perhaps that would be sufficient for your purposes.

##### Share on other sites
If I use the built-in functionality, I still need to figure out how to translate it, rotate it, and scale it to match the line segment. I'm not exactly sure how to do that.

Mike C.
http://www.coolgroups.com/zoomer

##### Share on other sites
Quote:
 If I use the built-in functionality, I still need to figure out how to translate it, rotate it, and scale it to match the line segment. I'm not exactly sure how to do that.
Right. So you need a translation and a rotation. Let's see...*goes to look at red book*...OpenGL constructs the cylinder along the z axis, extending from z = 0 to z = height. To me it makes more sense for the cylinder to be centered at the origin, so the first thing we'll do is translate it there.

Then, let's say your line segment is p0->p1. The center is (p0+p1)/2, so this is where we'll translate it to after we've rotated it. For our rotation, we need an axis-angle pair that will rotate the z axis into alignment with the vector p1-p0. So let's try to put it all together:

Vector3 center = (p0+p1)*.5; // Cylinder center
Vector3 v = p1-p0;
float height = v.Length(); // Cylinder height
float z = v.z / height;
if (z < -1) z = -1;
else if (z > 1) z = 1;
float angle = RadToDeg(acos(z)); // Angle by which to rotate
Vector3 axis = Vector3(0,0,1).Cross(v); // Or maybe the other way around
float length = axis.Length();
if (length < epsilon) {angle = 0; axis.Set(0,0,1);}
else {axis /= length;}
glTranslate(center.x, center.y, center.z); // Translate to cylinder center
glRotate(angle, axis.x, axis.y, axis.z); // Rotate into alignment
glTranslate(0, 0, -height * .5); // Translate so centered at origin
DrawGLCylinder(height);

Wow, what a mess - I probably didn't get that right. You might try it though (reverse the cross-product terms if it comes out backwards).

There are other ways to go about this, such as creating an orthonormal basis for the cylinder and/or creating it yourself with some trig. But who knows - maybe the above solution will work...

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 12
• 10
• 11
• 13
• ### Forum Statistics

• Total Topics
634096
• Total Posts
3015480
×