Vesuvian
Vesuvian

Reputation: 717

Chaining a path of 3D bezier segments

I am writing a simple path editor in Unity3D by storing a series of points, in/out tangents, and drawing a bezier between each pair of points:

enter image description here

The data for the path:

enter image description here

What I'm finding is the tangents don't behave as I would expect. I'm very much used to image editing applications where the tangents are actually tangental to the curve:

enter image description here

But if you look back to my first image you can see that the tangents have to be moved to more perpendicular angles to get a smooth shape.

At this point I think there are a few possibilities:

  1. There's a stupid mistake in my code somewhere.
  2. My understanding of beziers is poor, and I'm incorrect in assuming I can simply chain beziers in this fashion.
  3. Maybe my data is incompatible somehow with Unity's bezier drawing (I've briefly read about linear, cubic, quadratic etc beziers).

Does anyone have any advice for me on how I can improve my usage of beziers?

Thanks, Ves

Upvotes: 0

Views: 1887

Answers (1)

Vesuvian
Vesuvian

Reputation: 717

Alrighty, I figured out the problem.

The unity documentation is extremely unclear on what the tangents are supposed to be. In my code I was using local vectors for the tangents, when the DrawBezier method actually wants the absolute positions of the tangent points.

I've been able to fix the curve drawing by doing the following:

Handles.DrawBezier(pointA.position,
                   pointB.position,
                   pointA.position + pointA.outTangent,
                   pointB.position + pointB.inTangent,
                   Handles.color,
                   null,
                   1.0f);

enter image description here

The solution made sense when I understood what Heisenbug was saying about the 4 points in a cubic bezier. Thanks a ton guys!

Upvotes: 1

Related Questions