XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

WinForms Picking Anomolies

Last post 07/11/2009 18:19 by ShadowyCore. 3 replies.
  • 07/11/2009 6:00

    WinForms Picking Anomolies

    I seem to be having a bizzare problem using Viewport.Unproject() with WinForms. Since a picture's worth a thousand words, here's a screenshot of the problem. The green X is the location of the cursor at the time. The blue dot represents the origin of the picking ray, and the other end of that yellow line is the point 100 units down the direction. The red dot is a point I'm trying to find that shows the closest vertex on a grid to the point of intersection between the cursor and the grid.

    I would have thought that that yellow line would have been invisible, or at best a point because it was straight through the cursor. Am I doing something wrong with this?

                Vector3 nearSource = new Vector3(mState.X, mState.Y, device.Viewport.MinDepth); 
                Vector3 farSource = new Vector3(mState.X, mState.Y, device.Viewport.MaxDepth); 
                Matrix world = Matrix.Identity; // Matrix.CreateTranslation(0f, 0f, 0f); 
     
                Vector3 nearPoint = device.Viewport.Unproject(nearSource, cam.Projection, cam.View, world); 
                Vector3 farPont = device.Viewport.Unproject(farSource, cam.Projection, cam.View, world); 
                chk = new Ray(nearPoint, Vector3.Normalize(farPont - nearPoint)); 
     
                // "plane" is a Plane object with Normal 0,1,0 and D of 0 
                float? dist = chk.Intersects(plane); 
                Vector3 intersect; 
                if (dist.HasValue) 
                    intersect = chk.Position + chk.Direction * dist.Value; 
                else 
                    intersect = Vector3.Zero; 
     
                closestpt = grid.GetClosestVertex(intersect); 

    Freefall Game Engine - In Development
  • 07/11/2009 11:19 In reply to

    Re: WinForms Picking Anomolies

    ShadowyCore:
    I would have thought that that yellow line would have been invisible, or at best a point because it was straight through the cursor. Am I doing something wrong with this?
    But it's a ray through the cursor to the scene's maximum depth, so unless the cursor is in the center of the screen, you should get a perspective projection of it. I wouldn't have expected a horizontal line, though...

    You're using the Unproject method correctly, and I know it works fine in winforms (I just implemented picking in winforms the other day, though I'm picking models rather than points in a plane). I don't see anything wrong with your posted code at all, to be frank. So my only guess is that the problem lies elsewhere; perhaps in your GetClosestVertex method?
  • 07/11/2009 18:01 In reply to

    Re: WinForms Picking Anomolies

    It's certainly possible. Does the Ray.Intersects(Plane) overload work every time? I don't know for certain whether it does or not.

    EDIT:
    It's also worth noting that my GetClosestVertex method will, without fail, pick the closest vertex to the blue dot on the screen. I'm not sure why, as the origin of the ray should be close to the camera.
    Freefall Game Engine - In Development
  • 07/11/2009 18:19 In reply to

    Re: WinForms Picking Anomolies

    Sorry, when drawing the yellow line I neglected to add the origin of the ray to the direction * 100 for the second point, thus the horizontal line. Here's what it really looks like.

    The blue dot is really two dots; there's a green dot beneath it, the other end of the ray. The weird offset is still there, though. Could it have something to do with the other UI on the form?
    Freefall Game Engine - In Development
Page 1 of 1 (4 items) Previous Next