Advertisement

Mouse interaction in 3D Space

Started by October 09, 2002 01:39 AM
4 comments, last by MickePicke 22 years, 1 month ago
How do I get the coordinates when i click on a spot in 3D space? I can get the pixel I click on, but not the coordinates in my 3D world. How do i get them?
it''s a technique called ''picking'' that''s well treated in graphics. essentially, that pixel location on screen turns into a ray in 3 space, and you use that for intersection testing.

if you look around for picking, i''m sure you''ll find tons of resources.

dedrick@whiteknucklegames.com
dedrickwww.whiteknucklegames.com
Advertisement
I checked it out and I can only get a coordinate of a object.
I want to use it so that I can do a Command & Conquer-like game.
I select a object using picking, but then I want to click somewhere on my landscape (the whole landscape is only one object). If a can get the coordinate on the landscape which I clicked on, I can then move my selected object there. I have found a function called gluUnProject that does this, but I can´t get it to work. You need to have the xpos, ypos, and zpos. I can get the xpos and ypos from the mouse, but I can´t get the zpos.
If anyone knows how to use gluUnProject or something similar, please reply.
If ur landscape is an object u could cast a ray from x,y coordinates u have in the direction of the viewpoint and check where it intersects the landscape object. This is ur Z coord
same problem.
even if you knew how to use the gluUnProject, you wouldn''t get an x, y, and z pos that you need.

what you have to do is form a ray whose origin is at the camera''s position, that intersects the mousepoint on the projection plane. then you have to intersect that with your terrain for the final x,y,z position. there are tons of info out there on ray/triangle intersections, which is probably what you would end up using.

here''s a code snippet to form a ray that does as described. code is written for clarity, not speed:


  // given://  The mouse position in screen coords of the click://  POINT point;//  The camera with appropriate accessors//  Camera m_camera;//  The viewport with appropriate accessors//  Viewport m_viewport;//  The projection matrix//  Matrix44 m_projection;//  The view matrix//  Matrix44 m_view;Line line;Vector3 p1 = m_camera.GetPosition();Vector3 p2(point.x, point.y, 0.0f);// put this in normalized screen coords//  note this is different from device coords//  this ranges from [-1.0, 1.0] for x and y.//  also note that y is inverted relative to device space//  we choose the back plane as the projection planep2.x /= (0.5f * static_cast<real>(m_viewport.Width));p2.y /= (0.5f * static_cast<real>(m_viewport.Height));p2.x -= 1.0f;p2.y -= 1.0f;p2.y = -p2.y;p2.z = m_viewport.MaxZ;m_projection.Inverse();// a vector3 multiplied by a matrix44 is poorly defined.// for clarity, we make a Vector4 then reproject it back//  into three space by a division by wVector4 vec4(p2);vec4 *= m_projection;vec4 /= vec4.w;p2 = vec4;m_view.Inverse();// same as abovevec4 = p2;vec4 *= m_view;vec4 /= vec4.w;p2 = vec4;line.point = p1;line.direction = (p2 - p1).Normalize();// now do your intersection testing with line.  
dedrickwww.whiteknucklegames.com
Thanx Very very much for your helpful reply´s, cya
You haven´t heard the last of me, muhahahaha =)

This topic is closed to new replies.

Advertisement