Hello everyone,
I’m trying to create a spline in a dynamic way, to encapsulate an unknown number of objects and then fill up this spline with items, the problem I’m facing is that a segment of the spline, for no clear reason, just flips around, every spline point is generated dynamically and in the same way.
This is the code to fill up the Spline :
int32 NumInstances = FMath::FloorToInt(MySplineComponent->GetSplineLength() / DistanceBetweenSegments);
for (int32 i = 0; i < NumInstances; ++i) {
float SegmentLength = MySplineComponent->GetSplineLength() / NumInstances;
float Distance = SegmentLength * i;
FVector Location = MySplineComponent->GetLocationAtDistanceAlongSpline(Distance, ESplineCoordinateSpace::Local);
FVector NextPointLocation;
if (i < NumInstances - 1) {
NextPointLocation = MySplineComponent->GetLocationAtDistanceAlongSpline(Distance + SegmentLength, ESplineCoordinateSpace::Local);
} else {
FVector Tangent = MySplineComponent->GetTangentAtDistanceAlongSpline(Distance, ESplineCoordinateSpace::Local).GetSafeNormal();
NextPointLocation = Location + Tangent * SegmentLength; }
FVector Direction = (NextPointLocation - Location).GetSafeNormal();
FVector UpVector = FVector::UpVector;
FQuat Rotation = FQuat::FindBetweenNormals(FVector::ForwardVector, Direction);
FRotator NewRotator = Rotation.Rotator();
FVector RightVector = FVector::CrossProduct(UpVector, Direction).GetSafeNormal();
FVector NewUpVector = FVector::CrossProduct(Direction, RightVector);
NewRotator = FRotationMatrix::MakeFromXZ(Direction, NewUpVector).Rotator();
InstanceOfTrackSegment->AddInstance(FTransform(Rotation, Location));}
This is the code to add the spline point which causes the issues:
MyVectorLeft = FVector(Max.X, Min.Y, Min.Z);MyVectorRight = FVector(Max.X, Max.Y, Min.Z);
TangentTop = (MyVectorLeft + MyVectorRight) / 2.0f;
MyVectorLeft = FVector(Min.X, Min.Y, Min.Z);
MyVectorRight = FVector(Min.X, Max.Y, Min.Z);
TangentBottom = (MyVectorLeft + MyVectorRight) / 2.0f;
MidPoint = (TangentTop + TangentBottom) / 2.0f;
UE_LOG(LogTemp, Log, TEXT("My Vector Bottom: %s"), *MidPoint.ToString());
MySpline->AddSplinePoint(MidPoint, ESplineCoordinateSpace::Local, true);
data.SetIdBottomSplinePoint(MySpline->GetNumberOfSplinePoints()-1);
MySpline->UpdateSpline();
I’ve asked chatGPT for help but it has been of not much use either so if anyone knows how to solve this, here are pictures of the issue:
as you can see the objects follow the spline nicely up to that point, where they just flip around, with 3 wheels it works fine, the issue only appears when there is more than 1 straight connection at the bottom