Skip to main content
gamedev.net
🔒 Locked

Selecting objects

Started by zennehoy Oct 9, 2002 at 7:19 PM 4 replies 1.2k views
Original Post
zennehoy
zennehoy
I''me sure this has been asked before, but with the forum search not working I couldn''t find anything. I''me trying to select a 3d object on the screen using the mouse. I think I''me at a point where I can generate mouse position and direction vectors (ie a ray going into the screen with a starting point and an orientation), but I can''t figure out how to check if this ray runs through a triangle (three points). If anyone could point me to a tutorial on this or give me some more direct help, that would be great! Thanks, Zen I''me working in C++ and DirectX, so code for that would be preferable. Thanks.
Jim Adams
Jim Adams
Check the DX 8.x SDK sample called Pick. It has a few methods in there for detecting which triangle was clicked on. Only problem - Pick doesn''t account for meshes that are not located at the origin. to fix this problem, transform your ray and directional vector by the inversed world transformation of the mesh you are checking.
zennehoy
zennehoy
Hmmm, well I tried that, and it sort of works. It doesn't work correctly though. Depending on the angle I look at the object from, the selectable region for the same object changes.
Looking straight from the top, I get the following:


  
_____
|1___|_
||2 | |
|| | |
||___| |
|_____|


Region 1 is the object, region 2 is where it's selectable. Changing the viewing angle also changes the selectable region(2).

I must be forgetting a transform somewhere, but I can't figure out which one. Thanks for the help,
Zen

Edit: added source tags - Jim


[edited by - Jim Adams on October 9, 2002 9:54:44 PM]
Jim Adams
Jim Adams
quote:

// Compute the vector of the pick ray in screen space
v.x = ( ( ( 2.0f * x ) / s_width ) - 1 ) / matProj._11;
v.y = -( ( ( 2.0f * y ) / s_height ) - 1 ) / matProj._22;
v.z = 1.0f;



Make sure s_width, x, and y are all cast to float.

Are you using m._41, m._42, and m._43 as the ray''s x, y, z origin?

Are you transforming the ray''s position and directional vectors using D3DXVecTransformCoord and D3DXVecTransformNormal, respectively?

I use the exact code in my last book and haven''t noticed any problems. Maybe you should post the entire section of code for your object picking so we can get a better look at it.

zennehoy
zennehoy
I don''t know why this always happens this way, but just posting about a problem a few times will help solve it. I was initializing directx before my window resized itself, so I was using different values of s_width and s_height to initialize my projection matrix and calculating the ray direction.
Gaah do I hate stupid errors!!!
Thanks for the help!
Zen

Topic Locked

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

Sign in to reply to this topic.