Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Commit

Permalink
First pass on physics constraint management component
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-Pearson committed Nov 14, 2017
1 parent 1af70d8 commit 40e69c4
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ParkourGame/Source/ParkourGame/ParkourGameCharacter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.

#include "ParkourGameCharacter.h"

#include "Private/Physics/ConstraintManager.h"

// Engine
#include "HeadMountedDisplayFunctionLibrary.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
Expand Down Expand Up @@ -45,6 +49,8 @@ AParkourGameCharacter::AParkourGameCharacter()

// Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
// are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)

ConstraintManager = CreateDefaultSubobject<UConstraintManager>(TEXT("ConstraintManager"));
}

//////////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 3 additions & 0 deletions ParkourGame/Source/ParkourGame/ParkourGameCharacter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class AParkourGameCharacter : public ACharacter
/** Follow camera */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* FollowCamera;

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UConstraintManager* ConstraintManager;
public:
AParkourGameCharacter();

Expand Down
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 ParkourGame/Source/ParkourGame/Private/Physics/ConstraintManager.h
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;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "ParkourGameLogging.h"

DEFINE_LOG_CATEGORY(ParkourGame);
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);

0 comments on commit 40e69c4

Please sign in to comment.