Archived

This topic is now archived and is closed to further replies.

question on cylinders

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im having trouble positioning my cylinders. How would one go about making a cylinder that lies horizontally between the points x1,z1 and x2,z2? I have the lenght ok, its just sqrt((x2-x1)*(x2-x1)+(z2-z1)*(z2-z1)) but I cant seem to get it to point in the right direction, or be in the right position. Any suggestions on this? The lesson doesnt really explain this part. I thought about rotating the scene, but Im not sure how to calculate the angle of rotation to make the vector from x1,z1 point at x2,z2. (or which way to point it!) Thanks! Karl

Share this post


Link to post
Share on other sites
Here's some basic trigonometry.

Since tan(angle)=y/x, you can get angle=atan(y/x)

Subtract (x1,y1,z1) from both points and you get a segment with one end at the origin. Now x2' is the length of a triangle projected onto the xy or xz plane.

The rest is left for homework
--solo

[edited by - 5010 on June 5, 2003 11:28:57 AM]

Share this post


Link to post
Share on other sites
Alternatively if you want to build the matrix directly without mucking around with angles:

(ignore the fact it isn''t very optimized)


// draws a unit radius cylinder aligned with the +ve z axis

void draw_z_cylinder()
{
float r = 0;

vector3 start_0( cos(r), sin(r), 0 );
vector3 start_1( cos(r), sin(r), 1 );

vector3 p0_0 = start_0;
vector3 p0_1 = start_1;

for(int i=0;i<10;i++)
{
r += ((2*pi) / 10.0);

vector3 p1_0( cos(r), sin(r), 0 );
vector3 p1_1( cos(r), sin(r), 1 );

// draw ''quad'' between p1 & p0

glBegin(GL_TRIANGLE_STRIP);
glNormal3dv(p1_0); glVertex3dv( p1_0 );
glNormal3dv(p1_0); glVertex3dv( p1_1 );
glNormal3dv(p0_0); glVertex3dv( p0_0 );
glNormal3dv(p0_0); glVertex3dv( p0_1 );
glEnd();

p0_0 = p1_0;
p0_1 = p1_1;
}
}



Thats very basic for drawing a cylinder aligned to a particular axis (in this case the Z axis). Now we construct the matrix to rotate and scale that cylinder to the correct position.



void draw_align_cylinder(const vector3& p0,const vector3& p1,float radius,float length)
{
// build a matrix that is the transpose of the matching lookat matrix

vector3 up(0,1,0);

vector3 dir = p1 - p0;

if( fabs(dir.x) < epsilon && fabs(dir.z) < epsilon )
up = vector3(1,0,0);

// dir normalized is the new z axis

vector3 z_axis = normalize(dir);

vector3 x_axis = cross(z_axis,up);
vector3 y_axis = cross(z_axis,x_axis);

matrix4X4 m(x_axis.x,y_axis.x,z_axis.x,p0.x,
x_axis.y,y_axis.y,z_axis.y,p0.y,
x_axis.z,y_axis.z,z_axis.z,p0.z,
0.0,0.0,0.0,1.0);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glMultMatrixd(m);
glMultMatrixd(scale_4X4<double>(radius,radius,length));
draw_z_cylinder();
glPopMatrix();
}

Share this post


Link to post
Share on other sites