• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BlackJoker

Vector3.Unproject return NAN [SharpDX]

16 posts in this topic

Hi, I am trying to setup picking in my project, but there is strange issue, which not let me do this. Vecto3.Unproject returns NAN always.

 

I found out that its because of Matrix.Inverse inside that method and as a result inversed matrix have 0 in all fields.

All transmitted parameters are correct.

 

So, I wonder isis that correct that from WVP matrix during inversion I always receive Zero matrix?

 

I leave here some code to be sure I transfer parameters correct:
 

ZNear = 0.01;

ZFar = 10000000000000;

mouse coords and ViewPort also correct;

But both vectors receive NAN as a result value;

private void Pick(int mouseX, int mouseY, ref Matrix wvpMatrix, BoundingBox boundingBox)
        {

            Vector3 mouseNearVector = new Vector3(mouseX, mouseY, basicCamera.ZNear);
            Matrix m = Matrix.Identity;
            m = Matrix.Invert(wvpMatrix);
            Vector3 pointNear = Vector3.Unproject(mouseNearVector, 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, basicCamera.ZNear, basicCamera.ZFar,
                wvpMatrix);

            Vector3 mouseFarVector = new Vector3(mouseX, mouseY, basicCamera.ZFar);
            Vector3 pointFar = Vector3.Unproject(mouseFarVector, 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, basicCamera.ZNear, basicCamera.ZFar,
                wvpMatrix);

            Ray ray = new Ray(pointNear, Vector3.Normalize(pointFar - pointNear));
            float distance = 0;
            bool intersect = boundingBox.Intersects(ref ray, out distance);
            D2DText = "Intersect: " + intersect.ToString() + " distance = "+distance;
            intersect = false;
        }

Does Vector3.Unproject() method working correct or I am missing something?

 

Also it works really strange - sometime it returns true if no model under mouse and in the same time returns NAN for distance or return true and real distance value when no model under cursor is present.

It very strange that sometime picking is working at least somehow and the other time its not working at all.

Edited by BlackJoker
0

Share this post


Link to post
Share on other sites

Your far clip plane is way too big. It should probably be no bigger than 1000 or so.

2

Share this post


Link to post
Share on other sites

No effect - both points are still NAN with 1 and 1000. But with 0 and 100 far vector is calculated, but near vector still NAN with any value I give it. 

0

Share this post


Link to post
Share on other sites

So, I wonder isis that correct that from WVP matrix during inversion I always receive Zero matrix?

 

Not all matrices are invertible. The determinant of the matrix should be non-zero for it to be invertible. Try using Matrix.Inverse on the identity matrix - the result should be Identity. If that works, maybe your input matrix is the problem.

 

Also, your ZFar value is way too large. D3D (and maybe SharpDX as well?) converts 64-bot doubles to 32-bit floats internally, and the maximum integer that can be represented accurately as a 32-bit float, without precision loss is 16777216. Once you go above this value, weird things start to happen because of the precision loss.

Edited by tonemgub
0

Share this post


Link to post
Share on other sites


Not all matrices are invertible. The determinant of the matrix should be non-zero for it to be invertible. Try using Matrix.Inverse on the identity matrix - the result should be Identity. If that works, maybe your input matrix is the problem.
 
Also, your ZFar value is way too large. D3D (and maybe SharpDX as well?) converts 64-bot doubles to 32-bit floats internally, and the maximum integer that can be represented accurately as a 32-bit float, without precision loss is 16777216. Once you go above this value, weird things start to happen because of the precision loss.

 

Ok, I tested identity matrix and it works correct, so the problem could be in my projection matrix where I set znear and zfar values.

 

And I have one question regarding all this:

does mouseNearVector and mouseFarVector needs to contain ZNear and ZFar values corresponding? and Unproject method also needs to keep the same ZNear and ZFar? Or values can differ?

 

And why I can`t use for ex 100 000 as zFar or 0.01 as ZNear? In that case how I will create ray which could reach the far plane of camera view if this is possible by backbuffer and projection matrix?

 

 


 

0

Share this post


Link to post
Share on other sites

does mouseNearVector and mouseFarVector needs to contain ZNear and ZFar values corresponding? and Unproject method also needs to keep the same ZNear and ZFar? Or values can differ?

 

Sorry, I can't tell you if your existing values are ok. You'll have to test that yourself.

 

IMHO yes, you should try to keep all your values between -16777216 and 16777216. If possible, you should use values even closer to 0 then that, so that any operations done by D3D on them don't go under/over these values either. There is a good explanation of this here: http://stackoverflow.com/questions/12596695/why-does-a-float-variable-stop-incrementing-at-16777216-in-c

 

Also, I think that your initial ZFar value is already NaN by the time you pass it to Unproject. There's no way such a large number can fit in a float. Maybe you also get a compiler warning if it's a float and the initial value is too large so it sets it to NaN? Or if it's a double, you'll get a warning about it being converted to float when you pass it to Unproject...

 

About ZNear: if you set it too small, you will get terrible aliasing. I don't remember why this is, but the recommentadion is that you set it to at least 0.5.

 

 


In that case how I will create ray which could reach the far plane of camera view if this is possible by backbuffer and projection matrix?

You don't need to create a vector (ray) that reaches that far by Unproject. Just create a normalized one, and then you can use scalar multiplication to make it whatever length you want. You can also convert your normalized vector to use doubles instead of floats for this if you need it to be longer than 16777216 (does SharpDX have a Vector class which uses doubles?). Just keep in mind that when you pass those double values back to D3D, they will be converted back to float...

Edited by tonemgub
1

Share this post


Link to post
Share on other sites


Also, I think that your initial ZFar value is already NaN by the time you pass it to Unproject. There's no way such a large number can fit in a float. Maybe you also get a compiler warning if it's a float and the initial value is too large so it sets it to NaN?

 

No, ZFar is valid when I pass it to unproject and also my projection matrix works quite well without any problems, but i will try to descrease it to the valid float value.

0

Share this post


Link to post
Share on other sites

OK, I tried and it works, but why noone points me that 6th and 7th parameters in Unproject() is not ZNear and ZFar, but Viewport.MinDepth and ViewPort.MaxDepth which is 0 and 1.0f correspondingly?

0

Share this post


Link to post
Share on other sites

So, at last I coul unproject it and now it is not NAN, but I have the second question:

How to increase its length?

It return false when I far from the model and when I near - it always treu even if I not looking at the model. I suspect that I am inside bounding box and thats why it returns true.

 

SO, could you please tell how correctly make ray a maximum length possible?

0

Share this post


Link to post
Share on other sites

why noone points me that 6th and 7th parameters in Unproject() is not ZNear and ZFar, but Viewport.MinDepth and ViewPort.MaxDepth

Sorry, I'm not very good with SharpDX...

 

 


SO, could you please tell how correctly make ray a maximum length possible?

Why don't you just use Ray.GetPickRay: http://sharpdx.org/documentation/api/m-sharpdx-ray-getpickray?

 

Note also that from the description of Ray.Direction (http://sharpdx.org/documentation/api/f-sharpdx-ray-direction), it seems that Ray actually always holds a normalized (length 1) vector for the direction of the ray, so there's no such thing as a "maximum length" ray - SharpDX Rays always have length 1, and they are probably always treated as "infinite length" in calculations.

Edited by tonemgub
1

Share this post


Link to post
Share on other sites


Why don't you just use Ray.GetPickRay: http://sharpdx.org/documentation/api/m-sharpdx-ray-getpickray?

I don`t know why, but this method almost always return direction as NAN and correct position. Its strange, but it is. 

Unproject() work correct, but I can`t peek object because intersection gives me false when I far from object and true when I am inside bounds.

 

It returns true ONLY when camera is inside the boundbox.

 

This is how I check intersection.

Vector3 point;
bool intersect = ray.Intersects(ref box, out point);

The same result for all other intersections. So, I don`t know how to check intersection with the ray while camera is not inside the boundingbox...

0

Share this post


Link to post
Share on other sites

As far as I can tell so far, you are testing if a view-space Ray intersects with a world-space BoundingBox. To get a world-space ray, you have to multiply the Ray with the inverse of the view matrix.

 

But GetPickRay should also work IMHO. If I were you, I'd try figuring out why it returns NaN, by changing the input values,rather than re-implementing it's functionality from scratch. You had the same NaN problem with your own implementation, remember? And now we also know that it's pointless to give a huge value for ZFar, since in the end, Ray.Direction is always length 1 -- ZFar doesn't affect it in any way, except when you use a value that is too big, which will cause Ray.Direction to be filled wtih NaNs. Maybe if you reduce your ZFar value to something more acceptable, GetPickRay will work properly... First, give up the assumption that you have to use huge numbers for things to work properly (or that the computer can even interpret those huge numbers), then check, ouble-check,and triple-check all your variables in debug, to make sure they really are what you want them to be.

Edited by tonemgub
1

Share this post


Link to post
Share on other sites

OK, i found out the use case. Seems it works now. I have to increase ZNear to 10 to keep ZFar in my Projection Matrix with 16 000 000. In that case it works. Seems that than bigger ZFar, that zNear must also become bigger...

 

In near vector and far vector instead of ZNear, ZFar muste be 0 and 1 correspondingly. SO, both methods now working the same and at last picking is working correctly, but strange thing happens:

If i decrease ZNear in projection matrix <10 for ex, 8, ray becomes unstable and periodically looses boundingbox althought vectors are still NOT NAN. Some sort of magic happening here...

Edited by BlackJoker
0

Share this post


Link to post
Share on other sites

I don't understand why after everything I've explained, you still try to use large values for ZFar. Fine, then... just use ZFar=1000, because I'm telling you to smile.png.

 

 

 


Some sort of magic happening here...

There's nothing magic about it. It's something like this: SharpDX takes your huge 16000000 ZFar value and multiplies it by 2 (best case), or even by itself (worst case)... The result of this multiplication will be a number that is way above 16777216, and like I've been trying to tell you, that's where the crazy happens. You have to use values that account for whatever SharpDX or D3D might do with them. 1000 is pretty reasonable. Or do you have any reason that I don't know of to stick with 16000000?

Edited by tonemgub
0

Share this post


Link to post
Share on other sites

Or do you have any reason that I don't know of to stick with 16000000?

Yes. For now I use this huge numbers in my projection matrix to create projection for space simulator. If I use 1000, I wiil just don`t see nothing. I need at least 1 000 000 and more. IDK, maybe in future numbers will be changed, but for now I cannot do this.

But with 16 000 000 as zfar and 10 as znear works just fine.

 

BTW, ZFfar doesnt present in projection matrix in clear view. There is no huge numbers in result matrix, except position maybe. So, The only problem here is ration between ZFar and ZNear.

Edited by BlackJoker
0

Share this post


Link to post
Share on other sites


There is no huge numbers in result matrix, except position maybe.

There don't have to be... For bad things to happen, it's sufficient that an intermediary calculation produces a large value (that maybe then gets divided, subtracted or square-rooted back to a small value).

1

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  
Followers 0