TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

GJK: Collision detection algorithm in 2D/3D

149 pointsby amar-lakshabout 3 years ago

12 comments

Animatsabout 3 years ago
If you want the closest point between two convex polyhedra, GJK is useful, but it helps to think about the problem differently. You don&#x27;t really need the Minkowsky difference, which requires looking at all the points. It&#x27;s a local hill-climbing problem. You start with a point on each convex polyhedron and walk along the edges to a shorter distance between the points. You still need a simplex, but its geometric interpretation is clearer - it&#x27;s describing a point-point, point-edge, edge-edge, point-face, edge-face, or face-face closest situation, all of which are possible. So you advance one point of the simplex at a time.<p>Visualize a small cube sitting just above a big cube for the face-face situation. The closest vertices may be much further apart than the faces.<p>Performance is O(sqrt(N+M)). And that&#x27;s from a cold start. In practice, when simulating moving objects, you start the algorithm from the previous solution. If movement is slow relative to the frame time, it usually takes zero or one improvements to get the new closest points. So that&#x27;s O(1). This is very good performance.<p>There are some numerical issues. First, you need a convex hull. Which means no coplanar faces. Faces should be flat polygons, not just triangles. Otherwise, you can get stuck on the wrong triangle of a flat face. Of course, polygons are never completely flat in floating point, which creates other problems. A good solution is to require at least a 1 degree break angle at each edge. Convex hull programs can do things like that. This also provides some mesh reduction in case someone puts in something with a huge number of faces.<p>Second, there&#x27;s a loss of floating point significance if two faces are nearly parallel. This can cause the algorithm to go into an infinite loop, where the termination condition is not satisfied and the algorithm cycles between similar simplexes. If you&#x27;re building a physics engine, that&#x27;s what normally happens when one object comes to rest upon another. So that has to be dealt with.<p>I used to do ragdoll physics, back in the 1990s.[1] I used the implementation of the late Prof. Stephen Cameron at Oxford, and discovered the &quot;problem in the termination condition&quot; he mentions.[2] I had a somewhat clunky fix, but his was better.<p>[1] <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=-DaWIHc1VLY" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=-DaWIHc1VLY</a><p>[2] <a href="https:&#x2F;&#x2F;www.cs.ox.ac.uk&#x2F;people&#x2F;stephen.cameron&#x2F;distances&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.cs.ox.ac.uk&#x2F;people&#x2F;stephen.cameron&#x2F;distances&#x2F;</a>
评论 #30623949 未加载
erwincoumansabout 3 years ago
GJK handles closest points (and distance, separating axis) between two non-overlapping convex shapes. Once convex shapes penetrate, you can use EPA, the Expanding Polytope Algorithm, invented Gino van den Bergen (while we were working together at NaN&#x2F;Blender, around 20 years ago). See also <a href="http:&#x2F;&#x2F;dtecta.com&#x2F;publications" rel="nofollow">http:&#x2F;&#x2F;dtecta.com&#x2F;publications</a> and check out this recent implementation in C++ for both GJK and EPA is in Jolt Physics (used in the game Horizon Forbidden West): <a href="https:&#x2F;&#x2F;github.com&#x2F;jrouwe&#x2F;JoltPhysics&#x2F;tree&#x2F;master&#x2F;Jolt&#x2F;Geometry" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jrouwe&#x2F;JoltPhysics&#x2F;tree&#x2F;master&#x2F;Jolt&#x2F;Geome...</a>
评论 #30624966 未加载
WatchDogabout 3 years ago
In the context of a game engine, when you have fast moving objects, and your collision detection only runs a few times each second, how do you detect if objects should have collided, if the fast moving object fully passes through another, between each tick?
评论 #30622651 未加载
评论 #30622503 未加载
评论 #30622699 未加载
评论 #30622572 未加载
评论 #30623758 未加载
lifthrasiirabout 3 years ago
Reducible&#x27;s introduction to GJK [1] is also really great.<p>[1] <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=ajv46BSqcK4" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=ajv46BSqcK4</a>
评论 #30621339 未加载
bogwogabout 3 years ago
A similar, but IMO easier to understand and implement algorithm is Xenocollide: <a href="http:&#x2F;&#x2F;xenocollide.snethen.com&#x2F;" rel="nofollow">http:&#x2F;&#x2F;xenocollide.snethen.com&#x2F;</a>
评论 #30624156 未加载
评论 #30621955 未加载
abetuskabout 3 years ago
I&#x27;m in the process of learning about GJK but Vatti&#x27;s clipping algorithm [0] is a way to polynomially do Boolean operations on arbitrary 2d polygons (convex, concave, etc.). From Boolean operations, you can get collision detection.<p>Angus Johnson&#x27;s ClipperLib [1] is a library that implements this in C++ and other languages and there are Javascript ports available [2] [3].<p>[0] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Vatti_clipping_algorithm" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Vatti_clipping_algorithm</a><p>[1] <a href="http:&#x2F;&#x2F;www.angusj.com&#x2F;delphi&#x2F;clipper.php" rel="nofollow">http:&#x2F;&#x2F;www.angusj.com&#x2F;delphi&#x2F;clipper.php</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;junmer&#x2F;clipper-lib" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;junmer&#x2F;clipper-lib</a><p>[3] <a href="http:&#x2F;&#x2F;jsclipper.sourceforge.net&#x2F;6.2.1.0&#x2F;main_demo.html" rel="nofollow">http:&#x2F;&#x2F;jsclipper.sourceforge.net&#x2F;6.2.1.0&#x2F;main_demo.html</a>
bluxabout 3 years ago
I implemented GJK years ago based on a nice presentation by Casey Muratori: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=Qupqu1xe7Io" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=Qupqu1xe7Io</a>
评论 #30621983 未加载
gugagoreabout 3 years ago
Another perspective for understanding GJK, one that I have not yet seen thoroughly explained, is that it&#x27;s a special case of a convex quadratic program: find two points, each constrained to a respective polyhedron (e.g. intersection of halfspaces), with minimum distance between them. The constraints are linear inequalities, and the objective is convex quadratic.
uptownabout 3 years ago
Is GJK a suitable algorithm for preventing collisions of 2D text being randomly placed in a canvas at random angles?<p>I’m assuming the bounding boxes of the text would provide the vertices.<p>Is there anything better?
评论 #30624943 未加载
ameliusabout 3 years ago
I&#x27;m more interested in the shape that defines the set of points where there is no collision.
togaenabout 3 years ago
GJK is more than just collision detection. If you really just need to <i>detect</i> collisions, there are usually much simpler ways to do it.
brunoTbearabout 3 years ago
Misread the headline, thought this was a way for identifying when @pc was in a thread.
评论 #30621771 未加载