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

Unproject() inaccurate?

Last post 6/11/2008 3:24 PM by BenS1. 9 replies.
  • 6/8/2008 9:31 AM

    Unproject() inaccurate?

    Having finished the main engine of my game, I thought it was best to write a level editor rather than manually creating the xml level files as I have done so far.

    The game is what I call 2.5D. Its basically a 2D game (Think asteroids)  but using 3D models for improved lighting effects etc. (All objects are on the Z=0 plane).

    Anyway, for the level editor I used the WinForms tutorials as my basis and created a GameWorldControl to show my game level info. The next step was to allow the user to select objects (e.g. asteroids) and place them int he game world using the mouse.

    I'm using the Unproject method to convert the mouse co-ords into world co-ords, but it appears to be pretty inaccurate.

    At the centre of the screen the unproject seems to be perfectly accurate, but the further you move from the centre the bigger the error becomes (Linearly). At the screen edges the error is about 50 pixels, which equates to several thousand units in the game world.

    The snippet of code that tries to convert from client area co-ords to world co-ords is here:

     

                Vector3 clickpos = new Vector3(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y, 0); 
     
                // And project them back into world co-ords 
                Vector3 worldpos = GraphicsDevice.Viewport.Unproject(clickpos, m_ProjectionMatrix, m_ViewMatrix, Matrix.Identity); 
     

    And I'm using the exact same matrixies int he drawing code!

    I've seen the thread Unproject() Insanity and whilst there were some good theories in there, and Shawn confirmed that it is in the XNA bug database, it didn't really give a solution.

    Has anyone managed to their own accurate version (That ideally takes the same params as the original version)?

    Thanks for your help
    Ben

  • 6/8/2008 9:42 AM In reply to

    Re: Unproject() inaccurate?

    What code do you use to generate your view and projection matrices ? I was having problems with this function using too small or too big values as near and far plane. Now i got it working correctly.     
    Xna Game Developper Ankama Play
  • 6/8/2008 9:55 AM In reply to

    Re: Unproject() inaccurate?

    Hi jbriguet, the code I use to create the projection matrix is:

                float aspectRatio = (float)Width / (float)Height; 
                m_ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView( 
                                                                MathHelper.ToRadians(45.0f), aspectRatio, 
                                                                m_CameraHeight - 3000.0f, 
                                                                m_CameraHeight + 3000.0f); 
     

    And the view matrix: 

     
                // Setup the camera and view matrix  
                m_CameraPosition = new Vector3(0.0f, 0.0f, m_Level.CameraHeight);  
                m_ViewMatrix = Matrix.CreateLookAt(m_CameraPosition, Vector3.Zero, Vector3.Up);  
      

    Where CameraHeight is typically100000(!!!)

    The values may seem big, but they have to be as I'm currently using some of the models from the XNA samples/tutorials (Especially the asteroids type one) and the models are huge. If I reduce the distance betweent he near/far planes then the models get clipped.

    Thanks
    Ben
  • 6/8/2008 2:30 PM In reply to

    Re: Unproject() inaccurate?

    Maybe you should look this way, i got some problems using unusual values for near and far, resolved by using more classical values (like 10-10 000). You probably should try to apply a scale matrix to your models if they're too huge.
    Xna Game Developper Ankama Play
  • 6/8/2008 9:05 PM In reply to

    Re: Unproject() inaccurate?

    You seem to have misunderstood that last two arguments to CreatePerspectiveFieldOfView, I can't see why it should have anything to do with the height of the camera... the second last argument is almost always ~0.1 or 1, depending on what units you're using. The last argument is just a "draw distance" and shouldn't have anything to do with how high up your camera is..., these arguments are both relative to the camera's position, they aren't in world space. Near plane distance == "the minimum distance an object can be from the camera before it doesn't get drawn" ~ 0.1 metres for a first person shooter perhaps, or several metres in a space sim. Far plane distance == "the maximum distance an object can be from the camera before it doesn't get drawn", perhaps 2000 metres up to tens of kilometres, but you lose accuracy the further the near/far numbers are apart. Neither can be zero or negative, and far must be greater than near.
  • 6/9/2008 8:30 AM In reply to

    Re: Unproject() inaccurate?

    Hi Adam,

    Thanks for your comments.

    You raise an interesting point actually. I'm well aware of what a near plane and far plane are as I've been doing 3D graphics since the days of the Atari ST, however I've got no idea why I have made it relative to the camera height in this game! I must of done it for a reason, but I can't for the life of me remember why! :)

    The strange thing is that with my camera height being about 100000, that means my near and far planes are at 97000 and 103000! Which makes me wonder why all my objects which are positioned at Z=0 are drawn at all.

    I'll investigate further.

    Thanks
    Ben

  • 6/9/2008 8:40 AM In reply to

    Re: Unproject() inaccurate?


    jbriguet:
    Maybe you should look this way, i got some problems using unusual values for near and far, resolved by using more classical values (like 10-10 000). You probably should try to apply a scale matrix to your models if they're too huge.


    Ok let me see if I can see what's going on with my near/far planes and see if that helps.

    I don't think changing the scale is the solution though. I think reducing the scale will probably reduce in inaccuracy problem, but it feels like a bit of a hack. If Unproject works properly then it should work at any scale.

    Thanks
    Ben
  • 6/10/2008 12:03 PM In reply to

    Re: Unproject() inaccurate?

    Ok I sorted out my Near/Far planes but it made absolutely no difference. :(

    Its pretty fundimental that I get this to work else I can't use the mouse in my level editor. Any more suggestions?

    Thanks
    Ben

  • 6/11/2008 1:14 PM In reply to

    Re: Unproject() inaccurate?

    The only working solution I know of right now is to include MDX and use their Unproject().
  • 6/11/2008 3:24 PM In reply to

    Re: Unproject() inaccurate?


    Adam Miles:
    You seem to have misunderstood that last two arguments to CreatePerspectiveFieldOfView, I can't see why it should have anything to do with the height of the camera... the second last argument is almost always ~0.1 or 1, depending on what units you're using. The last argument is just a "draw distance" and shouldn't have anything to do with how high up your camera is..., these arguments are both relative to the camera's position, they aren't in world space. Near plane distance == "the minimum distance an object can be from the camera before it doesn't get drawn" ~ 0.1 metres for a first person shooter perhaps, or several metres in a space sim. Far plane distance == "the maximum distance an object can be from the camera before it doesn't get drawn", perhaps 2000 metres up to tens of kilometres, but you lose accuracy the further the near/far numbers are apart. Neither can be zero or negative, and far must be greater than near.

    Hi Adam,

    I've finally found where I got that code from... its from one of the tutorials! Build a Game in 60 Minutes 

    It looks like I was doing it right after all.

    Still doesn't help though... I still need an accurate Unproject method. It looks like I'll have to dig out the code I wrote way back in DirectX 5 to do it (When I started on an Age of Empires type game).

    Thanks
    Ben

Page 1 of 1 (10 items) Previous Next