From b7be0494d7dc784c0778fc86d79baa299764cce9 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Thu, 22 Feb 2018 21:53:14 -0500 Subject: [PATCH] refactor physics handle code --- RoomEscape/Source/RoomEscape/Grabber.cpp | 76 ++++++++---------------- RoomEscape/Source/RoomEscape/Grabber.h | 7 +++ 2 files changed, 31 insertions(+), 52 deletions(-) diff --git a/RoomEscape/Source/RoomEscape/Grabber.cpp b/RoomEscape/Source/RoomEscape/Grabber.cpp index f636a68..f2876aa 100644 --- a/RoomEscape/Source/RoomEscape/Grabber.cpp +++ b/RoomEscape/Source/RoomEscape/Grabber.cpp @@ -5,6 +5,7 @@ #include "GameFramework/Actor.h" #include "PhysicsEngine/PhysicsHandleComponent.h" #include "Components/InputComponent.h" +#include "Components/PrimitiveComponent.h" #include "Public/DrawDebugHelpers.h" #define OUT @@ -31,31 +32,14 @@ void UGrabber::BeginPlay() { void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); + SetPlayerLookingAt(); - // if physics handle is attached - // move object holding - // do nothing - /// Get player View point in this tick - FVector PlayerViewpointLocation; - FRotator PlayerViewpointRotation; - GetWorld()->GetFirstPlayerController( - )->GetPlayerViewPoint( - OUT PlayerViewpointLocation, - OUT PlayerViewpointRotation - ); - - FVector LineTraceEnd = PlayerViewpointLocation + PlayerViewpointRotation.Vector() * Reach; - + /// if physics handle is attached move object holding if (PhysicsHandle->GrabbedComponent) { - PhysicsHandle->SetTargetLocation(LineTraceEnd); + PhysicsHandle->SetTargetLocation(GetReachLineEnd()); } - - - - } - // Raycast and grab what is in reach void UGrabber::Grab() { UE_LOG(LogTemp, Warning, TEXT("GRABBING!")); @@ -65,16 +49,10 @@ void UGrabber::Grab() { auto ActorHit = HitResult.GetActor(); // if we hit something attach physics handle - // TODO: attach physics handle if (ActorHit) { - PhysicsHandle->GrabComponent( - ComponentToGrab, - NAME_None, - ComponentToGrab->GetOwner()->GetActorLocation(), - true //allow rotation - ); + PhysicsHandle->GrabComponent(ComponentToGrab, NAME_None, + ComponentToGrab->GetOwner()->GetActorLocation(), true); } - } // Release @@ -88,9 +66,7 @@ void UGrabber::Release() { // Look for attached physics handle void UGrabber::FindPhysicsHandleComponent() { PhysicsHandle = GetOwner()->FindComponentByClass(); - if (PhysicsHandle) { - - } else { + if (PhysicsHandle == nullptr) { UE_LOG(LogTemp, Error, TEXT("%s has no PhysicsHandle"), *(GetOwner()->GetName())); } } @@ -100,10 +76,8 @@ void UGrabber::SetUpInputComponent() { InputComponent = GetOwner()->FindComponentByClass(); if (InputComponent) { UE_LOG(LogTemp, Warning, TEXT("%s has a InputHandle"), *(GetOwner()->GetName())); - InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab); InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::Release); - } else { UE_LOG(LogTemp, Error, TEXT("%s has no InputHandle"), *(GetOwner()->GetName())); } @@ -111,27 +85,14 @@ void UGrabber::SetUpInputComponent() { FHitResult UGrabber::GetFirstPhysicsBodyInReach() const { - /// Get player View point in this tick - FVector PlayerViewpointLocation; - FRotator PlayerViewpointRotation; - GetWorld()->GetFirstPlayerController( - )->GetPlayerViewPoint( - OUT PlayerViewpointLocation, - OUT PlayerViewpointRotation - ); - - FVector LineTraceEnd = PlayerViewpointLocation + PlayerViewpointRotation.Vector() * Reach; - - ///Setup Query Parameters - FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetOwner()); /// Line-Trace (Ray-Cast) out to reach distance FHitResult Hit; - + FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetOwner()); GetWorld()->LineTraceSingleByObjectType( OUT Hit, - PlayerViewpointLocation, - LineTraceEnd, + GetReachLineStart(), + GetReachLineEnd(), FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody), TraceParameters ); @@ -144,7 +105,18 @@ FHitResult UGrabber::GetFirstPhysicsBodyInReach() const return Hit; } +FVector UGrabber::GetReachLineEnd() const { + return PlayerViewpointLocation + PlayerViewpointRotation.Vector() * Reach; +} -//DrawDebugLine(GetWorld(), PlayerViewpointLocation, LineTraceEnd, FColor(255, 0, 0), false, -// 0.f, 0, 5.0f -//); \ No newline at end of file +FVector UGrabber::GetReachLineStart() const { + return PlayerViewpointLocation; +} + +void UGrabber::SetPlayerLookingAt() { + GetWorld()->GetFirstPlayerController( + )->GetPlayerViewPoint( + OUT PlayerViewpointLocation, + OUT PlayerViewpointRotation + ); +} \ No newline at end of file diff --git a/RoomEscape/Source/RoomEscape/Grabber.h b/RoomEscape/Source/RoomEscape/Grabber.h index c662110..ae4efdd 100644 --- a/RoomEscape/Source/RoomEscape/Grabber.h +++ b/RoomEscape/Source/RoomEscape/Grabber.h @@ -38,4 +38,11 @@ private: // Return hit for first physics body in reach FHitResult GetFirstPhysicsBodyInReach() const; + + FVector PlayerViewpointLocation; + FRotator PlayerViewpointRotation; + + FVector GetReachLineEnd() const; + FVector GetReachLineStart() const; + void SetPlayerLookingAt(); };