Line Segment distance to a Point

I tried using Line#distance() but never really got it to work. I think it is because it is expecting an origin and a direction (a ray?) rather than two endpoints.

My question is there any way to do this in JME somewhere in its math library?

@Cmile said: I tried using Line#distance() but never really got it to work. I think it is because it is expecting an origin and a direction (a ray?) rather than two endpoints.

My question is there any way to do this in JME somewhere in its math library?

I’m not sure what you are talking about.

If you’ve defined the Line properly then that method will give you a distance to a point. If you are seeing otherwise then either something else is wrong or your perception of what is “right” is incorrect.

Edit: corrected my “two endpoints” comment. Is constructing the line your real problem? This shouldn’t be hard.

@pspeed said: I'm not sure what you are talking about.

If you’ve defined the Line properly then that method will give you a distance to a point. If you are seeing otherwise then either something else is wrong or your perception of what is “right” is incorrect.

Edit: corrected my “two endpoints” comment. Is constructing the line your real problem? This shouldn’t be hard.

Well, yeah. Constructing the line could be my problem. Here is how i did it: Given a line segment P1 to P2, and point C

[java] new Line(P1, P2).distance(C);[/java]

For testing i used a line segment with (0,0,0) to (0, 10, 0) and a point in (5, 3, 0). The result I expected was 5 since it’s just 5 units away from the nearest point in the line but I get 990 as a result.

@Cmile said: Well, yeah. Constructing the line could be my problem. Here is how i did it: Given a line segment P1 to P2, and point C

[java] new Line(P1, P2).distance(C);[/java]

For testing i used a line segment with (0,0,0) to (0, 10, 0) and a point in (5, 3, 0). The result I expected was 5 since it’s just 5 units away from the nearest point in the line but I get 990 as a result.

Well, Line’s constructor is pretty clear in the javadocs:
http://hub.jmonkeyengine.org/javadoc/com/jme3/math/Line.html#Line(com.jme3.math.Vector3f,%20com.jme3.math.Vector3f)

Line(Vector3f origin, Vector3f direction)

So instead of (P1, P2), you probably want: (P1, P2.subtract(P1).normalizeLocal())

1 Like

Oh it works! Thank you so much!

Just one last question. Since it’s a normalized vector and no length is given, Is it correct to assume that the point is being measured against a ray and that I still have to handle if a point is farther than P2?

@Cmile said: Oh it works! Thank you so much!

Just one last question. Since it’s a normalized vector and no length is given, Is it correct to assume that the point is being measured against a ray and that I still have to handle if a point is farther than P2?

Something feels wrong here because I do this sort of thing all the time…

Ah, what you really want:
LineSegment(Vector3f start, Vector3f end)

1 Like

also you can do start.subtract(end).distance() if start and end are vectors (assuming youre just wanting to find the distance), no need to bring lines in to this.

@icamefromspace said: Ah, what you really want: LineSegment(Vector3f start, Vector3f end)

Oh shame on me! I’ll go ahead and hit myself in the head now. I do not know how I could have missed the class with the exact name of the thing I needed. Thanks again!

@icamefromspace said: also you can do start.subtract(end).distance() if start and end are vectors (assuming youre just wanting to find the distance), no need to bring lines in to this.
I need the distance of a point to a line segment . pspeed has answered it perfectly.