Advertisement
Guest User

Grabber

a guest
Jan 28th, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include "BuildingEscape.h"
  2. #include "Grabber.h"
  3.  
  4. #define OUT
  5.  
  6.  
  7. // Sets default values for this component's properties
  8. UGrabber::UGrabber()
  9. {
  10.     // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
  11.     // off to improve performance if you don't need them.
  12.     PrimaryComponentTick.bCanEverTick = true;
  13. }
  14.  
  15.  
  16. // Called when the game starts
  17. void UGrabber::BeginPlay()
  18. {
  19.     Super::BeginPlay();
  20.     FindPhysicsHandleComponent();
  21.     SetupInputComponent();
  22. }
  23.  
  24. void UGrabber::FindPhysicsHandleComponent()
  25. {
  26.     PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
  27.     if (PhysicsHandle == nullptr)
  28.     {
  29.         UE_LOG(LogTemp, Error, TEXT("%s missing physics handle component"), *GetOwner()->GetName())
  30.     }
  31. }
  32.  
  33. void UGrabber::SetupInputComponent()
  34. {
  35.     InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
  36.     if (InputComponent)
  37.     {
  38.         InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab);
  39.         InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::Release);
  40.     }
  41.     else
  42.     {
  43.         UE_LOG(LogTemp, Error, TEXT("%s missing input component"), *GetOwner()->GetName())
  44.     }
  45. }
  46.  
  47. void UGrabber::Grab()
  48. {
  49.     /// LINE TRACE and see if we reach any actors with physics body collision channel set
  50.     auto HitResult = GetFirstPhysicsBodyInReach();
  51.     auto ComponentToGrab = HitResult.GetComponent(); // gets the mesh in our case
  52.     auto ActorHit = HitResult.GetActor();
  53.  
  54.     /// If we hit something, then attach a physics handle
  55.     if (ActorHit)
  56.     {
  57.         if (!PhysicsHandle) { return; }
  58.         PhysicsHandle->GrabComponent(
  59.             ComponentToGrab,
  60.             NAME_None, // no bones needed
  61.             ComponentToGrab->GetOwner()->GetActorLocation(),
  62.             true // disable rotation
  63.         );
  64.     }
  65. }
  66.  
  67. void UGrabber::Release()
  68. {
  69.     if (!PhysicsHandle) { return; }
  70.     PhysicsHandle->ReleaseComponent();
  71. }
  72.  
  73. // Called every frame
  74. void UGrabber::TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction )
  75. {
  76.     Super::TickComponent( DeltaTime, TickType, ThisTickFunction );
  77.  
  78.     if (!PhysicsHandle) { return; }
  79.     // if the physics handle is attached
  80.     if (PhysicsHandle->GrabbedComponent)
  81.     {
  82.         // move the object that we're holding
  83.         PhysicsHandle->SetTargetLocation(GetReachLineEnd());
  84.     }
  85.        
  86. }
  87.  
  88. const FHitResult UGrabber::GetFirstPhysicsBodyInReach()
  89. {
  90.     /// Line-trace (AKA ray-cast) out to reach distance
  91.     FHitResult HitResult;
  92.     FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetOwner());
  93.  
  94.     GetWorld()->LineTraceSingleByObjectType(
  95.         OUT HitResult,
  96.         GetReachLineStart(),
  97.         GetReachLineEnd(),
  98.         FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
  99.         TraceParameters
  100.     );
  101.  
  102.     return HitResult;
  103. }
  104.  
  105. FVector UGrabber::GetReachLineStart()
  106. {
  107.     FVector PlayerViewPointLocation;
  108.     FRotator PlayerViewPointRotation;
  109.     GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
  110.         OUT PlayerViewPointLocation,
  111.         OUT PlayerViewPointRotation
  112.     );
  113.  
  114.  
  115.     return PlayerViewPointLocation;
  116. }
  117.  
  118. FVector UGrabber::GetReachLineEnd()
  119. {
  120.     FVector PlayerViewPointLocation;
  121.     FRotator PlayerViewPointRotation;
  122.     GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
  123.         OUT PlayerViewPointLocation,
  124.         OUT PlayerViewPointRotation
  125.     );
  126.  
  127.  
  128.     return PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement