A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
ParentRelativeAttachmentComponent.cpp
Go to the documentation of this file.
1// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
2
4#include "VRBaseCharacter.h"
5#include "VRCharacter.h"
6//#include "Runtime/Engine/Private/EnginePrivate.h"
7//#include "VRSimpleCharacter.h"
8//#include "VRCharacter.h"
9
10
12 : Super(ObjectInitializer)
13{
14 PrimaryComponentTick.bCanEverTick = true;
15 PrimaryComponentTick.bStartWithTickEnabled = true;
16 // Let it sit in DuringPhysics like is the default
17 //PrimaryComponentTick.TickGroup = TG_PrePhysics;
18
19 bWantsInitializeComponent = true;
20
21 SetRelativeScale3D(FVector(1.0f, 1.0f, 1.0f));
22 SetRelativeLocation(FVector::ZeroVector);
23 YawTolerance = 0.0f;
24 bOffsetByHMD = false;
25
26 bLerpTransition = true;
27 LerpSpeed = 100.0f;
28 LastLerpVal = 0.0f;
29 LerpTarget = 0.0f;
30 bWasSetOnce = false;
31
32 LeftControllerTrans = FTransform::Identity;
33 RightControllerTrans = FTransform::Identity;
34
37
38 bUseFeetLocation = false;
39 CustomOffset = FVector::ZeroVector;
40
41 //YawRotationMethod = EVR_PRC_RotationMethod::PRC_ROT_HMD;
42}
43
45{
46 Super::InitializeComponent();
47
48 // Update our tracking
49 if (!bUseFeetLocation && AttachChar.IsValid()) // New case to early out and with less calculations
50 {
51 SetRelativeTransform(AttachChar->VRReplicatedCamera->GetRelativeTransform());
52 }
53
54}
55
57{
58 if (AVRCharacter* CharacterOwner = Cast<AVRCharacter>(this->GetOwner()))
59 {
60 AttachChar = CharacterOwner;
61 }
62 else
63 {
64 AttachChar.Reset();
65 }
66
67 if (AVRBaseCharacter * BaseCharacterOwner = Cast<AVRBaseCharacter>(this->GetOwner()))
68 {
69 AttachBaseChar = BaseCharacterOwner;
70 }
71 else
72 {
73 AttachBaseChar.Reset();
74 }
75
76 Super::OnAttachmentChanged();
77}
78
80{
82 {
83 //#TODO: bOffsetByHMD not supported with this currently, fix it, need to check for both camera and HMD
85
87 {
88 TrackedParentWaist.SetTranslation(TrackedParentWaist.GetTranslation() * FVector(1.0f, 1.0f, 0.0f));
89
91 {
92 FRotator InverseRot = UVRExpansionFunctionLibrary::GetHMDPureYaw_I(TrackedParentWaist.Rotator());
93
94 TrackedParentWaist.SetRotation(GetCalculatedRotation(InverseRot, DeltaTime));
95 }
96 }
97
98 TrackedParentWaist.AddToTranslation(CustomOffset);
99 SetRelativeTransform(TrackedParentWaist);
100
101 }
102 else if (AttachChar.IsValid()) // New case to early out and with less calculations
103 {
104 SetRelativeRotAndLoc(AttachChar->VRRootReference->curCameraLoc, AttachChar->VRRootReference->StoredCameraRotOffset, DeltaTime);
105 }
106 else if (IsLocallyControlled() && GEngine->XRSystem.IsValid() && GEngine->XRSystem->IsHeadTrackingAllowed())
107 {
108 FQuat curRot;
109 FVector curCameraLoc;
110 if (GEngine->XRSystem->GetCurrentPose(IXRTrackingSystem::HMDDeviceId, curRot, curCameraLoc))
111 {
112 if (bOffsetByHMD)
113 {
114 curCameraLoc.X = 0;
115 curCameraLoc.Y = 0;
116 }
117
119 {
120 FRotator InverseRot = UVRExpansionFunctionLibrary::GetHMDPureYaw_I(curRot.Rotator());
121 SetRelativeRotAndLoc(curCameraLoc, InverseRot, DeltaTime);
122 }
123 else
124 SetRelativeRotAndLoc(curCameraLoc, FRotator::ZeroRotator, DeltaTime);
125 }
126 }
127 else if (AttachBaseChar.IsValid())
128 {
129 if (AttachBaseChar->VRReplicatedCamera)
130 {
132 {
133 FRotator InverseRot = UVRExpansionFunctionLibrary::GetHMDPureYaw(AttachBaseChar->VRReplicatedCamera->GetRelativeRotation());
134 SetRelativeRotAndLoc(AttachBaseChar->VRReplicatedCamera->GetRelativeLocation(), InverseRot, DeltaTime);
135 }
136 else
137 SetRelativeRotAndLoc(AttachBaseChar->VRReplicatedCamera->GetRelativeLocation(), FRotator::ZeroRotator, DeltaTime);
138 }
139 }
140 else if (AActor* owner = this->GetOwner())
141 {
142 if (UCameraComponent* CameraOwner = owner->FindComponentByClass<UCameraComponent>())
143 {
145 {
146 FRotator InverseRot = UVRExpansionFunctionLibrary::GetHMDPureYaw(CameraOwner->GetRelativeRotation());
147 SetRelativeRotAndLoc(CameraOwner->GetRelativeLocation(), InverseRot, DeltaTime);
148 }
149 else
150 SetRelativeRotAndLoc(CameraOwner->GetRelativeLocation(), FRotator::ZeroRotator, DeltaTime);
151 }
152 }
153}
154
155void UParentRelativeAttachmentComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
156{
157 if (!bUpdateInCharacterMovement || !AttachChar.IsValid())
158 {
159 UpdateTracking(DeltaTime);
160 }
161 else
162 {
163 UCharacterMovementComponent * CharMove = AttachChar->GetCharacterMovement();
164 if (!CharMove || !CharMove->IsComponentTickEnabled() || !CharMove->IsActive())
165 {
166 // Our character movement isn't handling our updates, lets do it ourself.
167 UpdateTracking(DeltaTime);
168 }
169 }
170
171 Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
172}
UCLASS()
Definition VRCharacter.h:23
static FTransform Default_GetWaistOrientationAndPosition(FBPVRWaistTracking_Info &WaistTrackingInfo)
TWeakObjectPtr< AVRCharacter > AttachChar
UPROPERTY()
void SetRelativeRotAndLoc(FVector NewRelativeLocation, FRotator NewRelativeRotation, float DeltaTime)
void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override
TWeakObjectPtr< AVRBaseCharacter > AttachBaseChar
UPROPERTY()
FBPVRWaistTracking_Info OptionalWaistTrackingParent
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRTrackedParentInterface")
bool bUseFeetLocation
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRExpansionLibrary")
FVector CustomOffset
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRExpansionLibrary")
bool bOffsetByHMD
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRExpansionLibrary")
UParentRelativeAttachmentComponent(const FObjectInitializer &ObjectInitializer)
bool bIgnoreRotationFromParent
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRExpansionLibrary")
bool bUpdateInCharacterMovement
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRExpansionLibrary")
bool bLerpTransition
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRExpansionLibrary")
float YawTolerance
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRExpansionLibrary", meta = (ClampMin = "0",...
FQuat GetCalculatedRotation(FRotator InverseRot, float DeltaTime)
float LerpSpeed
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRExpansionLibrary", meta = (ClampMin = "0",...
static FRotator GetHMDPureYaw(FRotator HMDRotation)
UFUNCTION(BlueprintPure, Category = "VRExpansionFunctions", meta = (bIgnoreSelf = "true",...
static FRotator GetHMDPureYaw_I(FRotator HMDRotation)