0
\$\begingroup\$

I used this to help create a 3d cylindrical mesh that wraps around any y = f(x).
Each vertex is calculated by a point P:

  const P = (x,y,dy,r,u) => {
      const normal = -1/dy;
      return {
        x: x + r*Math.cos(u),
        y: y + r*Math.cos(u) * normal,
        z: r*Math.sin(u)
      }
    }

Images: x^2 sin(x)

As you can see there are issues when |dy| < 1. Obviously this is because of my calculation of the normal.
In general, I want the cylinders to face the proper direction, without any scaling.

I had tried to do a piecewise for |dy| < 1 but it isn't the elegant solution im looking for. I know that when the cylinder is facing vertically, the vertices are a function of x and z ( dy = inf so normal = 0 accounts for this), and when the cylinder is facing horizontally the vertices should be strictly in terms of y and z. This is where I get the divide by zero discontinuity and and the weird scaling when dividing by |dy| < 1

Basically, I am looking for some f(dy) that determines how much the x and y component contribute to the generation of the vertices.
full code

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I'm not sure how this function is trying to work, but an easier way for your application would be to precalculate the points for a circle at the origin and perpendicular to your line. Assuming your line is 2d and axis aligned and made up of points from p1 to pn, for any given line segment px to px+1, you can calculate an orthogonal basis matrix. Normalize the vector from px to px+1. We will call this line. Assume that line is aligned with the world forward/backward and up/down directions. Therefore, the world right vector is always perpendicular, so take Line X Right to get the Up vector of the line segment. With these three vectors, you can build a basis matrix to transform the points. Multiple each point by the basis matrix and add the new position to Px+1. The points are now all transformed into the space of the end point of the line segment regardless of how vertical it is.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .