From 099598d12fe69fb6d74d88a2e2d9be3efd13cb2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=BChlem?= <hu395906@win.rz.rwth-aachen.de> Date: Tue, 8 Apr 2025 17:04:19 +0200 Subject: [PATCH] style: Enforced coding style, grouped some code into methods --- Source/CharacterPlugin/Private/VHMovement.cpp | 121 ++++++++---------- Source/CharacterPlugin/Public/VHMovement.h | 9 +- 2 files changed, 59 insertions(+), 71 deletions(-) diff --git a/Source/CharacterPlugin/Private/VHMovement.cpp b/Source/CharacterPlugin/Private/VHMovement.cpp index 5d4377a2..9e667b0b 100644 --- a/Source/CharacterPlugin/Private/VHMovement.cpp +++ b/Source/CharacterPlugin/Private/VHMovement.cpp @@ -16,6 +16,7 @@ #include "Components/CapsuleComponent.h" #include "Components/SkeletalMeshComponent.h" #include "VHAnimInstance.h" +#include "UniversalObjectLocators/AnimInstanceLocatorFragment.h" // Sets default values for this component's properties UVHMovement::UVHMovement() @@ -98,32 +99,36 @@ void UVHMovement::BeginPlay() void UVHMovement::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); - // Calculate IKOffset for Foot alignment - FVector MeshLocation = AVirtualHumanPtr->GetBodyMesh()->GetComponentLocation(); + CalculateIK(DeltaTime); +} - // RightFoot +void UVHMovement::CalculateIK(const float DeltaTime) +{ + if (!AnimationInstance) return; - FHitResult Hit = IKFootTrace(RightFootSocket, IKTraceDistance); + // Calculate IKOffset for Foot alignment + const FVector MeshLocation = AVirtualHumanPtr->GetBodyMesh()->GetComponentLocation(); + + // RightFoot + FHitResult Hit = IKFootTrace(RightFootSocket); // the location we want the foot of the VA to be - FVector HitLocationRight = Hit.Location; + const FVector HitLocationRight = Hit.Location; - FVector offsetRight = HitLocationRight - MeshLocation; - float IKOffsetRight = offsetRight.Z - IKHipOffset; + const FVector OffsetRight = HitLocationRight - MeshLocation; + float IKOffsetRight = OffsetRight.Z - IKHipOffset; // LeftFoot - - Hit = IKFootTrace(LeftFootSocket, IKTraceDistance); + Hit = IKFootTrace(LeftFootSocket); // the location we want the foot of the VA to be - FVector HitLocationLeft = Hit.Location; + const FVector HitLocationLeft = Hit.Location; - FVector offsetLeft = HitLocationLeft - MeshLocation; - float IKOffsetLeft = offsetLeft.Z - IKHipOffset; + const FVector OffsetLeft = HitLocationLeft - MeshLocation; + float IKOffsetLeft = OffsetLeft.Z - IKHipOffset; // adjust the height of the VA, by lowering the hip bone - float footHeightDistance = abs(HitLocationRight.Z - HitLocationLeft.Z); float hipOffset = 0; @@ -140,21 +145,14 @@ void UVHMovement::TickComponent(float DeltaTime, ELevelTick TickType, FActorComp float footRotationAngleRAD = acosf(FVector::DotProduct(normal, up)); // check if VA is walking UP or DOWN and change sign of footRotationAngle accordingly - FVector velocity = AVirtualHumanPtr->GetVelocity(); - float normalToVelocity = FVector::DotProduct(normal, velocity); + const FVector Velocity = AVirtualHumanPtr->GetVelocity(); + const float NormalToVelocity = FVector::DotProduct(normal, Velocity); - if (velocity.Size() > 0) // only change when walking + if (Velocity.Size() > 0) // only change when walking { // if angle between normal and velocity is bigger than 90 degree value is negative. // that means VA is walking UP - if (normalToVelocity < 0) - { - bWalkingUp = true; - } - else - { - bWalkingUp = false; - } + bWalkingUp = NormalToVelocity < 0; } // the rotation introduces a height error @@ -202,19 +200,14 @@ void UVHMovement::TickComponent(float DeltaTime, ELevelTick TickType, FActorComp IKOffsetRight = FMath::Clamp(IKOffsetRight, 0.0f, IKMaxOffset); IKOffsetRightFoot = FMath::FInterpTo(IKOffsetRightFoot, IKOffsetRight, DeltaTime, IKInterpSpeed); - IKOffsetLeft = FMath::Clamp(IKOffsetLeft, 0.0f, IKMaxOffset); IKOffsetLeftFoot = FMath::FInterpTo(IKOffsetLeftFoot, IKOffsetLeft, DeltaTime, IKInterpSpeed); - // write variables to Animation Instance - if (AnimationInstance) - { - AnimationInstance->IKOffsetRight = -IKOffsetRightFoot; - AnimationInstance->IKOffsetLeft = IKOffsetLeftFoot; - AnimationInstance->IKHipOffset = IKHipOffset; - AnimationInstance->IKFootRotation = IKFootRotationAngle; - } + AnimationInstance->IKOffsetRight = -IKOffsetRightFoot; + AnimationInstance->IKOffsetLeft = IKOffsetLeftFoot; + AnimationInstance->IKHipOffset = IKHipOffset; + AnimationInstance->IKFootRotation = IKFootRotationAngle; } void UVHMovement::MoveToWaypoint() @@ -243,7 +236,6 @@ void UVHMovement::MoveToWaypoint() VirtualHumanAIController->DestroyAgent(); } - if (CurrentWaypointIterator < Waypoints.Num()) { CurrentWaypoint = Waypoints[CurrentWaypointIterator]; @@ -253,42 +245,41 @@ void UVHMovement::MoveToWaypoint() return; } - - UNavigationSystemV1* navSys = UNavigationSystemV1::GetCurrent(GetWorld()); - if (!navSys) + const UNavigationSystemV1* NavSys = UNavigationSystemV1::GetCurrent(GetWorld()); + if (!NavSys) { VH_ERROR("Navigation System is not valid\n"); return; } - auto path = navSys->FindPathToActorSynchronously(GetWorld(), AVirtualHumanPawn->GetActorLocation(), + auto Path = NavSys->FindPathToActorSynchronously(GetWorld(), AVirtualHumanPawn->GetActorLocation(), CurrentWaypoint); - FAIMoveRequest request = FAIMoveRequest(); - request.SetGoalActor(CurrentWaypoint); - request.SetUsePathfinding(true); - request.SetAllowPartialPath(true); + FAIMoveRequest Request = FAIMoveRequest(); + Request.SetGoalActor(CurrentWaypoint); + Request.SetUsePathfinding(true); + Request.SetAllowPartialPath(true); Cast<UCharacterMovementComponent>(AVirtualHumanPawn->GetMovementComponent())->MaxWalkSpeed = CurrentWaypoint-> GetWalkingSpeed(); - CurrentRequestID = VirtualHumanAIController->RequestMove(request, path->GetPath()); + CurrentRequestID = VirtualHumanAIController->RequestMove(Request, Path->GetPath()); - for (TPair<AVirtualHuman*, float>& child : ChildVH) + for (TPair<AVirtualHuman*, float>& Child : ChildVH) { - if (!child.Key) + if (!Child.Key) { VH_ERROR("Virtual Human does not have a valid child\n"); return; } - UVHMovement* childMovement = child.Key->FindComponentByClass<UVHMovement>(); - if (!childMovement->bLineMovement) + UVHMovement* ChildMovement = Child.Key->FindComponentByClass<UVHMovement>(); + if (!ChildMovement->bLineMovement) { - childMovement->CurrentWaypointIterator = CurrentWaypointIterator; - childMovement->bChildMovement = false; // stop AIController from executing ChildMoveToWaypoint() - childMovement->IntermediatePointIterator = 0; - childMovement->ChildMoveToWaypoint(); - childMovement->bChildMovement = true; // allows AIController to execute ChildMoveToWaypoint() + ChildMovement->CurrentWaypointIterator = CurrentWaypointIterator; + ChildMovement->bChildMovement = false; // stop AIController from executing ChildMoveToWaypoint() + ChildMovement->IntermediatePointIterator = 0; + ChildMovement->ChildMoveToWaypoint(); + ChildMovement->bChildMovement = true; // allows AIController to execute ChildMoveToWaypoint() } } CurrentWaypointIterator++; @@ -300,11 +291,10 @@ void UVHMovement::MoveToWaypoint() } } - /* * Moves VH to desired location. Supply with FVector as destination. */ -void UVHMovement::MoveToWaypoint(FVector TargetLocation, float Speed) +void UVHMovement::MoveToWaypoint(const FVector& TargetLocation, const float Speed) { if (!AVirtualHumanPawn) { @@ -341,7 +331,6 @@ void UVHMovement::ChildMoveToWaypoint() return; } - if (CurrentWaypointIterator < Waypoints.Num()) { CurrentWaypoint = Waypoints[CurrentWaypointIterator]; //ICurrentWaypoint is only incremented by the parent @@ -351,7 +340,6 @@ void UVHMovement::ChildMoveToWaypoint() return; } - UNavigationSystemV1* navSys = UNavigationSystemV1::GetCurrent(GetWorld()); if (!navSys) { @@ -405,7 +393,7 @@ void UVHMovement::ChildMoveToWaypoint() } if (IntermediatePointIterator == intermediatePoints.Num()) { - // so AIControlle does not call childMoveToWaypoint again + // So AIController does not call childMoveToWaypoint again // will be set to true by parent if he has reached the current waypoint bChildMovement = false; } @@ -423,7 +411,7 @@ TArray<AWaypoint*> UVHMovement::ShuffleArray(TArray<AWaypoint*> Array) const Array.Swap(Index, i); } } - + return Array; } @@ -473,7 +461,7 @@ void UVHMovement::WaypointMovement() }); - // then sort by distance from one WP to the next one + // then sort by distance from one WP to the next one for (int i = 1; i < Waypoints.Num(); ++i) { for (int j = 2; j < Waypoints.Num(); ++j) @@ -799,7 +787,7 @@ TArray<FVector> UVHMovement::CreateIntermediatePoints(AWaypoint* lastWP, AWaypoi } -float UVHMovement::CalculateChildSpeed(AWaypoint* nextWaypoint, TArray<FVector> points) +float UVHMovement::CalculateChildSpeed(AWaypoint* nextWaypoint, TArray<FVector> points) const { if (points.Num() < 2) { @@ -833,7 +821,7 @@ float UVHMovement::CalculateChildSpeed(AWaypoint* nextWaypoint, TArray<FVector> return childSpeed; } -float UVHMovement::CalculateChildSpeed(AWaypoint* nextWaypoint, FVector childPoint) +float UVHMovement::CalculateChildSpeed(AWaypoint* nextWaypoint, FVector childPoint) const { UNavigationPath* parentPath = UNavigationSystemV1::FindPathToLocationSynchronously( GetWorld(), ParentVH->GetActorLocation(), nextWaypoint->GetActorLocation()); @@ -856,11 +844,11 @@ float UVHMovement::CalculateChildSpeed(AWaypoint* nextWaypoint, FVector childPoi return childSpeed; } -FHitResult UVHMovement::IKFootTrace(FName Socket, float TraceDistance) +FHitResult UVHMovement::IKFootTrace(const FName& Socket) const { - USkeletalMeshComponent* mesh = AVirtualHumanPtr->GetBodyMesh(); - FVector SocketLocation = mesh->GetSocketLocation(Socket); - FVector ActorLocation = AVirtualHumanPtr->GetActorLocation(); + const USkeletalMeshComponent* Mesh = AVirtualHumanPtr->GetBodyMesh(); + const FVector SocketLocation = Mesh->GetSocketLocation(Socket); + const FVector ActorLocation = AVirtualHumanPtr->GetActorLocation(); // line trace start location FVector LineTraceStart = SocketLocation; @@ -874,8 +862,7 @@ FHitResult UVHMovement::IKFootTrace(FName Socket, float TraceDistance) FCollisionQueryParams TraceParams; TraceParams.bTraceComplex = true; TraceParams.AddIgnoredActor(AVirtualHumanPtr); - bool hitFound = GetWorld()-> + GetWorld()-> LineTraceSingleByChannel(Hit, LineTraceStart, LineTraceEnd, ECC_Visibility, TraceParams); - // DrawDebugLine(GetWorld(), LineTraceStart, LineTraceEnd, FColor::Cyan, false, 0.1f); return Hit; } diff --git a/Source/CharacterPlugin/Public/VHMovement.h b/Source/CharacterPlugin/Public/VHMovement.h index 24962de6..400574e6 100644 --- a/Source/CharacterPlugin/Public/VHMovement.h +++ b/Source/CharacterPlugin/Public/VHMovement.h @@ -39,7 +39,7 @@ public: virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; - void MoveToWaypoint(FVector TargetLocation, float Speed); + void MoveToWaypoint(const FVector& TargetLocation, const float Speed); void MoveToWaypoint(); void ChildMoveToWaypoint(); @@ -147,12 +147,13 @@ private: TArray<AWaypoint*> ShuffleArray(TArray<AWaypoint*> Array) const; - FHitResult IKFootTrace(FName Socket, float TraceDistance); + inline void CalculateIK(const float DeltaTime); + FHitResult IKFootTrace(const FName& Socket) const; void CreateChildPoints(); TArray<FVector> CreateIntermediatePoints(AWaypoint* last, AWaypoint* current, AWaypoint* next); - float CalculateChildSpeed(AWaypoint* nextWaypoint, TArray<FVector> childPoints); - float CalculateChildSpeed(AWaypoint* nextWaypoint, FVector childPoint); + float CalculateChildSpeed(AWaypoint* nextWaypoint, TArray<FVector> childPoints) const; + float CalculateChildSpeed(AWaypoint* nextWaypoint, FVector childPoint) const; int IntermediatePointIterator; -- GitLab