Download

Insert point in Spline

Hey everyone,

I want to insert a point in a spline without changing the spline.


so b+c is a

I have tried to implement it:



void ASplineActor::InsertPoint(int pIndex, FVector pWorldLocation, bool pFinal)
{
//pWorldLocation IS a point on the spline

float InputKey = SplineComponent->FindInputKeyClosestToWorldLocation(pWorldLocation);

FVector PrevInTangent = SplineComponent->GetArriveTangentAtSplinePoint(pIndex - 1, ESplineCoordinateSpace::World);
FVector PrevOutTangent = SplineComponent->GetLeaveTangentAtSplinePoint(pIndex - 1, ESplineCoordinateSpace::World);


FVector FollowingTangent = SplineComponent->GetArriveTangentAtSplinePoint(pIndex + 1, ESplineCoordinateSpace::World);

FVector Tangent = SplineComponent->GetTangentAtSplineInputKey(InputKey, ESplineCoordinateSpace::World);

SplineComponent->AddSplinePointAtIndex(pWorldLocation, pIndex, ESplineCoordinateSpace::World, true);

float PrevToCurrent = SplineComponent->GetDistanceAlongSplineAtSplinePoint(pIndex) - SplineComponent->GetDistanceAlongSplineAtSplinePoint(pIndex - 1);
float CurrentToFollowing = SplineComponent->GetDistanceAlongSplineAtSplinePoint(pIndex + 1) - SplineComponent->GetDistanceAlongSplineAtSplinePoint(pIndex);

float PrevMultiplier = PrevToCurrent / (PrevToCurrent + CurrentToFollowing);
float FollowingMultiplier = CurrentToFollowing / (PrevToCurrent + CurrentToFollowing);

PrevOutTangent *= PrevMultiplier;
FollowingTangent *= FollowingMultiplier;

FVector InTangent = Tangent * PrevMultiplier;
FVector OutTangent = Tangent * FollowingMultiplier;

SplineComponent->SetTangentsAtSplinePoint(pIndex, InTangent, OutTangent, ESplineCoordinateSpace::World, false);
//SplineComponent->SetTangentAtSplinePoint(pIndex, Tangent, ESplineCoordinateSpace::World, false);
SplineComponent->SetTangentsAtSplinePoint(pIndex - 1, PrevInTangent, PrevOutTangent, ESplineCoordinateSpace::World, false);
SplineComponent->SetTangentAtSplinePoint(pIndex + 1, FollowingTangent, ESplineCoordinateSpace::World, true);
ConstructAllPoints();
}


The second part (c) seems to work, at least I can’t spot a difference there. For the first part (b) it is better than without correction but it doesn’t completely match the old spline.

Is the approach with the Multipliers the right way to do it?

I hope somebody can solve the problem here. Thanks in advance!