Skip to content

Commit

Permalink
Fix for #8
Browse files Browse the repository at this point in the history
Use Pawn position, not owner actor, for pathfinding.
  • Loading branch information
midgen committed Jan 12, 2019
1 parent b67c9eb commit 78f83d0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Source/UESVON/Private/AITask_SVONMoveTo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ void UAITask_SVONMoveTo::RequestPathSynchronous()
UE_LOG(UESVON, Error, TEXT("SVONMoveTo: Requesting Synchronous pathfinding!"));
#endif

if (myNavComponent->FindPathImmediate(GetOwnerActor()->GetActorLocation(), MoveRequest.IsMoveToActorRequest() ? MoveRequest.GetGoalActor()->GetActorLocation() : MoveRequest.GetGoalLocation(), &mySVONPath))
if (myNavComponent->FindPathImmediate(myNavComponent->GetPawnPosition(), MoveRequest.IsMoveToActorRequest() ? MoveRequest.GetGoalActor()->GetActorLocation() : MoveRequest.GetGoalLocation(), &mySVONPath))
{
myResult.Code = ESVONPathfindingRequestResult::Success;
}
Expand All @@ -354,7 +354,7 @@ void UAITask_SVONMoveTo::RequestPathAsync()
myAsyncTaskComplete = false;

// Request the async path
svonNavComponent->FindPathAsync(GetOwnerActor()->GetActorLocation(), MoveRequest.IsMoveToActorRequest() ? MoveRequest.GetGoalActor()->GetActorLocation() : MoveRequest.GetGoalLocation(), myAsyncTaskComplete, &mySVONPath);
svonNavComponent->FindPathAsync(myNavComponent->GetPawnPosition(), MoveRequest.IsMoveToActorRequest() ? MoveRequest.GetGoalActor()->GetActorLocation() : MoveRequest.GetGoalLocation(), myAsyncTaskComplete, &mySVONPath);

myResult.Code = ESVONPathfindingRequestResult::Deferred;
}
Expand Down
34 changes: 24 additions & 10 deletions Source/UESVON/Private/SVONNavigationComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ USVONNavigationComponent::USVONNavigationComponent()

mySVONPath = MakeShareable<FSVONNavigationPath>(new FSVONNavigationPath());

// ...
}


Expand All @@ -36,7 +35,7 @@ void USVONNavigationComponent::BeginPlay()
/** Are we inside a valid nav volume ? */
bool USVONNavigationComponent::HasNavVolume()
{
return myCurrentNavVolume && GetOwner() && myCurrentNavVolume->EncompassesPoint(GetOwner()->GetActorLocation()) && myCurrentNavVolume->GetMyNumLayers() > 0;
return myCurrentNavVolume && GetOwner() && myCurrentNavVolume->EncompassesPoint(GetPawnPosition()) && myCurrentNavVolume->GetMyNumLayers() > 0;
}

bool USVONNavigationComponent::FindVolume()
Expand All @@ -48,7 +47,7 @@ bool USVONNavigationComponent::FindVolume()
for (AActor* actor : navVolumes)
{
ASVONVolume* volume = Cast<ASVONVolume>(actor);
if (volume && volume->EncompassesPoint(GetOwner()->GetActorLocation()))
if (volume && volume->EncompassesPoint(GetPawnPosition()))
{
myCurrentNavVolume = volume;
return true;
Expand All @@ -69,7 +68,7 @@ void USVONNavigationComponent::TickComponent(float DeltaTime, ELevelTick TickTyp
}
else if (myCurrentNavVolume->IsReadyForNavigation() && !myIsBusy)
{
FVector location = GetOwner()->GetActorLocation();
FVector location = GetPawnPosition();
if (DebugPrintMortonCodes)
{
DebugLocalPosition(location);
Expand All @@ -80,7 +79,6 @@ void USVONNavigationComponent::TickComponent(float DeltaTime, ELevelTick TickTyp
int q;
if (myJobQueue.Dequeue(q))
{
//GetWorld()->PersistentLineBatcher->Flush();
if (q > 0)
{
myPointDebugIndex = 0;
Expand Down Expand Up @@ -138,7 +136,7 @@ SVONLink USVONNavigationComponent::GetNavPosition(FVector& aPosition)

myLastLocation = navLink;

FVector targetPos = GetOwner()->GetActorLocation() + (GetOwner()->GetActorForwardVector() * 10000.f);
//FVector targetPos = GetPawnPosition() + (GetOwner()->GetActorForwardVector() * 10000.f);

if (DebugPrintCurrentPosition)
{
Expand All @@ -147,8 +145,8 @@ SVONLink USVONNavigationComponent::GetNavPosition(FVector& aPosition)

bool isValid = myCurrentNavVolume->GetLinkPosition(navLink, currentNodePosition);

DrawDebugLine(GetWorld(), GetOwner()->GetActorLocation(), currentNodePosition, isValid ? FColor::Green : FColor::Red, false, -1.f, 0, 10.f);
DrawDebugString(GetWorld(), GetOwner()->GetActorLocation() + FVector(0.f, 0.f, -50.f), navLink.ToString(), NULL, FColor::Yellow, 0.01f);
DrawDebugLine(GetWorld(), GetPawnPosition(), currentNodePosition, isValid ? FColor::Green : FColor::Red, false, -1.f, 0, 10.f);
DrawDebugString(GetWorld(), GetPawnPosition() + FVector(0.f, 0.f, -50.f), navLink.ToString(), NULL, FColor::Yellow, 0.01f);
}

}
Expand Down Expand Up @@ -295,14 +293,30 @@ void USVONNavigationComponent::DebugLocalPosition(FVector& aPosition)
for (int i = 0; i < myCurrentNavVolume->GetMyNumLayers() - 1; i++)
{
FIntVector pos;
SVONMediator::GetVolumeXYZ(GetOwner()->GetActorLocation(), *myCurrentNavVolume, i, pos);
SVONMediator::GetVolumeXYZ(GetPawnPosition(), *myCurrentNavVolume, i, pos);
uint_fast64_t code = morton3D_64_encode(pos.X, pos.Y, pos.Z);
FString codeString = FString::FromInt(code);
DrawDebugString(GetWorld(), GetOwner()->GetActorLocation() + FVector(0.f, 0.f, i*50.0f), pos.ToString() + " - " + codeString, NULL, FColor::White, 0.01f);
DrawDebugString(GetWorld(), GetPawnPosition() + FVector(0.f, 0.f, i*50.0f), pos.ToString() + " - " + codeString, NULL, FColor::White, 0.01f);
}


}

}


FVector USVONNavigationComponent::GetPawnPosition()
{
FVector result;

AController * controller = Cast<AController>(GetOwner());

if (controller)
{
if (APawn* pawn = controller->GetPawn())
result = pawn->GetActorLocation();
}

return result;

}
4 changes: 4 additions & 0 deletions Source/UESVON/Public/SVONNavigationComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class UESVON_API USVONNavigationComponent : public UActorComponent
// Print current layer/morton code information
void DebugLocalPosition(FVector& aPosition);



FSVONNavPathSharedPtr mySVONPath;

SVONLink myLastLocation;
Expand All @@ -72,6 +74,8 @@ class UESVON_API USVONNavigationComponent : public UActorComponent
int myPointDebugIndex;

public:

virtual FVector GetPawnPosition();

virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

Expand Down

0 comments on commit 78f83d0

Please sign in to comment.