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

#include <GravityPhysicsVolume.h>

Inheritance diagram for AGravityPhysicsVolume:
AGravityPhysicsVolumeSpline

Public Member Functions

 AGravityPhysicsVolume (const FObjectInitializer &ObjectInitializer)
 
virtual void ActorEnteredVolume (class AActor *Other) override
 
virtual void ActorLeavingVolume (class AActor *Other) override
 
virtual FVector GetGravity (class USceneComponent *SceneComponent) const
 
virtual FVector GetGravityDirection (class USceneComponent *SceneComponent) const
 
virtual float GetGravityMagnitude (class USceneComponent *SceneComponent) const
 
virtual float GetFinalGravityScale (class USceneComponent *SceneComponent) const
 
virtual void K2_SetFixedGravityDirection (const FVector &NewGravityDirection)
 
virtual void SetFixedGravityDirection (const FVector &NewFixedGravityDirection)
 
virtual void SetSplineTangentGravityDirection (AActor *NewGravityActor)
 
virtual void SetPointGravityDirection (const FVector &NewGravityPoint)
 
virtual void SetPointGravityDirectionFromActor (AActor *NewGravityActor)
 
virtual void SetLineGravityDirection (const FVector &NewGravityLineStart, const FVector &NewGravityLineEnd)
 
virtual void SetSegmentGravityDirection (const FVector &NewGravitySegmentStart, const FVector &NewGravitySegmentEnd)
 
virtual void SetSplineGravityDirection (AActor *NewGravityActor)
 
virtual void K2_SetPlaneGravityDirection (const FVector &NewGravityPlaneBase, const FVector &NewGravityPlaneNormal)
 
virtual void SetPlaneGravityDirection (const FVector &NewGravityPlaneBase, const FVector &NewGravityPlaneNormal)
 
virtual void SetSplinePlaneGravityDirection (AActor *NewGravityActor)
 
virtual void SetBoxGravityDirection (const FVector &NewGravityBoxOrigin, const FVector &NewGravityBoxExtent)
 
virtual void SetBoxGravityDirectionFromActor (AActor *NewGravityActor)
 
virtual void SetCollisionGravityDirection (AActor *NewGravityActor)
 
virtual float GetGravityScale () const
 
virtual void SetGravityScale (const float NewGravityScale)
 

Public Attributes

FVector CharacterFallVelocity
 

Protected Member Functions

virtual void BeginPlay () override
 
virtual void Tick (float DeltaTime) override
 

Protected Attributes

bool bDebuggingMode = false
 
TArray< TObjectPtr< AActor > > TrackedActors
 
TArray< class IALSCharacterInterface * > TrackedCharacters
 
EGravityDirectionMode GravityDirectionMode
 
FVector GravityVectorA
 
FVector GravityVectorB
 
TObjectPtr< AActor > GravityActor
 
float GravityScale
 

Constructor & Destructor Documentation

◆ AGravityPhysicsVolume()

AGravityPhysicsVolume::AGravityPhysicsVolume ( const FObjectInitializer & ObjectInitializer)
47 : Super(ObjectInitializer)
48{
49#if WITH_EDITORONLY_DATA
50 // Create and configure attached text render component
51 TextRenderComponent = CreateEditorOnlyDefaultSubobject<UTextRenderComponent>(TextRenderComponentName);
52 if (!IsRunningCommandlet() && TextRenderComponent != nullptr)
53 {
54 TextRenderComponent->bHiddenInGame = true;
55 TextRenderComponent->HorizontalAlignment = EHorizTextAligment::EHTA_Center;
56 TextRenderComponent->Text = FText::AsCultureInvariant(TEXT("Gravity Physics Volume"));
57 TextRenderComponent->SetUsingAbsoluteRotation(true);
58 TextRenderComponent->SetupAttachment(GetBrushComponent());
59 }
60#endif // WITH_EDITORONLY_DATA
61
62 // Change 'tick' configuration
63 PrimaryActorTick.bCanEverTick = true;
64 PrimaryActorTick.bStartWithTickEnabled = false;
65
66 GravityActor = nullptr;
68 GravityScale = 1.0f;
69 GravityVectorA = FVector(0.0f, 0.0f, -1.0f);
70 GravityVectorB = FVector::ZeroVector;
71 CharacterFallVelocity = FVector::ZeroVector;
72
73#if WITH_EDITOR
74 bDebuggingMode = true;
75#endif
76
77}
float GravityScale
Definition GravityPhysicsVolume.h:204
FVector GravityVectorB
Definition GravityPhysicsVolume.h:196
FVector CharacterFallVelocity
Definition GravityPhysicsVolume.h:155
FVector GravityVectorA
Definition GravityPhysicsVolume.h:192
EGravityDirectionMode GravityDirectionMode
Definition GravityPhysicsVolume.h:188
bool bDebuggingMode
Definition GravityPhysicsVolume.h:175
TObjectPtr< AActor > GravityActor
Definition GravityPhysicsVolume.h:200

Member Function Documentation

◆ ActorEnteredVolume()

void AGravityPhysicsVolume::ActorEnteredVolume ( class AActor * Other)
overridevirtual

Called when an Actor enters this volume.

