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

#include <ALSPlayerCameraManager.h>

Inheritance diagram for AALSPlayerCameraManager:

Public Member Functions

 AALSPlayerCameraManager ()
 
void OnPossess (AALSBaseCharacter *NewCharacter)
 
float GetCameraBehaviorParam (FName CurveName) const
 
void DrawDebugTargets (FVector PivotTargetLocation)
 

Public Attributes

TObjectPtr< AALSBaseCharacterControlledCharacter = nullptr
 
TObjectPtr< USkeletalMeshComponent > CameraBehavior = nullptr
 

Protected Member Functions

virtual void UpdateViewTargetInternal (FTViewTarget &OutVT, float DeltaTime) override
 
bool CustomCameraBehavior (float DeltaTime, FVector &Location, FRotator &Rotation, float &FOV)
 

Static Protected Member Functions

static FVector CalculateAxisIndependentLag (FVector CurrentLocation, FVector TargetLocation, FRotator CameraRotation, FVector LagSpeeds, float DeltaTime)
 

Protected Attributes

FVector RootLocation
 
FTransform SmoothedPivotTarget
 
FVector PivotLocation
 
FVector TargetCameraLocation
 
FRotator TargetCameraRotation
 
FRotator DebugViewRotation
 
FVector DebugViewOffset
 

Private Attributes

TObjectPtr< UALSDebugComponentALSDebugComponent = nullptr
 

Detailed Description

Player camera manager class

Constructor & Destructor Documentation

◆ AALSPlayerCameraManager()

AALSPlayerCameraManager::AALSPlayerCameraManager ( )
36{
37 CameraBehavior = CreateDefaultSubobject<USkeletalMeshComponent>(NAME_CameraBehavior);
38 CameraBehavior->SetupAttachment(GetRootComponent());
39 CameraBehavior->bHiddenInGame = true;
40}
const FName NAME_CameraBehavior(TEXT("CameraBehavior"))
TObjectPtr< USkeletalMeshComponent > CameraBehavior
Definition ALSPlayerCameraManager.h:50

Member Function Documentation

◆ CalculateAxisIndependentLag()

FVector AALSPlayerCameraManager::CalculateAxisIndependentLag ( FVector CurrentLocation,
FVector TargetLocation,
FRotator CameraRotation,
FVector LagSpeeds,
float DeltaTime )
staticprotected
123{
124 TRACE_CPUPROFILER_EVENT_SCOPE(AALSPlayerCameraManager::CalculateAxisIndependentLag);
125 SCOPE_CYCLE_COUNTER(STATGROUP_ALS_Camera_Manager);
126
127 CameraRotation.Roll = 0.0f;
128 CameraRotation.Pitch = 0.0f;
129 const FVector UnrotatedCurLoc = CameraRotation.UnrotateVector(CurrentLocation);
130 const FVector UnrotatedTargetLoc = CameraRotation.UnrotateVector(TargetLocation);
131
132 const FVector ResultVector(
133 FMath::FInterpTo(UnrotatedCurLoc.X, UnrotatedTargetLoc.X, DeltaTime, LagSpeeds.X),
134 FMath::FInterpTo(UnrotatedCurLoc.Y, UnrotatedTargetLoc.Y, DeltaTime, LagSpeeds.Y),
135 FMath::FInterpTo(UnrotatedCurLoc.Z, UnrotatedTargetLoc.Z, DeltaTime, LagSpeeds.Z));
136
137 return CameraRotation.RotateVector(ResultVector);
138}
static FVector CalculateAxisIndependentLag(FVector CurrentLocation, FVector TargetLocation, FRotator CameraRotation, FVector LagSpeeds, float DeltaTime)
Definition ALSPlayerCameraManager.cpp:120

◆ CustomCameraBehavior()

bool AALSPlayerCameraManager::CustomCameraBehavior ( float DeltaTime,
FVector & Location,
FRotator & Rotation,
float & FOV )
protected
141{
142 TRACE_CPUPROFILER_EVENT_SCOPE(AALSPlayerCameraManager::CustomCameraBehavior);
143 SCOPE_CYCLE_COUNTER(STATGROUP_ALS_Camera_Manager);
144
146 {
147 return false;
148 }
149
150 // Step 1: Get Camera Parameters from CharacterBP via the Camera Interface
151 const FTransform& PivotTarget = ControlledCharacter->GetThirdPersonPivotTarget();
152 const FVector& FPTarget = ControlledCharacter->GetFirstPersonCameraTarget();
153 const FVector& ADSTarget = ControlledCharacter->GetAimDownSightCameraTarget();
154
155 float TPFOV = 90.0f;
156 float FPFOV = 90.0f;
157 bool bRightShoulder = false;
158 const float ADSFOV = ControlledCharacter->GetAimDownSightFOV();
159 ControlledCharacter->GetCameraParameters(TPFOV, FPFOV, bRightShoulder);
160
161 // Step 2: Calculate Target Camera Rotation. Use the Control Rotation and interpolate for smooth camera rotation.
162 const FRotator& InterpResult = FMath::RInterpTo(GetCameraRotation(),
163 GetOwningPlayerController()->GetControlRotation(), DeltaTime,
165
166 TargetCameraRotation = UKismetMathLibrary::RLerp(InterpResult, DebugViewRotation,
167 GetCameraBehaviorParam(TEXT("Override_Debug")), true);
168
169 // Step 3: Calculate the Smoothed Pivot Target (Orange Sphere).
170 // Get the 3P Pivot Target (Green Sphere) and interpolate using axis independent lag for maximum control.
171 const FVector LagSpd(GetCameraBehaviorParam(NAME_PivotLagSpeed_X),
174
175 const FVector& AxisIndpLag = CalculateAxisIndependentLag(SmoothedPivotTarget.GetLocation(),
176 PivotTarget.GetLocation(), TargetCameraRotation, LagSpd,
177 DeltaTime);
178
179 SmoothedPivotTarget.SetRotation(PivotTarget.GetRotation());
180 SmoothedPivotTarget.SetLocation(AxisIndpLag);
181 SmoothedPivotTarget.SetScale3D(FVector::OneVector);
182
183 // Step 4: Calculate Pivot Location (BlueSphere). Get the Smoothed
184 // Pivot Target and apply local offsets for further camera control.
186 SmoothedPivotTarget.GetLocation() +
187 UKismetMathLibrary::GetForwardVector(SmoothedPivotTarget.Rotator()) * GetCameraBehaviorParam(
189 UKismetMathLibrary::GetRightVector(SmoothedPivotTarget.Rotator()) * GetCameraBehaviorParam(
191 UKismetMathLibrary::GetUpVector(SmoothedPivotTarget.Rotator()) * GetCameraBehaviorParam(
193
194 // Step 5: Calculate Target Camera Location. Get the Pivot location and apply camera relative offsets.
195 TargetCameraLocation = UKismetMathLibrary::VLerp(
197 UKismetMathLibrary::GetForwardVector(TargetCameraRotation) * GetCameraBehaviorParam(
199 UKismetMathLibrary::GetRightVector(TargetCameraRotation) * GetCameraBehaviorParam(NAME_CameraOffset_Y)
200 +
201 UKismetMathLibrary::GetUpVector(TargetCameraRotation) * GetCameraBehaviorParam(NAME_CameraOffset_Z),
202 PivotTarget.GetLocation() + DebugViewOffset,
204
205 // Step 6: Trace for an object between the camera and character to apply a corrective offset.
206 // Trace origins are set within the Character BP via the Camera Interface.
207 // Functions like the normal spring arm, but can allow for different trace origins regardless of the pivot
208 FVector TraceOrigin;
209 float TraceRadius;
210 ECollisionChannel TraceChannel = ControlledCharacter->GetThirdPersonTraceParams(TraceOrigin, TraceRadius);
211
212 UWorld* World = GetWorld();
213 check(World);
214
215 FCollisionQueryParams Params;
216 Params.AddIgnoredActor(this);
217 Params.AddIgnoredActor(ControlledCharacter);
218
219 FHitResult HitResult;
220 const FCollisionShape SphereCollisionShape = FCollisionShape::MakeSphere(TraceRadius);
221 const bool bHit = World->SweepSingleByChannel(HitResult, TraceOrigin, TargetCameraLocation, FQuat::Identity,
222 TraceChannel, SphereCollisionShape, Params);
223
224 if (ALSDebugComponent && ALSDebugComponent->GetShowTraces())
225 {
227 TraceOrigin,
229 SphereCollisionShape,
230 EDrawDebugTrace::Type::ForOneFrame,
231 bHit,
232 HitResult,
233 FLinearColor::Red,
234 FLinearColor::Green,
235 5.0f);
236 }
237
238 if (HitResult.IsValidBlockingHit())
239 {
240 TargetCameraLocation += HitResult.Location - HitResult.TraceEnd;
241 }
242
243 // Step 8: Lerp First Person Override and return target camera parameters.
244 FTransform TargetCameraTransform(TargetCameraRotation, TargetCameraLocation, FVector::OneVector);
245
246 FTransform FPTargetCameraTransform(TargetCameraRotation, FPTarget, FVector::OneVector);
247
248 FTransform ADSTargetCameraTransform(TargetCameraRotation, ADSTarget, FVector::OneVector);
249
250
251 const FTransform& ADSTransform = UKismetMathLibrary::TLerp(FPTargetCameraTransform, ADSTargetCameraTransform,
254
255
256 const FTransform& MixedTransform = UKismetMathLibrary::TLerp(TargetCameraTransform, ADSTransform, //FPTargetCameraTransform
259
260 const FTransform& TargetTransform = UKismetMathLibrary::TLerp(MixedTransform,
262 FVector::OneVector),
265
266 Location = TargetTransform.GetLocation();
267 Rotation = TargetTransform.Rotator();
268 const float CalculateFPFOV = FMath::Lerp(FPFOV, ADSFOV, GetCameraBehaviorParam(NAME_Weight_ADS));
269
270 FOV = FMath::Lerp(TPFOV, CalculateFPFOV, GetCameraBehaviorParam(NAME_Weight_FirstPerson));
271
272 return true;
273}
const FName NAME_PivotLagSpeed_X(TEXT("PivotLagSpeed_X"))
const FName NAME_PivotOffset_X(TEXT("PivotOffset_X"))
const FName NAME_Override_Debug(TEXT("Override_Debug"))
const FName NAME_PivotOffset_Y(TEXT("PivotOffset_Y"))
const FName NAME_Weight_FirstPerson(TEXT("Weight_FirstPerson"))
const FName NAME_PivotLagSpeed_Z(TEXT("PivotLagSpeed_Z"))
const FName NAME_CameraOffset_Z(TEXT("CameraOffset_Z"))
const FName NAME_RotationLagSpeed(TEXT("RotationLagSpeed"))
const FName NAME_Weight_ADS(TEXT("Weight_ADS"))
const FName NAME_PivotOffset_Z(TEXT("PivotOffset_Z"))
const FName NAME_CameraOffset_Y(TEXT("CameraOffset_Y"))
const FName NAME_PivotLagSpeed_Y(TEXT("PivotLagSpeed_Y"))
const FName NAME_CameraOffset_X(TEXT("CameraOffset_X"))
TObjectPtr< UALSDebugComponent > ALSDebugComponent
Definition ALSPlayerCameraManager.h:76
FRotator DebugViewRotation
Definition ALSPlayerCameraManager.h:69
FVector TargetCameraLocation
Definition ALSPlayerCameraManager.h:63
FRotator TargetCameraRotation
Definition ALSPlayerCameraManager.h:66
FVector PivotLocation
Definition ALSPlayerCameraManager.h:60
TObjectPtr< AALSBaseCharacter > ControlledCharacter
Definition ALSPlayerCameraManager.h:47
float GetCameraBehaviorParam(FName CurveName) const
Definition ALSPlayerCameraManager.cpp:74
bool CustomCameraBehavior(float DeltaTime, FVector &Location, FRotator &Rotation, float &FOV)
Definition ALSPlayerCameraManager.cpp:140
FTransform SmoothedPivotTarget
Definition ALSPlayerCameraManager.h:57
FVector DebugViewOffset
Definition ALSPlayerCameraManager.h:72
static void DrawDebugSphereTraceSingle(const UWorld *World, const FVector &Start, const FVector &End, const FCollisionShape &CollisionShape, EDrawDebugTrace::Type DrawDebugType, bool bHit, const FHitResult &OutHit, FLinearColor TraceColor, FLinearColor TraceHitColor, float DrawTime)
Definition ALSDebugComponent.cpp:337

◆ DrawDebugTargets()

void AALSPlayerCameraManager::DrawDebugTargets ( FVector PivotTargetLocation)

Implemented debug logic in BP

◆ GetCameraBehaviorParam()

float AALSPlayerCameraManager::GetCameraBehaviorParam ( FName CurveName) const
75{
76 TRACE_CPUPROFILER_EVENT_SCOPE(AALSPlayerCameraManager::GetCameraBehaviorParam);
77 SCOPE_CYCLE_COUNTER(STATGROUP_ALS_Camera_Manager);
78
79 UAnimInstance* Inst = CameraBehavior->GetAnimInstance();
80 if (Inst)
81 {
82 return Inst->GetCurveValue(CurveName);
83 }
84 return 0.0f;
85}

◆ OnPossess()

void AALSPlayerCameraManager::OnPossess ( AALSBaseCharacter * NewCharacter)
43{
44 TRACE_CPUPROFILER_EVENT_SCOPE(AALSPlayerCameraManager::OnPossess);
45 SCOPE_CYCLE_COUNTER(STATGROUP_ALS_Camera_Manager);
46
47 // Set "Controlled Pawn" when Player Controller Possesses new character. (called from Player Controller)
48 check(NewCharacter);
49 ControlledCharacter = NewCharacter;
50
51 // Update references in the Camera Behavior AnimBP.
52 UALSPlayerCameraBehavior* CastedBehv = Cast<UALSPlayerCameraBehavior>(CameraBehavior->GetAnimInstance());
53 if (CastedBehv)
54 {
55 NewCharacter->SetCameraBehavior(CastedBehv);
56 CastedBehv->MovementState = NewCharacter->GetMovementState();
57 CastedBehv->MovementAction = NewCharacter->GetMovementAction();
58 CastedBehv->bRightShoulder = NewCharacter->IsRightShoulder();
59 CastedBehv->Gait = NewCharacter->GetGait();
60 CastedBehv->SetRotationMode(NewCharacter->GetRotationMode());
61 CastedBehv->Stance = NewCharacter->GetStance();
62 CastedBehv->ViewMode = NewCharacter->GetViewMode();
63 CastedBehv->bAimDownSights = NewCharacter->IsAimingDownSights();
64 }
65
66 // Initial position
67 const FVector& TPSLoc = ControlledCharacter->GetThirdPersonPivotTarget().GetLocation();
68 SetActorLocation(TPSLoc);
69 SmoothedPivotTarget.SetLocation(TPSLoc);
70
72}
EALSMovementAction GetMovementAction() const
Definition ALSBaseCharacter.h:108
bool IsRightShoulder() const
Definition ALSBaseCharacter.h:303
EALSMovementState GetMovementState() const
Definition ALSBaseCharacter.h:99
EALSRotationMode GetRotationMode() const
Definition ALSBaseCharacter.h:138
void SetCameraBehavior(UALSPlayerCameraBehavior *CamBeh)
Definition ALSBaseCharacter.h:336
EALSViewMode GetViewMode() const
Definition ALSBaseCharacter.h:147
EALSGait GetGait() const
Definition ALSBaseCharacter.h:126
bool IsAimingDownSights() const
Definition ALSBaseCharacter.h:309
EALSStance GetStance() const
Definition ALSBaseCharacter.h:114
void OnPossess(AALSBaseCharacter *NewCharacter)
Definition ALSPlayerCameraManager.cpp:42
Definition ALSDebugComponent.h:17
Definition ALSPlayerCameraBehavior.h:21
EALSMovementState MovementState
Definition ALSPlayerCameraBehavior.h:28
void SetRotationMode(EALSRotationMode RotationMode)
Definition ALSPlayerCameraBehavior.cpp:10
EALSMovementAction MovementAction
Definition ALSPlayerCameraBehavior.h:31
bool bAimDownSights
Definition ALSPlayerCameraBehavior.h:43
bool bRightShoulder
Definition ALSPlayerCameraBehavior.h:55
EALSViewMode ViewMode
Definition ALSPlayerCameraBehavior.h:52
EALSStance Stance
Definition ALSPlayerCameraBehavior.h:49
EALSGait Gait
Definition ALSPlayerCameraBehavior.h:46

◆ UpdateViewTargetInternal()

void AALSPlayerCameraManager::UpdateViewTargetInternal ( FTViewTarget & OutVT,
float DeltaTime )
overrideprotectedvirtual
88{
89 TRACE_CPUPROFILER_EVENT_SCOPE(AALSPlayerCameraManager::UpdateViewTargetInternal);
90 SCOPE_CYCLE_COUNTER(STATGROUP_ALS_Camera_Manager);
91
92 // Partially taken from base class
93
94 if (OutVT.Target)
95 {
96 FVector OutLocation;
97 FRotator OutRotation;
98 float OutFOV;
99
100 if (OutVT.Target->IsA<AALSBaseCharacter>())
101 {
102 if (CustomCameraBehavior(DeltaTime, OutLocation, OutRotation, OutFOV))
103 {
104 OutVT.POV.Location = OutLocation;
105 OutVT.POV.Rotation = OutRotation;
106 OutVT.POV.FOV = OutFOV;
107 }
108 else
109 {
110 OutVT.Target->CalcCamera(DeltaTime, OutVT.POV);
111 }
112 }
113 else
114 {
115 OutVT.Target->CalcCamera(DeltaTime, OutVT.POV);
116 }
117 }
118}
Definition ALSBaseCharacter.h:38
virtual void UpdateViewTargetInternal(FTViewTarget &OutVT, float DeltaTime) override
Definition ALSPlayerCameraManager.cpp:87

Member Data Documentation

◆ ALSDebugComponent

TObjectPtr<UALSDebugComponent> AALSPlayerCameraManager::ALSDebugComponent = nullptr
private

◆ CameraBehavior

TObjectPtr<USkeletalMeshComponent> AALSPlayerCameraManager::CameraBehavior = nullptr

◆ ControlledCharacter

TObjectPtr<AALSBaseCharacter> AALSPlayerCameraManager::ControlledCharacter = nullptr

◆ DebugViewOffset

FVector AALSPlayerCameraManager::DebugViewOffset
protected

◆ DebugViewRotation

FRotator AALSPlayerCameraManager::DebugViewRotation
protected

◆ PivotLocation

FVector AALSPlayerCameraManager::PivotLocation
protected

◆ RootLocation

FVector AALSPlayerCameraManager::RootLocation
protected

◆ SmoothedPivotTarget

FTransform AALSPlayerCameraManager::SmoothedPivotTarget
protected

◆ TargetCameraLocation

FVector AALSPlayerCameraManager::TargetCameraLocation
protected

◆ TargetCameraRotation

FRotator AALSPlayerCameraManager::TargetCameraRotation
protected

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