Spline Circle Forward Vector Flips

DigitalGreenTea
Fri, 25 Oct 2024 15:42:18 +0000
Programming & Scripting

Hi!

I’m trying to create a spline circle, and I have spline meshes attached to it. I noticed spline point 2 and 3 (the last two) have flipped forward vectors, which causes my spline meshes to flip also.

This is the code I’m using:

{
    if (!SplineComponent) return;
    SplineComponent->ClearSplinePoints(false);

    // Add spline points in a circle, constrained to Y and Z axes
    const int32 NumberOfPoints = 4;
    for (int32 i = 0; i < NumberOfPoints; ++i)
    {
        float Angle = (i / static_cast<float>(NumberOfPoints)) * 2.0f * PI;
        float Y = SplineRadius * FMath::Cos(Angle);
        float Z = SplineRadius * FMath::Sin(Angle);
        float X = 0.0f;  // X remains constant in my use case

        FVector Position(X, Y, Z);
        FVector Tangent = FVector(0.0f, -SplineRadius * FMath::Sin(Angle), SplineRadius * FMath::Cos(Angle));
        SplineComponent->AddSplinePoint(Position, ESplineCoordinateSpace::Local, false);
        SplineComponent->SetTangentAtSplinePoint(i, Tangent, ESplineCoordinateSpace::Local, false);
    }
    SplineComponent->UpdateSpline();
}

Anybody have some ideas how I can fix the forward vector flips? I haven’t across any solutions online, and changing the up vector also had no effect.

4 posts - 1 participant