Spline Tangents in C++

Hello :slight_smile:

How can I properly define / calculate tangents (the curve-factor) for spline components in c++ and how do I set them?

When I use the base- splines GetLocalLocationAndTangentAtSplinePoint function I just get straight lines. In fact, I always just get straight lines, but if I change something in the tangent values the mesh is additionally distorted, self-twisting, self-intersecting, almost everything, but not round or smooth, it is straight… I want a simple smooth curve, how can I acchive that?

Spline Components are unfortunaly 0% explained for C++ implementations. :frowning:

Thanks! :slight_smile:

I’m stuck on almost the same problem.

Hi

In C++ you can create a spline component using the standard approach inside the constructor of eg an actor containing the spline:

SplineComp = ObjectInitializer.CreateDefaultSubobject<USplineComponent>(this, TEXT("SplineComp"));

This creates a spline component with two points. You can add additional points to the spline with:

SplineComp->AddSplineWorldPoint(FVector(x,y,z)); //add a point in world space

or equivalently AddSplineLocalPoint to define the point in local space (ie relative to the actor containing the spline.)

You would obviously also need to move the originally created points

SplineComp->SetWorldLocationAtSplinePoint(0, FVector(x,y,z));
SplineComp->SetWorldLocationAtSplinePoint(1, FVector(x1,y1,z1));

Then you can subsequently get the position / tangent along the spline with the various accessors e.g.

FVector dir = GetWorldDirectionAtDistanceAlongSpline(dist); 

To use GetLocalLocationAndTangentAtSplinePoint you would do e.g.

FVector loc;
FVector tan;
SplineComp->GetLocalLocationAndTangentAtSplinePoint(i,loc,tan); 

This will return the location and tangent of the i’th point in the spline in loc and tan respectively. This is zero based so if you have added 2 points to the spline then i=3 would return the loc/tan of the last point in the spline.

By default the spline curve is automatically calculated, but you can modify the incoming and outgoing tangents at each point that you have defined for the spline by setting properties in the SplineInfo array e.g.

	SplineComp->SplineInfo.Points[2].InterpMode = CIM_CurveUser;// CIM_CurveAutoClamped;
	SplineComp->SplineInfo.Points[2].ArriveTangent.Set(x1, y1, z1);
	SplineComp->SplineInfo.Points[2].LeaveTangent.Set(x2, y2, z2);

This is equivalent to dragging the red handles in the editor when you click on a point in the spline where x1,y1,z1 / x2,y2,z2 would vectors describing the two handles. The longer the handle the more slowly the spline curve bends away from the direction defined by the tangent

HTH

1 Like