Skip to main content
gamedev.net
🔒 Locked

selection

Started by mrmage 6 replies 1.3k views
Original Post
mrmage
mrmage
ok i have a plane made up of squares say 100x100 which is viewed from a birds eye perspective, now i want to be able to pinpoint a position on the plane by mouse click. i want the selection too be fairly accurate and open gl based selection doesnt really cut what i need to be able to do i was wondering if there is another option for selection? or do i just have to do the maths? if so i know how to find the closest point on a plane to a vector but how would i go about working out the vector of the mouse click? i guess the center of the viewport would just be the direction vector of what the camera is looking at but how does it change as you head out per pixel?
mrmage
mrmage
bumpy
V-man
V-man
Do the math. Use ray to triangle intersection test. Search for MagicSoftware and perhaps there are some other libs out there that can do it as well. My lib can do it too (http://www.geocities.com/vmelkon/glhlibrary.html)
Search for glhRayTriangle in the header file.
Ravuya
Ravuya
I use gluProject/gluUnproject to handle my picking-related tasks. Once you've unprojected the mouse cursor position, you can shoot a ray in the general direction and do triangle intersection. It's relatively simple.
rotalever
rotalever
Quote:
Original post by Ravuya
I use gluProject/gluUnproject to handle my picking-related tasks. Once you've unprojected the mouse cursor position, you can shoot a ray in the general direction and do triangle intersection. It's relatively simple.


But you should also use optimizations like octtree/quadtree or spatial subdivision. I tested spatial subdivision on a 1024² terrain and it is much faster (1-2ms compared to >20ms).
mrmage
mrmage
ok so using gluUnproject will give me a position vector of the mouse click but how do i work out the direction vector?
CodeMunkie
CodeMunkie
You could just do two "unprojects" at different depths to get your direction:

//origin will be a point on the view plane
gluUnProject(winX, winY, 0.0, modelview, projection, viewport, &posX, &posY, &posZ);
pick_ray.origin = Vector3(posX, posY, posZ);

//direction will point out from the view plane
gluUnProject(winX, winY, 1.0, modelview, projection, viewport, &posX, &posY, &posZ);
pick_ray.direction = (Vector3(posX, posY, posZ)-pick_ray.origin).direction();

This way works for me.
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.
zedz
zedz
Quote:
Original post by mrmage
ok so using gluUnproject will give me a position vector of the mouse click but how do i work out the direction vector?


perhaps
position vector of the mouse click - eye position.
though why do u need it cause the result u asked for in the original question is given by gluUnproject

personally ild go with V-mans solution though

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.