Hello
I have a list of lines which go from point A to B and i am looking to find from my mouse position (which i have in world position) which line is closest to the mouse?
I am curious what would be the best way to do this?
Hello
I have a list of lines which go from point A to B and i am looking to find from my mouse position (which i have in world position) which line is closest to the mouse?
I am curious what would be the best way to do this?
Vector2.Distance() (or vector3), measure the distance, and whichever is shorter… is the closest?
How would i know the distance of a line since its going to have many distances along the line?
So, your trying to find the nearest point ON a line, in relation to the mouse your saying?
Say you have 4 lines that make a square
Your mouse is in that square, and you want to highlight the edge the mouse is closest to. Thats basically the idea i am going for here.
Well, yeah, still would probably be using the vector measuring classes, or just pretty much what they do internally, which is take the difference of two sums. You’d be like “Vector3 difference = SomePointIn2DOr3D - SomeOtherPoint;”
Yeah i assumed that to but given the line has infinitely many points i am not entirely sure how i would know what the other point would be to check.
Ohh right, well that depends how your doing the lines, if there is a way to get a point some percentage along each line, you could just sample the lines at like every 10 percent or so, and compare the points rather than the positions of the lines. If the lines aren’t really complex, you might be able to get away with just comparing the position of the center of the lines…
these are a couple of my functions from my Mathw Class
public static Vector3 ClosestPointToLine(Vector3 lineOrigin, Vector3 LineDirection, Vector3 point)
{
Vector3 originToPoint = point - lineOrigin;
float distanceFromOrigin = Vector3.Dot(LineDirection,originToPoint.normalized) * originToPoint.magnitude;
return lineOrigin + LineDirection.normalized * distanceFromOrigin;
}
public static Vector3 ClosestPointToClampedLine(Vector3 lineStart, Vector3 lineEnd, Vector3 point)
{
Vector3 LineDirection = lineEnd - lineStart;
Vector3 originToPoint = point - lineStart;
float distanceFromOrigin = Vector3.Dot(LineDirection,originToPoint.normalized) * originToPoint.magnitude;
return lineStart + LineDirection.normalized * Mathf.Clamp(distanceFromOrigin,0,LineDirection.magnitude);
}
ClosestPointToLine() finds the closest point along an infinite line towards a specified point
ClosestPointToClampedLine() finds the closest point along a line towards a specified point, but clamped between two specified limits along the line.
you can use ClosestPointToClampedLine() on each line, subtract that against the mouse position, and then find which ever has the smallest sqrMagnitude, and that will be your closest line.
Just a small addition. Thinking of this problem, I imagine that a click inside a square to send four lines in separate directions (T-Shaped). Whichever line was shortest on its hit is closest. As a thought exercise ![]()