   [SOLVED] point on line
  Hey!This is the problem i have:I draw a 3D line in glOrtho. Now i click with the mouse on that line. How do i know which exact point on that line did i click? (object coordinates, not screen or window coordinates)[Edited by - NDIR on August 7, 2010 3:53:44 PM]
  i have 2D (screen and world) representation of the line, projection matrices... i just need to convert mouse click coordinates to line coordinates.   ;  Well a line is in theory infinitely thin so you can never click on it (in theory!)In practice, a line is only a couple pixels thick (im guessing) so it's difficult to click directly on a line.I think I have a solution for you though (:You said it's 2d right?What i would do is take the click point and do a "Find closest point on line segment to point" between the line and the point you clicked on the screen.From there I would do a distance check between the point you just found on the line and the point the clicked on the screen.If the distance is less than some maximum amount that you decide on, then treat is as if the person clicked on the line at that point.   ;  well it's not exactly 2D. i have a 3D line so coordinates of the line when it is displayed on screen are not equal to line coordinates (since the line coordinates get transformed from object -> world -> screen space)   ;  Well in that case, cast a ray into the screen from the place the person clicked on the screen (check out glUnproject).From there do a "closest point on line to line segment" test to get the point on the line on the screen that is closest to where they clicked.From there, find the closest point on the ray they cast to that point.Then check distance test between those points to make sure they are closer than some amount you define.Or convert the shapes to 2d and do what i said in the other post :P   ;  1)Unproject the mouse positionUse a dotproduct to find the closest point on the line.You will need a depth value to unproject the mouse coordinates. You can obtain this from the depth buffer, but that means the user will have to click right on the line which can be hard when the line is thin. If that is the case, you can draw a larger rectangle offscreen as a bounding box.Now we have the unprojected point we can do this:given the line v0 to v1the unproject point p0the closes point on the line is v0 + the dotproduct of v1-v0 and p0-v0 times v1 (if im correct)2)(a little bit harder but at least you dont have to get values from the depth buffer)treat the mouse click as a vector coming from the camera going into the sceneyou will need a copy of the camera direction vector use this method to find the closest points beween the two lines (the line in the scene and the mouse click)http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline3d/   ;  if i convert them to 2D i will only get 2D coordinate of clicked point and that i can not useor in theory; i could:- take 2D coordinates where i clicked- convert to object coordinates using gluUnProject- (do some more inverse matrix operations)but the problem is that for unproject to work properly i need fragment Z and how can i get this fragment Z, but when i perform this calculations the target line has not been drawn yet.   ;  @Defeatist    i'll try the 2. since i prefer geometric approaches to this kind of operations   ;  Solved! I used 2) which Defeatist suggested. Thanks all!   