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

Unproject does not return correct results [XNA 2.0]

Last post 13/06/2008 13:11 by testi. 7 replies.
  • 20/12/2007 8:39

    Unproject does not return correct results [XNA 2.0]

    Hello,

    there is something wrong with Unproject in XNA 2.0. I used it in my XNA 1.1 code and everything worked like expected. In XNA 2.0 it was reimplemented in c# and it seems like something went wrong. It doesn't work properly!

    Here is a very basic example that shows that:

    Vector3 a = m_viewport.Project(new Vector3(0.4f, 0.3f, 0.2f), m_projection, m_view, Matrix.Identity);
    Vector3 b = m_viewport.Unproject(a, m_projection, m_view, Matrix.Identity);

    b should now contain 0.4, 0.3, 0.2, but it doesnt! The results are way off.

    Here are the matrix values copied from the immediate window before doing the Project call:
    m_projection
    {M11:0,9395257 M12:0 M13:0 M14:0}
    {M21:0 M22:1,732051 M23:0 M24:0}
    {M31:0 M32:0 M33:-1,001001 M34:-1}
    {M41:0 M42:0 M43:-0,1001001 M44:0}

    m_view
    {M11:1 M12:0 M13:0 M14:0}
    {M21:0 M22:1 M23:0 M24:0}
    {M31:0 M32:0 M33:1 M34:0}
    {M41:0 M42:0 M43:-8 M44:1}

    Here is how these were created:

    m_view = Matrix.CreateTranslation(-m_position) * Matrix.Transpose(Matrix.CreateFromQuaternion(m_rotation));

    fieldOfView = (float)Math.PI / 3f;
    aspectRatio = (float)m_viewport.Width / m_viewport.Height; // 1.8
    nearPlaneDistance = 0.1f;
    farPlaneDistance = 100f
    m_projection = Matrix.CreatePerspectiveFieldOfView(fieldOfView, aspectRatio, nearPlaneDistance, farPlaneDistance);


    Maybe somebody from the XNA team or a forum member can reproduce this for verification.

    Any help is greatly appreciated - thanks.


  • 20/12/2007 10:11 In reply to

    Re: Unproject does not return correct results [XNA 2.0]

    There's another thread in the forum claiming perhaps that the changes to Unproject have made it inconsistent/buggy:

    http://forums.xna.com/thread/36985.aspx

    I believe too there is now a connect issue filed against this, so I would recommend voting on it (unless you're the person that filed it, in which case I'm sure it'll be looked into when everyone is back at work properly in the new year.

  • 20/12/2007 10:59 In reply to

    Re: Unproject does not return correct results [XNA 2.0]

    I actually read the other thread before writing this, but it seemed like the author was satisfied with his result and I didn't if both issues are actually the same.
    How can I vote for a connect issue?
  • 20/12/2007 11:52 In reply to

    Re: Unproject does not return correct results [XNA 2.0]

    https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=318306&SiteID=226

    You'll need to login to your passport account to be able to vote.

  • 14/04/2008 9:11 In reply to

    Re: Unproject does not return correct results [XNA 2.0]

    Hi,

    I have just discovered the hard way that unproject doesn't return the right results... it seems to be missing a transform or something in there. My resulting mouse ray seems to always go through the origin (starting at the correct position, to an extent).

    I tried using the connect link to vote (above) but I got a page not found.

    I think it might be time to pull out my old matrix class and try and get its unproject working... Unless the xna unproject can be fixed..?

    public static Vector3 Unproject(Camera camera, Vector3 screenSpace)

    {

    return camera.Viewport.Unproject(screenSpace, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity);

    }

    Vector3 screenNear = new Vector3((float)x, (float)y, 0f);

    Vector3 screenFar = new Vector3((float)x, (float)y, 1f);

    Vector3 worldNear = Unproject(camera, screenNear);

    Vector3 worldFar = Unproject(camera, screenFar);

    Vector3 direction = worldFar - worldNear;

    direction.Normalize();

    linee.Start = worldNear;

    linee.End = worldNear + (direction * 1000f);

     

    in theory the line that this code draws should be invisible, under the mouse cursor. Instead, it can be seen to always pass through the origin.

     

    -dexy

  • 17/04/2008 18:24 In reply to

    Re: Unproject does not return correct results [XNA 2.0]

    Even if you cannot vote you should upload your repro case here is how http://forums.xna.com/thread/1864.aspx

    (you got the 404 becuase i think you need to be registered and logged into connect to see bugs)

    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!!!
  • 17/04/2008 23:51 In reply to

    Re: Unproject does not return correct results [XNA 2.0]

    Thanks Zman,
    I think I got the 404 because I wasn't logged in to connect at the time. I logged in then went searching for unproject manually.
    I'll make a little repro example when I get a chance to demonstrate problems with unproject(). It will also contain my solution to the problem, which I have outlined in a post
    here. (http://forums.xna.com/thread/57432.aspx)

    The samples suffer from the problem, but because of the ray points being transformed by the object's world matrices, the problem is not so obvious. You can however see problems if you rotate the camera, while hovering over an object, in certain positions, intersections will not be recognised (and the label doesnt appear even though you are over an object).

    As I have mentioned before, I'm not going to rely on the Viewport.Unproject() being fixed, since I wrote my own unproject method, but I really think it should be fixed because I think anyone who tries using it is going to run into the same nightmare I did.

    -dexy
  • 13/06/2008 13:11 In reply to

    Re: Unproject does not return correct results [XNA 2.0]

    The way I see it, the bug I explained in the very first post of this topic still exists in XNA 3.0 CTP.

    Edit: Looking at the reflected code, it seems that both methods divide by the the float number in the WithinEpsilon if clause. That seems definitly wrong, but multiplying only brings me closer to the correct result and does not return the correct one. Doing the experiment in the first post with a parallel projection matrix returns the correct result btw (I use that as a workaround for now).

    Has anybody tried to reproduce this?

Page 1 of 1 (8 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG