Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Not entirely certain of the nomenclature here -- basically, after placing a model in world coordinates and setting up a 3D camera to look at it the model has been projected onto the screen in a 2D fashion.

What I'd like to do is determine if the mouse is inside the projected view of the model.

Is there a way to "unproject" in the XNA framework? Or what is this process called as, so that I can better search for it?

share|improve this question
    
It's called 3D picking. As far as I know, XNA does have an unproject, but I don't use it so I'm not sure. –  Byte56 Jul 9 '12 at 17:58
2  
Thanks, I was able to find a sample code when I knew to search on "Picking" –  user1169578 Jul 9 '12 at 18:13
    
Great! I suppose I could make an answer, but I think you should make one detailing what you found. Then accept it as the correct answer when you're allowed to do so. –  Byte56 Jul 9 '12 at 18:16
    
I will, am still working out the details of it all. Thanks for the suggestion. –  user1169578 Jul 10 '12 at 15:09
    
XNA has unproject (as well as project) in the viewport: GraphicsDevice.Viewport.Unproject(...) –  Daniel Carlsson Aug 13 '12 at 11:39
add comment

1 Answer

I can suggest a method i use

First of all, you must create a ray that represents your mouse position in 3d space

vector3 near = new vector3(mouse.x,mouse.y,0);
vector3 far = new vector3(mouse.x,moue.y,1);

near GraphicsDevive.Viewport.Unproject(near,view,projection,world);
near GraphicsDevive.Viewport.Unproject(far,view,projection,world);

matrix world must be inverse of your models world matrix.

vector3 direction = far - near;
Ray ray = (near,direction);

the test this ray for collision with models bounding sphere.

I demand excude for this arbitrary answer that can't be pasted in your project, but im little bit lazy to consult references.

UPDATE. http://xbox.create.msdn.com/en-US/education/catalog/sample/picking_triangle Here is an accurate example of model picking. It is more accurate than mine, but it involves ModelProcessor modifying.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.