A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
GS_Melee.cpp
Go to the documentation of this file.
1// Fill out your copyright notice in the Description page of Project Settings.
2
4#include "VRGripInterface.h"
5#include "PhysicalMaterials/PhysicalMaterial.h"
6#include "PhysicsEngine/PhysicsConstraintActor.h"
7#include "PhysicsEngine/PhysicsConstraintComponent.h"
9#include "VRGlobalSettings.h"
10#include "DrawDebugHelpers.h"
12
13UGS_Melee::UGS_Melee(const FObjectInitializer& ObjectInitializer) :
14 Super(ObjectInitializer)
15{
16 bIsActive = true;
18 bDenyLateUpdates = true;
19
20
24 OrientationComponentRelativeFacing = FTransform::Identity;
25
29
30 //RollingVelocityAverage = FVector::ZeroVector;
31 bIsLodged = false;
32
33 //bCanEverTick = true;
34 bCheckLodge = false;
35 bIsHeld = false;
36 bCanEverTick = false;
40}
41
43{
44 TArray<FBPGripPair> HoldingControllers;
45
46 bool bIsHeldOther;
47 IVRGripInterface::Execute_IsHeld(GetParent(), HoldingControllers, bIsHeldOther);
48
49 float PHand = 0.0f;
50 float SHand = 0.0f;
52
53 FBPActorGripInformation* FrontHandGrip = nullptr;
54 FBPActorGripInformation* RearHandGrip = nullptr;
55
58
59 int NumControllers = HoldingControllers.Num();
60
61 for (FBPGripPair& Grip : HoldingControllers)
62 {
63 if (NumControllers > 1)
64 {
65 FBPActorGripInformation* GripInfo = Grip.HoldingController->GetGripPtrByID(Grip.GripID);
66 if (GripInfo)
67 {
68 float GripDistanceOnPrimaryAxis = 0.f;
69 FTransform relTransform(GripInfo->RelativeTransform.ToInverseMatrixWithScale());
70 relTransform = relTransform.GetRelativeTransform(OrientationComponentRelativeFacing);
71
72 // This is the Forward vector projected transform
73 // The most negative one of these is the rearmost hand
74 FVector localLoc = relTransform.GetTranslation();
75
77 {
79 {
80 if (GripInfo->bIsSlotGrip)
81 {
82 PrimaryHand = Grip;
84 }
85 else
86 {
87 if (!PrimaryHand.IsValid())
88 {
89 PrimaryHand = Grip;
90 }
91
92 SecondaryHand = Grip;
93 }
94 }break;
97 {
98
99 if (((PrimaryHandSelectionType == EVRMeleePrimaryHandType::VRPHAND_Rear) ? localLoc.X < PHand : localLoc.X > PHand) || !PrimaryHand.HoldingController)
100 {
101 PrimaryHand = Grip;
102 PHand = localLoc.X;
104 }
105
107 {
108 SecondaryHand = Grip;
109 SHand = localLoc.X;
110 }
111
112 }break;
113 default:break;
114 }
115 }
116 }
117 else
118 {
119 PrimaryHand = Grip;
121 }
122 }
123
124
126 {
128
130 {
132
133 if (GripInfo && GripInfoS)
134 {
135 FVector Primary = GripInfo->RelativeTransform.InverseTransformPositionNoScale(FVector::ZeroVector);
136 FVector Secondary = GripInfoS->RelativeTransform.InverseTransformPositionNoScale(FVector::ZeroVector);
137
138 FVector Final = (COMType == EVRMeleeComType::VRPMELEECOM_PrimaryHand) ? Primary : ((Primary + Secondary) / 2.f);
139 ObjectRelativeGripCenter.SetLocation(Final);
140 }
141 }
142 else
143 {
144 if (GripInfo)
145 {
146
148 {
149 FVector gripLoc = GripInfo->RelativeTransform.InverseTransformPositionNoScale(FVector::ZeroVector);
150 FVector secGripLoc = GripInfo->SecondaryGripInfo.SecondaryRelativeTransform.GetLocation();
151 FVector finalloc = (COMType == EVRMeleeComType::VRPMELEECOM_PrimaryHand) ? gripLoc : (gripLoc + secGripLoc) / 2.f;
152 FVector finalScaled = finalloc * GripInfo->RelativeTransform.GetScale3D();
153
154 FTransform ownerTrans = GetOwner()->GetActorTransform();
155
156 //DrawDebugSphere(GetWorld(), ownerTrans.TransformPosition(finalScaled), 4.0f, 32, FColor::Orange, true);
157
158
159 ObjectRelativeGripCenter.SetLocation(finalScaled);
160 //PrimaryHand.HoldingController->ReCreateGrip(*GripInfo);
161 }
162 else
163 {
164 ObjectRelativeGripCenter = FTransform::Identity;
165 }
166 }
167 }
168 }
169}
170
171void UGS_Melee::UpdateHandPositionAndRotation(FBPGripPair HandPair, FTransform HandWorldTransform, FVector& LocDifference, float& RotDifference, bool bUpdateLocation, bool bUpdateRotation)
172{
173 LocDifference = FVector::ZeroVector;
174
175 if (HandPair.IsValid())
176 {
177 FBPActorGripInformation* GripInfo = HandPair.HoldingController->GetGripPtrByID(HandPair.GripID);
178
179 if (GripInfo)
180 {
181 // Make hand relative to object transform
182 FTransform RelativeTrans = GripInfo->RelativeTransform.Inverse();
183 FVector OriginalLoc = RelativeTrans.GetLocation();
184 FQuat OriginalRot = RelativeTrans.GetRotation();
185
186 // Get our current parent transform
187 FTransform ParentTransform = GetParentTransform();
188
189 FQuat orientationRot = OrientationComponentRelativeFacing.GetRotation();
190
191 if (bUpdateLocation)
192 {
193 FVector currentRelVec = orientationRot.RotateVector(ParentTransform.InverseTransformPosition(HandWorldTransform.GetLocation()));
194 FVector currentLoc = orientationRot.RotateVector(RelativeTrans.GetLocation());
195 currentLoc.X = currentRelVec.X;
196 RelativeTrans.SetLocation(orientationRot.UnrotateVector(currentLoc));
197 }
198
199 if (bUpdateRotation)
200 {
201 FRotator currentRelRot = (orientationRot * (ParentTransform.GetRotation().Inverse() * HandWorldTransform.GetRotation())).Rotator();
202 FRotator currentRot = (orientationRot * RelativeTrans.GetRotation()).Rotator();
203 currentRot.Roll = currentRelRot.Roll;
204 RelativeTrans.SetRotation(orientationRot.Inverse() * currentRot.Quaternion());
205 }
206
207 GripInfo->RelativeTransform = RelativeTrans.Inverse();
208 HandPair.HoldingController->UpdatePhysicsHandle(*GripInfo, true);
210
211 LocDifference = RelativeTrans.GetLocation() - OriginalLoc;
212 RotDifference = RelativeTrans.GetRotation().Rotator().Roll - OriginalRot.Rotator().Roll;
213
214 // Instead of recreating, can directly set local pose here
215
216
217 FBPGripPair SecHand = SecondaryHand;
219
220 if (SecondaryHand.IsValid() && !(SecHand == SecondaryHand))
221 {
222
225
227 if (HandleInfo)
228 {
231 }
232
234
235 switch (COMType)
236 {
239 {
241 }break;
242
244 {
246 }
247 }
248
250 if (HandleInfo)
251 {
253 {
255 }
256 else
257 {
259 }
261 }
262 }
263
266 }
267 }
268}
269
270void UGS_Melee::UpdateHandPosition(FBPGripPair HandPair, FVector HandWorldPosition, FVector& LocDifference)
271{
272 LocDifference = FVector::ZeroVector;
273
274 if (HandPair.IsValid())
275 {
276 FBPActorGripInformation* GripInfo = HandPair.HoldingController->GetGripPtrByID(HandPair.GripID);
277
278 if (GripInfo)
279 {
280 // Make hand relative to object transform
281 FTransform RelativeTrans = GripInfo->RelativeTransform.Inverse();
282 FVector OriginalLoc = RelativeTrans.GetLocation();
283
284 // Get our current parent transform
285 FTransform ParentTransform = GetParentTransform();
286
287 FQuat orientationRot = OrientationComponentRelativeFacing.GetRotation();
288 FVector currentRelVec = orientationRot.RotateVector(ParentTransform.InverseTransformPosition(HandWorldPosition));
289 //currentRelVec = OrientationComponentRelativeFacing.GetRotation().UnrotateVector(currentRelVec);
290
291 FVector currentLoc = orientationRot.RotateVector(RelativeTrans.GetLocation());
292 currentLoc.X = currentRelVec.X;
293
294 RelativeTrans.SetLocation(orientationRot.UnrotateVector(currentLoc));
295 GripInfo->RelativeTransform = RelativeTrans.Inverse();
296 HandPair.HoldingController->UpdatePhysicsHandle(*GripInfo, true);
298
299 LocDifference = RelativeTrans.GetLocation() - OriginalLoc;
300
301 // Instead of recreating, can directly set local pose here
302
303
304 FBPGripPair SecHand = SecondaryHand;
306
307 if (SecondaryHand.IsValid() && !(SecHand == SecondaryHand))
308 {
309
312
314 if (HandleInfo)
315 {
318 }
319
321
322 switch (COMType)
323 {
326 {
328 }break;
329
331 {
333 }
334 }
335
337 if (HandleInfo)
338 {
340 {
342 }
343 else
344 {
346 }
348 }
349 }
350
353 }
354 }
355}
356
358{
359 PrimaryHand = PrimaryGrip;
360 SecondaryHand = SecondaryGrip;
361}
362
364{
365 if (!bIsActive)
366 return;
367
369}
370
372{
373 if (!bIsActive)
374 return;
375
376 // Not storing an id, we should only be doing this once
377// GetOwner()->OnActorHit.AddDynamic(this, &UGS_Melee::OnActorHit);
378
379 // This lets us change the grip settings prior to actually starting the grip off
380 //SetTickEnabled(true);
381 //bCheckLodge = true;
382 bIsHeld = true;
383
384 //if (GrippingController->HasGripAuthority(GripInformation))
385 {
387
388 // If we have multiple hands then alter the grip settings here for what we have already, the other will wait until post event
390 {
393
395 if (HandleInfo)
396 {
399 }
400
402
403 switch (COMType)
404 {
407 {
409 }break;
410
412 {
414 }
415 }
416
418 if (HandleInfo)
419 {
421 {
423 }
424 else
425 {
427 }
429 }
430 }
431 }
432}
433
434void UGS_Melee::OnGripRelease_Implementation(UGripMotionControllerComponent* ReleasingController, const FBPActorGripInformation& GripInformation, bool bWasSocketed)
435{
436
437 if (!bIsActive)
438 return;
439
440 // Refresh our IsHeld var
441 TArray<FBPGripPair> HoldingControllers;
442 IVRGripInterface::Execute_IsHeld(GetParent(), HoldingControllers, bIsHeld);
443
444 //if(!bAlwaysTickPenetration)
445 //SetTickEnabled(false);
446
447 //if (!bAlwaysTickPenetration)
448 // bCheckLodge = false;
449
450 if (SecondaryHand.IsValid() && SecondaryHand.HoldingController == ReleasingController && SecondaryHand.GripID == GripInformation.GripID)
451 {
453 }
454 else if (PrimaryHand.IsValid() && PrimaryHand.HoldingController == ReleasingController && PrimaryHand.GripID == GripInformation.GripID)
455 {
457 {
460 }
461 else
462 {
464 }
465 }
466
467
468 if (PrimaryHand.IsValid())
469 {
470
472 if (HandleInfo)
473 {
475
476 if (GripInfo)
477 {
478 //Reset defaults here still!!!
479 HandleInfo->LinConstraint.XDrive.bEnablePositionDrive = true;
480 HandleInfo->LinConstraint.XDrive.bEnableVelocityDrive = true;
481 HandleInfo->LinConstraint.XDrive.Stiffness = GripInfo->Stiffness;
482 HandleInfo->LinConstraint.XDrive.Damping = GripInfo->Damping;
483
484 HandleInfo->LinConstraint.YDrive = HandleInfo->LinConstraint.XDrive;
485 HandleInfo->LinConstraint.ZDrive = HandleInfo->LinConstraint.XDrive;
486
487 HandleInfo->AngConstraint.SwingDrive.bEnablePositionDrive = false;
488 HandleInfo->AngConstraint.SwingDrive.bEnableVelocityDrive = false;
489 HandleInfo->AngConstraint.TwistDrive.bEnablePositionDrive = false;
490 HandleInfo->AngConstraint.TwistDrive.bEnableVelocityDrive = false;
491 HandleInfo->AngConstraint.AngularDriveMode = EAngularDriveMode::SLERP;
492 HandleInfo->AngConstraint.SlerpDrive.bEnablePositionDrive = true;
493 HandleInfo->AngConstraint.SlerpDrive.bEnableVelocityDrive = true;
494
496 {
497 HandleInfo->AngConstraint.SlerpDrive.Damping = GripInfo->AdvancedGripSettings.PhysicsSettings.AngularDamping;
498 HandleInfo->AngConstraint.SlerpDrive.Stiffness = GripInfo->AdvancedGripSettings.PhysicsSettings.AngularStiffness;
499 }
500 else
501 {
502 HandleInfo->AngConstraint.SlerpDrive.Damping = GripInfo->Damping * 1.4f;
503 HandleInfo->AngConstraint.SlerpDrive.Stiffness = GripInfo->Stiffness * 1.5f;
504 }
505
506 FBPAdvGripSettings AdvSettings = IVRGripInterface::Execute_AdvancedGripSettings(GripInfo->GrippedObject);
508
510 }
511 }
512 }
513
514}
515
517{
518 // Grip base has no super of this
519
520 if (AActor * Owner = GetOwner())
521 {
522 FName CurrentCompName = NAME_None;
523 bool bSearchRootComp = WeaponRootOrientationComponent.IsValid();
524 int RemainingCount = PenetrationNotifierComponents.Num();
525 for (UActorComponent* ChildComp : Owner->GetComponents())
526 {
527 CurrentCompName = ChildComp->GetFName();
528 if (CurrentCompName == NAME_None)
529 continue;
530
531 if (bSearchRootComp && CurrentCompName == WeaponRootOrientationComponent)
532 {
533 bSearchRootComp = false;
534 if (USceneComponent * SceneComp = Cast<USceneComponent>(ChildComp))
535 {
536 OrientationComponentRelativeFacing = SceneComp->GetRelativeTransform();
537 }
538 }
539
540 if (FBPLodgeComponentInfo * Found = PenetrationNotifierComponents.FindByKey(CurrentCompName))
541 {
542 if (UPrimitiveComponent * PrimComp = Cast<UPrimitiveComponent>(ChildComp))
543 {
544 Found->TargetComponent = TWeakObjectPtr<UPrimitiveComponent>(PrimComp);
545 //PrimComp->OnComponentHit.AddDynamic(this, &UGS_Melee::OnLodgeHitCallback);
546 }
547
548 // Decrement even if it failed the cast, they just had it wrong.
549 RemainingCount--;
550 }
551
552 if (!bSearchRootComp && RemainingCount < 1)
553 {
554 break;
555 }
556 }
557
558 // If we found at least one penetration object
559 if (RemainingCount < PenetrationNotifierComponents.Num())
560 {
561 Owner->OnActorHit.AddDynamic(this, &UGS_Melee::OnLodgeHitCallback);
562 bCheckLodge = true;
563 }
564 }
565}
566
567void UGS_Melee::OnEndPlay_Implementation(const EEndPlayReason::Type EndPlayReason)
568{
569 if (AActor * Owner = GetOwner())
570 {
571 Owner->OnActorHit.RemoveDynamic(this, &UGS_Melee::OnLodgeHitCallback);
572 }
573}
574
575void UGS_Melee::OnLodgeHitCallback(AActor* SelfActor, AActor* OtherActor, FVector NormalImpulse, const FHitResult& Hit)
576{
577 if (!Hit.GetComponent())
578 return;
579
580 if (!bCheckLodge || !bIsActive || bIsLodged || OtherActor == SelfActor)
581 {
583 {
584 OnMeleeInvalidHit.Broadcast(OtherActor, Hit.GetComponent(), NormalImpulse, Hit);
585 }
586 return;
587 }
588
589 // Escape out if we are not held and are not set to always tick penetration
591 return;
592
593 TArray<FBPHitSurfaceProperties> AllowedPenetrationSurfaceTypes;
594
595 if (OverrideMeleeSurfaceSettings.Num() > 0)
596 {
597 // Use our local copy
598 AllowedPenetrationSurfaceTypes = OverrideMeleeSurfaceSettings;
599 }
600 else
601 {
602 // Use the global settings
603 UVRGlobalSettings::GetMeleeSurfaceGlobalSettings(AllowedPenetrationSurfaceTypes);
604 }
605
606 FBPHitSurfaceProperties HitSurfaceProperties;
607 if (Hit.PhysMaterial.IsValid())
608 {
609 HitSurfaceProperties.SurfaceType = Hit.PhysMaterial->SurfaceType;
610 }
611
612 if (AllowedPenetrationSurfaceTypes.Num())
613 {
614 // Reject bad surface types
615 if (!Hit.PhysMaterial.IsValid())
616 {
617 OnMeleeInvalidHit.Broadcast(OtherActor, Hit.GetComponent(), NormalImpulse, Hit);
618 return;
619 }
620
621 EPhysicalSurface PhysSurfaceType = Hit.PhysMaterial->SurfaceType;
622 int32 IndexOfSurface = AllowedPenetrationSurfaceTypes.IndexOfByPredicate([&PhysSurfaceType](const FBPHitSurfaceProperties& Entry) { return Entry.SurfaceType == PhysSurfaceType; });
623
624 if (IndexOfSurface != INDEX_NONE)
625 {
626 HitSurfaceProperties = AllowedPenetrationSurfaceTypes[IndexOfSurface];
627 }
628 else
629 {
630 // Surface is not part of our default list, don't allow penetration
631 HitSurfaceProperties.bSurfaceAllowsPenetration = false;
632 // Not a valid surface type to throw an event
633 //return;
634 }
635 }
636
637 /*if (UPrimitiveComponent * root = Cast<UPrimitiveComponent>(SelfActor->GetRootComponent()))
638 {
639 if (FBodyInstance * rBodyInstance = root->GetBodyInstance())
640 {
641 //float mass =rBodyInstance->GetBodyMass();
642 //ImpulseVelocity = (NormalImpulse / rBodyInstance->GetBodyMass()).SizeSquared();
643 RollingVelocityAverage += FVector::CrossProduct(RollingAngVelocityAverage, Hit.ImpactPoint - (rBodyInstance->GetCOMPosition()));
644 }
645 }*/
646
647// FVector FrameToFrameVelocity = RollingVelocityAverage;
648
649 // If we hit, then regardless of penetration, end the velocity history and reset
650// RollingVelocityAverage = FVector::ZeroVector;
651// RollingAngVelocityAverage = FVector::ZeroVector;
652
653 bool bHadFirstHit = false;
654 FBPLodgeComponentInfo FirstHitComp;
655
656 float HitNormalImpulse = NormalImpulse.SizeSquared();
657
659 {
660 if (!LodgeData.TargetComponent.IsValid())
661 continue;
662
663 FBox LodgeLocalBox = LodgeData.TargetComponent->CalcLocalBounds().GetBox();
664 FVector LocalHit = LodgeData.TargetComponent->GetComponentTransform().InverseTransformPosition(Hit.ImpactPoint);
665 //FBox LodgeBox = LodgeData.TargetComponent->Bounds.GetBox();
666 if (LodgeData.TargetComponent.IsValid() && LodgeLocalBox.IsInsideOrOn(LocalHit))//LodgeBox.IsInsideOrOn(Hit.ImpactPoint))
667 {
668 FVector ForwardVec = LodgeData.TargetComponent->GetForwardVector();
669
670 // Using swept objects hit normal as we are looking for a facing from ourselves
671 float DotValue = FMath::Abs(FVector::DotProduct(Hit.Normal, ForwardVec));
672 float Velocity = NormalImpulse.ProjectOnToNormal(ForwardVec).SizeSquared();//FrameToFrameVelocity.ProjectOnToNormal(ForwardVec);
673 // Check if the velocity was strong enough along our axis to count as a lodge event
674 // Also that our facing was in the relatively correct direction
675
677 {
678 if (LodgeData.ZoneType != EVRMeleeZoneType::VRPMELLE_ZONETYPE_Hit && DotValue >= (1.0f - LodgeData.AcceptableForwardProductRange) && (Velocity * HitSurfaceProperties.StabVelocityScaler) >= FMath::Square(LodgeData.PenetrationVelocity))
679 {
680 OnShouldLodgeInObject.Broadcast(LodgeData, OtherActor, Hit.GetComponent(), Hit.GetComponent()->GetCollisionObjectType(), HitSurfaceProperties, NormalImpulse, Hit);
681 return;
682 //break;
683 }
684 }
685
686 float HitImpulse = LodgeData.bIgnoreForwardVectorForHitImpulse ? HitNormalImpulse : Velocity;
687
688 if (!bHadFirstHit && LodgeData.ZoneType > EVRMeleeZoneType::VRPMELLE_ZONETYPE_Stab && DotValue >= (1.0f - LodgeData.AcceptableForwardProductRangeForHits) && HitImpulse >= FMath::Square(LodgeData.MinimumHitVelocity))
689 {
690 bHadFirstHit = true;
691 FirstHitComp = LodgeData;
692 }
693 }
694 }
695
696 if (bHadFirstHit)
697 {
698 OnMeleeHit.Broadcast(FirstHitComp, OtherActor, Hit.GetComponent(), Hit.GetComponent()->GetCollisionObjectType(), HitSurfaceProperties, NormalImpulse, Hit);
699 }
700 else
701 {
702 OnMeleeInvalidHit.Broadcast(OtherActor, Hit.GetComponent(), NormalImpulse, Hit);
703 }
704}
705
706
707void UGS_Melee::HandlePrePhysicsHandle(UGripMotionControllerComponent* GrippingController, const FBPActorGripInformation &GripInfo, FBPActorPhysicsHandleInformation * HandleInfo, FTransform & KinPose)
708{
709 if (!bIsActive)
710 return;
711
712 if (WeaponRootOrientationComponent != NAME_None)
713 {
714 // Alter to rotate to x+ if we have an orientation component
715 FQuat DeltaQuat = OrientationComponentRelativeFacing.GetRotation();
716
717 // This moves the kinematic actor to face its X+ in the direction designated
718 KinPose.SetRotation(KinPose.GetRotation() * (HandleInfo->RootBoneRotation.GetRotation().Inverse() * DeltaQuat));
719 HandleInfo->COMPosition.SetRotation(HandleInfo->COMPosition.GetRotation() * (HandleInfo->RootBoneRotation.GetRotation().Inverse() * DeltaQuat));
720 }
721}
722
724{
725 if (!bIsActive)
726 return;
727
728 if (SecondaryHand.IsValid() )// && GrippingController == PrimaryHand.HoldingController)
729 {
730 if (GrippingController == SecondaryHand.HoldingController && HandleInfo->GripID == SecondaryHand.GripID)
731 {
733 }
734 else if (GrippingController == PrimaryHand.HoldingController && HandleInfo->GripID == PrimaryHand.GripID)
735 {
737 {
739 }
740 else
741 {
743 }
744 }
745
747 SetComBetweenHands(GrippingController, HandleInfo);
748 }
749 else
750 {
752 {
754 }
755
756 //HandleInfo->bSetCOM = false; // Should i remove this?
757 HandleInfo->bSkipResettingCom = false;
758 }
759}
760
762{
763
764 if (!GrippingController || !HandleInfo)
765 return;
766
768 {
769 //if (PrimaryHand.HoldingController == GrippingController)
770 {
771 if (UPrimitiveComponent * PrimComp = Cast<UPrimitiveComponent>(GetParentSceneComp()))
772 {
773 if (FBodyInstance * rBodyInstance = PrimComp->GetBodyInstance())
774 {
775 FPhysicsCommand::ExecuteWrite(rBodyInstance->ActorHandle, [&](const FPhysicsActorHandle& Actor)
776 {
777 FTransform localCom = FPhysicsInterface::GetComTransformLocal_AssumesLocked(Actor);
778 localCom.SetLocation((HandleInfo->RootBoneRotation * ObjectRelativeGripCenter).GetLocation());
779 FPhysicsInterface::SetComLocalPose_AssumesLocked(Actor, localCom);
780
781 });
782 }
783 }
784 }
785
786 HandleInfo->bSetCOM = true; // Should i remove this?
787 HandleInfo->bSkipResettingCom = true;
788 }
789}
790
791/*void UGS_Melee::Tick(float DeltaTime)
792{
793 AActor* myOwner = GetOwner();
794
795 if (UPrimitiveComponent * root = Cast<UPrimitiveComponent>(myOwner->GetRootComponent()))
796 {
797 FBodyInstance* rBodyInstance = root->GetBodyInstance();
798 if (rBodyInstance && rBodyInstance->IsValidBodyInstance())
799 {
800 RollingVelocityAverage = rBodyInstance->GetUnrealWorldVelocity();
801 RollingAngVelocityAverage = rBodyInstance->GetUnrealWorldAngularVelocityInRadians();
802 }
803 }
804}*/
805
807{
808 /*if (PrimaryHand.IsValid() && Controller != PrimaryHand.HoldingController)
809 {
810 return true;
811 }*/
812
813 return false;
814}
815
817(
818 UGripMotionControllerComponent* GrippingController,
819 float DeltaTime, FTransform & WorldTransform,
820 const FTransform &ParentTransform,
822 AActor * actor,
823 UPrimitiveComponent * root,
824 bool bRootHasInterface,
825 bool bActorHasInterface,
826 bool bIsForTeleport
827)
828{
829 if (!GrippingController)
830 return false;
831
832 // Just simple transform setting
833 WorldTransform = Grip.RelativeTransform * Grip.AdditionTransform * ParentTransform;
835 {
836 WorldTransform.SetLocation((GrippingController->GetPivotLocation() + Grip.SecondaryGripInfo.SecondaryAttachment->GetComponentLocation()) / 2.f);
837 }
838
839 // Check the grip lerp state, this it ouside of the secondary attach check below because it can change the result of it
841 {
842 switch (Grip.SecondaryGripInfo.GripLerpState)
843 {
846 {
847 if (Grip.SecondaryGripInfo.curLerp > 0.01f)
848 Grip.SecondaryGripInfo.curLerp -= DeltaTime;
849 else
850 {
851 /*if (Grip.SecondaryGripInfo.bHasSecondaryAttachment &&
852 Grip.AdvancedGripSettings.SecondaryGripSettings.bUseSecondaryGripSettings &&
853 Grip.AdvancedGripSettings.SecondaryGripSettings.SecondaryGripScaler_DEPRECATED < 1.0f)
854 {
855 Grip.SecondaryGripInfo.GripLerpState = EGripLerpState::ConstantLerp_DEPRECATED;
856 }
857 else*/
859 }
860
861 }break;
862 //case EGripLerpState::ConstantLerp_DEPRECATED:
864 default:break;
865 }
866 }
867
868 // Handle the interp and multi grip situations, re-checking the grip situation here as it may have changed in the switch above.
870 {
871 FTransform SecondaryTransform = Grip.RelativeTransform * ParentTransform;
872
873 // Checking secondary grip type for the scaling setting
875
876 if (bRootHasInterface)
877 SecondaryType = IVRGripInterface::Execute_SecondaryGripType(root);
878 else if (bActorHasInterface)
879 SecondaryType = IVRGripInterface::Execute_SecondaryGripType(actor);
880
881 // If the grip is a custom one, skip all of this logic we won't be changing anything
882 if (SecondaryType != ESecondaryGripType::SG_Custom)
883 {
884 // Variables needed for multi grip transform
885 FVector BasePoint = ParentTransform.GetLocation(); // Get our pivot point
886 const FTransform PivotToWorld = FTransform(FQuat::Identity, BasePoint);
887 const FTransform WorldToPivot = FTransform(FQuat::Identity, -BasePoint);
888
889 FVector frontLocOrig;
890 FVector frontLoc;
891
892 // Ending lerp out of a multi grip
894 {
895 frontLocOrig = (/*WorldTransform*/SecondaryTransform.TransformPosition(Grip.SecondaryGripInfo.SecondaryRelativeTransform.GetLocation())) - BasePoint;
897
898 frontLocOrig = FMath::Lerp(frontLoc, frontLocOrig, FMath::Clamp(Grip.SecondaryGripInfo.curLerp / Grip.SecondaryGripInfo.LerpToRate, 0.0f, 1.0f));
899 }
900 else // Is in a multi grip, might be lerping into it as well.
901 {
902 //FVector curLocation; // Current location of the secondary grip
903
904 // Calculates the correct secondary attachment location and sets frontLoc to it
905 CalculateSecondaryLocation(frontLoc, BasePoint, Grip, GrippingController);
906
907 frontLocOrig = (/*WorldTransform*/SecondaryTransform.TransformPosition(Grip.SecondaryGripInfo.SecondaryRelativeTransform.GetLocation())) - BasePoint;
908
909 // Apply any smoothing settings and lerping in / constant lerping
910 ApplySmoothingAndLerp(Grip, frontLoc, frontLocOrig, DeltaTime);
911
913 }
914
915 // Get any scaling addition from a scaling secondary grip type
916 FVector Scaler = FVector(1.0f);
918 {
919 GetAnyScaling(Scaler, Grip, frontLoc, frontLocOrig, SecondaryType, SecondaryTransform);
920 }
921
922 Grip.SecondaryGripInfo.SecondaryGripDistance = FVector::Dist(frontLocOrig, frontLoc);
923
924 /*if (Grip.AdvancedGripSettings.SecondaryGripSettings.bUseSecondaryGripSettings && Grip.AdvancedGripSettings.SecondaryGripSettings.bUseSecondaryGripDistanceInfluence_DEPRECATED)
925 {
926 float rotScaler = 1.0f - FMath::Clamp((Grip.SecondaryGripInfo.SecondaryGripDistance - Grip.AdvancedGripSettings.SecondaryGripSettings.GripInfluenceDeadZone_DEPRECATED) / FMath::Max(Grip.AdvancedGripSettings.SecondaryGripSettings.GripInfluenceDistanceToZero_DEPRECATED, 1.0f), 0.0f, 1.0f);
927 frontLoc = FMath::Lerp(frontLocOrig, frontLoc, rotScaler);
928 }*/
929
930 // Skip rot val for scaling only
931 if (SecondaryType != ESecondaryGripType::SG_ScalingOnly)
932 {
933 // Get the rotation difference from the initial second grip
934 FQuat rotVal = FQuat::FindBetweenVectors(frontLocOrig, frontLoc);
935
936 // Rebase the world transform to the pivot point, add the rotation, remove the pivot point rebase
937 WorldTransform = WorldTransform * WorldToPivot * FTransform(rotVal, FVector::ZeroVector, Scaler) * PivotToWorld;
938 }
939 else
940 {
941 // Rebase the world transform to the pivot point, add the scaler, remove the pivot point rebase
942 WorldTransform = WorldTransform * WorldToPivot * FTransform(FQuat::Identity, FVector::ZeroVector, Scaler) * PivotToWorld;
943 }
944 }
945
946
947 // Fixup X rotation to keep it aligned with the primary hand.
948 //WorldTransform = Grip.RelativeTransform * Grip.AdditionTransform * ParentTransform;
949
950 // Get primary hand relative position
951 FTransform InverseTrans(Grip.RelativeTransform.ToInverseMatrixWithScale());
952
953 // Get the original location of the hand
954 FVector origLocation = InverseTrans.GetLocation();
955
956 FVector orientedvector = FVector::VectorPlaneProject(origLocation, -OrientationComponentRelativeFacing.GetRotation().GetForwardVector());
957 FVector newLocation = FVector::VectorPlaneProject(WorldTransform.InverseTransformPosition(GrippingController->GetPivotLocation()), OrientationComponentRelativeFacing.GetRotation().GetForwardVector());
958
959 FQuat DeltaQuat = FQuat::FindBetweenVectors(orientedvector, newLocation);
960
961 WorldTransform.SetRotation(DeltaQuat * WorldTransform.GetRotation());
962 }
963
964 return true;
965}
ESecondaryGripType
UENUM(Blueprintable)
virtual void GetAnyScaling(FVector &Scaler, FBPActorGripInformation &Grip, FVector &frontLoc, FVector &frontLocOrig, ESecondaryGripType SecondaryType, FTransform &SecondaryTransform)
virtual void ApplySmoothingAndLerp(FBPActorGripInformation &Grip, FVector &frontLoc, FVector &frontLocOrig, float DeltaTime)
virtual void CalculateSecondaryLocation(FVector &frontLoc, const FVector &BasePoint, FBPActorGripInformation &Grip, UGripMotionControllerComponent *GrippingController)
FTransform OrientationComponentRelativeFacing
Definition GS_Melee.h:346
bool bIsLodged
Definition GS_Melee.h:282
EVRMeleePrimaryHandType PrimaryHandSelectionType
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Settings")
Definition GS_Melee.h:408
TArray< FBPLodgeComponentInfo > PenetrationNotifierComponents
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Settings")
Definition GS_Melee.h:376
UGS_Melee(const FObjectInitializer &ObjectInitializer)
Definition GS_Melee.cpp:13
virtual bool Wants_DenyTeleport_Implementation(UGripMotionControllerComponent *Controller) override
Definition GS_Melee.cpp:806
bool bIsHeld
Definition GS_Melee.h:379
bool bOnlyPenetrateWithTwoHands
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Melee|Lodging")
Definition GS_Melee.h:323
bool bUsePrimaryHandSettingsWithOneHand
UPROPERTY(BlueprintReadOnly, Category = "Weapon Settings")
Definition GS_Melee.h:427
virtual void OnGrip_Implementation(UGripMotionControllerComponent *GrippingController, const FBPActorGripInformation &GripInformation) override
Definition GS_Melee.cpp:371
bool bHasValidPrimaryHand
Definition GS_Melee.h:394
virtual void OnLodgeHitCallback(AActor *SelfActor, AActor *OtherActor, FVector NormalImpulse, const FHitResult &Hit)
UFUNCTION()
Definition GS_Melee.cpp:575
void SetPrimaryAndSecondaryHands(FBPGripPair &PrimaryGrip, FBPGripPair &SecondaryGrip)
UFUNCTION(BlueprintCallable, Category = "Weapon Settings")
Definition GS_Melee.cpp:357
void SetComBetweenHands(UGripMotionControllerComponent *GrippingController, FBPActorPhysicsHandleInformation *HandleInfo)
Definition GS_Melee.cpp:761
FVROnMeleeOnHit OnMeleeHit
UPROPERTY(BlueprintAssignable, Category = "Melee|Hit")
Definition GS_Melee.h:299
FVROnMeleeInvalidHitSignature OnMeleeInvalidHit
UPROPERTY(BlueprintAssignable, Category = "Melee|Hit")
Definition GS_Melee.h:306
bool bCheckLodge
Definition GS_Melee.h:378
EVRMeleeComType COMType
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Settings")
Definition GS_Melee.h:435
FBPGripPair SecondaryHand
UPROPERTY(BlueprintReadOnly, Category = "Weapon Settings")
Definition GS_Melee.h:420
void UpdateHandPosition(FBPGripPair HandPair, FVector HandWorldPosition, FVector &LocDifference)
UFUNCTION(BlueprintCallable, Category = "Weapon Settings")
Definition GS_Melee.cpp:270
FBPAdvancedPhysicsHandleSettings SecondaryHandPhysicsSettings
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Settings")
Definition GS_Melee.h:457
virtual void HandlePrePhysicsHandle(UGripMotionControllerComponent *GrippingController, const FBPActorGripInformation &GripInfo, FBPActorPhysicsHandleInformation *HandleInfo, FTransform &KinPose) override
Definition GS_Melee.cpp:707
virtual void OnSecondaryGrip_Implementation(UGripMotionControllerComponent *Controller, USceneComponent *SecondaryGripComponent, const FBPActorGripInformation &GripInformation) override
Definition GS_Melee.cpp:363
FTransform ObjectRelativeGripCenter
Definition GS_Melee.h:437
virtual bool GetWorldTransform_Implementation(UGripMotionControllerComponent *GrippingController, float DeltaTime, FTransform &WorldTransform, const FTransform &ParentTransform, FBPActorGripInformation &Grip, AActor *actor, UPrimitiveComponent *root, bool bRootHasInterface, bool bActorHasInterface, bool bIsForTeleport) override
Definition GS_Melee.cpp:817
FBPGripPair PrimaryHand
UPROPERTY(BlueprintReadOnly, Category = "Weapon Settings")
Definition GS_Melee.h:414
virtual void OnEndPlay_Implementation(const EEndPlayReason::Type EndPlayReason) override
Definition GS_Melee.cpp:567
FName WeaponRootOrientationComponent
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Settings")
Definition GS_Melee.h:345
virtual void OnGripRelease_Implementation(UGripMotionControllerComponent *ReleasingController, const FBPActorGripInformation &GripInformation, bool bWasSocketed=false) override
Definition GS_Melee.cpp:434
void UpdateHandPositionAndRotation(FBPGripPair HandPair, FTransform HandWorldTransform, FVector &LocDifference, float &RotDifference, bool bUpdateLocation=true, bool bUpdateRotation=true)
UFUNCTION(BlueprintCallable, Category = "Weapon Settings")
Definition GS_Melee.cpp:171
FBPAdvancedPhysicsHandleSettings PrimaryHandPhysicsSettings
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Settings")
Definition GS_Melee.h:449
FVROnMeleeShouldLodgeSignature OnShouldLodgeInObject
UPROPERTY(BlueprintAssignable, Category = "Melee|Lodging")
Definition GS_Melee.h:292
void UpdateDualHandInfo()
Definition GS_Melee.cpp:42
bool bAlwaysTickPenetration
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Melee|Lodging")
Definition GS_Melee.h:314
virtual void HandlePostPhysicsHandle(UGripMotionControllerComponent *GrippingController, FBPActorPhysicsHandleInformation *HandleInfo) override
Definition GS_Melee.cpp:723
virtual void OnBeginPlay_Implementation(UObject *CallingOwner) override
Definition GS_Melee.cpp:516
bool bAutoSetPrimaryAndSecondaryHands
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Settings")
Definition GS_Melee.h:391
TArray< FBPHitSurfaceProperties > OverrideMeleeSurfaceSettings
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Melee|Lodging")
Definition GS_Melee.h:332
UCLASS(Blueprintable, meta = (BlueprintSpawnableComponent), ClassGroup = MotionController)
bool UpdatePhysicsHandle(uint8 GripID, bool bFullyRecreate=true)
void NotifyGripTransformChanged(const FBPActorGripInformation &GripInfo)
FBPActorPhysicsHandleInformation * GetPhysicsGrip(const FBPActorGripInformation &GripInfo)
FBPActorGripInformation * GetGripPtrByID(uint8 IDToLookForGrip)
static void GetMeleeSurfaceGlobalSettings(TArray< FBPHitSurfaceProperties > &OutMeleeSurfaceSettings)
UFUNCTION(BlueprintCallable, Category = "MeleeSettings")
bool bInjectPostPhysicsHandle
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "GSSettings")
FTransform GetParentTransform(bool bGetWorldTransform=true, FName BoneName=NAME_None)
UFUNCTION(BlueprintPure, Category = "VRGripScript")
bool bInjectPrePhysicsHandle
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "GSSettings")
bool bDenyLateUpdates
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "GSSettings")
EGSTransformOverrideType WorldTransformOverrideType
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "GSSettings")
bool bIsActive
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "GSSettings")
AActor * GetOwner()
UFUNCTION(BlueprintPure, Category = "VRGripScript")
bool bCanEverTick
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "TickSettings")
USceneComponent * GetParentSceneComp()
UFUNCTION(BlueprintCallable, Category = "VRGripScript")
UObject * GetParent()
UFUNCTION(BlueprintPure, Category = "VRGripScript")
USTRUCT(BlueprintType, Category = "VRExpansionLibrary")
FBPAdvGripSettings AdvancedGripSettings
UPROPERTY(BlueprintReadOnly, Category = "Settings")
bool bIsSlotGrip
UPROPERTY(BlueprintReadWrite, Category = "Settings")
FTransform AdditionTransform
UPROPERTY(BlueprintReadWrite, NotReplicated, Category = "Settings")
FTransform_NetQuantize RelativeTransform
UPROPERTY(BlueprintReadWrite, Category = "Settings")
float Stiffness
UPROPERTY(BlueprintReadOnly, Category = "Settings")
FBPSecondaryGripInfo SecondaryGripInfo
UPROPERTY(BlueprintReadOnly, Category = "Settings")
UObject * GrippedObject
UPROPERTY(BlueprintReadOnly, Category = "Settings")
uint8 GripID
UPROPERTY(BlueprintReadOnly, Category = "Settings")
float Damping
UPROPERTY(BlueprintReadOnly, Category = "Settings")
USTRUCT(BlueprintType, Category = "VRExpansionLibrary")
FAngularDriveConstraint AngConstraint
FLinearDriveConstraint LinConstraint
EPhysicsGripCOMType PhysicsGripLocationSettings
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PhysicsSettings", meta = (editcondition = "bU...
bool bUsePhysicsSettings
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PhysicsSettings")
float AngularDamping
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PhysicsSettings", meta = (editcondition = "bU...
bool bUseCustomAngularValues
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PhysicsSettings", meta = (editcondition = "bU...
float AngularStiffness
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PhysicsSettings", meta = (editcondition = "bU...
USTRUCT(BlueprintType, Category = "VRExpansionLibrary")
FBPAdvGripPhysicsSettings PhysicsSettings
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AdvancedGripSettings")
bool FillTo(FBPActorPhysicsHandleInformation *HandleInfo) const
USTRUCT(BlueprintType, Category = "VRExpansionLibrary")
UGripMotionControllerComponent * HoldingController
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GripPair")
uint8 GripID
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GripPair")
USTRUCT(BlueprintType, Category = "Lodging")
Definition GS_Melee.h:75
bool bSurfaceAllowsPenetration
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Surface Property")
Definition GS_Melee.h:85
TEnumAsByte< EPhysicalSurface > SurfaceType
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Surface Property")
Definition GS_Melee.h:116
float StabVelocityScaler
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Surface Property")
Definition GS_Melee.h:109
USTRUCT(BlueprintType, Category = "Lodging")
Definition GS_Melee.h:135
USceneComponent * SecondaryAttachment
UPROPERTY(BlueprintReadOnly, Category = "SecondaryGripInfo")
EGripLerpState GripLerpState
float LerpToRate
UPROPERTY()
float SecondaryGripDistance
UPROPERTY(BlueprintReadOnly, NotReplicated, Category = "SecondaryGripInfo")
bool bHasSecondaryAttachment
UPROPERTY(BlueprintReadOnly, Category = "SecondaryGripInfo")
FTransform_NetQuantize SecondaryRelativeTransform
UPROPERTY(BlueprintReadOnly, Category = "SecondaryGripInfo")