mouse click coords to screen coords

Started by
15 comments, last by __Daedalus__ 19 years, 7 months ago
Quote:Original post by danromeo
Check out the directx Pick sample...cut and paste the code...you're done.

Seems like somebody posts the same question every day or two...and I was guilty of it myself once. MS provided a lot of fairly concise samples with DirectX that illustrate a lot of the concepts that people commonly use, and they included a SampleBrowser app to make samples easy to find. For all of the complaining that everybody does (including myself) about MS's documentation, they made up for that by providing public domain code that can often be pasted right into your program. I think their logic was that it's easier step through the code to see what's going on than to read some really confusing tutorial. We can save ourselves a lot of wheel-reinventing by following the breadcrumbs that Uncle Bill left for us.


Microsoft have a habit of releasing convoluted and confusing code. There are so many more resources in book or web tutorial form that are simpler to read and easier to understand. nitzan is doing the right thing by trying to understand what is going on in the code. It is not usually a good idea to just copy-paste code without understanding the algorithm.
Advertisement
if you are looking to get a book to help you on this (despite us working on computers/having the internet, books are an excellent learning tool), then try

Introduction to 3D Game Programming with DirectX 9.0
by Frank D. Luna

it has a chapter all about picking, which is why i got it in the first place, but it also has lots of other information on bounding volumes and even HLSL. I'll try and find a link for it somewhere and edit this post.

EDIT: heres the link to it on this site.

http://www.gamedev.net/columns/books/bookdetails.asp?productid=386
I was wondering if you all can help me since the top is picking. I've been messing around with this off and on for the last two weeks, and am really close, I hope, to having it solved but I've run out of ideas.

To give you the context of the problem, the program models the solar system, kind of taking it to scale was a bad idea. So the planets orbit around the sun and the camera is locked at a 45 degree down angle, no rotation, but the user can scroll the camera around. I started with the DX9 SDK example on picking, and then went and bought the book Intro 3D Programming with DX9, and am using the bounding spheres that he recommends. The problem is that the picking hotspots don't seem to rotate with the planets, they stay where the planet starts. The code for the transformations looks like this:
(pseudo code)

GetViewMatrix(view)

CalculatePickingRay(mouseX, mouseY)

mesh = planets_transformations_during_drawing;

takeInverse(InverseMesh, mesh)

takeInverse(InverseView, view)

TransformRay(view)

TransformRay(mesh)

CheckRaySphereInersect(planet.bounding_sphere)

I've played with the order of transformations, multipling them together, not taking inverses, I did find that if I don't take the inverse of the mesh it does rotate but backwards and much faster than the planet, and taking the transpose of each matrix. So what simple error am I making?

Thanks for the help

EDIT:

WHOPS, silly me. I forgot to set the view back to 3d space. I was drawing some 2d stuff above the 3d objects and forgot to reset it when calculating the pick ray.

Thank you for all the help everyone. It works great now.

Nitzan
@ MountainLion2

i presume your talking og the Frank D Luna book?

you need to transform the bounding volumes to the position of your planets. they wont go there on their own! :P

you could either update their position every cycle, or (and this is what i did, dont know if its the best way though) move the bounding spheres to the planets position when your checking for a hit.

i know the book doesnt spell this out very well, it doesnt mention that you have to move the spheres around (which is common sense really)

when you do your test for a hit, set the boundingspheres center to the planets coordinates, and the radius should be a constant depending on the size of the planet.

i know were kinda hijacking somebody elses thread, but i cant help but help! :P
Multiverse,

Thanks for the help, and I apalogize to the others for jumping in on their thread. I thought it'd be a good place to ask my question without having to start another thread on picking :)

I set the bounding spheres center coord to those of my planets, and I asked the computer to be a pal and draw some spheres to show me where the bounding spheres were. The spheres are moving along with the planets, but when I go to click on them, nothing happens :( Do I need to do something to adjust the ray? I am setting the center corrdinates after the bounding sphere has been calculated.

Oh, and yes its the Frank D. Luna book, which is very good and worth getting. But after this class I'm looking forward to going backk t OpenGL.

MountainLion2
Quote:Original post by MountainLion2
Multiverse,

Thanks for the help, and I apalogize to the others for jumping in on their thread. I thought it'd be a good place to ask my question without having to start another thread on picking :)

I set the bounding spheres center coord to those of my planets, and I asked the computer to be a pal and draw some spheres to show me where the bounding spheres were. The spheres are moving along with the planets, but when I go to click on them, nothing happens :( Do I need to do something to adjust the ray? I am setting the center corrdinates after the bounding sphere has been calculated.

Oh, and yes its the Frank D. Luna book, which is very good and worth getting. But after this class I'm looking forward to going backk t OpenGL.

MountainLion2


With reference to my code above, if you have computed a test ray origin and ray direction in world coordinates, all you need to do is transform both vectors by the inverse of the planet you are testing's world transformation matrix. This moves the ray in to the body space of the planet. Then you do the intersection test.

This topic is closed to new replies.

Advertisement