Space Plunder
Loading...
Searching...
No Matches
UBTTask_Rotate Class Reference

A task node that rotates the AI character by a specified number of degrees. More...

#include <BTTask_Rotate.h>

Inheritance diagram for UBTTask_Rotate:

Classes

struct  FBTTaskRotateMemory
 

Public Member Functions

 UBTTask_Rotate ()
 

Public Attributes

float RotationDegrees = 90.0f
 
float ForwardDistance = 500.0f
 
bool bInstantTurn = true
 
float InterpolationDuration = 2.0f
 
float RandomDeviation = 0.1f
 
bool bLookAround = false
 
int32 TimesToLookAround = 1
 
bool bDebuggingMode = false
 
bool bShowCurrentFocus = true
 
bool bShowText = true
 
float PointSize = 20.0f
 
FColor TargetFocalPointColor = FColor::Red
 
float TargetFocalPointDuration = 2.0f
 
FColor CurrentFocalPointColor = FColor::Yellow
 

Protected Member Functions

virtual EBTNodeResult::Type ExecuteTask (UBehaviorTreeComponent &OwnerComp, uint8 *NodeMemory) override
 
virtual EBTNodeResult::Type AbortTask (UBehaviorTreeComponent &OwnerComp, uint8 *NodeMemory) override
 
virtual void TickTask (UBehaviorTreeComponent &OwnerComp, uint8 *NodeMemory, float DeltaSeconds) override
 
virtual uint16 GetInstanceMemorySize () const override
 
void InstantTurn (const FBTTaskRotateMemory *RotateMemory, AAIController *AIController) const
 
void Turn (FBTTaskRotateMemory *RotateMemory, AAIController *AIController, const bool bForward=true)
 
virtual FString GetStaticDescription () const override
 

Static Protected Member Functions

static void ResetNumbers (FBTTaskRotateMemory *RotateMemory)
 

Detailed Description

A task node that rotates the AI character by a specified number of degrees.

This task node rotates the AI character by a specified number of degrees. The rotation can be instant or interpolated over a specified duration. The rotation can be performed either to the left or to the right based on the sign of the RotationDegrees property.

The task node also provides debugging options to visualize the current focal point of rotation and the target focal point.

Note
This class is part of the AIToolkit module.
See also
UBTTaskNode

Constructor & Destructor Documentation

◆ UBTTask_Rotate()

UBTTask_Rotate::UBTTask_Rotate ( )
16{
17 NodeName = TEXT("Rotate");
18 INIT_TASK_NODE_NOTIFY_FLAGS();
19#if WITH_EDITOR
20 bDebuggingMode = true;
21#endif // WITH_EDITOR
22}
bool bDebuggingMode
Definition BTTask_Rotate.h:55

Member Function Documentation

◆ AbortTask()

EBTNodeResult::Type UBTTask_Rotate::AbortTask ( UBehaviorTreeComponent & OwnerComp,
uint8 * NodeMemory )
overrideprotectedvirtual
122{
123 return EBTNodeResult::Aborted;
124}

◆ ExecuteTask()

