{"id":20906161,"url":"https://github.com/distributedlife/sat-collisions","last_synced_at":"2025-06-19T22:37:07.585Z","repository":{"id":141572879,"uuid":"43998403","full_name":"distributedlife/sat-collisions","owner":"distributedlife","description":null,"archived":false,"fork":false,"pushed_at":"2016-05-10T12:19:59.000Z","size":29,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-19T14:24:06.317Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/distributedlife.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-10-10T06:48:50.000Z","updated_at":"2016-05-10T12:19:55.000Z","dependencies_parsed_at":"2024-07-09T18:47:37.014Z","dependency_job_id":null,"html_url":"https://github.com/distributedlife/sat-collisions","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distributedlife%2Fsat-collisions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distributedlife%2Fsat-collisions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distributedlife%2Fsat-collisions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distributedlife%2Fsat-collisions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/distributedlife","download_url":"https://codeload.github.com/distributedlife/sat-collisions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243297675,"owners_count":20268824,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T13:29:17.750Z","updated_at":"2025-03-12T21:29:23.835Z","avatar_url":"https://github.com/distributedlife.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# distributedlife-sat\n\nThis project is a rewrite of [Jim Riecken's SAT.js](http://jriecken.github.io/sat-js/) library. I rewrote it on a flight back to see family in coding style that suites me. I also trimmed out stuff I don't think I need. It's not compatible due to API changes but all the tests still pass.\n\n# About\n\nSAT.js is a simple JavaScript library for performing collision detection (and projection-based collision response) of simple 2D shapes.  It uses the [Separating Axis Theorem](http://en.wikipedia.org/wiki/Hyperplane_separation_theorem) (hence the name)\n\nIt supports detecting collisions between:\n - Circles (using Voronoi Regions.)\n - Convex Polygons (and simple Axis-Aligned Boxes, which are of course, convex polygons.)\n\nIt also supports checking whether a point is inside a circle or polygon.\n\nWhen a collision occurs your callback gets invoked.\n\nIt's released under the [MIT](http://en.wikipedia.org/wiki/MIT_License) license.\n\nTo use it in node.js, you can run `npm install distributedlife-sat` and then use it with `var SAT = require('distributedlife-sat');`\n\n# Functions\n\nI took a more functional approach than the original. All you have is functions and data. Functions return new versions of data, so through away your old stuff.\n\nThe original SAT.js library has a bunch of performance stuff around limiting reallocs, etc. I have stripped all that out as using this for high-performance stuff isn't an objective.\n\n\n## Collisions\nI collapsed the original set of functions that run different algorithms depending on whether you have one or two circles, polygons or points into a single function. This works out what structure you have and calls the appropriate algorithm.\n\n- if you have a `radius`, you're a circle\n- if you have an `x` and a `y`, you're a vector\n- otherwise, you're a polygon\n\n```\nfunction callMeOnCollision () {\n  console.log('ouch');\n}\n\nvar test = require('distributedlife-sat').collisions.test;\ntest(a, b, callMeOnCollision)\n```\n\nAt present we call your callback on collision and you get no further information about who was doing, what direction they were heading, etc.\n\n## Shapes\n\nInside is a helper file with a bunch of shapes. These make it easier for you to create objects for colliding. These objects exist in your physics models. I make no promises about how easy it may be to render them. I like to keep my physics model and my rendering model separate.\n\nMost shapes take a point that is their anchor in world space. For the circle, this will be the center. For the rectangles you have the option of a centre oriented `c` object or a top-left `tl` oriented one.\n```\nvar shapes = require('distributedlife-sat').shapes;\n\nvar origin = {x: 0, y: 0};\nvar width = 25;\nvar height = 25;\n\ncircle(origin, 25);\ntlSquare(origin, width);\ntlBox(origin, width, height);\ntlRectangle(origin, width, height);\ncSquare(origin, width);\ncBox(origin, width, height);\ncRectangle(origin, width, height);\npolygon(points, [{x: 0, y: 0}, {x: 100, y: 100}, {x: 0, y: 100}]);\ntriangle(origin, width);\n```\n\nThe `box` and `rectangles` are synonyms.\n\n## Useful Vector Functions\n\nThese functions return new vectors and without modifying the supplied vectors.\n\n- **sub** (v1, v2) - subtract v2 from v1 and return the new vector\n- **add** (v1, v2) - add v2 to v1 and return the new vector\n- **dot** (v1, v2) - return the dot product of v1 and v2\n- **len2** (v) - return dot product of the vector and itself\n- **len** (v) - return the magnitude of the vector\n- **normalise** (v) - return the normal of the vector\n- **scale** (v, x, y) - scale v1 by x and y. If y is not supplied it scales v.x and v.y by the x value.\n- **perp** (v) - return the vector perpendicular to the supplied vector\n- **rotate** (v, angle) - return the vector that is a rotate of `angle` on `v`\n- **reverse** (v) - return the reverse of the vector\n- **project** (v1, v2) - project v1 on v2.\n- **reflect** (v, axis) - reflect the vector along the supplied axis\n\n## Useful Polygon Functions\n\nAll of these functions return new polygons and do not modify the supplied polygons.\n\n- **rotate** (polygon, angle) - rotate polygon by angle\n- **translate** (polygon, vector) - translate polygon by vector\n- **setPoints** (polygon, points) - set the points of the polygon\n- **setOffset** (polygon, offset) - change the offset of the polygon from it's world origin\n- **setAngle** (polygon, angle) - set the angle of the polygon\n- **getAABB** (polygon) - return the Axis-Aligned Bounding Box for the polygon\n\n## Useful Circle Functions\n\n- getAABB (circle) - return the Axis Aligned Bounding Box for the supplied circle.\n\n\n# Tests\n\nTo run the tests from your console:\n\n```\nmocha\n```\n\nTo install `mocha` you will need to have run `npm install` after cloning the repo.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdistributedlife%2Fsat-collisions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdistributedlife%2Fsat-collisions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdistributedlife%2Fsat-collisions/lists"}