Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

[.net] Problem with Vector3.Unproject

Started by Misha 3 replies 3.6k views
Original Post
Misha
Misha
Hi, I have a static method that Unprojects screen coordinates into 3D. But sometimes Vector3.Unproject returns wrong result. For example: //Coordinates x = 753; y = 516; //Set the points that should be Unprojected nearVector.X = x; nearVector.Y = y; nearVector.Z = 0; farVector.X = x; farVector.Y = y; farVector.Z = 1; //Then I'll project them back into screen coordinates //(to show the problem) Vector3 ufv; Vector3 unv; //Unproject the points nearVector.Unproject(engine.activeDevice.Viewport, engine.activeDevice.Transform.Projection, engine.activeDevice.Transform.View, engine.activeDevice.Transform.World); farVector.Unproject(engine.activeDevice.Viewport, engine.activeDevice.Transform.Projection, engine.activeDevice.Transform.View, engine.activeDevice.Transform.World); //And Get back the scren coordinates ufv = farVector; unv = nearVector; ufv.Project(engine.activeDevice.Viewport, engine.activeDevice.Transform.Projection, engine.activeDevice.Transform.View, engine.activeDevice.Transform.World); unv.Project(engine.activeDevice.Viewport, engine.activeDevice.Transform.Projection, engine.activeDevice.Transform.View, engine.activeDevice.Transform.World); //------------------ So, after the first call all works great, and Unproject -> project is correct 1 Here is the example X 753.0001 Y 515.999939 But, after all following calls, I have the wrong results, like this 2 Click X 981.5509 Y 440.579651 If Someone Has an Idea, about the mistake, Please answer
Phillip Hamlyn
Phillip Hamlyn
I had problems with Unproject when used with Intersect. It might just be related to your problem ? The problem generated problems with mouse-picking on meshes using Unproject and Intersect where the intersect point (and therefore the unproject) were slightly but noticibly inaccurate. This was caused in the end by my Near Clipping Plane being too close to the camera. Mine was set at 0.5 and when I extended it to 1.0 the problem went away - caused I think by a floating point artefact.

Phillip
The Frugal Gourmet
The Frugal Gourmet
Idea: Try just using Matrix.Identity instead of the world projection.
Co-creator of Star Bandits -- a graphical Science Fiction multiplayer online game, in the style of "Trade Wars'.
Misha
Misha
Hi!
Thanks for answers :)
I found the mistake yesterday.
After that code I drawed a mesh (with a world Transform)
So
engine.activeDevice.Transform.World = Matrix.Translation(0,0,0);
before Unproject solved the problem
DrGUI
DrGUI
I found that Vector3.Project was leaking Direct3D.Viewport[s] onto the GC heap.
Which is rather strange since that is a value type but there it was in the CLR Profiler, so I just rewrote that function.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.