views:

622

answers:

2

In my current OpenGL project I am trying to make the links of a chain hug the contours of a Bezier curve. How can I find the angle between two points on the curve so that I can position the links of the chain so that they follow the curve.

Here is a picture of the curve and chain, I need some way of rotating the links so that they follow the curve.

Does anybody here know how to do this?

A: 

Let the points on your bezier curve be A and B. Normalize the Vector AB so it has length 1. Let this be AB_norm. Then use asin(AB_norm.y) or acos(AB_norm.x) to get the angle. An angle of 0 degrees is a horizontal vector to the right, then. C-style pseudocode follows:

 get_angle(Point A, Point B) {
   AB.x = B.x - A.x;
   AB.y = B.y - A.y;
   length = sqrt(AB.x * AB.x + AB.y * AB.y);

   AB_norm.y /= AB.y / length;
   angle = asin(AB_norm.y);
   // or
   // AB_norm.x /= AB.x / length;
   // angle = acos(AB_norm.x);
 }

 angle = get_angle(A, B);
 glRotatef(angle, 0.0f, 0.0f, 1.0f);
 // Draw the chain link here
schnaader
+4  A: 

Maybe something like this is what you need.

How to calculate the tangent to a Bezier curve

This is hard to find online. It must be a secret closely held by those who know. Oh you can find the math formulae, but have fun interpreting them if you are not a mathematician. So what is a poor developer to do? Go back to school.

I spent a couple days bashing my skull over this one. I googled my brains out (which was easier once my skull was sufficiently bashed). Then one bright beautiful Saturday, I was holed up in my developer's dungeon resting my weary bones. I had the TV on in front of me and Wikipedia to the right and there I was lazily switching between watching them both.

Continue Reading...

duffymo
Reading the linked article, all of the hours I spent trying to understand and code my own bezier curves suddenly clicked into place. Thanks!
Liam
+1 for the beautiful animations alone. I love seeing this type of stuff :)
Jonathan Sampson