Last active
October 15, 2016 19:21
-
-
Save error454/8dc5b3c6ca0f8abfc6e29807542ccb3e to your computer and use it in GitHub Desktop.
Screen Edge Detection UE4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This code is meant to be placed in a Player Camera Manager | |
// ScreenX - screen space coordinate from [-1, 1] left to right that you want to find the world location of | |
// ScreenY - screen space coordinate from [-1, 1] top to bottom that you want to find the world location of | |
// OutWorldLocation - The vector to put the resulting world location in | |
// OutWorldDirection - The vector to put the resulting world direction in | |
// Controller - Controller to use for deprojection | |
// ViewportSize - The size of the viewport | |
bool APlayerCameraManager::DeprojectScreenSpaceToWorld(float ScreenX, float ScreenY, FVector& OutWorldLocation, FVector& OutWorldDirection, APlayerController* Controller, const FVector2D ViewportSize) const | |
{ | |
return Controller->DeprojectScreenPositionToWorld((ScreenX * ViewportSize.X + ViewportSize.X) * 0.5f, (ScreenY * ViewportSize.Y + ViewportSize.Y) * 0.5f, OutWorldLocation, OutWorldDirection); | |
} | |
void APlayerCameraManager::SomeOtherUpdateFunction() | |
{ | |
// The vectors in world space that we'll be finding | |
FVector BoundaryTopLeft; | |
FVector BoundaryBottomRight; | |
FVector BoundaryCenter; | |
// We need to find the boundaries of our desired screen space. There are some assumptions here: | |
// * The controller of this camera owns/possesses a pawn | |
// * The pawn is on the screen | |
// * The pawn's world location is more or less the center of his bounding bolume | |
FVector dir; | |
const FVector playerLoc = Controller->GetPawn()->GetActorLocation(); | |
const FVector cameraLoc = GetCameraLocation(); | |
const float distanceFromPlayerToCam = FVector::Dist(playerLoc, cameraLoc); | |
const FVector2D ViewportSize = FVector2D(GEngine->GameViewport->Viewport->GetSizeXY()); | |
// You'd likely have these two vectors to adjust what percent of the screen that you're finding the bounds of. For the whole screen | |
// it'd be as below. But you could also find the bounds of just the central portion of the screen [-0.5, 0.5] if you wanted to | |
// do safe zones etc. | |
FVector ScreenSpaceMin = FVector2D(-1.f, -1.f); | |
FVector ScreenSpaceMax = FVector2D(1.f, 1.f); | |
// There's a chance that deprojection might fail below, you deal with that | |
// The result of this deprojection is that BoundaryTopLeft will have the starting location of the desired screen bounds | |
// and dir will have the direction vector that lets us travel along that screen bound. | |
DeprojectScreenSpaceToWorld(ScreenSpaceMin.X, ScreenSpaceMin.Y, BoundaryTopLeft, dir, Controller, ViewportSize); | |
// This calculation uses the distance from the camera to the player to determine the world space boundary location | |
// that is on the same plane as the player pawn simply by traveling along the boundary that was previously found. | |
BoundaryTopLeft = BoundaryTopLeft + dir * distanceFromPlayerToCam; | |
// Now the same thing for the other two world space locations. | |
DeprojectScreenSpaceToWorld(ScreenSpaceMax.X, ScreenSpaceMax.Y, BoundaryBottomRight, dir, Controller, ViewportSize); | |
BoundaryBottomRight = BoundaryBottomRight + dir * distanceFromPlayerToCam; | |
DeprojectScreenSpaceToWorld(0, 0, BoundaryCenter, dir, Controller, ViewportSize); | |
BoundaryCenter = BoundaryCenter + dir * distanceFromPlayerToCam; | |
// You now have world space coordinate of the left/right/center. You can calculate the width and height of this play area as follows | |
const float CurrentTravelWidth = ((BoundaryBottomRight - BoundaryTopLeft) * 0.5f).ProjectOnTo(GetActorRightVector()).Size(); | |
const float CurrentTravelHeight = ((BoundaryTopLeft - BoundaryBottomRight) * 0.5f).ProjectOnTo(GetActorUpVector()).Size(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment