Sign in to follow this  
JohannesH

Viewport.project unproject question

Recommended Posts

Using XNA 3.1, I am trying to get world x,y coordinates back from a mouse click. I am trying to find the world x,y position at z=0, however I can't seem to figure out how. I unproject twice in the sample below. AT first with z=0, then with z=1. Since my mouse test data is 10,10 and my world 0,0,0 is at about 200,200 pixels, I would have expected x,y coordinates to be very different than what I get. :-( How do I get my x,y world coordinates from my Mouse X,Y assuming z at 0 ??? result from code below: MouseY,MouseY=10,10 -> -7.763712 ,-0.5084655 ,-4.569074 MouseY,MouseY 2=10,10 -> 5322.184 ,-2543.376 ,623.7775 Pixel=9.999941 ,10 ,-5.522809E-07 This is the code I am using: int MouseX = 10; int MouseY=10; Vector3 pos = new Vector3(); pos = GraphicsDevice.Viewport.Unproject(new Vector3(MouseX, MouseY, 0), effect.Projection, effect.View, effect.World); System.Diagnostics.Debug.WriteLine("MouseY,MouseY=" + MouseX + "," + MouseY + " -> " + pos.X + " ," + pos.Y + " ," + pos.Z); Vector3 pos2 = new Vector3(); pos2 = GraphicsDevice.Viewport.Unproject(new Vector3(MouseX, MouseY, 1), effect.Projection, effect.View, effect.World); System.Diagnostics.Debug.WriteLine("MouseY,MouseY 2=" + MouseX + "," + MouseY + " -> " + pos2.X + " ," + pos2.Y + " ," + pos2.Z); pos = GraphicsDevice.Viewport.Project(new Vector3(pos.X, pos.Y, pos.Z ), effect.Projection, effect.View, effect.World); System.Diagnostics.Debug.WriteLine("Pixel=" + pos.X + " ," + pos.Y + " ," + pos.Z);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this