2

I’m designing a 3d game and am having trouble computing where the mouse pointer is actually pointing on the flat map when taking into account perspective which skews the view.

Rendered image showing surfaces and lines described below

Let’s assume that the flat plane is part of the map, and the tilted plane is the viewport. Player’s camera is rotated arbitrarily but always pointing up, and has a defined position $C$. Cyan line crosses both $C$ and $O$, so cyan vector $\vec{d}=\overline{CO}$ is known.

Orange line is parallel to cyan line, and were all objects rotated and translated so that skewed surface becomes $z=0$ and $C$ becomes $(0, 0, 0)$, then the point of intersection $M$ between orange line and skewed surface would become $(x,y,0)$.

I know that, if the system was translated and rotated like so, the point I’m looking for would be somewhere on the line $\overline{CM}$ between $C$ and $M$, but can’t figure out exactly where.

How do I get the point of intersection between the flat surface and the orange vector?

Mirac7
  • 305

1 Answers1

0

I figured out the answer:

Point $C$ is the location of the camera, it is looking at $(0, 0, 0)$. That means that we have cyan vector $\vec{d}=(-M_x,-M_y,-M_z)$. Length of $\vec{d}$ is $l=\sqrt{M_x^2+M_y^2+M_z^2}$.

We now define $\vec{d'}=\frac{\vec{d}}{l}$ in order to get a vector with $|\vec{d'}|=1$.

We introduce vector $\vec{u}$ as $\vec{u}=(0, 0, 1)$. This is generalisation, as it determines which direction is up relative to the camera's view. In my case, I use $(0, 0, 1)$, because that's hardcoded in my game.

Again, for generalisation purposes, we define $\vec{u'}=\frac{\vec{u}}{|\vec{u}|}$.

We'll need one more vector, $\vec{v}=\vec{u'}\times\vec{d'}$.

Now, what the camera sees depends on the field of view. We know the angle which the camera sees, let's call it $\varphi$. Then, $t_{FOV}=\tan{\frac{\varphi}{2}}$.

We create new vectors $\vec{u''}=\vec{u'}t_{FOV}$ and $\vec{v''}=\vec{v'}t_{FOV}$.

We know the point of intersection of orange vector and skewed plane is $M$ which is really the point on the screen we see, and we know the size of the screen is $(w,h)$. The coordinate lies somewhere on this vector:

$$\vec{m}_x=\vec{d}_x+\vec{u}_x(1-\frac{2M_y}{h})+\vec{v}_x(\frac{2M_x}{w}-1)$$ $$\vec{m}_y=\vec{d}_y+\vec{u}_y(1-\frac{2M_y}{h})+\vec{v}_y(\frac{2M_x}{w}-1)$$ $$\vec{m}_z=\vec{d}_z+\vec{u}_z(1-\frac{2M_y}{h})+\vec{v}_z(\frac{2M_x}{w}-1)$$

Now we define $\vec{m''}=\frac{\vec{m'}}{|\vec{m'}|}$ and $\lambda=-\frac{\vec{d}_Z}{\vec{m''}_z}$

Finally, The point we are looking for is

$$(-\vec{d}_x-\vec{m''}_x\lambda, -\vec{d}_y-\vec{m''}_y\lambda)$$

Mirac7
  • 305