Random mesh flipping with USplineMeshComponents along USplineComponent

Hi there,

I created a class that accepts FTransforms, adds them to a USplineComponent and then creates USplineMeshComponents along that spline. Works nicely, besides every now and then the placed mesh is rotated by 180 degree and the order of the SplineMeshComponents looks very odd around that one (see picture).

Below is my code, I’d be happy for every hint at what I did wrong.

Best regards

Robert

FSplinePoint ASplineMesh::SplinePointFromTransform(float InputKey, const FTransform &Transform)
{
    FQuat Rotation = Transform.GetRotation();
    return FSplinePoint(InputKey, Transform.GetTranslation(), Rotation.GetForwardVector(), Rotation.GetForwardVector(), Rotation.Rotator(), Transform.GetScale3D(), ESplinePointType::CurveCustomTangent);
}

void ASplineMesh::ClearSplinePoints()
{
    Spline->ClearSplinePoints();
    UE_LOG(LogCarla, Log, TEXT("Number of Spline Points: %d"), Spline->GetNumberOfSplinePoints());
}

void ASplineMesh::AddSplinePoint(const FTransform &Transform)
{    
    Spline->AddPoint(SplinePointFromTransform(static_cast<float>(Spline->GetNumberOfSplinePoints()), Transform), true);
    UE_LOG(LogCarla, Log, TEXT("Number of Spline Points: %d"), Spline->GetNumberOfSplinePoints());
}

void ASplineMesh::CreateMesh()
{
    DeleteMesh();
    const int32 SplinePoints = Spline->GetNumberOfSplinePoints();
    FVector StartPosition;
    FVector StartTangent;
    FVector EndPosition;
    FVector EndTangent;
    for(int SplineCount = 0; SplineCount < (SplinePoints - 1); SplineCount++)
    {
        StartPosition = Spline->GetLocationAtSplinePoint(SplineCount, ESplineCoordinateSpace::Local);
        StartTangent = Spline->GetTangentAtSplinePoint(SplineCount, ESplineCoordinateSpace::Local);
        EndPosition = Spline->GetLocationAtSplinePoint(SplineCount+1, ESplineCoordinateSpace::Local);
        EndTangent = Spline->GetTangentAtSplinePoint(SplineCount+1, ESplineCoordinateSpace::Local);
        UE_LOG(LogCarla, Log, TEXT("Adding new element from point %d (Loc: x=%.2f y=%.2f z=%.2f, Rot: p=%.2f r=%.2f y=%.2f) to point %d (Loc: x=%.2f y=%.2f z=%.2f, Rot: p=%.2f r=%.2f y=%.2f)"), SplineCount, StartPosition.X, StartPosition.Y, StartPosition.Z, StartTangent.X, StartTangent.Y, StartTangent.Z, SplineCount+1, EndPosition.X, EndPosition.Y, EndPosition.Z, EndTangent.X, EndTangent.Y, EndTangent.Z);
        USplineMeshComponent* SplineMesh = NewObject<USplineMeshComponent>(this, USplineMeshComponent::StaticClass());
        SplineMesh->SetStaticMesh(StaticMesh);
        SplineMesh->SetStartAndEnd(StartPosition, StartTangent, EndPosition, EndTangent, true);
        SplineMesh->RegisterComponentWithWorld(GetWorld());
        SplineMesh->CreationMethod = EComponentCreationMethod::UserConstructionScript;
        SplineMesh->SetMobility(EComponentMobility::Movable);
        SplineMesh->AttachToComponent(Spline, FAttachmentTransformRules::KeepRelativeTransform);
        Meshes.Add(Cast<UStaticMeshComponent>(SplineMesh));
        UE_LOG(LogCarla, Log, TEXT("Number of Spline Meshes: %d"), Meshes.Num());
    }
}