Created
February 11, 2020 17:17
-
-
Save kanzwataru/145906206f1089c02f8dbea327962f86 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #include "LegConstraintComponent.h" | |
| ULegConstraintComponent::ULegConstraintComponent() { | |
| PrimaryComponentTick.bCanEverTick = true; | |
| } | |
| void ULegConstraintComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction * ThisTickFunction) | |
| { | |
| Super::TickComponent(DeltaTime, TickType, ThisTickFunction); | |
| if (!Pelvis || !LeftLeg || !RightLeg || !LeftKnee || !RightKnee) | |
| return; | |
| const FVector LeftKneePos = LeftKnee->GetActorLocation(); | |
| const FVector RightKneePos = RightKnee->GetActorLocation(); | |
| //UE_LOG(LogTemp, Warning, TEXT("%f %f %f"), LeftKneePos.X, LeftKneePos.Y, LeftKneePos.Z); | |
| const FVector KneeMid = FMath::Lerp(LeftKneePos, RightKneePos, 0.5f); | |
| const float KneeDist = FVector::Dist(LeftKneePos, RightKneePos); | |
| //UE_LOG(LogTemp, Warning, TEXT("%f"), KneeDist); | |
| for (int i = 0; i < Number; ++i) { | |
| const float Degree = (float(i) / Number) * 360; | |
| const float Radian = FMath::DegreesToRadians(Degree); | |
| const FVector RelativePos = { | |
| FMath::Sin(Radian) * KneeDist, | |
| FMath::Cos(Radian) * KneeDist, | |
| 0.0f | |
| }; | |
| FVector Pos = KneeMid + RelativePos; | |
| const float LeftDist = FVector::Dist(Pos, LeftKneePos); | |
| const float RightDist = FVector::Dist(Pos, RightKneePos); | |
| const float ClosestBlend = LeftDist / RightDist; | |
| Pos.Z = FMath::Lerp(LeftKneePos.Z, RightKneePos.Z, ClosestBlend); | |
| const FTransform Transform = FTransform(FQuat::Identity, Pos, FVector(0.5f)); | |
| UpdateInstanceTransform(i, Transform, true); | |
| } | |
| MarkRenderStateDirty(); | |
| } | |
| void ULegConstraintComponent::BeginPlay() | |
| { | |
| Super::BeginPlay(); | |
| for (int i = 0; i < Number; ++i) { | |
| AddInstanceWorldSpace(FTransform(FQuat::Identity, FVector(0, 0, 0), FVector(0.5f))); | |
| } | |
| } |
This file contains hidden or 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
| #pragma once | |
| #include "CoreMinimal.h" | |
| #include "Components/InstancedStaticMeshComponent.h" | |
| #include "LegConstraintComponent.generated.h" | |
| /** | |
| * | |
| */ | |
| UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent)) | |
| class TESTBENCH_API ULegConstraintComponent : public UInstancedStaticMeshComponent | |
| { | |
| GENERATED_BODY() | |
| protected: | |
| virtual void BeginPlay() override; | |
| public: | |
| UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LegConstraint") | |
| int Number = 16; | |
| UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LegConstraint") | |
| AActor *Pelvis; | |
| UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LegConstraint") | |
| AActor *LeftLeg; | |
| UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LegConstraint") | |
| AActor *RightLeg; | |
| UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LegConstraint") | |
| AActor *LeftKnee; | |
| UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LegConstraint") | |
| AActor *RightKnee; | |
| ULegConstraintComponent(); | |
| virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment