Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions Source/MMT/Private/MMTPawn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ AMMTPawn::AMMTPawn()
SecondaryTick.TickGroup = TG_PostPhysics;
SecondaryTick.bCanEverTick = true;
SecondaryTick.bStartWithTickEnabled = true;

// Bind function delegate
OnCalculateCustomPhysics.BindUObject(this, &AMMTPawn::CustomPhysics);
}

// Called when the game starts or when spawned
Expand All @@ -27,6 +24,16 @@ void AMMTPawn::BeginPlay()
RootBodyInstance = PawnRootComponent->GetBodyInstance();
}

UWorld* World = GetWorld();
if (World)
{
FPhysScene* PScene = World->GetPhysicsScene();
if (PScene)
{
// Register physics step delegate
OnPhysSceneStepHandle = PScene->OnPhysSceneStep.AddUObject(this, &AMMTPawn::PhysSceneStep);
}
}
Super::BeginPlay();


Expand All @@ -42,16 +49,21 @@ void AMMTPawn::BeginPlay()

}

// Called every frame
void AMMTPawn::Tick(float DeltaTime)
// Called whenever this actor is being removed from a level
void AMMTPawn::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Super::Tick(DeltaTime);

// Add custom physics on Root's BodyInstance
if (RootBodyInstance != NULL) {
RootBodyInstance->AddCustomPhysics(OnCalculateCustomPhysics);
UWorld* World = GetWorld();
if (World)
{
FPhysScene* PScene = World->GetPhysicsScene();
if (PScene)
{
// Unregister physics step delegate
PScene->OnPhysSceneStep.Remove(OnPhysSceneStepHandle);
}
}

Super::EndPlay(EndPlayReason);
}

// Called every frame after physics update
Expand All @@ -78,8 +90,8 @@ void AMMTPawn::SetupPlayerInputComponent(class UInputComponent* PlayerInputCompo
Super::SetupPlayerInputComponent(PlayerInputComponent);
}

// Called by OnCalculateCustomPhysics delegate when physics update is initiated
void AMMTPawn::CustomPhysics(float DeltaTime, FBodyInstance* BodyInstance)
// Called by OnPhysSceneStepHandle delegate when physics update is initiated
void AMMTPawn::PhysSceneStep(FPhysScene* PhysScene, uint32 SceneType, float DeltaTime)
{
MMTPawnTransform = MMTGetTransformThisPawn();
MMTPhysicsTick(DeltaTime);
Expand Down
12 changes: 6 additions & 6 deletions Source/MMT/Public/MMTPawn.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class MMT_API AMMTPawn : public APawn
// Called when the game starts or when spawned
virtual void BeginPlay() override;

// Called every frame
virtual void Tick(float DeltaSeconds) override;
// Called whenever this actor is being removed from a level
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason);

// Called every frame after physics update
void TickPostPhysics(float DeltaSeconds, ELevelTick TickType, FSecondaryTickFunction& ThisTickFunction);

Expand All @@ -33,9 +33,9 @@ class MMT_API AMMTPawn : public APawn
UPROPERTY(BlueprintReadOnly)
FTransform MMTPawnTransform = FTransform::Identity;

// Delegate for running custom physics update
FCalculateCustomPhysics OnCalculateCustomPhysics;
void CustomPhysics(float DeltaTime, FBodyInstance* BodyInstance);
// Delegate for physics step
FDelegateHandle OnPhysSceneStepHandle;
void PhysSceneStep(FPhysScene* PhysScene, uint32 SceneType, float DeltaTime);

/* This event is called on every physics tick, including sub-steps. */
UFUNCTION(BlueprintNativeEvent, meta = (DisplayName = "MMT Physics Tick"), Category = "MMT physics sub-stepping")
Expand Down