3
$\begingroup$

I try to project a 3D vector (a direction, not a position) in screen space, but it does not return satisfying results:

 FVector LineDirectionOnScreen = 
   EdMode->ViewMatrices.GetViewProjMatrix().TransformVector(LineDirection);
 FVector2D LineDirectionOnScreen2D(
   LineDirectionOnScreen.X * 0.5f * (float)EdMode->ViewRect.Width(), 
   LineDirectionOnScreen.Y * 0.5f * (float)EdMode->ViewRect.Height());

When LineDirection is (0,0,1) it always returns a something like (0,Y) (Y can be anything), no matter how the camera is rotated.

What is wrong with my code?

$\endgroup$
  • $\begingroup$ Please explain why you downvote when you do. $\endgroup$ – arthur.sw Dec 2 '15 at 9:58
  • $\begingroup$ I dont know why it was downvoted. but this seems like a close dupllicate to your other question. $\endgroup$ – joojaa Dec 2 '15 at 23:48
3
$\begingroup$

I don't think you can simply use TransformVector when the matrix you're transforming by involves a projection matrix. To fully project to screen space, you have to divide by W, which TransformVector doesn't do (it simply multiplies by the matrix without translation).

Also, because of the divide by W, transforming vectors (as opposed to points) by a projection matrix is probably not what you actually want. If what you want is the screen-space vector between two points, you should transform and divide-by-W each point, then subtract them to get a screen-space vector. Otherwise the vector will likely be inaccurate.

$\endgroup$
  • $\begingroup$ Ok, thanks for this answer! I used TransformVector after reading the first paragraph of this page about homogenous coordinates. I understood I should not worry about W when projecting a direction, but maybe I'm wrong. $\endgroup$ – arthur.sw Dec 3 '15 at 11:30
  • $\begingroup$ I thought about projecting both points on my screen, but I changed my mind because I thought it was not necessary. $\endgroup$ – arthur.sw Dec 3 '15 at 11:34
  • $\begingroup$ @arthur.sw That article is OK as far as affine transforms go, but when projections get involved, things are more complicated. $\endgroup$ – Nathan Reed Dec 3 '15 at 18:40
  • $\begingroup$ Yes, I would like to have more details about this, maybe I should ask this question on math.stackexchange? $\endgroup$ – arthur.sw Dec 5 '15 at 15:34
  • $\begingroup$ @arthur.sw What details are you interested in? I could add a quick example showing how the divide-by-W affects vectors, if that would be helpful. $\endgroup$ – Nathan Reed Dec 5 '15 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.