c++mathvectormath

How to align rows of Vectors(points) to center?


I posted this question an few days ago and from the comments i did not formulate the question as much as i might have thought i needed.

Here is my updated version with more information.

I have the following.

enter image description here

Rows of points aligned to the left of the center.

Achieved via:

// Loop for basically CurrentWaveCount + 1
for (int32 Index = CurrentWaveCount; Index >= 0; Index--)
{
    FHitResult HitResult(ForceInit);
    FCollisionQueryParams TraceParams = FCollisionQueryParams();
    TraceParams.AddIgnoredActor(GetOwner());
    TraceParams.AddIgnoredActor(this);

    //////////////////////////////////////////////////////////////////////////

    FVector CastLocation = (GetActorForwardVector() * (CurrentWaveCount * WaveSpawnDistanceIncrement) + GetActorLocation());
    CastLocation += (-GetActorRightVector() * (Index * ChildWaveActorSeperationIncrement));

    //////////////////////////////////////////////////////////////////////////

    CastLocation.Z += FixedCastTraceHeightOffset;

    FVector TraceStart = CastLocation;
    FVector TraceEnd = TraceStart + (-FVector::UpVector * MaxFallbackTraceDistance);

    GetWorld()->LineTraceSingleByChannel(HitResult, TraceStart, TraceEnd, ECC_FixedCastableTraceChannel, TraceParams);

The CastLocation variable is what controls the position of each Point.

However i would like to align them to the center instead of offset to one side and i cannot work out the Math to do this.

Answers dont have to be in C++ and can be just Math as i can work it out from there if that helps.

Thanks.

enter image description here

Current:

enter image description here

Expected:

enter image description here

Updated Solution Code:

// Loop for basically CurrentWaveCount + 1
for (int32 Index = CurrentWaveCount; Index >= 0; Index--)
{
    FHitResult HitResult(ForceInit);
    FCollisionQueryParams TraceParams = FCollisionQueryParams();
    TraceParams.AddIgnoredActor(GetOwner());
    TraceParams.AddIgnoredActor(this);

    //////////////////////////////////////////////////////////////////////////

    float HalfWidth = CurrentWaveCount * ChildWaveActorSeperationIncrement / 2;

    FVector CastLocation = (GetActorForwardVector() * (CurrentWaveCount * WaveSpawnDistanceIncrement) + GetActorLocation());
    CastLocation += -GetActorRightVector() * (Index * ChildWaveActorSeperationIncrement);
    CastLocation += GetActorRightVector() * HalfWidth;

    //////////////////////////////////////////////////////////////////////////

    CastLocation.Z += FixedCastTraceHeightOffset;

    FVector TraceStart = CastLocation;
    FVector TraceEnd = TraceStart + (-FVector::UpVector * MaxFallbackTraceDistance);

    GetWorld()->LineTraceSingleByChannel(HitResult, TraceStart, TraceEnd, ECC_FixedCastableTraceChannel, TraceParams);

Solution

  • I am making the following assumptions:

    Then the issue would appear to be here:

    CastLocation += (-GetActorRightVector() * (Index * ChildWaveActorSeperationIncrement));
    

    If ChildWaveActorSeperationIncrement is always nonnegative (and Index is non-negative), then this line always displaces the cast location somewhere to the left of blondie.

    The maximum leftward-displacement is when Index is largest, so it is CurrentWaveCount * ChildWaveActorSeperationIncrement. This is also the width of the row in RightVector units. To center up the rows as requested, we just need to displace each row to the right by half of this width, by adding (in pseudocode):

    half_width = CurrentWaveCount * ChildWaveActorSeperationIncrement / 2
    CastLocation = ExistingCastLocation + GetActorRightVector() * half_width