• Advertisement
Sign in to follow this  

[.net] Problem with Vector3.Unproject

This topic is 4342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement