4
$\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$
2
  • $\begingroup$ Please explain why you downvote when you do. $\endgroup$
    – arthur.sw
    Dec 2, 2015 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, 2015 at 23:48

1 Answer 1

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$
6
  • $\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, 2015 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, 2015 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$ Dec 3, 2015 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, 2015 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$ Dec 5, 2015 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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