[Game of Purpose] Day 59 – Following a path either by distance or Spline points

Today I was playing around with supporting 2 ways of following a path.

By distance – Given a path it moves to a point every x distance.
By points – Given a path it moves to each point the Spline has configured.

https://blueprintue.com/blueprint/zmu…


This content originally appeared on DEV Community and was authored by Maciej Sawicki

Today I was playing around with supporting 2 ways of following a path.

  • By distance - Given a path it moves to a point every x distance.
  • By points - Given a path it moves to each point the Spline has configured.

https://blueprintue.com/blueprint/zmunmar-/

Image description

And my full path-following code looks like this:

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Components/SplineComponent.h"
#include "DrawDebugHelpers.h"
#include "SplineFollowerBase.generated.h"

UENUM(BlueprintType)
enum class EFollowingType : uint8
{
    ByDistance UMETA(DisplayName = "By Distance"),
    ByPoints UMETA(DisplayName="By Points")
};

UCLASS(BlueprintType, Blueprintable)
class SKY_PATROL_API USplineFollowerBase : public UActorComponent
{
    GENERATED_BODY()

public:
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spline")
    USplineComponent* TargetSpline;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spline")
    float DistanceToMove = 200.0f;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spline")
    EFollowingType FollowingType = EFollowingType::ByDistance;

protected:
    UFUNCTION(BlueprintCallable)
    UPARAM(DisplayName="NextTargetDistance") float CalculateNextTargetDistanceBase();

    UFUNCTION(BlueprintCallable)
    UPARAM(DisplayName="NextPointIndex") int CalculateNextPointIndexBase();

private:
    float NextTargetDistance = -1.0f;
    int NextPointIndex = -1;
};
#include "SplineFollowerBase.h"

#include "Utils/MyStringUtils.h"

float USplineFollowerBase::CalculateNextTargetDistanceBase()
{
    check(TargetSpline != nullptr);

    const auto TargetSplineTotalLength = TargetSpline->GetSplineLength();

    // staring the movement
    if (NextTargetDistance < 0.0f)
    {
        NextTargetDistance = 0.0f;
    }
    // looping
    else if (NextTargetDistance >= TargetSplineTotalLength)
    {
        NextTargetDistance = 0.0f;
    }
    else
    {
        NextTargetDistance = FMath::Clamp(NextTargetDistance + DistanceToMove, 0.0f, TargetSplineTotalLength);
    }

    PrintString(FString::Printf(TEXT("NextTargetDistance: %f"), NextTargetDistance));


    return NextTargetDistance;
}

int USplineFollowerBase::CalculateNextPointIndexBase()
{
    check(TargetSpline != nullptr);

    const auto TargetSplineTotalPoints = TargetSpline->GetNumberOfSplinePoints();

    NextPointIndex = (NextPointIndex + 1) % TargetSplineTotalPoints;

    PrintString(FString::Printf(TEXT("NextPointIndex: %d"), NextPointIndex));

    return NextPointIndex;
}


This content originally appeared on DEV Community and was authored by Maciej Sawicki


Print Share Comment Cite Upload Translate Updates
APA

Maciej Sawicki | Sciencx (2024-07-16T23:37:31+00:00) [Game of Purpose] Day 59 – Following a path either by distance or Spline points. Retrieved from https://www.scien.cx/2024/07/16/game-of-purpose-day-59-following-a-path-either-by-distance-or-spline-points/

MLA
" » [Game of Purpose] Day 59 – Following a path either by distance or Spline points." Maciej Sawicki | Sciencx - Tuesday July 16, 2024, https://www.scien.cx/2024/07/16/game-of-purpose-day-59-following-a-path-either-by-distance-or-spline-points/
HARVARD
Maciej Sawicki | Sciencx Tuesday July 16, 2024 » [Game of Purpose] Day 59 – Following a path either by distance or Spline points., viewed ,<https://www.scien.cx/2024/07/16/game-of-purpose-day-59-following-a-path-either-by-distance-or-spline-points/>
VANCOUVER
Maciej Sawicki | Sciencx - » [Game of Purpose] Day 59 – Following a path either by distance or Spline points. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/16/game-of-purpose-day-59-following-a-path-either-by-distance-or-spline-points/
CHICAGO
" » [Game of Purpose] Day 59 – Following a path either by distance or Spline points." Maciej Sawicki | Sciencx - Accessed . https://www.scien.cx/2024/07/16/game-of-purpose-day-59-following-a-path-either-by-distance-or-spline-points/
IEEE
" » [Game of Purpose] Day 59 – Following a path either by distance or Spline points." Maciej Sawicki | Sciencx [Online]. Available: https://www.scien.cx/2024/07/16/game-of-purpose-day-59-following-a-path-either-by-distance-or-spline-points/. [Accessed: ]
rf:citation
» [Game of Purpose] Day 59 – Following a path either by distance or Spline points | Maciej Sawicki | Sciencx | https://www.scien.cx/2024/07/16/game-of-purpose-day-59-following-a-path-either-by-distance-or-spline-points/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.