question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

intersectSegmentCircleDisplace does not work when the segment is partially inside the circle

See original GitHub issue

Issue details

The Intersector.intersectSegmentCircleDisplace() function is wrong when one of the endpoint of the segment is inside the circle. bitmap The current algorithm calcultates the projection of the circle center on the infinite line (start, end), but returns no intersection if the projection point is not within the segment endpoints (as in the diagram above)

Also, the function is supposed to “returns by how much and in what direction the line has to move away from the circle to not intersect.” but it returns by how much and in what direction the line has to move to cross the circle center.

Here’s my correction:

	public static float intersectSegmentCircleDisplace(Vector2 start, Vector2 end, Vector2 point, float radius, Vector2 displacement) {
		float u = (point.x - start.x) * (end.x - start.x) + (point.y - start.y) * (end.y - start.y);
		float d = start.dst(end);
		u /= d * d;
		if (u < 0)
			tmp2.set(start);
		else if (u > 1)
			tmp2.set(end);
		else {
			tmp.set(end.x, end.y).sub(start.x, start.y);
			tmp2.set(start.x, start.y).add(tmp.scl(u));
		}
		d = tmp2.dst(point.x, point.y);
		if (d < radius) {
			displacement.set(tmp2).sub(point).nor();
			return radius - d;
		} else
			return Float.POSITIVE_INFINITY;
	}

Reproduction steps/code

System.out.println(Intersector.intersectSegmentCircleDisplace(new Vector2(0,0), new Vector2(0,1), new Vector2(0,2),1.5f, new Vector2())); returns Infinity instead of 0.5

Version of LibGDX and/or relevant dependencies

1.9.6

Please select the affected platforms

  • Android
  • iOS (robovm)
  • iOS (MOE)
  • HTML/GWT
  • Windows
  • Linux
  • MacOS

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
obigucommented, Feb 18, 2018

Wow! I guess nobody has ever used this one, it’s pretty bad. The implementation of intersectSegmentCircleDisplace is wrong but I believe it should be aligned with the intersectSegmentCircle implementation which is correct and more efficient:

  1. Operators should also check for equality (<= and >=)
  2. Avoid square roots such as Vector2.dst for efficiency

On a more general note I see several things that could be improved:

  1. I miss basic important utility mehods such as the orthoghonal projection of a point in a line or the intersection between line and circle (not a segment).
  2. Use Circle as argument instead of center and radius. The reason why some of these methods don’t accept it is because Circle class was created afterwards (they were there on first commit! 😃.
  3. Reuse code when possible
  4. Add some tests

I’ll work on a PR see where it goes that includes a fix for this.

0reactions
obigucommented, Mar 6, 2018

@anigart Thank you, it’s been fixed

Read more comments on GitHub >

github_iconTop Results From Across the Web

Index (libGDX Core 0.9.9 API) - javadoc.io
Convenience method that returns a Files.FileType.Absolute file handle. ACCEL - Static variable in class com.badlogic.gdx.input.RemoteSender · accept(SocketHints) ...
Read more >
Index (libGDX Core 1.7.1 API)
What partial content range types this server supports. acceptTimeout - Variable in class ... Returns true if the key was not already in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found