refactor physics handle code

This commit is contained in:
Tyrel Souza 2018-02-22 21:53:14 -05:00
parent d7a96d278d
commit b7be0494d7
2 changed files with 31 additions and 52 deletions

View File

@ -5,6 +5,7 @@
#include "GameFramework/Actor.h" #include "GameFramework/Actor.h"
#include "PhysicsEngine/PhysicsHandleComponent.h" #include "PhysicsEngine/PhysicsHandleComponent.h"
#include "Components/InputComponent.h" #include "Components/InputComponent.h"
#include "Components/PrimitiveComponent.h"
#include "Public/DrawDebugHelpers.h" #include "Public/DrawDebugHelpers.h"
#define OUT #define OUT
@ -31,31 +32,14 @@ void UGrabber::BeginPlay() {
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{ {
Super::TickComponent(DeltaTime, TickType, ThisTickFunction); Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
SetPlayerLookingAt();
// if physics handle is attached /// if physics handle is attached move object holding
// 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 (PhysicsHandle->GrabbedComponent) { if (PhysicsHandle->GrabbedComponent) {
PhysicsHandle->SetTargetLocation(LineTraceEnd); PhysicsHandle->SetTargetLocation(GetReachLineEnd());
} }
} }
// Raycast and grab what is in reach // Raycast and grab what is in reach
void UGrabber::Grab() { void UGrabber::Grab() {
UE_LOG(LogTemp, Warning, TEXT("GRABBING!")); UE_LOG(LogTemp, Warning, TEXT("GRABBING!"));
@ -65,16 +49,10 @@ void UGrabber::Grab() {
auto ActorHit = HitResult.GetActor(); auto ActorHit = HitResult.GetActor();
// if we hit something attach physics handle // if we hit something attach physics handle
// TODO: attach physics handle
if (ActorHit) { if (ActorHit) {
PhysicsHandle->GrabComponent( PhysicsHandle->GrabComponent(ComponentToGrab, NAME_None,
ComponentToGrab, ComponentToGrab->GetOwner()->GetActorLocation(), true);
NAME_None,
ComponentToGrab->GetOwner()->GetActorLocation(),
true //allow rotation
);
} }
} }
// Release // Release
@ -88,9 +66,7 @@ void UGrabber::Release() {
// Look for attached physics handle // Look for attached physics handle
void UGrabber::FindPhysicsHandleComponent() { void UGrabber::FindPhysicsHandleComponent() {
PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>(); PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
if (PhysicsHandle) { if (PhysicsHandle == nullptr) {
} else {
UE_LOG(LogTemp, Error, TEXT("%s has no PhysicsHandle"), *(GetOwner()->GetName())); UE_LOG(LogTemp, Error, TEXT("%s has no PhysicsHandle"), *(GetOwner()->GetName()));
} }
} }
@ -100,10 +76,8 @@ void UGrabber::SetUpInputComponent() {
InputComponent = GetOwner()->FindComponentByClass<UInputComponent>(); InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
if (InputComponent) { if (InputComponent) {
UE_LOG(LogTemp, Warning, TEXT("%s has a InputHandle"), *(GetOwner()->GetName())); UE_LOG(LogTemp, Warning, TEXT("%s has a InputHandle"), *(GetOwner()->GetName()));
InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab); InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab);
InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::Release); InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::Release);
} else { } else {
UE_LOG(LogTemp, Error, TEXT("%s has no InputHandle"), *(GetOwner()->GetName())); UE_LOG(LogTemp, Error, TEXT("%s has no InputHandle"), *(GetOwner()->GetName()));
} }
@ -111,27 +85,14 @@ void UGrabber::SetUpInputComponent() {
FHitResult UGrabber::GetFirstPhysicsBodyInReach() const 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 /// Line-Trace (Ray-Cast) out to reach distance
FHitResult Hit; FHitResult Hit;
FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetOwner());
GetWorld()->LineTraceSingleByObjectType( GetWorld()->LineTraceSingleByObjectType(
OUT Hit, OUT Hit,
PlayerViewpointLocation, GetReachLineStart(),
LineTraceEnd, GetReachLineEnd(),
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody), FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
TraceParameters TraceParameters
); );
@ -144,7 +105,18 @@ FHitResult UGrabber::GetFirstPhysicsBodyInReach() const
return Hit; return Hit;
} }
FVector UGrabber::GetReachLineEnd() const {
return PlayerViewpointLocation + PlayerViewpointRotation.Vector() * Reach;
}
//DrawDebugLine(GetWorld(), PlayerViewpointLocation, LineTraceEnd, FColor(255, 0, 0), false, FVector UGrabber::GetReachLineStart() const {
// 0.f, 0, 5.0f return PlayerViewpointLocation;
//); }
void UGrabber::SetPlayerLookingAt() {
GetWorld()->GetFirstPlayerController(
)->GetPlayerViewPoint(
OUT PlayerViewpointLocation,
OUT PlayerViewpointRotation
);
}

View File

@ -38,4 +38,11 @@ private:
// Return hit for first physics body in reach // Return hit for first physics body in reach
FHitResult GetFirstPhysicsBodyInReach() const; FHitResult GetFirstPhysicsBodyInReach() const;
FVector PlayerViewpointLocation;
FRotator PlayerViewpointRotation;
FVector GetReachLineEnd() const;
FVector GetReachLineStart() const;
void SetPlayerLookingAt();
}; };