Dear all,
I have run into problem with trigonometry. Although I though that I made everything correctly, it seems, that I am making some stupid mistake, which is slowly driving me crazy. I have actor with attached camera to spring arm, which has set pitch to -60deg. No other rotation is set anywhere.
I am using deprojection of mouse and then I draw line in direction of camera (it is easy in case of ortho camera, which I have) based on some easy calculation I made. My goal is to find location in parallel plane to ground (will always be flat plane). All I get is result as you may see in picture 1. If I put some magic constant to code I get result from picture 2, which is exactly I need. I have extended the code as much as possible to be readable for you.
APlayerController * controller = Cast<APlayerController>(this->GetController());
FVector mouseOrigin;
FVector mouseDirection;
float mouseX, mouseY;
controller->GetMousePosition(mouseX, mouseY);
controller->DeprojectMousePositionToWorld(mouseOrigin, mouseDirection);
float angleB = FMath::Abs(springArm->GetComponentRotation().Pitch) + 4.5f; //is not aligned without 4.5f ultra magic constant
float angleA = 90.f - angleB;
float sideB = springArm->TargetArmLength;
float sideC = sideB / UKismetMathLibrary::DegSin(angleA);
float sideA = FMath::Sqrt(FMath::Square(sideC) - FMath::Square(sideB));
float cameraOrtoHeight = camera->OrthoWidth / camera->AspectRatio;
float viewportHeight = GEngine->GameViewport->Viewport->GetSizeXY().Y;
float newSideA = sideA + ((cameraOrtoHeight / 2.f) - (cameraOrtoHeight * (mouseY / viewportHeight)));
GEngine->AddOnScreenDebugMessage(1, 1.f, FColor::White, FString::FromInt(mouseY));
float newSideB = newSideA / UKismetMathLibrary::DegSin(angleB);
newSideB = FMath::Sqrt(FMath::Square(newSideB) - FMath::Square(newSideA));
DrawDebugLine(GetWorld(), mouseOrigin, mouseOrigin + mouseDirection * newSideB, FColor::Red, true, 10.f);
I am fighting it over 4 hours without result.