DeprojectScreenPositionToWorld and ProjectWorldLocationToScreen don't match

DeprojectScreenPositionToWorld and ProjectWorldLocationToScreen seem to not match.

FVector2D ScreenLoc;
bool Pressed;
GetInputTouchState(ETouchIndex::Touch1, ScreenLoc.X, ScreenLoc.Y, Pressed);

FVector WorldLoc, WorldDir;

UE_LOG(LogScript, Warning, TEXT("ScreenLoc Dep %f %f"), ScreenLoc.X, ScreenLoc.Y);
DeprojectScreenPositionToWorld(ScreenLoc.X, ScreenLoc.Y, WorldLoc, WorldDir);

ProjectWorldLocationToScreen(WorldLoc, ScreenLoc);
UE_LOG(LogScript, Warning, TEXT("ScreenLoc Proj %f %f"), ScreenLoc.X, ScreenLoc.Y);
	
FVector2D ViewportSize;
GEngine->GameViewport->GetViewportSize(ViewportSize);
UE_LOG(LogScript, Warning, TEXT("ViewportSize %f %f"), ViewportSize.X, ViewportSize.Y);

FVector TraceEnd = WorldLoc + WorldDir * 10240;
	
FHitResult HitInfo;
FCollisionQueryParams QParams; 
FCollisionObjectQueryParams OParams;
GetWorld()->LineTraceSingle(HitInfo, WorldLoc, TraceEnd, QParams, OParams);
DrawDebugSphere(GetWorld(), HitInfo.ImpactPoint, 10, 10, FColor::Red, false, 1);

DeprojectScreenPositionToWorld works fine, I get the DebugSphere exactly where I touched.
But the screen coordinates that I get from ProjectWorldLocationToScreen(WorldLoc, ScreenLoc); don’t match.

I get this in the log, when I pressed near the bottom, right edge:

LogScript:Warning: ScreenLoc Dep 1136.159180 425.000000
LogScript:Warning: ScreenLoc Proj 806.860779 301.869476
LogScript:Warning: ViewportSize 1139.000000 428.000000

I’m using version 4.4.0.

Hi,

I know that is a quite old question, but nevertheless I would like to contribute.

I stumbled upon a similar issue with ProjectWorldLocationToScreen. I wanted to project the location of an actor to screen coordinates and then see if that actor was within a selection box. I can’t say if I managed to fix the problem, but have a look at the following function:

bool ProjectWorldToScreen(UPlayer* Player, FVector WorldLocation, FVector2D& ScreenLocation)
{
	// Code from: APlayerController::ProjectWorldLocationToScreen 
	ULocalPlayer* LocalPlayer = Cast<ULocalPlayer>(Player);
	if (LocalPlayer != NULL && LocalPlayer->ViewportClient != NULL && LocalPlayer->ViewportClient->Viewport != NULL)
	{
		// Create a view family for the game viewport
		FSceneViewFamilyContext ViewFamily(FSceneViewFamily::ConstructionValues(
			LocalPlayer->ViewportClient->Viewport,
			LocalPlayer->GetWorld()->Scene,
			LocalPlayer->ViewportClient->EngineShowFlags)
			.SetRealtimeUpdate(true));

		// Calculate a view where the player is to update the streaming from the players start location
		FVector ViewLocation;
		FRotator ViewRotation;
		FSceneView* SceneView = LocalPlayer->CalcSceneView(&ViewFamily, /*out*/ ViewLocation, /*out*/ ViewRotation, LocalPlayer->ViewportClient->Viewport);

		if (SceneView)
		{
			// This does not return the desired result
			//return SceneView->WorldToPixel(WorldLocation, ScreenLocation);

			FIntPoint size = LocalPlayer->ViewportClient->Viewport->GetSizeXY();
			int ClipX = size.X;
			int ClipY = size.Y;

			// Code taken from UCanvas::Project
			FPlane V = SceneView->Project(WorldLocation);
			FVector resultVec(V);
			resultVec.X = (ClipX / 2.f) + (resultVec.X*(ClipX / 2.f));
			resultVec.Y *= -1.f * GProjectionSignY;
			resultVec.Y = (ClipY / 2.f) + (resultVec.Y*(ClipY / 2.f));

			// if behind the screen, clamp depth to the screen
			if (V.W <= 0.0f)
			{
				resultVec.Z = 0.0f;
			}
			
			ScreenLocation.X = resultVec.X;
			ScreenLocation.Y = resultVec.Y;

			return true;
		}
	}

	return false;
}

In my case it fixed my problem.

Figured the issue has to do with Resolution Scale
Doesn’t happen if I have it set it to 100%.

I am also having this problem (and solution above works for me as well). I get this problem due to ScreenPercentage being set to something other than 100%.

I used this for an enemy indicator at the edge of the screen. When W is <= 0.f , I also mirrored X and Y

// if behind the screen, clamp depth to the screen
if (V.W <= 0.0f)
{
    resultVec.Z = 0.0f;
    resultVec.X *= -1.f;
    resultVec.Y *= -1.f;
}