HitTest - Calculate offset created by perspective.

53 Views Asked by At

I developing a Direct X application and I need to made a Hit Test. Inside my application, it's like have always 2 Camera. The first one was called Ortho2D and the second Called Perspective3D.

Ortho2D (Locked, no Z-Buffer)

  • Eye (0, 0, 10, 0)
  • At (0, 0, 0, 0)
  • Up (0, 1, 0, 0)

Perspective3D (Free)

  • Eye (0, 300, 1000, 0)
  • At (0, 0, 0, 0)
  • Up (0, 1, 0, 0)

The Current HitTest work pretty well in Ortho2D, but I get some problem about Perspective3D... I'm sure it's about FOVy, nearClipping, FarClipping and aspect ratio. But before, There is the history of HitTest function.

  1. I begin to create my function by testing my hit point with the area of the triangle ( Check whether a point is within a 3D Triangle) . At first, it's seem working well only in Ortho2D... but, wait a minute, this calculation was only valid if hit point respect triangle.
  2. Then I decided to getting only z with a basic planar projection. OK, it's resolve Ortho2D glitch, but it's non-precise for Perspective3D.
  3. OK, Now that I've resolve projection to align with camera. It's really work well in Ortho2D, but for the Perspective3D I still have offset precision

There is the basic calculation that I've transfer inside Mathematica.

(* Variables *)
P1 = {-150, 0, -150}
P2 = {150, 0, -150}
P3 = {150, 0, 150}
HP = {122,36,0}
Cam = {0, 300, 1000}
CamAt = {0, 0, 0}
CamUp = {0, 1, 0}
FOVy = 70
NearClipping = 0.1
FarClipping = 2000
Width = 1920
Height = 1080

(* Calculation *)
U = P2 - P1;
V = P3 - P1;
UVNorm = Cross[U, V]

Dir = CamAt - Cam
W0 = HP - P1
a = -Dot[UVNorm, W0]
b = Dot[UVNorm, Dir]
r = a/b

HP2 = HP + r* Dir

(* Apply HitPoint2 with Triangle Area *)

With this calculation the HitPoint(122, 36, 0) result to HitPoint2(122, 0, -120). But in reality, I have target the second point of the triangle and the HitPoint2 must be nearest (150, 0, -150). The Coord of Third Triangle Point on Screen was (160, -42)

Now, how calculate the derivation create by the frustum view?

Does I must apply before projection on HitPoint or during the projection or after?

Does I can apply on HitPoint or I must absolutely apply on every triangle  point?

Thanks.