Parameters
Other- Actor that entered this volume
141{
142 UE_LOG(LogTemp, Error, TEXT("Entered Volume"));
143 Super::ActorEnteredVolume(Other);
144 if(Other != nullptr)
145 {
146 UE_LOG(LogTemp, Error, TEXT("Actor: %s Entered Gravity"), *Other->GetHumanReadableName());
147 }
148 // Remove all empty positions of the TrackedActors list
149 TrackedActors.RemoveAll([](const AActor* Actor)
150 {
151 return Actor == nullptr;
152 });
153
154 // Remove all empty positions of the TrackedCharacters list
156 {
157 return Character == nullptr;
158 });
159
160 if (IsValid(Other))
161 {
162 IALSCharacterInterface* Character = Cast<IALSCharacterInterface>(Other);
163 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
164 {
165 IALSGravityMovementInterface* ALSGravityMovement = Character->GetALSGravityMovementInterface();
166 if(ALSGravityMovement == nullptr){return;}
167 // Just change gravity settings of Characters
168 switch (GravityDirectionMode)
169 {
171 {
172 ALSGravityMovement->SetFixedGravityDirection(GravityVectorA);
173 break;
174 }
175
177 {
178 if(IsValid(GravityActor))
179 {
181 }
182 else
183 {
184 ALSGravityMovement->SetSplineTangentGravityDirection(this);
185 }
186 break;
187 }
188
190 {
191 if (IsValid(GravityActor))
192 {
194 }
195 else
196 {
197 ALSGravityMovement->SetPointGravityDirection(GravityVectorA);
198 }
199
200 break;
201 }
202
204 {
206 break;
207 }
208
210 {
212 break;
213 }
214
216 {
217 if (IsValid(GravityActor))
218 {
219 ALSGravityMovement->SetSplineGravityDirection(GravityActor);
220 }
221 else
222 {
223 ALSGravityMovement->SetSplineGravityDirection(this);
224 }
225
226 break;
227 }
228
230 {
232 break;
233 }
234
236 {
237 if (IsValid(GravityActor))
238 {
239 ALSGravityMovement->SetSplinePlaneGravityDirection(GravityActor);
240 }
241 else
242 {
243 ALSGravityMovement->SetSplinePlaneGravityDirection(this);
244 }
245
246 break;
247 }
248
250 {
251 if (IsValid(GravityActor))
252 {
254 }
255 else
256 {
258 }
259
260 break;
261 }
262
264 default:
265 {
266 if (IsValid(GravityActor))
267 {
268 ALSGravityMovement->SetCollisionGravityDirection(GravityActor);
269 }
270
271 break;
272 }
273
274 }
275
276 // Launch walking Character if configured
277 if(!CharacterFallVelocity.IsZero() && ALSGravityMovement->GetIsWalking())
278 {
279 ALSGravityMovement->LaunchCharacter(CharacterFallVelocity);
280 }
281
282 TrackedCharacters.AddUnique(Character);
283 }
284 else
285 {
286 // Make sure the received Actor is valid and has physics
287 const UPrimitiveComponent* Primitive = Cast<UPrimitiveComponent>(Other->GetRootComponent());
288 if (Primitive != nullptr && Primitive->IsAnySimulatingPhysics())
289 {
290 TrackedActors.AddUnique(Other);
291 }
292 }
293 }
294 SetActorTickEnabled(TrackedActors.Num() > 0);
295}
TArray< class IALSCharacterInterface * > TrackedCharacters
Definition GravityPhysicsVolume.h:182
TArray< TObjectPtr< AActor > > TrackedActors
Definition GravityPhysicsVolume.h:178
Definition ALSCharacterInterface.h:24
Definition ALSGravityMovementInterface.h:19
virtual void SetPointGravityDirection(const FVector &NewGravityPoint)=0
virtual void SetBoxGravityDirection(const FVector &NewGravityBoxOrigin, const FVector &NewGravityBoxExtent)=0
virtual bool GetIsWalking() const =0
virtual void SetSplineTangentGravityDirection(AActor *NewGravityActor)=0
virtual void SetSplinePlaneGravityDirection(AActor *NewGravityActor)=0
virtual void SetLineGravityDirection(const FVector &NewGravityLineStart, const FVector &NewGravityLineEnd)=0
virtual void SetPlaneGravityDirection(const FVector &NewGravityPlaneBase, const FVector &NewGravityPlaneNormal)=0
virtual void SetPointGravityDirectionFromActor(AActor *NewGravityActor)=0
virtual void LaunchCharacter(const FVector &LaunchVel)=0
virtual void SetSplineGravityDirection(AActor *NewGravityActor)=0
virtual void SetCollisionGravityDirection(AActor *NewGravityActor)=0
virtual void SetFixedGravityDirection(const FVector &NewFixedGravityDirection)=0
virtual void SetBoxGravityDirectionFromActor(AActor *NewGravityActor)=0
virtual void SetSegmentGravityDirection(const FVector &NewGravitySegmentStart, const FVector &NewGravitySegmentEnd)=0

◆ ActorLeavingVolume()

void AGravityPhysicsVolume::ActorLeavingVolume ( class AActor * Other)
overridevirtual

Called when an Actor leaves this volume.

Parameters
Other- Actor that left this volume, can be nullptr
298{
299 UE_LOG(LogTemp, Error, TEXT("Leave Volume"));
300 Super::ActorLeavingVolume(Other);
301 if(Other != nullptr)
302 {
303 UE_LOG(LogTemp, Warning, TEXT("Actor: %s Left Gravity"), *Other->GetHumanReadableName());
304 }
305
306 // Remove the received Actor from the TrackedActors list
307 if (Other != nullptr)
308 {
309 IALSCharacterInterface* Character = Cast<IALSCharacterInterface>(Other);
310 if(Character != nullptr)
311 {
313 }
314 else
315 {
316 TrackedActors.Remove(Other);
317 }
318 }
319 // Remove all empty positions of the TrackedActors list
320 TrackedActors.RemoveAll([](const AActor* Actor)
321 {
322 return Actor == nullptr;
323 });
324
325 // Remove all empty positions of the TrackedNinjas list
327 {
328 return Character == nullptr;
329 });
330
331 SetActorTickEnabled(TrackedActors.Num() > 0);
332
333}

◆ BeginPlay()

void AGravityPhysicsVolume::BeginPlay ( )
overrideprotectedvirtual
80{
81 Super::BeginPlay();
82 TArray<AActor*> ActorsToIgnore;
83 TArray<FHitResult> HitResults;
84 TArray<AActor*> OverlappingActors;
85
86 if(UKismetSystemLibrary::BoxTraceMulti(this, GetActorLocation(), GetActorLocation(), GetBrushComponent()->Bounds.BoxExtent,
87 FRotator::ZeroRotator, UEngineTypes::ConvertToTraceType(ECC_EngineTraceChannel1), false, ActorsToIgnore,
89 {
90 for(auto& HitResult : HitResults)
91 {
92 if(HitResult.GetActor() != nullptr)
93 {
94 OverlappingActors.AddUnique(HitResult.GetActor());
95 // ActorEnteredVolume(HitResult.GetActor());
96 }
97 }
98 }
99 for(const auto OverlappingActor : OverlappingActors)
100 {
101 ActorEnteredVolume(OverlappingActor);
102 }
103 // TArray<AActor*> OverlappingActors;
104 // GetBrushComponent()->GetOverlappingActors(OverlappingActors);
105 // // GetOverlappingActors(OverlappingActors);
106 // for(const auto OverlappingActor : OverlappingActors)
107 // {
108 // UE_LOG(LogTemp, Error, TEXT("OverlappingActor: %s"), *OverlappingActor->GetHumanReadableName());
109 // ActorEnteredVolume(OverlappingActor);
110 // }
111}
virtual void ActorEnteredVolume(class AActor *Other) override
Definition GravityPhysicsVolume.cpp:140
static EDrawDebugTrace::Type GetDrawDebugType(const bool bDebuggingMode)
Definition BaseHelpersBPLib.cpp:910

◆ GetFinalGravityScale()

float AGravityPhysicsVolume::GetFinalGravityScale ( class USceneComponent * SceneComponent) const
virtual

Obtains the scale factor that affects magnitude of current gravity vector that influences a SceneComponent.

Parameters
SceneComponent- component in space affected by gravity
Returns
final gravity scale factor
851{
852 return GetGravityScale();
853}
virtual float GetGravityScale() const
Definition GravityPhysicsVolume.cpp:1155

◆ GetGravity()

FVector AGravityPhysicsVolume::GetGravity ( class USceneComponent * SceneComponent) const
virtual

Obtains the gravity vector that influences a SceneComponent.

Parameters
SceneComponent- component in space affected by gravity
Returns
current gravity
336{
337 SCOPE_CYCLE_COUNTER(STAT_PhysicsVolumeGetGravity);
338 if (GetGravityScale() == 0.0f)
339 {
340 return FVector::ZeroVector;
341 }
342 FVector Gravity = FVector::ZeroVector;
343
345 {
347 {
348 Gravity = GravityVectorA * (FMath::Abs(GetGravityZ()) * GetFinalGravityScale(SceneComponent));
349 break;
350 }
351
353 {
354 USplineComponent* Spline = nullptr;
355 if (IsValid(GravityActor))
356 {
357 Spline = Cast<USplineComponent>(
358 GravityActor->GetComponentByClass(USplineComponent::StaticClass()));
359 }
360 else
361 {
362 Spline = Cast<USplineComponent>(GetComponentByClass(USplineComponent::StaticClass()));
363 }
364
365 if (Spline != nullptr)
366 {
367 const FVector Point = SceneComponent->GetComponentLocation();
368 const FVector GravityDir = Spline->FindDirectionClosestToWorldLocation(
369 Point, ESplineCoordinateSpace::Type::World);
370 if (!GravityDir.IsZero())
371 {
372 Gravity = GravityDir * (FMath::Abs(GetGravityZ()) * GetFinalGravityScale(SceneComponent));
373 }
374
375#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
376 if (GravityPhysicsVolumeCVars::ShowGravity > 0 && !GravityDir.IsZero())
377 {
378 DrawDebugDirectionalArrow(GetWorld(), Point, Point + GravityDir * 100.0f,
379 1000.0f, FColor::Green, false, 0.02f, 0, 4.0f);
380 }
381#endif // !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
382 }
383
384 break;
385 }
386
388 {
389 FVector GravityPoint = GravityVectorA;
390
391 if (IsValid(GravityActor))
392 {
393 GravityPoint = GravityActor->GetActorLocation();
394 }
395
396 const FVector GravityDir = GravityPoint - SceneComponent->GetComponentLocation();
397 if (!GravityDir.IsZero())
398 {
399 Gravity = GravityDir.GetSafeNormal() *
400 (FMath::Abs(GetGravityZ()) * GetFinalGravityScale(SceneComponent));
401 }
402
403#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
405 {
406 DrawDebugSphere(GetWorld(), GravityPoint, 4.0, 8, FColor::Green, false, 0.02f, 0, 10.0f);
407 }
408#endif // !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
409
410 break;
411 }
412
414 {
415 const FVector Point = SceneComponent->GetComponentLocation();
416 const FVector GravityDir = FMath::ClosestPointOnInfiniteLine(
418 if (!GravityDir.IsZero())
419 {
420 Gravity = GravityDir.GetSafeNormal() *
421 (FMath::Abs(GetGravityZ()) * GetFinalGravityScale(SceneComponent));
422 }
423
424#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
426 {
427 DrawDebugLine(GetWorld(), GravityVectorA + (GravityVectorA - GravityVectorB),
428 GravityVectorB + (GravityVectorB - GravityVectorA), FColor::Green, false, 0.02f, 0, 4.0f);
429 DrawDebugSphere(GetWorld(), GravityVectorA, 4.0, 8, FColor::Blue, false, 0.02f, 0, 10.0f);
430 DrawDebugSphere(GetWorld(), GravityVectorB, 4.0, 8, FColor::Blue, false, 0.02f, 0, 10.0f);
431 }
432#endif // !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
433
434 break;
435 }
436
438 {
439 const FVector Point = SceneComponent->GetComponentLocation();
440 const FVector GravityDir = FMath::ClosestPointOnLine(GravityVectorA,
442 if (!GravityDir.IsZero())
443 {
444 Gravity = GravityDir.GetSafeNormal() *
445 (FMath::Abs(GetGravityZ()) * GetFinalGravityScale(SceneComponent));
446 }
447
448#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
450 {
451 DrawDebugLine(GetWorld(), GravityVectorA, GravityVectorB, FColor::Green, false, 0.02f, 0, 4.0f);
452 DrawDebugSphere(GetWorld(), GravityVectorA, 4.0, 8, FColor::Blue, false, 0.02f, 0, 10.0f);
453 DrawDebugSphere(GetWorld(), GravityVectorB, 4.0, 8, FColor::Blue, false, 0.02f, 0, 10.0f);
454 }
455#endif // !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
456
457 break;
458 }
459
461 {
462 USplineComponent* Spline = nullptr;
463 if (IsValid(GravityActor))
464 {
465 Spline = Cast<USplineComponent>(
466 GravityActor->GetComponentByClass(USplineComponent::StaticClass()));
467 }
468 else
469 {
470 Spline = Cast<USplineComponent>(GetComponentByClass(USplineComponent::StaticClass()));
471 }
472
473 if (Spline != nullptr)
474 {
475 const FVector Point = SceneComponent->GetComponentLocation();
476 const FVector GravityPoint = Spline->FindLocationClosestToWorldLocation(
477 Point, ESplineCoordinateSpace::Type::World);
478 const FVector GravityDir = GravityPoint - Point;
479 if (!GravityDir.IsZero())
480 {
481 Gravity = GravityDir.GetSafeNormal() *
482 (FMath::Abs(GetGravityZ()) * GetFinalGravityScale(SceneComponent));
483 }
484
485#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
487 {
488 DrawDebugSphere(GetWorld(), GravityPoint, 4.0, 8, FColor::Green, false, 0.02f, 0, 10.0f);
489 }
490#endif // !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
491 }
492
493 break;
494 }
495
497 {
498 const FVector Point = SceneComponent->GetComponentLocation();
499 const FVector GravityDir = FVector::PointPlaneProject(Point,
501 if (!GravityDir.IsZero())
502 {
503 Gravity = GravityDir.GetSafeNormal() *
504 (FMath::Abs(GetGravityZ()) * GetFinalGravityScale(SceneComponent));
505 }
506
507#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
509 {
510 DrawDebugSolidPlane(GetWorld(), FPlane(GravityVectorA, GravityVectorB), GravityVectorA,
511 FVector2D(500.0f, 500.0f), FColor::Green);
512 }
513#endif // !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
514
515 break;
516 }
517
519 {
520 USplineComponent* Spline = nullptr;
521 if (IsValid(GravityActor))
522 {
523 Spline = Cast<USplineComponent>(
524 GravityActor->GetComponentByClass(USplineComponent::StaticClass()));
525 }
526 else
527 {
528 Spline = Cast<USplineComponent>(GetComponentByClass(USplineComponent::StaticClass()));
529 }
530
531 if (Spline != nullptr)
532 {
533 const FVector Point = SceneComponent->GetComponentLocation();
534
535 const float InputKey = Spline->FindInputKeyClosestToWorldLocation(Point);
536 const FVector ClosestLocation = Spline->GetLocationAtSplineInputKey(
537 InputKey, ESplineCoordinateSpace::Type::World);
538 const FVector ClosestUpVector = Spline->GetUpVectorAtSplineInputKey(
539 InputKey, ESplineCoordinateSpace::Type::World);
540
541 const FVector GravityDir = FVector::PointPlaneProject(Point,
542 ClosestLocation, ClosestUpVector) - Point;
543 if (!GravityDir.IsZero())
544 {
545 Gravity = GravityDir.GetSafeNormal() *
546 (FMath::Abs(GetGravityZ()) * GetFinalGravityScale(SceneComponent));
547 }
548
549#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
551 {
552 DrawDebugSolidPlane(GetWorld(), FPlane(ClosestLocation, ClosestUpVector),
553 ClosestLocation, FVector2D(500.0f, 500.0f), FColor::Green);
554 }
555#endif // !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
556 }
557
558 break;
559 }
560
562 {
563 FVector BoxOrigin = GravityVectorA;
564 FVector BoxExtent = GravityVectorB;
565
566 if (IsValid(GravityActor))
567 {
568 GravityActor->GetActorBounds(true, BoxOrigin, BoxExtent);
569 }
570
571 const FVector Point = SceneComponent->GetComponentLocation();
572 const FVector GravityDir = FBox(BoxOrigin - BoxExtent,
573 BoxOrigin + BoxExtent).GetClosestPointTo(Point) - Point;
574 if (!GravityDir.IsZero())
575 {
576 Gravity = GravityDir.GetSafeNormal() *
577 (FMath::Abs(GetGravityZ()) * GetFinalGravityScale(SceneComponent));
578 }
579
580#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
582 {
583 DrawDebugSolidBox(GetWorld(), BoxOrigin, BoxExtent, FColor::Green);
584 }
585#endif // !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
586
587 break;
588 }
589
591 {
592 if (IsValid(GravityActor))
593 {
594 const FVector Point = SceneComponent->GetComponentLocation();
595
596 FVector ClosestPoint;
597 if (Cast<UPrimitiveComponent>(GravityActor->GetRootComponent())->GetClosestPointOnCollision(
598 Point, ClosestPoint) > 0.0f)
599 {
600 const FVector GravityDir = ClosestPoint - Point;
601 if (!GravityDir.IsZero())
602 {
603 Gravity = GravityDir.GetSafeNormal() *
604 (FMath::Abs(GetGravityZ()) * GetFinalGravityScale(SceneComponent));
605 }
606
607#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
609 {
610 DrawDebugSphere(GetWorld(), ClosestPoint, 4.0, 8, FColor::Green, false, 0.02f, 0, 10.0f);
611 }
612#endif // !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
613 }
614 }
615
616 break;
617 }
618 }
619
620#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
621 if (GravityPhysicsVolumeCVars::ShowGravity > 0 && !Gravity.IsZero())
622 {
623 const FVector Point = SceneComponent->GetComponentLocation();
624 DrawDebugDirectionalArrow(GetWorld(), Point, Point + Gravity, 1000.0f,
625 FColor::Red, false, 0.02f, 0, 7.0f);
626 }
627#endif // !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
628
629 return Gravity;
630}
virtual float GetFinalGravityScale(class USceneComponent *SceneComponent) const
Definition GravityPhysicsVolume.cpp:850
static int32 ShowGravity
Definition GravityPhysicsVolume.cpp:36

◆ GetGravityDirection()

FVector AGravityPhysicsVolume::GetGravityDirection ( class USceneComponent * SceneComponent) const
virtual

Obtains the normalized direction of gravity that influences a SceneComponent.

Note
Could return zero gravity
Parameters
SceneComponent- component in space affected by gravity
Returns
normalized direction of gravity
633{
634 SCOPE_CYCLE_COUNTER(STAT_PhysicsVolumeGetGravityDirection);
635
636 if (GetGravityScale() == 0.0f)
637 {
638 return FVector::ZeroVector;
639 }
640
641 FVector GravityDir = FVector::ZeroVector;
642
643 switch (GravityDirectionMode)
644 {
646 {
647 GravityDir = GravityVectorA * ((GetFinalGravityScale(SceneComponent) > 0.0f) ? 1.0f : -1.0f);
648 break;
649 }
650
652 {
653 USplineComponent* Spline = nullptr;
654 if (IsValid(GravityActor))
655 {
656 Spline = Cast<USplineComponent>(
657 GravityActor->GetComponentByClass(USplineComponent::StaticClass()));
658 }
659 else
660 {
661 Spline = Cast<USplineComponent>(GetComponentByClass(USplineComponent::StaticClass()));
662 }
663
664 if (Spline != nullptr)
665 {
666 GravityDir = Spline->FindDirectionClosestToWorldLocation(
667 SceneComponent->GetComponentLocation(), ESplineCoordinateSpace::Type::World) *
668 ((GetFinalGravityScale(SceneComponent) > 0.0f) ? 1.0f : -1.0f);
669 }
670
671 break;
672 }
673
675 {
676 FVector GravityPoint = GravityVectorA;
677
678 if (IsValid(GravityActor))
679 {
680 GravityPoint = GravityActor->GetActorLocation();
681 }
682
683 GravityDir = GravityPoint - SceneComponent->GetComponentLocation();
684 if (!GravityDir.IsZero())
685 {
686 GravityDir = GravityDir.GetSafeNormal() *
687 ((GetFinalGravityScale(SceneComponent) > 0.0f) ? 1.0f : -1.0f);
688 }
689
690 break;
691 }
692
694 {
695 const FVector Point = SceneComponent->GetComponentLocation();
696 GravityDir = FMath::ClosestPointOnInfiniteLine(
698 if (!GravityDir.IsZero())
699 {
700 GravityDir = GravityDir.GetSafeNormal() *
701 ((GetFinalGravityScale(SceneComponent) > 0.0f) ? 1.0f : -1.0f);
702 }
703
704 break;
705 }
706
708 {
709 const FVector Point = SceneComponent->GetComponentLocation();
710 GravityDir = FMath::ClosestPointOnLine(GravityVectorA,
712 if (!GravityDir.IsZero())
713 {
714 GravityDir = GravityDir.GetSafeNormal() *
715 ((GetFinalGravityScale(SceneComponent) > 0.0f) ? 1.0f : -1.0f);
716 }
717
718 break;
719 }
720
722 {
723 USplineComponent* Spline = nullptr;
724 if (IsValid(GravityActor))
725 {
726 Spline = Cast<USplineComponent>(
727 GravityActor->GetComponentByClass(USplineComponent::StaticClass()));
728 }
729 else
730 {
731 Spline = Cast<USplineComponent>(GetComponentByClass(USplineComponent::StaticClass()));
732 }
733
734 if (Spline != nullptr)
735 {
736 const FVector Point = SceneComponent->GetComponentLocation();
737 const FVector GravityPoint = Spline->FindLocationClosestToWorldLocation(
738 Point, ESplineCoordinateSpace::Type::World);
739 GravityDir = GravityPoint - Point;
740 if (!GravityDir.IsZero())
741 {
742 GravityDir = GravityDir.GetSafeNormal() *
743 ((GetFinalGravityScale(SceneComponent) > 0.0f) ? 1.0f : -1.0f);
744 }
745 }
746
747 break;
748 }
749
751 {
752 const FVector Point = SceneComponent->GetComponentLocation();
753 GravityDir = FVector::PointPlaneProject(Point, GravityVectorA,
755 if (!GravityDir.IsZero())
756 {
757 GravityDir = GravityDir.GetSafeNormal() *
758 ((GetFinalGravityScale(SceneComponent) > 0.0f) ? 1.0f : -1.0f);
759 }
760
761 break;
762 }
763
765 {
766 USplineComponent* Spline = nullptr;
767 if (IsValid(GravityActor))
768 {
769 Spline = Cast<USplineComponent>(
770 GravityActor->GetComponentByClass(USplineComponent::StaticClass()));
771 }
772 else
773 {
774 Spline = Cast<USplineComponent>(GetComponentByClass(USplineComponent::StaticClass()));
775 }
776
777 if (Spline != nullptr)
778 {
779 const FVector Point = SceneComponent->GetComponentLocation();
780
781 const float InputKey = Spline->FindInputKeyClosestToWorldLocation(Point);
782 const FVector ClosestLocation = Spline->GetLocationAtSplineInputKey(
783 InputKey, ESplineCoordinateSpace::Type::World);
784 const FVector ClosestUpVector = Spline->GetUpVectorAtSplineInputKey(
785 InputKey, ESplineCoordinateSpace::Type::World);
786
787 GravityDir = FVector::PointPlaneProject(Point,
788 ClosestLocation, ClosestUpVector) - Point;
789 if (!GravityDir.IsZero())
790 {
791 GravityDir = GravityDir.GetSafeNormal() *
792 ((GetFinalGravityScale(SceneComponent) > 0.0f) ? 1.0f : -1.0f);
793 }
794 }
795
796 break;
797 }
798
800 {
801 FVector BoxOrigin = GravityVectorA;
802 FVector BoxExtent = GravityVectorB;
803
804 if (IsValid(GravityActor))
805 {
806 GravityActor->GetActorBounds(true, BoxOrigin, BoxExtent);
807 }
808
809 const FVector Point = SceneComponent->GetComponentLocation();
810 GravityDir = FBox(BoxOrigin - BoxExtent,
811 BoxOrigin + BoxExtent).GetClosestPointTo(Point) - Point;
812 if (!GravityDir.IsZero())
813 {
814 GravityDir = GravityDir.GetSafeNormal() *
815 ((GetFinalGravityScale(SceneComponent) > 0.0f) ? 1.0f : -1.0f);
816 }
817
818 break;
819 }
820
822 {
823 if (IsValid(GravityActor))
824 {
825 const FVector Point = SceneComponent->GetComponentLocation();
826
827 FVector ClosestPoint;
828 if (Cast<UPrimitiveComponent>(GravityActor->GetRootComponent())->GetClosestPointOnCollision(
829 Point, ClosestPoint) > 0.0f)
830 {
831 GravityDir = ClosestPoint - Point;
832 if (!GravityDir.IsZero())
833 {
834 GravityDir = GravityDir.GetSafeNormal() *
835 ((GetFinalGravityScale(SceneComponent) > 0.0f) ? 1.0f : -1.0f);
836 }
837 }
838 }
839 break;
840 }
841 }
842 return GravityDir;
843}

◆ GetGravityMagnitude()

float AGravityPhysicsVolume::GetGravityMagnitude ( class USceneComponent * SceneComponent) const
virtual

Obtains the absolute (positive) magnitude of gravity that influences a SceneComponent.

Parameters
SceneComponent- component in space affected by gravity
Returns
magnitude of gravity
846{
847 return FMath::Abs(GetGravityZ() * GetFinalGravityScale(SceneComponent));
848}

◆ GetGravityScale()

float AGravityPhysicsVolume::GetGravityScale ( ) const
virtual

Obtains the scale factor that affects magnitude of current gravity.

Returns
gravity scale factor
1156{
1157 return GravityScale;
1158}

◆ K2_SetFixedGravityDirection()

void AGravityPhysicsVolume::K2_SetFixedGravityDirection ( const FVector & NewGravityDirection)
virtual

Sets a new fixed gravity direction.

Note
It can be influenced by GravityScale
Parameters
NewGravityDirection- new gravity direction, assumes it isn't normalized
856{
857 SetFixedGravityDirection(NewGravityDirection.GetSafeNormal());
858}
virtual void SetFixedGravityDirection(const FVector &NewFixedGravityDirection)
Definition GravityPhysicsVolume.cpp:860

◆ K2_SetPlaneGravityDirection()

void AGravityPhysicsVolume::K2_SetPlaneGravityDirection ( const FVector & NewGravityPlaneBase,
const FVector & NewGravityPlaneNormal )
virtual

Sets a new infinite plane which gravity direction points to.

Note
It can be influenced by GravityScale
Parameters
NewGravityPlaneBase- a point that belongs to the plane
NewGravityPlaneNormal- normal of the plane, assumes it isn't normalized
1029{
1030 SetPlaneGravityDirection(NewGravityPlaneBase, NewGravityPlaneNormal.GetSafeNormal());
1031}
virtual void SetPlaneGravityDirection(const FVector &NewGravityPlaneBase, const FVector &NewGravityPlaneNormal)
Definition GravityPhysicsVolume.cpp:1033

◆ SetBoxGravityDirection()

void AGravityPhysicsVolume::SetBoxGravityDirection ( const FVector & NewGravityBoxOrigin,
const FVector & NewGravityBoxExtent )
virtual

Sets a new axis-aligned box which gravity direction points to.

Note
It can be influenced by GravityScale
Parameters
NewGravityBoxOrigin- origin of the box
NewGravityBoxExtent- extents of the box
1086{
1088 GravityVectorA == NewGravityBoxOrigin && GravityVectorB == NewGravityBoxExtent)
1089 {
1090 return;
1091 }
1092
1094 GravityVectorA = NewGravityBoxOrigin;
1095 GravityVectorB = NewGravityBoxExtent;
1096 GravityActor = nullptr;
1097
1098 // Change gravity settings of Characters
1100 {
1101 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
1102 {
1103 Character->GetALSGravityMovementInterface()->SetBoxGravityDirection(NewGravityBoxOrigin, NewGravityBoxExtent);
1104 }
1105 }
1106}

◆ SetBoxGravityDirectionFromActor()

void AGravityPhysicsVolume::SetBoxGravityDirectionFromActor ( AActor * NewGravityActor)
virtual

Sets a new axis-aligned box which gravity direction points to.

Note
It can be influenced by GravityScale
Parameters
NewGravityActor- Actor that provides its collision bounding box as gravity target
1109{
1111 GravityActor == NewGravityActor)
1112 {
1113 return;
1114 }
1115
1117 GravityActor = NewGravityActor;
1118
1119 // Change gravity settings of Characters
1121 {
1122 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
1123 {
1124 Character->GetALSGravityMovementInterface()->SetBoxGravityDirectionFromActor(NewGravityActor);
1125 }
1126 }
1127}

◆ SetCollisionGravityDirection()

void AGravityPhysicsVolume::SetCollisionGravityDirection ( AActor * NewGravityActor)
virtual

Sets a new collision geometry which gravity direction points to.

Note
It can be influenced by GravityScale
Parameters
NewGravityActor- Actor that owns the PrimitiveComponent that has collision geometry
1130{
1131 if (NewGravityActor == nullptr ||
1133 GravityActor == NewGravityActor))
1134 {
1135 return;
1136 }
1137
1138 const UPrimitiveComponent* Primitive = Cast<UPrimitiveComponent>(NewGravityActor->GetRootComponent());
1139 if (Primitive != nullptr)
1140 {
1142 GravityActor = NewGravityActor;
1143
1144 // Change gravity settings of Characters
1146 {
1147 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
1148 {
1149 Character->GetALSGravityMovementInterface()->SetCollisionGravityDirection(NewGravityActor);
1150 }
1151 }
1152 }
1153}

◆ SetFixedGravityDirection()

void AGravityPhysicsVolume::SetFixedGravityDirection ( const FVector & NewFixedGravityDirection)
virtual

Sets a new fixed gravity direction.

Note
It can be influenced by GravityScale
Parameters
NewFixedGravityDirection- new fixed gravity direction, assumes it is normalized
861{
862 if (NewFixedGravityDirection.IsZero() ||
864 GravityVectorA == NewFixedGravityDirection))
865 {
866 return;
867 }
868
870 GravityVectorA = NewFixedGravityDirection;
871
872 // Change gravity settings of Characters
874 {
875 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
876 {
877 Character->GetALSGravityMovementInterface()->SetFixedGravityDirection(NewFixedGravityDirection);
878 }
879 }
880}

◆ SetGravityScale()

void AGravityPhysicsVolume::SetGravityScale ( const float NewGravityScale)
virtual

Sets a new scale factor that affects magnitude of current gravity.

Parameters
NewGravityScale- new gravity scale factor
1161{
1162 GravityScale = NewGravityScale;
1163}

◆ SetLineGravityDirection()

void AGravityPhysicsVolume::SetLineGravityDirection ( const FVector & NewGravityLineStart,
const FVector & NewGravityLineEnd )
virtual

Sets a new infinite line which gravity direction points to.

Note
It can be influenced by GravityScale
Parameters
NewGravityLineStart- a point that belongs to the infinite line
NewGravityLineEnd- another point that belongs to the infinite line
954{
955 if (NewGravityLineStart == NewGravityLineEnd ||
957 GravityVectorA == NewGravityLineStart && GravityVectorB == NewGravityLineEnd))
958 {
959 return;
960 }
961
963 GravityVectorA = NewGravityLineStart;
964 GravityVectorB = NewGravityLineEnd;
965
966 // Change gravity settings of Characters
968 {
969 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
970 {
971 Character->GetALSGravityMovementInterface()->SetLineGravityDirection(NewGravityLineStart, NewGravityLineEnd);
972 }
973 }
974}

◆ SetPlaneGravityDirection()

void AGravityPhysicsVolume::SetPlaneGravityDirection ( const FVector & NewGravityPlaneBase,
const FVector & NewGravityPlaneNormal )
virtual

Sets a new infinite plane which gravity direction points to.

Note
It can be influenced by GravityScale
Parameters
NewGravityPlaneBase- a point that belongs to the plane
NewGravityPlaneNormal- normal of the plane, assumes it is normalized
1035{
1036 if (NewGravityPlaneNormal.IsZero() ||
1038 GravityVectorA == NewGravityPlaneBase && GravityVectorB == NewGravityPlaneNormal))
1039 {
1040 return;
1041 }
1042
1044 GravityVectorA = NewGravityPlaneBase;
1045 GravityVectorB = NewGravityPlaneNormal;
1046
1047 // Change gravity settings of Characters
1049 {
1050 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
1051 {
1052 Character->GetALSGravityMovementInterface()->SetPlaneGravityDirection(NewGravityPlaneBase, NewGravityPlaneNormal);
1053 }
1054 }
1055}

◆ SetPointGravityDirection()

void AGravityPhysicsVolume::SetPointGravityDirection ( const FVector & NewGravityPoint)
virtual

Sets a new point which gravity direction points to.

Note
It can be influenced by GravityScale
Parameters
NewGravityPoint- new point which gravity direction points to
910{
912 GravityVectorA == NewGravityPoint)
913 {
914 return;
915 }
916
918 GravityVectorA = NewGravityPoint;
919 GravityActor = nullptr;
920
921 // Change gravity settings of Characters
923 {
924 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
925 {
926 Character->GetALSGravityMovementInterface()->SetPointGravityDirection(NewGravityPoint);
927 }
928 }
929}

◆ SetPointGravityDirectionFromActor()

void AGravityPhysicsVolume::SetPointGravityDirectionFromActor ( AActor * NewGravityActor)
virtual

Sets a new point which gravity direction points to.

Note
It can be influenced by GravityScale
Parameters
NewGravityActor- Actor that provides its location as gravity point
932{
934 GravityActor == NewGravityActor)
935 {
936 return;
937 }
938
940 GravityActor = NewGravityActor;
941
942 // Change gravity settings of Characters
944 {
945 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
946 {
947 Character->GetALSGravityMovementInterface()->SetPointGravityDirectionFromActor(NewGravityActor);
948 }
949 }
950}

