A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
VRAIController.cpp
Go to the documentation of this file.
1// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
2
3#include "VRAIController.h"
4#include "NetworkingDistanceConstants.h" // Needed for the LinOfSightTo function override to work
5#include "Navigation/CrowdFollowingComponent.h"
6//#include "Runtime/Engine/Private/EnginePrivate.h"
7
8
10{
11 if (const AVRBaseCharacter * VRChar = Cast<const AVRBaseCharacter>(Actor))
12 {
13 return VRChar->GetVRLocation_Inline();
14 }
15 else
16 return Super::GetFocalPointOnActor(Actor); // Actor != nullptr ? Actor->GetActorLocation() : FAISystem::InvalidLocation;
17}
18
19bool AVRAIController::LineOfSightTo(const AActor* Other, FVector ViewPoint, bool bAlternateChecks) const
20{
21 if (Other == nullptr)
22 {
23 return false;
24 }
25
26 if (ViewPoint.IsZero())
27 {
28 FRotator ViewRotation;
29 GetActorEyesViewPoint(ViewPoint, ViewRotation);
30
31 // if we still don't have a view point we simply fail
32 if (ViewPoint.IsZero())
33 {
34 return false;
35 }
36 }
37
38 static FName NAME_LineOfSight = FName(TEXT("LineOfSight"));
39 FVector TargetLocation = Other->GetTargetLocation(GetPawn());
40
41 FCollisionQueryParams CollisionParams(NAME_LineOfSight, true, this->GetPawn());
42 CollisionParams.AddIgnoredActor(Other);
43
44 bool bHit = GetWorld()->LineTraceTestByChannel(ViewPoint, TargetLocation, ECC_Visibility, CollisionParams);
45 if (!bHit)
46 {
47 return true;
48 }
49
50 // if other isn't using a cylinder for collision and isn't a Pawn (which already requires an accurate cylinder for AI)
51 // then don't go any further as it likely will not be tracing to the correct location
52 const APawn * OtherPawn = Cast<const APawn>(Other);
53 if (!OtherPawn && Cast<UCapsuleComponent>(Other->GetRootComponent()) == NULL)
54 {
55 return false;
56 }
57
58 // Changed this up to support my VR Characters
59 const AVRBaseCharacter * VRChar = Cast<const AVRBaseCharacter>(Other);
60 const FVector OtherActorLocation = VRChar != nullptr ? VRChar->GetVRLocation_Inline() : Other->GetActorLocation();
61
62 const float DistSq = (OtherActorLocation - ViewPoint).SizeSquared();
63 if (DistSq > FARSIGHTTHRESHOLDSQUARED)
64 {
65 return false;
66 }
67
68 if (!OtherPawn && (DistSq > NEARSIGHTTHRESHOLDSQUARED))
69 {
70 return false;
71 }
72
73 float OtherRadius, OtherHeight;
74 Other->GetSimpleCollisionCylinder(OtherRadius, OtherHeight);
75
76 if (!bAlternateChecks || !bLOSflag)
77 {
78 //try viewpoint to head
79 bHit = GetWorld()->LineTraceTestByChannel(ViewPoint, OtherActorLocation + FVector(0.f, 0.f, OtherHeight), ECC_Visibility, CollisionParams);
80 if (!bHit)
81 {
82 return true;
83 }
84 }
85
86 if (!bSkipExtraLOSChecks && (!bAlternateChecks || bLOSflag))
87 {
88 // only check sides if width of other is significant compared to distance
89 if (OtherRadius * OtherRadius / (OtherActorLocation - ViewPoint).SizeSquared() < 0.0001f)
90 {
91 return false;
92 }
93 //try checking sides - look at dist to four side points, and cull furthest and closest
94 FVector Points[4];
95 Points[0] = OtherActorLocation - FVector(OtherRadius, -1 * OtherRadius, 0);
96 Points[1] = OtherActorLocation + FVector(OtherRadius, OtherRadius, 0);
97 Points[2] = OtherActorLocation - FVector(OtherRadius, OtherRadius, 0);
98 Points[3] = OtherActorLocation + FVector(OtherRadius, -1 * OtherRadius, 0);
99 int32 IndexMin = 0;
100 int32 IndexMax = 0;
101 float CurrentMax = (Points[0] - ViewPoint).SizeSquared();
102 float CurrentMin = CurrentMax;
103 for (int32 PointIndex = 1; PointIndex < 4; PointIndex++)
104 {
105 const float NextSize = (Points[PointIndex] - ViewPoint).SizeSquared();
106 if (NextSize > CurrentMin)
107 {
108 CurrentMin = NextSize;
109 IndexMax = PointIndex;
110 }
111 else if (NextSize < CurrentMax)
112 {
113 CurrentMax = NextSize;
114 IndexMin = PointIndex;
115 }
116 }
117
118 for (int32 PointIndex = 0; PointIndex < 4; PointIndex++)
119 {
120 if ((PointIndex != IndexMin) && (PointIndex != IndexMax))
121 {
122 bHit = GetWorld()->LineTraceTestByChannel(ViewPoint, Points[PointIndex], ECC_Visibility, CollisionParams);
123 if (!bHit)
124 {
125 return true;
126 }
127 }
128 }
129 }
130 return false;
131}
132
133AVRDetourCrowdAIController::AVRDetourCrowdAIController(const FObjectInitializer& ObjectInitializer)
134 : Super(ObjectInitializer.SetDefaultSubobjectClass<UCrowdFollowingComponent>(TEXT("PathFollowingComponent")))
135{
136
137}
virtual bool LineOfSightTo(const AActor *Other, FVector ViewPoint=FVector(ForceInit), bool bAlternateChecks=false) const override
virtual FVector GetFocalPointOnActor(const AActor *Actor) const override
FVector GetVRLocation_Inline() const
AVRDetourCrowdAIController(const FObjectInitializer &ObjectInitializer=FObjectInitializer::Get())