EBTNodeResult::Type UBTTask_Rotate::ExecuteTask ( UBehaviorTreeComponent & OwnerComp,
uint8 * NodeMemory )
overrideprotectedvirtual
25{
26 TRACE_CPUPROFILER_EVENT_SCOPE(UBTTask_Rotate::ExecuteTask);
27 SCOPE_CYCLE_COUNTER(STATGROUP_AIToolKit_Behaviors);
28 SCOPE_CYCLE_COUNTER(STATGROUP_AIToolKit_Tasks);
29
30 FBTTaskRotateMemory* RotateMemory = reinterpret_cast<FBTTaskRotateMemory*>(NodeMemory);
31 if(OwnerComp.GetAIOwner() == nullptr || OwnerComp.GetAIOwner()->GetPawn() == nullptr || RotateMemory == nullptr){return EBTNodeResult::Failed;}
32 RotateMemory->LookAroundCount = 1;
33 ResetNumbers(RotateMemory);
34
35 //- Get the Characters View //
36 FVector ActorsEyeLocation;
37 FRotator ActorsEyeRotation;
38 OwnerComp.GetAIOwner()->GetPawn()->GetActorEyesViewPoint(ActorsEyeLocation, ActorsEyeRotation);
39
40 //- Save Vars for tick //
41 RotateMemory->StartRotation = ActorsEyeRotation;
42 RotateMemory->OriginalRotation = RotateMemory->StartRotation;
43 RotateMemory->StartLocation = ActorsEyeLocation;
44 RotateMemory->OriginalLocation = RotateMemory->StartLocation;
45
46 //- Set Random Number //
47 RotateMemory->RandomDeviationMultiplier = FMath::RandRange(0.0f, RandomDeviation);
48
49 //- This is the characters forward view + the rotation in Degrees + 90 = right turn //
50 const FRotator TargetRotation = FRotator(ActorsEyeRotation.Pitch, (ActorsEyeRotation.Yaw + RotationDegrees), ActorsEyeRotation.Roll);
51 const FRotator TargetOppositeRotation = FRotator(ActorsEyeRotation.Pitch, (ActorsEyeRotation.Yaw - RotationDegrees), ActorsEyeRotation.Roll);
52
53
54 // //- Forward Vector from the Character Rotation, x Distance + Location //
55 RotateMemory->FocalPoint = (ActorsEyeLocation + (ActorsEyeRotation.Vector() * ForwardDistance));
56
57 //- Location + Forward Vector Toward target Rotation, x Distance forward //
58 RotateMemory->TargetLocation = (ActorsEyeLocation + (TargetRotation.Vector() * ForwardDistance));
59 RotateMemory->TargetOppositeLocation = (ActorsEyeLocation + (TargetOppositeRotation.Vector() * ForwardDistance));
60
61 if(bInstantTurn)
62 {
63 if(AAIController* AIController = OwnerComp.GetAIOwner())
64 {
65 InstantTurn(RotateMemory, AIController);
66 }
67 return EBTNodeResult::Succeeded;
68 }
69 return EBTNodeResult::InProgress;
70}
float ForwardDistance
Definition BTTask_Rotate.h:39
float RotationDegrees
Definition BTTask_Rotate.h:35
static void ResetNumbers(FBTTaskRotateMemory *RotateMemory)
Definition BTTask_Rotate.cpp:113
bool bInstantTurn
Definition BTTask_Rotate.h:41
void InstantTurn(const FBTTaskRotateMemory *RotateMemory, AAIController *AIController) const
Definition BTTask_Rotate.cpp:72
float RandomDeviation
Definition BTTask_Rotate.h:46
virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent &OwnerComp, uint8 *NodeMemory) override
Definition BTTask_Rotate.cpp:24

◆ GetInstanceMemorySize()

uint16 UBTTask_Rotate::GetInstanceMemorySize ( ) const
overrideprotectedvirtual
202{
203 return sizeof(FBTTaskRotateMemory);
204}

◆ GetStaticDescription()

FString UBTTask_Rotate::GetStaticDescription ( ) const
overrideprotectedvirtual
90{
91 FString Description;
93 {
94 Description += FString::Printf(TEXT("\n DEBUG ACTIVE"));
95 }
96 Description += FString::Printf(TEXT("\nDegrees: %i"), FMath::RoundToInt(RotationDegrees));
97 Description += FString::Printf(TEXT("\nDuration: %i + %i"), FMath::RoundToInt(InterpolationDuration), FMath::RoundToInt(RandomDeviation));
98 if (bLookAround)
99 {
100 Description += FString::Printf(TEXT("\nLook Around: %s Times"), *FString::FromInt(TimesToLookAround));
101 }
102 Description += FString::Printf(TEXT("\nInstant Turn: %s"), bInstantTurn ? TEXT("True") : TEXT("False"));
103 return Description;
104}
bool bLookAround
Definition BTTask_Rotate.h:49
float InterpolationDuration
Definition BTTask_Rotate.h:44
int32 TimesToLookAround
Definition BTTask_Rotate.h:51

◆ InstantTurn()

void UBTTask_Rotate::InstantTurn ( const FBTTaskRotateMemory * RotateMemory,
AAIController * AIController ) const
protected
73{
74 TRACE_CPUPROFILER_EVENT_SCOPE(UBTTask_Rotate::InstantTurn);
75 AIController->SetFocalPoint(RotateMemory->TargetLocation);
77 {
78 DrawDebugPoint(GetWorld(), RotateMemory->TargetLocation, PointSize, TargetFocalPointColor, false, 2.0f);
79 if(bShowText)
80 {
81 const FString ActorName = AIController->GetPawn()->GetName();
82 const FString PrintMessage = (ActorName + " Target Focal Point");
83 DrawDebugString(GetWorld(), RotateMemory->TargetLocation, PrintMessage, nullptr, FColor::White, TargetFocalPointDuration);
84 }
85 }
86}
FColor TargetFocalPointColor
Definition BTTask_Rotate.h:63
bool bShowText
Definition BTTask_Rotate.h:59
float TargetFocalPointDuration
Definition BTTask_Rotate.h:65
float PointSize
Definition BTTask_Rotate.h:61

◆ ResetNumbers()

void UBTTask_Rotate::ResetNumbers ( FBTTaskRotateMemory * RotateMemory)
staticprotected
114{
115 //- Reset all Tick floats //
116 RotateMemory->TimeElapsed = 0.0f;
117 RotateMemory->Alpha = 0.0f;
118}

◆ TickTask()

void UBTTask_Rotate::TickTask ( UBehaviorTreeComponent & OwnerComp,
uint8 * NodeMemory,
float DeltaSeconds )
overrideprotectedvirtual
127{
128 TRACE_CPUPROFILER_EVENT_SCOPE(UBTTask_Rotate::TickTask);
129 SCOPE_CYCLE_COUNTER(STATGROUP_AIToolKit_Behaviors);
130 SCOPE_CYCLE_COUNTER(STATGROUP_AIToolKit_Tasks);
131
132 Super::TickTask(OwnerComp, NodeMemory, DeltaSeconds);
133 FBTTaskRotateMemory* RotateMemory = reinterpret_cast<FBTTaskRotateMemory*>(NodeMemory);
134 if(RotateMemory == nullptr){return;}
135 const bool bForward = RotateMemory->LookAroundCount % 2 == 1;
136 RotateMemory->TimeElapsed += DeltaSeconds;
137
138 if(RotateMemory->TimeElapsed < ((InterpolationDuration) * RotateMemory->LookAroundCount))
139 {
140 Turn(RotateMemory, OwnerComp.GetAIOwner(), bForward);
141 }
142 else if(bLookAround && RotateMemory->LookAroundCount < TimesToLookAround)
143 {
144 ResetNumbers(RotateMemory);
145 RotateMemory->LookAroundCount++;
146 // if(RotateMemory->TimeElapsed < (InterpolationDuration * Multiplier))
147 // {
148 //
149 // }
150 // RotateMemory->LookAroundCount++;
151 // ResetNumbers(RotateMemory);
152 // RotateMemory->TargetLocation = RotateMemory->TargetOppositeLocation;
153 // OwnerComp.GetAIOwner()->GetPawn()->GetActorEyesViewPoint(RotateMemory->StartLocation, RotateMemory->StartRotation);
154 }
155 else if(bLookAround && RotateMemory->LookAroundCount >= TimesToLookAround)
156 {
157 if(RotateMemory->TargetLocation == (RotateMemory->OriginalLocation + (RotateMemory->OriginalRotation.Vector() * ForwardDistance)))
158 {
159 FinishLatentTask(OwnerComp, EBTNodeResult::Succeeded);
160 }
161 ResetNumbers(RotateMemory);
162 RotateMemory->TargetLocation = (RotateMemory->OriginalLocation + (RotateMemory->OriginalRotation.Vector() * ForwardDistance));
163 }
164 else
165 {
166 FinishLatentTask(OwnerComp, EBTNodeResult::Succeeded);
167 }
168}
virtual void TickTask(UBehaviorTreeComponent &OwnerComp, uint8 *NodeMemory, float DeltaSeconds) override
Definition BTTask_Rotate.cpp:126
void Turn(FBTTaskRotateMemory *RotateMemory, AAIController *AIController, const bool bForward=true)
Definition BTTask_Rotate.cpp:170

◆ Turn()

void UBTTask_Rotate::Turn ( FBTTaskRotateMemory * RotateMemory,
AAIController * AIController,
const bool bForward = true )
protected
171{
172 const float TotalDuration = (InterpolationDuration + RotateMemory->RandomDeviationMultiplier);
173 if(bForward)
174 {
175 RotateMemory->Alpha = FMath::Clamp((RotateMemory->TimeElapsed / (TotalDuration)), 0.0f, 1.0f);
176 }
177 else
178 {
179 RotateMemory->Alpha = FMath::Clamp(1.0f - (RotateMemory->TimeElapsed - InterpolationDuration - RotateMemory->RandomDeviationMultiplier) / TotalDuration, 0.0f, 1.0f);
180 }
181
182 const FVector CurrentFocalPoint = (RotateMemory->StartLocation + (RotateMemory->StartRotation.Vector() * ForwardDistance));
183 const FVector LerpVector = UKismetMathLibrary::VLerp(CurrentFocalPoint, RotateMemory->TargetLocation, RotateMemory->Alpha);
184
185 if(AIController != nullptr)
186 {
187 AIController->SetFocalPoint(LerpVector);
189 {
190 DrawDebugPoint(GetWorld(), LerpVector, PointSize, CurrentFocalPointColor, false, 0.1f);
191 if(bShowText && AIController->GetPawn() != nullptr)
192 {
193 const FString ActorName = AIController->GetPawn()->GetName();
194 const FString PrintMessage = (ActorName + " Focal Point");
195 DrawDebugString(GetWorld(), LerpVector, PrintMessage, AIController->GetPawn(), FColor::White, 0.1f);
196 }
197 }
198 }
199}
bool bShowCurrentFocus
Definition BTTask_Rotate.h:57
FColor CurrentFocalPointColor
Definition BTTask_Rotate.h:67

Member Data Documentation

◆ bDebuggingMode

bool UBTTask_Rotate::bDebuggingMode = false

◆ bInstantTurn

bool UBTTask_Rotate::bInstantTurn = true

◆ bLookAround

bool UBTTask_Rotate::bLookAround = false

◆ bShowCurrentFocus

bool UBTTask_Rotate::bShowCurrentFocus = true

◆ bShowText

bool UBTTask_Rotate::bShowText = true

◆ CurrentFocalPointColor

FColor UBTTask_Rotate::CurrentFocalPointColor = FColor::Yellow

◆ ForwardDistance

float UBTTask_Rotate::ForwardDistance = 500.0f

◆ InterpolationDuration

float UBTTask_Rotate::InterpolationDuration = 2.0f

◆ PointSize

float UBTTask_Rotate::PointSize = 20.0f

◆ RandomDeviation

float UBTTask_Rotate::RandomDeviation = 0.1f

◆ RotationDegrees

float UBTTask_Rotate::RotationDegrees = 90.0f

◆ TargetFocalPointColor

FColor UBTTask_Rotate::TargetFocalPointColor = FColor::Red

◆ TargetFocalPointDuration

float UBTTask_Rotate::TargetFocalPointDuration = 2.0f

◆ TimesToLookAround

int32 UBTTask_Rotate::TimesToLookAround = 1

The documentation for this class was generated from the following files: