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

#include <GravityActor.h>

Inheritance diagram for AGravityActor:
AGravityActorSpline

Public Member Functions

 AGravityActor ()
 
virtual void Tick (float DeltaTime) override
 
virtual void ActorEnteredVolume (AActor *SelfActor, AActor *Other)
 
void OnActorEnteredVolume (AActor *Other)
 
virtual void ActorLeavingVolume (AActor *SelfActor, AActor *Other)
 
void OnActorLeavingVolume (AActor *Other)
 
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
 
float GetGravityZ () const
 

Protected Attributes

TObjectPtr< UBoxComponent > BoxComponent
 
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

◆ AGravityActor()

AGravityActor::AGravityActor ( )
45{
46
47 PrimaryActorTick.bCanEverTick = true;
48 PrimaryActorTick.bStartWithTickEnabled = false;
49
50 BoxComponent = CreateDefaultSubobject<UBoxComponent>("Box Collision");
51 RootComponent = BoxComponent;
52 OnActorBeginOverlap.AddUniqueDynamic(this, &AGravityActor::ActorEnteredVolume);
53 OnActorEndOverlap.AddUniqueDynamic(this, &AGravityActor::ActorLeavingVolume);
54
55 GravityActor = nullptr;
57 GravityScale = 1.0f;
58 GravityVectorA = FVector(0.0f, 0.0f, -1.0f);
59 GravityVectorB = FVector::ZeroVector;
60 CharacterFallVelocity = FVector::ZeroVector;
61
62#if WITH_EDITOR
63 bDebuggingMode = true;
64#endif
65
66#if WITH_EDITORONLY_DATA
67 // Create and configure attached text render component
68 TextRenderComponent = CreateEditorOnlyDefaultSubobject<UTextRenderComponent>(TextRenderComponentName);
69 if (!IsRunningCommandlet() && TextRenderComponent != nullptr)
70 {
71 TextRenderComponent->bHiddenInGame = true;
72 TextRenderComponent->HorizontalAlignment = EHorizTextAligment::EHTA_Center;
73 TextRenderComponent->Text = FText::AsCultureInvariant(TEXT("Gravity Physics Volume"));
74 // TextRenderComponent->SetUsingAbsoluteRotation(true);
75 TextRenderComponent->SetupAttachment(BoxComponent);
76 }
77
78 PreviewIcon = CreateEditorOnlyDefaultSubobject<UBillboardComponent>(PreviewIconComponentName);
79 if (!IsRunningCommandlet() && PreviewIcon != nullptr)
80 {
81 PreviewIcon->bHiddenInGame = true;
82 // PreviewIcon->HorizontalAlignment = EHorizTextAligment::EHTA_Center;
83 // PreviewIcon->Text = FText::AsCultureInvariant(TEXT("Gravity Physics Volume"));
84 // PreviewIcon->SetUsingAbsoluteRotation(true);
85 PreviewIcon->SetupAttachment(BoxComponent);
86 }
87#endif // WITH_EDITORONLY_DATA
88}
EGravityDirectionMode GravityDirectionMode
Definition GravityActor.h:208
FVector GravityVectorA
Definition GravityActor.h:212
TObjectPtr< AActor > GravityActor
Definition GravityActor.h:220
virtual void ActorEnteredVolume(AActor *SelfActor, AActor *Other)
Definition GravityActor.cpp:150
virtual void ActorLeavingVolume(AActor *SelfActor, AActor *Other)
Definition GravityActor.cpp:307
bool bDebuggingMode
Definition GravityActor.h:193
TObjectPtr< UBoxComponent > BoxComponent
Definition GravityActor.h:187
FVector GravityVectorB
Definition GravityActor.h:216
float GravityScale
Definition GravityActor.h:224
FVector CharacterFallVelocity
Definition GravityActor.h:159

Member Function Documentation

◆ ActorEnteredVolume()

void AGravityActor::ActorEnteredVolume ( AActor * SelfActor,
AActor * Other )
virtual

Called when an Actor enters this volume.

Parameters
Other- Actor that entered this volume
151{
152 UE_LOG(LogTemp, Error, TEXT("Entered Actor"));
153 // if(Other != nullptr)
154 // {
155 // UE_LOG(LogTemp, Error, TEXT("Actor: %s Entered Gravity Actor"), *Other->GetHumanReadableName());
156 // }
157 // Remove all empty positions of the TrackedActors list
158 TrackedActors.RemoveAll([](const AActor* Actor)
159 {
160 return Actor == nullptr;
161 });
162
163 // Remove all empty positions of the TrackedCharacters list
165 {
166 return Character == nullptr;
167 });
168
169 if (IsValid(Other))
170 {
171 IALSCharacterInterface* Character = Cast<IALSCharacterInterface>(Other);
172 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
173 {
174 IALSGravityMovementInterface* ALSGravityMovement = Character->GetALSGravityMovementInterface();
175 if(ALSGravityMovement == nullptr){return;}
176 // Just change gravity settings of Characters
177 switch (GravityDirectionMode)
178 {
180 {
181 ALSGravityMovement->SetFixedGravityDirection(GravityVectorA);
182 break;
183 }
184
186 {
187 if(IsValid(GravityActor))
188 {
190 }
191 else
192 {
193 ALSGravityMovement->SetSplineTangentGravityDirection(this);
194 }
195 break;
196 }
197
199 {
200 if (IsValid(GravityActor))
201 {
203 }
204 else
205 {
206 ALSGravityMovement->SetPointGravityDirection(GravityVectorA);
207 }
208
209 break;
210 }
211
213 {
215 break;
216 }
217
219 {
221 break;
222 }
223
225 {
226 if (IsValid(GravityActor))
227 {
228 ALSGravityMovement->SetSplineGravityDirection(GravityActor);
229 }
230 else
231 {
232 ALSGravityMovement->SetSplineGravityDirection(this);
233 }
234
235 break;
236 }
237
239 {
241 break;
242 }
243
245 {
246 if (IsValid(GravityActor))
247 {
248 ALSGravityMovement->SetSplinePlaneGravityDirection(GravityActor);
249 }
250 else
251 {
252 ALSGravityMovement->SetSplinePlaneGravityDirection(this);
253 }
254
255 break;
256 }
257
259 {
260 if (IsValid(GravityActor))
261 {
263 }
264 else
265 {
267 }
268
269 break;
270 }
271
273 default:
274 {
275 if (IsValid(GravityActor))
276 {
277 ALSGravityMovement->SetCollisionGravityDirection(GravityActor);
278 }
279
280 break;
281 }
282
283 }
284
285 // Launch walking Character if configured
286 if(!CharacterFallVelocity.IsZero() && ALSGravityMovement->GetIsWalking())
287 {
288 ALSGravityMovement->LaunchCharacter(CharacterFallVelocity);
289 }
290
291 TrackedCharacters.AddUnique(Character);
292 }
293 else
294 {
295 // Make sure the received Actor is valid and has physics
296 const UPrimitiveComponent* Primitive = Cast<UPrimitiveComponent>(Other->GetRootComponent());
297 if (Primitive != nullptr && Primitive->IsAnySimulatingPhysics())
298 {
299 TrackedActors.AddUnique(Other);
300 }
301 }
302 }
303 SetActorTickEnabled(TrackedActors.Num() > 0);
305}
TArray< class IALSCharacterInterface * > TrackedCharacters
Definition GravityActor.h:200
TArray< TObjectPtr< AActor > > TrackedActors
Definition GravityActor.h:196
void OnActorEnteredVolume(AActor *Other)
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 AGravityActor::ActorLeavingVolume ( AActor * SelfActor,
AActor * Other )
virtual

Called when an Actor leaves this volume.

Parameters
Other- Actor that left this volume, can be nullptr
308{
309 UE_LOG(LogTemp, Error, TEXT("Leave Actor"));
310 // if(Other != nullptr)
311 // {
312 // UE_LOG(LogTemp, Warning, TEXT("Actor: %s Left Gravity Actor"), *Other->GetHumanReadableName());
313 // }
314
315 // Remove the received Actor from the TrackedActors list
316 if(Other != nullptr)
317 {
318 IALSCharacterInterface* Character = Cast<IALSCharacterInterface>(Other);
319 if(Character != nullptr)
320 {
322 }
323 else
324 {
325 TrackedActors.Remove(Other);
326 }
327 }
328 // Remove all empty positions of the TrackedActors list
329 TrackedActors.RemoveAll([](const AActor* Actor)
330 {
331 return Actor == nullptr;
332 });
333
334 // Remove all empty positions of the TrackedNinjas list
336 {
337 return Character == nullptr;
338 });
339
340 SetActorTickEnabled(TrackedActors.Num() > 0);
342}
void OnActorLeavingVolume(AActor *Other)

◆ BeginPlay()

void AGravityActor::BeginPlay ( )
overrideprotectedvirtual

Reimplemented in AGravityActorSpline.

91{
92 Super::BeginPlay();
93 const TArray<AActor*> ActorsToIgnore;
94 TArray<FHitResult> HitResults;
95 TArray<AActor*> OverlappingActors;
96
97 if(UKismetSystemLibrary::BoxTraceMulti(this, GetActorLocation(), GetActorLocation(), BoxComponent->GetScaledBoxExtent(),
98 FRotator::ZeroRotator, UEngineTypes::ConvertToTraceType(ECC_EngineTraceChannel1), false, ActorsToIgnore,
100 {
101 for(auto& HitResult : HitResults)
102 {
103 if(HitResult.GetActor() != nullptr)
104 {
105 OverlappingActors.AddUnique(HitResult.GetActor());
106 }
107 }
108 }
109 for(const auto OverlappingActor : OverlappingActors)
110 {
111 ActorEnteredVolume(this, OverlappingActor);
112 }
113
114
115}
static EDrawDebugTrace::Type GetDrawDebugType(const bool bDebuggingMode)
Definition BaseHelpersBPLib.cpp:910

◆ GetFinalGravityScale()

float AGravityActor::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
860{
861 return GetGravityScale();
862}
virtual float GetGravityScale() const
Definition GravityActor.cpp:1164

◆ GetGravity()

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

◆ GetGravityDirection()

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

Obtains the normalized direction of gravity that infl uences a SceneComponent.

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

◆ GetGravityMagnitude()

float AGravityActor::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
855{
856 return FMath::Abs(GetGravityZ() * GetFinalGravityScale(SceneComponent));
857}

◆ GetGravityScale()

float AGravityActor::GetGravityScale ( ) const
virtual

Obtains the scale factor that affects magnitude of current gravity.

Returns
gravity scale factor
1165{
1166 return GravityScale;
1167}

◆ GetGravityZ()

float AGravityActor::GetGravityZ ( ) const
protected
118{
119 const UWorld* MyWorld = GetWorld();
120 return MyWorld ? MyWorld->GetGravityZ() : UPhysicsSettings::Get()->DefaultGravityZ;
121}

◆ K2_SetFixedGravityDirection()

void AGravityActor::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
865{
866 SetFixedGravityDirection(NewGravityDirection.GetSafeNormal());
867}
virtual void SetFixedGravityDirection(const FVector &NewFixedGravityDirection)
Definition GravityActor.cpp:869

◆ K2_SetPlaneGravityDirection()

void AGravityActor::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
1038{
1039 SetPlaneGravityDirection(NewGravityPlaneBase, NewGravityPlaneNormal.GetSafeNormal());
1040}
virtual void SetPlaneGravityDirection(const FVector &NewGravityPlaneBase, const FVector &NewGravityPlaneNormal)
Definition GravityActor.cpp:1042

◆ OnActorEnteredVolume()

void AGravityActor::OnActorEnteredVolume ( AActor * Other)

◆ OnActorLeavingVolume()

void AGravityActor::OnActorLeavingVolume ( AActor * Other)

◆ SetBoxGravityDirection()

void AGravityActor::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
1095{
1097 GravityVectorA == NewGravityBoxOrigin && GravityVectorB == NewGravityBoxExtent)
1098 {
1099 return;
1100 }
1101
1103 GravityVectorA = NewGravityBoxOrigin;
1104 GravityVectorB = NewGravityBoxExtent;
1105 GravityActor = nullptr;
1106
1107 // Change gravity settings of Characters
1109 {
1110 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
1111 {
1112 Character->GetALSGravityMovementInterface()->SetBoxGravityDirection(NewGravityBoxOrigin, NewGravityBoxExtent);
1113 }
1114 }
1115}

◆ SetBoxGravityDirectionFromActor()

void AGravityActor::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
1118{
1120 GravityActor == NewGravityActor)
1121 {
1122 return;
1123 }
1124
1126 GravityActor = NewGravityActor;
1127
1128 // Change gravity settings of Characters
1130 {
1131 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
1132 {
1133 Character->GetALSGravityMovementInterface()->SetBoxGravityDirectionFromActor(NewGravityActor);
1134 }
1135 }
1136}

◆ SetCollisionGravityDirection()

