This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass on physics constraint management component
- Loading branch information
1 parent
1af70d8
commit 40e69c4
Showing
6 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
ParkourGame/Source/ParkourGame/Private/Physics/ConstraintManager.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include "ConstraintManager.h" | ||
|
||
#include "PhysicsEngine/PhysicsConstraintActor.h" | ||
#include "PhysicsEngine/PhysicsConstraintComponent.h" | ||
|
||
|
||
// Sets default values for this component's properties | ||
UConstraintManager::UConstraintManager() | ||
{ | ||
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features | ||
// off to improve performance if you don't need them. | ||
PrimaryComponentTick.bCanEverTick = false; | ||
} | ||
|
||
|
||
// Called when the game starts | ||
void UConstraintManager::BeginPlay() | ||
{ | ||
Super::BeginPlay(); | ||
|
||
|
||
} | ||
|
||
FGuid UConstraintManager::CreateConstraint(const FName& Bone, UPrimitiveComponent* OtherComp, const FName& OtherBone /*= NAME_None*/) | ||
{ | ||
UWorld* WorldPtr = GetWorld(); | ||
|
||
if (!OtherComp || Bone == NAME_None || !WorldPtr) | ||
return FGuid(); | ||
|
||
// Create constraint comp | ||
USkeletalMeshComponent* SkelMeshComp = GetSkeletalMeshComp(); | ||
UPhysicsConstraintComponent* NewConstraintComp = NewObject<UPhysicsConstraintComponent>(OtherComp->GetOwner()); | ||
|
||
if (!ensure(NewConstraintComp) || !ensure(SkelMeshComp)) | ||
return FGuid(); | ||
|
||
// Setup the constraint actor | ||
FConstraintInstance ConstraintInstance; | ||
NewConstraintComp->ConstraintInstance = ConstraintInstance; | ||
|
||
NewConstraintComp->SetWorldLocation(OtherComp->GetOwner()->GetActorLocation()); | ||
NewConstraintComp->AttachToComponent (OtherComp, FAttachmentTransformRules::KeepWorldTransform, OtherBone); | ||
NewConstraintComp->SetConstrainedComponents(OtherComp, OtherBone, SkelMeshComp, Bone); | ||
|
||
// @TODO : Bind to constraint broken event and handle that case | ||
|
||
// Add to our list of managed constraints | ||
FManagedPhysicsConstraint& NewConstraint = m_ActiveConstraints[m_ActiveConstraints.AddDefaulted()]; | ||
|
||
NewConstraint.ConstraintID.NewGuid(); | ||
NewConstraint.ConstraintComp = NewConstraintComp; | ||
NewConstraint.TargetBone = Bone; | ||
NewConstraint.OtherComp = OtherComp; | ||
NewConstraint.OtherBone = OtherBone; | ||
|
||
return NewConstraint.ConstraintID; | ||
} | ||
|
||
void UConstraintManager::DestroyConstraint(const FGuid& Constraint) | ||
{ | ||
int32 RemoveIndex = m_ActiveConstraints.FindLastByPredicate([&](const FManagedPhysicsConstraint& QueryConstraint) { | ||
return QueryConstraint.ConstraintID == Constraint; | ||
}); | ||
|
||
if (RemoveIndex == INDEX_NONE) | ||
return; | ||
|
||
UPhysicsConstraintComponent* ConstraintCompPtr = m_ActiveConstraints[RemoveIndex].ConstraintComp.Get(); | ||
|
||
if (ConstraintCompPtr) | ||
{ | ||
// @TODO Unbind from constrain broken event | ||
ConstraintCompPtr->BreakConstraint(); | ||
ConstraintCompPtr->DestroyComponent(); | ||
} | ||
|
||
m_ActiveConstraints.RemoveAtSwap(RemoveIndex); | ||
} | ||
|
||
void UConstraintManager::GetConstraintByTargetBone(const FName& Bone, TArray<FGuid>& outConstraints) const | ||
{ | ||
GetConstraintsByPredicate([&](const FManagedPhysicsConstraint& Constraint) { | ||
return Constraint.TargetBone == Bone; | ||
}, outConstraints); | ||
} | ||
|
||
USkeletalMeshComponent* UConstraintManager::GetSkeletalMeshComp() | ||
{ | ||
USkeletalMeshComponent* CompPtr = m_SkeletalMeshComp.Get(); | ||
|
||
if (CompPtr) return CompPtr; | ||
|
||
return Cast<USkeletalMeshComponent>(GetOwner()->GetComponentByClass(USkeletalMeshComponent::StaticClass())); | ||
} |
67 changes: 67 additions & 0 deletions
67
ParkourGame/Source/ParkourGame/Private/Physics/ConstraintManager.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#pragma once | ||
|
||
#include "CoreMinimal.h" | ||
#include "Components/ActorComponent.h" | ||
#include "ConstraintManager.generated.h" | ||
|
||
|
||
// @TODO: have a lookup table of constraint profile data we can reference for different situations | ||
|
||
//typedef FGuid FConstraintID; | ||
|
||
class UPhysicsConstraintComponent; | ||
|
||
struct FManagedPhysicsConstraint | ||
{ | ||
public: | ||
FGuid ConstraintID; | ||
|
||
TWeakObjectPtr<UPhysicsConstraintComponent> ConstraintComp; | ||
|
||
FName TargetBone = NAME_None; | ||
|
||
TWeakObjectPtr<USceneComponent> OtherComp; | ||
FName OtherBone = NAME_None; | ||
}; | ||
|
||
|
||
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) | ||
class UConstraintManager : public UActorComponent | ||
{ | ||
GENERATED_BODY() | ||
|
||
public: | ||
// Sets default values for this component's properties | ||
UConstraintManager(); | ||
|
||
// Called when the game starts | ||
virtual void BeginPlay() override; | ||
|
||
public: | ||
|
||
UFUNCTION(BlueprintCallable, Category = "ConstraintManager", meta = (AutoCreateRefTerm="Bone,OtherBone")) | ||
FGuid CreateConstraint(const FName& Bone, UPrimitiveComponent* OtherComp, const FName& OtherBone = NAME_None); | ||
|
||
UFUNCTION(BlueprintCallable, Category = "ConstraintManager") | ||
void DestroyConstraint(const FGuid& Constraint); | ||
|
||
template<typename Func> | ||
void GetConstraintsByPredicate(Func Predicate, TArray<FGuid>& outConstraints) const | ||
{ | ||
for (const FManagedPhysicsConstraint& Constraint : m_ActiveConstraints) | ||
{ | ||
if (Predicate(Constraint)) | ||
outConstraints.Add(Constraint.ConstraintID); | ||
} | ||
} | ||
|
||
UFUNCTION(BlueprintCallable, Category = "ConstraintManager", meta = (AutoCreateRefTerm="Bone")) | ||
void GetConstraintByTargetBone(const FName& Bone, TArray<FGuid>& outConstraints) const; | ||
|
||
private: | ||
|
||
USkeletalMeshComponent* GetSkeletalMeshComp(); | ||
|
||
TWeakObjectPtr<USkeletalMeshComponent> m_SkeletalMeshComp; | ||
TArray<FManagedPhysicsConstraint> m_ActiveConstraints; | ||
}; |
3 changes: 3 additions & 0 deletions
3
ParkourGame/Source/ParkourGame/Private/Utils/ParkourGameLogging.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#include "ParkourGameLogging.h" | ||
|
||
DEFINE_LOG_CATEGORY(ParkourGame); |
5 changes: 5 additions & 0 deletions
5
ParkourGame/Source/ParkourGame/Public/Utils/ParkourGameLogging.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include "CoreMinimal.h" | ||
|
||
DECLARE_LOG_CATEGORY_EXTERN(ParkourGame, Log, All); |