Trigonometry problem with ortho camera

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.

Let P0 be the vector origin of your plane

Let N be the normal vector of your plane

Let L0 be the vector origin of your camera

Let L be the normalized direction of your camera

Let K be the point of intersection of the cameras direction and the plane

Then the inersection between the camera and the plane can be found as
d = ((P0 - L0) dot N) / (L dot N)

K = d*L + L0

Although I agree with you, that solving it algebraic way is far more elegant, I am interested, why the simple pythagoras theorem does not work in this case. Where does the 4,5 deg deviation come from? I have checked all transforms and everything is set correctly am I missing something or… ?

I think there’s a disconnect between the reality of the situation and the situation your describing in the code. For instance your side B is set to the target length of the spring arm but in the picture you provided that would be side A. In general you depiction is hard to follow so it’s probably best to reduced to the essence of the problem which is to find the intersection between a vector and a plane.

Oh, I see. The target lies in the place where side B and side C meets, so the spring arm is really side B.

But I understand, that your solution is working and all is good, but that does not answer my question, why is there the angle discrepancy? :slight_smile: I’ll try to form explanation of problem better.