◆ SetSegmentGravityDirection()

void AGravityPhysicsVolume::SetSegmentGravityDirection ( const FVector & NewGravitySegmentStart,
const FVector & NewGravitySegmentEnd )
virtual

Sets a new segment line which gravity direction points to.

Note
It can be influenced by GravityScale
Parameters
NewGravitySegmentStart- start point of the segment line
NewGravitySegmentEnd- end point of the segment line
978{
979 if (NewGravitySegmentStart == NewGravitySegmentEnd ||
981 GravityVectorA == NewGravitySegmentStart && GravityVectorB == NewGravitySegmentEnd))
982 {
983 return;
984 }
985
987 GravityVectorA = NewGravitySegmentStart;
988 GravityVectorB = NewGravitySegmentEnd;
989
990 // Change gravity settings of Characters
992 {
993 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
994 {
995 Character->GetALSGravityMovementInterface()->SetSegmentGravityDirection(NewGravitySegmentStart, NewGravitySegmentEnd);
996 }
997 }
998}

◆ SetSplineGravityDirection()

void AGravityPhysicsVolume::SetSplineGravityDirection ( AActor * NewGravityActor)
virtual

Sets a new spline which gravity direction points to.

Note
It can be influenced by GravityScale
Parameters
NewGravityActor- Actor that provides a spline
1001{
1002 if (NewGravityActor == nullptr ||
1004 GravityActor == NewGravityActor))
1005 {
1006 return;
1007 }
1008
1009 const USplineComponent* Spline = Cast<USplineComponent>(
1010 NewGravityActor->GetComponentByClass(USplineComponent::StaticClass()));
1011 if (Spline != nullptr)
1012 {
1014 GravityActor = NewGravityActor;
1015
1016 // Change gravity settings of Characters
1018 {
1019 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
1020 {
1021 Character->GetALSGravityMovementInterface()->SetSplineGravityDirection(NewGravityActor);
1022 }
1023 }
1024 }
1025}

◆ SetSplinePlaneGravityDirection()

void AGravityPhysicsVolume::SetSplinePlaneGravityDirection ( AActor * NewGravityActor)
virtual

Sets a new infinite plane determined by closest spline point and spline up vector which gravity direction points to.

Note
It can be influenced by GravityScale
Parameters
NewGravityActor- Actor that provides a spline
1058{
1059 if (NewGravityActor == nullptr ||
1061 GravityActor == NewGravityActor))
1062 {
1063 return;
1064 }
1065
1066 const USplineComponent* Spline = Cast<USplineComponent>(
1067 NewGravityActor->GetComponentByClass(USplineComponent::StaticClass()));
1068 if (Spline != nullptr)
1069 {
1071 GravityActor = NewGravityActor;
1072
1073 // Change gravity settings of Characters
1075 {
1076 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
1077 {
1078 Character->GetALSGravityMovementInterface()->SetSplinePlaneGravityDirection(NewGravityActor);
1079 }
1080 }
1081 }
1082}

◆ SetSplineTangentGravityDirection()

void AGravityPhysicsVolume::SetSplineTangentGravityDirection ( AActor * NewGravityActor)
virtual

Sets a new gravity direction determined by closest spline tangent.

Note
It can be influenced by GravityScale
Parameters
NewGravityActor- Actor that provides a spline
883{
884 if (NewGravityActor == nullptr ||
886 GravityActor == NewGravityActor))
887 {
888 return;
889 }
890
891 const USplineComponent* Spline = Cast<USplineComponent>(
892 NewGravityActor->GetComponentByClass(USplineComponent::StaticClass()));
893 if (Spline != nullptr)
894 {
896 GravityActor = NewGravityActor;
897
898 // Change gravity settings of Characters
900 {
901 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
902 {
903 Character->GetALSGravityMovementInterface()->SetSplineTangentGravityDirection(NewGravityActor);
904 }
905 }
906 }
907}

◆ Tick()

void AGravityPhysicsVolume::Tick ( float DeltaTime)
overrideprotectedvirtual
114{
115 Super::Tick(DeltaTime);
116 for(const AActor* TrackedActor : TrackedActors)
117 {
118 if(IsValid(TrackedActor) && TrackedActor->GetRootComponent() != nullptr)
119 {
120 UPrimitiveComponent* Primitive = Cast<UPrimitiveComponent>(TrackedActor->GetRootComponent());
121 if(Primitive != nullptr && Primitive->IsGravityEnabled())
122 {
123 // Add force combination of reverse engine's gravity and custom gravity
124 const FVector GravityForce = FVector(0.0f, 0.0f, GetGravityZ() * -1.0f) + GetGravity(Primitive);
125
126 USkeletalMeshComponent* SkeletalMesh = Cast<USkeletalMeshComponent>(Primitive);
127 if(SkeletalMesh != nullptr)
128 {
129 SkeletalMesh->AddForceToAllBodiesBelow(GravityForce, NAME_None, true, true);
130 }
131 else
132 {
133 Primitive->AddForce(GravityForce, NAME_None, true);
134 }
135 }
136 }
137 }
138}
virtual FVector GetGravity(class USceneComponent *SceneComponent) const
Definition GravityPhysicsVolume.cpp:335

Member Data Documentation

◆ bDebuggingMode

bool AGravityPhysicsVolume::bDebuggingMode = false
protected

◆ CharacterFallVelocity

FVector AGravityPhysicsVolume::CharacterFallVelocity

Imparts this falling velocity to entering walking Ninjas.

◆ GravityActor

TObjectPtr<AActor> AGravityPhysicsVolume::GravityActor
protected

Optional Actor that determines direction of gravity.

◆ GravityDirectionMode

EGravityDirectionMode AGravityPhysicsVolume::GravityDirectionMode
protected

Mode that determines direction of gravity.

◆ GravityScale

float AGravityPhysicsVolume::GravityScale
protected

Gravity vector is multiplied by this amount.

◆ GravityVectorA

FVector AGravityPhysicsVolume::GravityVectorA
protected

Stores information that determines direction of gravity.

◆ GravityVectorB

FVector AGravityPhysicsVolume::GravityVectorB
protected

Stores additional information that determines direction of gravity.

◆ TrackedActors

TArray<TObjectPtr<AActor> > AGravityPhysicsVolume::TrackedActors
protected

List of tracked Actors that are affected by gravity settings.

◆ TrackedCharacters

TArray<class IALSCharacterInterface*> AGravityPhysicsVolume::TrackedCharacters
protected

List of tracked Characters that are affected by gravity settings.


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