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

Viewport.Unproject inconsistency in XNA 2.0 Beta

Last post 12/14/2007 9:11 AM by Nightin. 6 replies.
  • 12/13/2007 4:58 AM

    Viewport.Unproject inconsistency in XNA 2.0 Beta

    When I ported my game from XNA 1.0 Refresh to XNA 2.0 (Beta), the picking isn't working properly.
    The pick ray jitters a lot and I guess it's the problem with Viewport.Unproject.
    Whenever my view matrix changed a little bit, the direction of the pick ray changed back and forth.
    I'm using the approach refered in the XNA documentation to compute the pick ray.

    After I implemented my own version of pick ray generation, the jitter problem went away and everything went back to normal :)

    This isn't happening in XNA 1.0, so I guess it's probably a potential bug in XNA 2.0.

    Is there anyone experienced the same problem? Tell me why this is happening.

  • 12/13/2007 10:47 AM In reply to

    Re: Viewport.Unproject inconsistency in XNA 2.0 Beta

    Can you post the solution somewhere so we can try a repro (comment out one of the versions so we can switch between it).
    Play Kissy Poo - a game for 4 year olds on Xbox and windows
    The ZBuffer
    News and information for XNA
      Follow The Zman on twitter, Email me
        Please read the forum FAQs - Bug/Feature reporting
          Don't forget to mark good answers and good playtest feedback when you see it!!!
  • 12/14/2007 3:08 AM In reply to

    Re: Viewport.Unproject inconsistency in XNA 2.0 Beta

    We've also had problems with Viewport.Unproject when creating a ray for picking. When using x,y,1 - x,y,0 it sometimes created an inversed ray. We solved the problem by using x,y,0.75f - x,y,0.25f instead; since we normalize the result anyway.
  • 12/14/2007 5:00 AM In reply to

    Re: Viewport.Unproject inconsistency in XNA 2.0 Beta

    I just converted the Triangle Picking with Triangle Accuracy sample from 1.0 to 2.0 to see if I could see any issues with the way Unproject worked and I could see none. No jittering, no inverse rays, it just worked. It does appear that Unproject (and possibly Project, I didn't check) are now implemented in C# rather than making a native call as in 1.0, but as I say, I can't see anything wrong with it. I don't pretend to be able to quite understand the math behind the Unproject method, but jittering isn't likely to occur unless you're feeding it different arguments each time you call it, it could conceivably be buggy, but its pretty unlikely Unproject is non-deterministic.
  • 12/14/2007 6:39 AM In reply to

    Re: Viewport.Unproject inconsistency in XNA 2.0 Beta

    This is the sample program modified from XNA tutorial, hope it demonstrates something.

    I'm using Visual Studio 2005 Team + XNA 2.0 Build 2.0.11022.3.

    http://files-upload.com/files/677911/Unproject.zip

    The link will be expired in 14 days :(

  • 12/14/2007 7:39 AM In reply to

    Re: Viewport.Unproject inconsistency in XNA 2.0 Beta

    I've been having a look at that sample, and while I can't really see that you're doing anything wrong per se, the problem is certainly lessened substantially by pushing your near clip plane out to 10 or 100, which suggests to me there might be some floating point inaccuracy going on, perhaps when using/normalising vectors somewhere, but as yet I can't fix it for sure.

  • 12/14/2007 9:11 AM In reply to

    Re: Viewport.Unproject inconsistency in XNA 2.0 Beta

    Pushing the near clip plane isn't quite a good solution to this, here's the method I worked out for generating pick ray:

    void UpdatePickRay()
    {
    MouseState mouseState = Mouse.GetState();

    Vector3 v;
    v.X = (((2.0f * mouseState.X) / screenWidth) - 1);
    v.Y = -(((2.0f * mouseState.Y) / screenHeight) - 1);
    v.Z = 0.0f;

    pickRay.Position.X = viewInverse.M41;
    pickRay.Position.Y = viewInverse.M42;
    pickRay.Position.Z = viewInverse.M43;
    pickRay.Direction = Vector3.Normalize(
    Vector3.Transform(v, viewProjectionInverse) - pickRay.Position);
    }

    Where viewInverse, projectionInverse and viewProjectionInverse are computed every frame using

     
    projection = camera.Projection;
    viewProjection = view * projection;
    viewInverse = Matrix.Invert(view);
    projectionInverse = Matrix.Invert(projection);
    viewProjectionInverse = projectionInverse * ViewInverse;
     
    This method works well for picking.
    The only difference is that the pick ray position is exacly the eye position using this method,
    by using the method refered in the XNA documentation, pick ray position is on the near clip plane.
    But in most cases this difference is trivial.
Page 1 of 1 (7 items) Previous Next