A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
GS_LerpToHand.cpp
Go to the documentation of this file.
1// Fill out your copyright notice in the Description page of Project Settings.
2
3
6#include "Math/DualQuat.h"
7
8UGS_LerpToHand::UGS_LerpToHand(const FObjectInitializer& ObjectInitializer) :
9 Super(ObjectInitializer)
10{
11 bIsActive = false;
12 bDenyAutoDrop = true; // Always deny auto dropping while this script is active
14
16 LerpDuration = 1.f;
17 LerpSpeed = 0.0f;
18 CurrentLerpTime = 0.0f;
19 OnGripTransform = FTransform::Identity;
20 bUseCurve = false;
21 MinDistanceForLerp = 0.0f;
22 MinSpeedForLerp = 0.f;
23 MaxSpeedForLerp = 0.f;
25}
26
27//void UGS_InteractibleSettings::BeginPlay_Implementation() {}
29{
30 const UVRGlobalSettings& VRSettings = *GetDefault<UVRGlobalSettings>();
31
32 // Removed this, let per object scripts overide
33 // Dont run if the global lerping is enabled
34 /*if (VRSettings.bUseGlobalLerpToHand)
35 {
36 bIsActive = false;
37 return;
38 }*/
39
40 TargetGrip = GripInformation.GripID;
41
42 OnGripTransform = GetParentTransform(true, GripInformation.GrippedBoneName);
43 UObject* ParentObj = this->GetParent();
44
45 FTransform TargetTransform = GripInformation.RelativeTransform * GrippingController->GetPivotTransform();
46 float Distance = FVector::Dist(OnGripTransform.GetLocation(), TargetTransform.GetLocation());
47 if (MinDistanceForLerp > 0.0f && Distance < MinDistanceForLerp)
48 {
49 // Don't init
50 OnLerpToHandFinished.Broadcast();
51 return;
52 }
53 else
54 {
55 float LerpScaler = 1.0f;
56 float DistanceToSpeed = Distance / LerpDuration;
57 if (DistanceToSpeed < MinSpeedForLerp)
58 {
59 LerpScaler = MinSpeedForLerp / DistanceToSpeed;
60 }
61 else if (MaxSpeedForLerp > 0.f && DistanceToSpeed > MaxSpeedForLerp)
62 {
63 LerpScaler = MaxSpeedForLerp / DistanceToSpeed;
64 }
65 else
66 {
67 LerpScaler = 1.0f;
68 }
69
70 // Get the modified lerp speed
71 LerpSpeed = ((1.f / LerpDuration) * LerpScaler);
72
73 OnLerpToHandBegin.Broadcast();
74
75 if (FBPActorGripInformation* GripInfo = GrippingController->GetGripPtrByID(GripInformation.GripID))
76 {
77 GripInfo->bIsLerping = true;
78 }
79 }
80
81
82
83 bIsActive = true;
84 CurrentLerpTime = 0.0f;
85}
86
87void UGS_LerpToHand::OnGripRelease_Implementation(UGripMotionControllerComponent * ReleasingController, const FBPActorGripInformation & GripInformation, bool bWasSocketed)
88{
89 if(GripInformation.GripID == TargetGrip)
90 {
92 bIsActive = false;
93 }
94}
95
97(
98 UGripMotionControllerComponent* GrippingController,
99 float DeltaTime, FTransform & WorldTransform,
100 const FTransform &ParentTransform,
102 AActor * actor,
103 UPrimitiveComponent * root,
104 bool bRootHasInterface,
105 bool bActorHasInterface,
106 bool bIsForTeleport
107)
108{
109 if (!root)
110 return false;
111
112 if (LerpDuration <= 0.f || !Grip.bIsLerping)
113 {
114 Grip.bIsLerping = false;
115 GrippingController->OnLerpToHandFinished.Broadcast(Grip);
116 bIsActive = false;
117 }
118
119 FTransform NA = OnGripTransform;//root->GetComponentTransform();
120
121 float Alpha = 0.0f;
122
123 CurrentLerpTime += DeltaTime * LerpSpeed;
124 float OrigAlpha = FMath::Clamp(CurrentLerpTime, 0.f, 1.0f);
125 Alpha = OrigAlpha;
126
127 if (bUseCurve)
128 {
129 if (FRichCurve * richCurve = OptionalCurveToFollow.GetRichCurve())
130 {
131 /*if (CurrentLerpTime > richCurve->GetLastKey().Time)
132 {
133 // Stop lerping
134 OnLerpToHandFinished.Broadcast();
135 CurrentLerpTime = 0.0f;
136 bIsActive = false;
137 return true;
138 }
139 else*/
140 {
141 Alpha = FMath::Clamp(richCurve->Eval(Alpha), 0.f, 1.f);
142 //CurrentLerpTime += DeltaTime;
143 }
144 }
145 }
146
147 FTransform NB = WorldTransform;
148 NA.NormalizeRotation();
149 NB.NormalizeRotation();
150
151 // Quaternion interpolation
153 {
154 WorldTransform.Blend(NA, NB, Alpha);
155 }
156
157 // Euler Angle interpolation
159 {
160 WorldTransform.SetTranslation(FMath::Lerp(NA.GetTranslation(), NB.GetTranslation(), Alpha));
161 WorldTransform.SetScale3D(FMath::Lerp(NA.GetScale3D(), NB.GetScale3D(), Alpha));
162
163 FRotator A = NA.Rotator();
164 FRotator B = NB.Rotator();
165 WorldTransform.SetRotation(FQuat(A + (Alpha * (B - A))));
166 }
167 // Dual quaternion interpolation
168 else
169 {
170 if ((NB.GetRotation() | NA.GetRotation()) < 0.0f)
171 {
172 NB.SetRotation(NB.GetRotation()*-1.0f);
173 }
174 WorldTransform = (FDualQuat(NA)*(1 - Alpha) + FDualQuat(NB)*Alpha).Normalized().AsFTransform(FMath::Lerp(NA.GetScale3D(), NB.GetScale3D(), Alpha));
175 }
176
177 // Turn it off if we need to
178 if (OrigAlpha == 1.0f)
179 {
180 OnLerpToHandFinished.Broadcast();
181 Grip.bIsLerping = false;
182 GrippingController->OnLerpToHandFinished.Broadcast(Grip);
183 CurrentLerpTime = 0.0f;
184 bIsActive = false;
185 }
186
187 return true;
188}
#define INVALID_VRGRIP_ID
virtual void OnGrip_Implementation(UGripMotionControllerComponent *GrippingController, const FBPActorGripInformation &GripInformation) override
virtual void OnGripRelease_Implementation(UGripMotionControllerComponent *ReleasingController, const FBPActorGripInformation &GripInformation, bool bWasSocketed) override
FVRLerpToHandFinishedSignature OnLerpToHandBegin
UPROPERTY(BlueprintAssignable, Category = "LerpEvents")
float MinSpeedForLerp
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "LerpSettings")
float MaxSpeedForLerp
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "LerpSettings")
virtual bool GetWorldTransform_Implementation(UGripMotionControllerComponent *OwningController, float DeltaTime, FTransform &WorldTransform, const FTransform &ParentTransform, FBPActorGripInformation &Grip, AActor *actor, UPrimitiveComponent *root, bool bRootHasInterface, bool bActorHasInterface, bool bIsForTeleport) override
FRuntimeFloatCurve OptionalCurveToFollow
UPROPERTY(Category = "LerpCurve", EditAnywhere, meta = (editcondition = "bUseCurve"))
UGS_LerpToHand(const FObjectInitializer &ObjectInitializer)
FVRLerpToHandFinishedSignature OnLerpToHandFinished
UPROPERTY(BlueprintAssignable, Category = "LerpEvents")
float LerpDuration
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "LerpSettings")
EVRLerpInterpolationMode LerpInterpolationMode
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "LerpSettings")
bool bUseCurve
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "LerpCurve")
float MinDistanceForLerp
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "LerpSettings")
FTransform OnGripTransform
UCLASS(Blueprintable, meta = (BlueprintSpawnableComponent), ClassGroup = MotionController)
FVROnControllerGripSignature OnLerpToHandFinished
UPROPERTY(BlueprintAssignable, Category = "Grip Events")
FBPActorGripInformation * GetGripPtrByID(uint8 IDToLookForGrip)
UCLASS(config = Engine, defaultconfig)
bool bDenyAutoDrop
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "GSSettings")
FTransform GetParentTransform(bool bGetWorldTransform=true, FName BoneName=NAME_None)
UFUNCTION(BlueprintPure, Category = "VRGripScript")
EGSTransformOverrideType WorldTransformOverrideType
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "GSSettings")
bool bIsActive
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "GSSettings")
UObject * GetParent()
UFUNCTION(BlueprintPure, Category = "VRGripScript")
USTRUCT(BlueprintType, Category = "VRExpansionLibrary")
FTransform_NetQuantize RelativeTransform
UPROPERTY(BlueprintReadWrite, Category = "Settings")
uint8 GripID
UPROPERTY(BlueprintReadOnly, Category = "Settings")
FName GrippedBoneName
UPROPERTY(BlueprintReadWrite, Category = "Settings")
bool bIsLerping
UPROPERTY(BlueprintReadOnly, NotReplicated, Category = "Settings")