SAT.js
A javascript implementation of the Separating Axis Theorem for convex polygons and polyhedra
Install / Use
/learn @pboechat/SAT.jsREADME
SAT.js
SAT.js is a Javascript implementation of the Separating Axis Theorem for convex polygons and polyhedra.
Requires three.js.
Based on this article.
Usage
Add three.js and SAT.js to your page:
<script type="text/javascript" src="three.js"></script>
<script type="text/javascript" src="SAT.js"></script>
Polygons
With the vertices of your polygon, construct a SAT.Polygon:
var polyA = new SAT.Polygon([new THREE.Vector2(-0.5, -0.5),
new THREE.Vector2(0.5, -0.5),
new THREE.Vector2(0.5, 0.5),
new THREE.Vector2(-0.5, 0.5),
]);
var polyB = new SAT.Polygon(verticesB);
Notice that the vertices must be in counter-clockwise order.
Polyhedra
With the vertices, edges and faces of your polyhedron, construct a SAT.Shape:
// 1x1x1 cube centered in origin
var polyA = new SAT.Shape(
// vertices
[
new THREE.Vector3(-0.5, 0.5, 0.5), new THREE.Vector3(-0.5, -0.5, 0.5),
new THREE.Vector3(0.5, -0.5, 0.5), new THREE.Vector3(0.5, 0.5, 0.5),
new THREE.Vector3(0.5, 0.5, -0.5), new THREE.Vector3(0.5, -0.5, -0.5),
new THREE.Vector3(-0.5, -0.5, -0.5), new THREE.Vector3(-0.5, 0.5, -0.5),
],
// edges
[
[0, 1],
[1, 2],
[2, 3],
[3, 0],
[4, 5],
[5, 6],
[6, 7],
[7, 4],
[3, 4],
[2, 5],
[0, 7],
[1, 6]
],
// faces
[
[0, 1, 2, 3], // front
[4, 5, 6, 7], // back
[3, 2, 5, 4], // right
[7, 6, 1, 0], // left
[7, 0, 3, 4], // top
[5, 2, 1, 6] // bottom
]);
var polyB = new SAT.Shape(verticesB, edgesB, facesB);
Your faces must reference your vertices in counter-clockwise order.
Collision Checking
SAT.CheckCollision(polyA, polyB)
Demo
Examine the SAT algorithm step-by-step here.
Video
Related Skills
node-connect
349.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.5kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
