View Full Version : torus knot problem

10-26-2004, 02:40 PM
im using the following code to draw a torus knot:

float r,x,y,z;
int DrawGLScene(GLvoid)


for(double i = 0; i < 2 * PI; i += PI / 720)
glColor3f(1.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glColor3f(0.0, 1.0, 0.0);

return TRUE;
i have 2 questions:
1) how do i make the camera follow the knot or make the knot 'move'
2) how do i make the triangles be conected not just a secuence of triangles ??
thanks in advance

10-27-2004, 11:38 PM
2) how do i make the triangles be conected not just a secuence of triangles ?Dust off the old calculus book and and lookup the chapter on vector valued functions. You can differentiate each component with respect to the time variable. The first derivative is velocity, the second is acceleration. The cross product of these 2 vectors defines a vector, B, that's orthogonal to both. The cross product of B and T (velocity), defines a third vector, N. These 3 vectors collectively define what's sometimes called a TNB triad. With a TNB, you can wind a circle around a point on the knot using N as the x-axis and B as the y-axis, for example. Create one of these "rings" at each time step. Once you have all the rings along the length of the knot, you could render the entire knot as a single tri-strip (like a string of little cylinders, so to speak).

1) how do i make the camera follow the knot or make the knot 'move'You can use the TNB you created above as a basis for the camera. Just stuff the TNB vectors into the columns of your camera matrix.

Note that there will be a twist in the orientation with this approach, since the N vector always points in the direction of curve concavity, so you might experiment with various splines instead if you don't care for that behavior.

10-28-2004, 04:04 PM
hi, thanks for your help, i have a new question, i have now T and N, what's the cross product, could you help me with some code plz thanks!

10-29-2004, 01:26 AM
I put together a lttle glut-based demo of this. You can copy the source and paste it into a console project. The code should build as is.


It might be cool to try and animate the knot over time; perform a morph of sorts. The camera movement code needs a good time function, but I opted to leave it out in the interest of brevity.

10-29-2004, 08:20 AM
thanks a lot !!