void AGravityActor::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
1139{
1140 if (NewGravityActor == nullptr ||
1142 GravityActor == NewGravityActor))
1143 {
1144 return;
1145 }
1146
1147 const UPrimitiveComponent* Primitive = Cast<UPrimitiveComponent>(NewGravityActor->GetRootComponent());
1148 if (Primitive != nullptr)
1149 {
1151 GravityActor = NewGravityActor;
1152
1153 // Change gravity settings of Characters
1155 {
1156 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
1157 {
1158 Character->GetALSGravityMovementInterface()->SetCollisionGravityDirection(NewGravityActor);
1159 }
1160 }
1161 }
1162}

◆ SetFixedGravityDirection()

void AGravityActor::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
870{
871 if (NewFixedGravityDirection.IsZero() ||
873 GravityVectorA == NewFixedGravityDirection))
874 {
875 return;
876 }
877
879 GravityVectorA = NewFixedGravityDirection;
880
881 // Change gravity settings of Characters
883 {
884 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
885 {
886 Character->GetALSGravityMovementInterface()->SetFixedGravityDirection(NewFixedGravityDirection);
887 }
888 }
889}

◆ SetGravityScale()

void AGravityActor::SetGravityScale ( const float NewGravityScale)
virtual

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

Parameters
NewGravityScale- new gravity scale factor
1170{
1171 GravityScale = NewGravityScale;
1172}

◆ SetLineGravityDirection()

void AGravityActor::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
963{
964 if (NewGravityLineStart == NewGravityLineEnd ||
966 GravityVectorA == NewGravityLineStart && GravityVectorB == NewGravityLineEnd))
967 {
968 return;
969 }
970
972 GravityVectorA = NewGravityLineStart;
973 GravityVectorB = NewGravityLineEnd;
974
975 // Change gravity settings of Characters
977 {
978 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
979 {
980 Character->GetALSGravityMovementInterface()->SetLineGravityDirection(NewGravityLineStart, NewGravityLineEnd);
981 }
982 }
983}

◆ SetPlaneGravityDirection()

void AGravityActor::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
1044{
1045 if (NewGravityPlaneNormal.IsZero() ||
1047 GravityVectorA == NewGravityPlaneBase && GravityVectorB == NewGravityPlaneNormal))
1048 {
1049 return;
1050 }
1051
1053 GravityVectorA = NewGravityPlaneBase;
1054 GravityVectorB = NewGravityPlaneNormal;
1055
1056 // Change gravity settings of Characters
1058 {
1059 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
1060 {
1061 Character->GetALSGravityMovementInterface()->SetPlaneGravityDirection(NewGravityPlaneBase, NewGravityPlaneNormal);
1062 }
1063 }
1064}

◆ SetPointGravityDirection()

void AGravityActor::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
919{
921 GravityVectorA == NewGravityPoint)
922 {
923 return;
924 }
925
927 GravityVectorA = NewGravityPoint;
928 GravityActor = nullptr;
929
930 // Change gravity settings of Characters
932 {
933 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
934 {
935 Character->GetALSGravityMovementInterface()->SetPointGravityDirection(NewGravityPoint);
936 }
937 }
938}

◆ SetPointGravityDirectionFromActor()

void AGravityActor::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
941{
943 GravityActor == NewGravityActor)
944 {
945 return;
946 }
947
949 GravityActor = NewGravityActor;
950
951 // Change gravity settings of Characters
953 {
954 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
955 {
956 Character->GetALSGravityMovementInterface()->SetPointGravityDirectionFromActor(NewGravityActor);
957 }
958 }
959}

◆ SetSegmentGravityDirection()

void AGravityActor::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
987{
988 if (NewGravitySegmentStart == NewGravitySegmentEnd ||
990 GravityVectorA == NewGravitySegmentStart && GravityVectorB == NewGravitySegmentEnd))
991 {
992 return;
993 }
994
996 GravityVectorA = NewGravitySegmentStart;
997 GravityVectorB = NewGravitySegmentEnd;
998
999 // Change gravity settings of Characters
1001 {
1002 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
1003 {
1004 Character->GetALSGravityMovementInterface()->SetSegmentGravityDirection(NewGravitySegmentStart, NewGravitySegmentEnd);
1005 }
1006 }
1007}

◆ SetSplineGravityDirection()

void AGravityActor::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
1010{
1011 if (NewGravityActor == nullptr ||
1013 GravityActor == NewGravityActor))
1014 {
1015 return;
1016 }
1017
1018 const USplineComponent* Spline = Cast<USplineComponent>(
1019 NewGravityActor->GetComponentByClass(USplineComponent::StaticClass()));
1020 if (Spline != nullptr)
1021 {
1023 GravityActor = NewGravityActor;
1024
1025 // Change gravity settings of Characters
1027 {
1028 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
1029 {
1030 Character->GetALSGravityMovementInterface()->SetSplineGravityDirection(NewGravityActor);
1031 }
1032 }
1033 }
1034}

◆ SetSplinePlaneGravityDirection()

void AGravityActor::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
1067{
1068 if (NewGravityActor == nullptr ||
1070 GravityActor == NewGravityActor))
1071 {
1072 return;
1073 }
1074
1075 const USplineComponent* Spline = Cast<USplineComponent>(
1076 NewGravityActor->GetComponentByClass(USplineComponent::StaticClass()));
1077 if (Spline != nullptr)
1078 {
1080 GravityActor = NewGravityActor;
1081
1082 // Change gravity settings of Characters
1084 {
1085 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
1086 {
1087 Character->GetALSGravityMovementInterface()->SetSplinePlaneGravityDirection(NewGravityActor);
1088 }
1089 }
1090 }
1091}

◆ SetSplineTangentGravityDirection()

void AGravityActor::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
892{
893 if (NewGravityActor == nullptr ||
895 GravityActor == NewGravityActor))
896 {
897 return;
898 }
899
900 const USplineComponent* Spline = Cast<USplineComponent>(
901 NewGravityActor->GetComponentByClass(USplineComponent::StaticClass()));
902 if (Spline != nullptr)
903 {
905 GravityActor = NewGravityActor;
906
907 // Change gravity settings of Characters
909 {
910 if(Character != nullptr && Character->GetALSGravityMovementInterface() != nullptr)
911 {
912 Character->GetALSGravityMovementInterface()->SetSplineTangentGravityDirection(NewGravityActor);
913 }
914 }
915 }
916}

◆ Tick()

void AGravityActor::Tick ( float DeltaTime)
overridevirtual

Reimplemented in AGravityActorSpline.

124{
125 Super::Tick(DeltaTime);
126 for(const AActor* TrackedActor : TrackedActors)
127 {
128 if(IsValid(TrackedActor) && TrackedActor->GetRootComponent() != nullptr)
129 {
130 UPrimitiveComponent* Primitive = Cast<UPrimitiveComponent>(TrackedActor->GetRootComponent());
131 if(Primitive != nullptr && Primitive->IsGravityEnabled())
132 {
133 // Add force combination of reverse engine's gravity and custom gravity
134 const FVector GravityForce = FVector(0.0f, 0.0f, GetGravityZ() * -1.0f) + GetGravity(Primitive);
135
136 USkeletalMeshComponent* SkeletalMesh = Cast<USkeletalMeshComponent>(Primitive);
137 if(SkeletalMesh != nullptr)
138 {
139 SkeletalMesh->AddForceToAllBodiesBelow(GravityForce, NAME_None, true, true);
140 }
141 else
142 {
143 Primitive->AddForce(GravityForce, NAME_None, true);
144 }
145 }
146 }
147 }
148}
virtual FVector GetGravity(class USceneComponent *SceneComponent) const
Definition GravityActor.cpp:344

Member Data Documentation

◆ bDebuggingMode

bool AGravityActor::bDebuggingMode = false
protected

◆ BoxComponent

TObjectPtr<UBoxComponent> AGravityActor::BoxComponent
protected

◆ CharacterFallVelocity

FVector AGravityActor::CharacterFallVelocity

Imparts this falling velocity to entering walking Character.

◆ GravityActor

TObjectPtr<AActor> AGravityActor::GravityActor
protected

Optional Actor that determines direction of gravity.

◆ GravityDirectionMode

EGravityDirectionMode AGravityActor::GravityDirectionMode
protected

Mode that determines direction of gravity.

◆ GravityScale

float AGravityActor::GravityScale
protected

Gravity vector is multiplied by this amount.

◆ GravityVectorA

FVector AGravityActor::GravityVectorA
protected

Stores information that determines direction of gravity.

◆ GravityVectorB

FVector AGravityActor::GravityVectorB
protected

Stores additional information that determines direction of gravity.

◆ TrackedActors

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

List of tracked Actors that are affected by gravity settings.

◆ TrackedCharacters

TArray<class IALSCharacterInterface*> AGravityActor::TrackedCharacters
protected

List of tracked Characters that are affected by gravity settings.


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