Jump to content

  • Log In with Google      Sign In   
  • Create Account


picking in 2d


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 giugio   Members   -  Reputation: 209

Like
0Likes
Like

Posted 15 November 2012 - 05:57 AM

hello.
I implemented a simple 2d drawing sysytem that draws points and lines(for now)
I must implement a picking in 2d for intersection of a mouse click and a point and a line.
Has a sense create a ray for the intersection in 2d?
how i can implement these algorithm?
I also have to consider the modelview matrix and the scale/rotate that i can apply to the document .
there is an example?
thanks.

Edited by giugio, 15 November 2012 - 06:12 AM.


Sponsor:

#2 Yours3!f   Members   -  Reputation: 1249

Like
1Likes
Like

Posted 15 November 2012 - 07:29 AM

hello.
I implemented a simple 2d drawing sysytem that draws points and lines(for now)
I must implement a picking in 2d for intersection of a mouse click and a point and a line.
Has a sense create a ray for the intersection in 2d?
how i can implement these algorithm?
I also have to consider the modelview matrix and the scale/rotate that i can apply to the document .
there is an example?
thanks.


hey there, I don't really know how to deal with lines (approximate them as thin quads maybe?), but I've recently done this using triangles/quads:
http://www.gamedev.net/topic/633528-2d-picking-theory/

you might get an idea.

#3 Lauris Kaplinski   Members   -  Reputation: 841

Like
0Likes
Like

Posted 17 November 2012 - 03:56 AM

Mathematically speaking you cannot find the intersection of mouse and point/line because the latter do not have an area. What you probably need is finding the distance between mouse cursor and your object - line or point. And if the distance is below certain value you treat your object as "selected".

To find distance between two pints you can simply use Euclidean distance formula.

Here is an example (in C) how to find distance between line segment and point. You have to test for three possible cases - the closest point to cusrsor is one endpoint, other endpoint or certain mid-line point.

static double
nr_line_point_distance2 (float Ax, float Ay, float Bx, float By, float Px, float Py)
{
double Dx, Dy, s;
double dist2;
Dx = Bx - Ax;
Dy = By - Ay;
s = ((Px - Ax) * Dx + (Py - Ay) * Dy) / (Dx * Dx + Dy * Dy);
if (s <= 0.0) {
  dist2 = (Px - Ax) * (Px - Ax) + (Py - Ay) * (Py - Ay);
} else if (s >= 1.0) {
  dist2 = (Px - Bx) * (Px - Bx) + (Py - By) * (Py - By);
} else {
  double Qx, Qy;
  Qx = Ax + s * Dx;
  Qy = Ay + s * Dy;
  dist2 = (Px - Qx) * (Px - Qx) + (Py - Qy) * (Py - Qy);
}
return sqrt (dist2);
}

Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS