{"id":15489687,"url":"https://github.com/pablopunk/build-your-own-basic-physics-engine-in-javascript","last_synced_at":"2025-03-28T16:25:50.024Z","repository":{"id":74867429,"uuid":"135052928","full_name":"pablopunk/build-your-own-basic-physics-engine-in-javascript","owner":"pablopunk","description":"Extensible physics engine in Javascript","archived":false,"fork":false,"pushed_at":"2018-08-20T18:56:48.000Z","size":68,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-19T08:58:37.779Z","etag":null,"topics":["engine","es6","extensible","javascript","physics","plugins"],"latest_commit_sha":null,"homepage":"https://physics.now.sh","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pablopunk.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2018-05-27T14:17:47.000Z","updated_at":"2018-08-20T18:56:50.000Z","dependencies_parsed_at":"2023-07-15T07:33:33.880Z","dependency_job_id":null,"html_url":"https://github.com/pablopunk/build-your-own-basic-physics-engine-in-javascript","commit_stats":{"total_commits":18,"total_committers":1,"mean_commits":18.0,"dds":0.0,"last_synced_commit":"ba37894354b1d943686d497582f58c34fd7fbc9b"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pablopunk%2Fbuild-your-own-basic-physics-engine-in-javascript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pablopunk%2Fbuild-your-own-basic-physics-engine-in-javascript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pablopunk%2Fbuild-your-own-basic-physics-engine-in-javascript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pablopunk%2Fbuild-your-own-basic-physics-engine-in-javascript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pablopunk","download_url":"https://codeload.github.com/pablopunk/build-your-own-basic-physics-engine-in-javascript/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246061170,"owners_count":20717387,"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":["engine","es6","extensible","javascript","physics","plugins"],"created_at":"2024-10-02T07:06:58.806Z","updated_at":"2025-03-28T16:25:49.988Z","avatar_url":"https://github.com/pablopunk.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Physics engine in Javascript\n\n\u003e https://physics.now.sh\n\n## Run\n\n```sh\n$ npm install   # install dependencies\n$ npm run dev   # watch for files to change\n$ npm start     # init server\n$ npm run build # build for production (dist/ folder)\n```\n\n## Structure\n\n```\n+-- index.html            (the view)\n+-- src/                  (the JS code)\n    +-- index.js          (the logic entry point)\n    +-- object.js         (the physical object model)\n    +-- plugins/          (extensible plugins for object behaviour)\n    |   +-- bounds.js     (make sure the objects are inside the frame)\n    |   +-- draw.js       (draw each object into the canvas)\n    |   +-- index.js      (export objectPlugins and playerPlugins)\n    |   +-- keys.js       (make WASD move the player)\n    |   +-- mouse.js      (mouse and touch move the player)\n    |   +-- speedLimit.js (make sure objects don't get a fine)\n    +-- render.js         (the main loop)\n```\n\n## Plugins\n\nPlugins contain the logic that happens to each object on each frame.\n\nLet's say you wanna add **gravity** to this world. You can create a plugin inside `src/plugins/gravity.js` and then export it in `src/plugins/index.js`:\n\n```js\nexport objectPlugins = [\n  // ...\n  require('./gravity')\n]\n```\n\nA plugin looks like this:\n\n### `init` function\n\nIt takes all world `objects` as an argument and also a `props` object containig the canvas context, the world object and the height/width: `{ ctx, world, w, h }`.\n\n```js\n// src/plugins/gravity.js\nmodule.exports.init = (objects, props) =\u003e {\n  // You can initialize here anything you want based on all objects and props\n}\n```\n\n### `run` function\n\nThis is the logic that the plugin actually does. In this example, we can make things fall. It takes the object as an argument as well as the props.\n\n```js\n// src/plugins/gravity.js\nconst g = 0.1 // value of gravity on each render\nmodule.exports.run = (object, props) =\u003e {\n  object.speedY(g)\n  // You should NOT check here if there's a collision with the ground.\n  // Collisions should be a separate plugin\n}\n```\n\n\n[*Inspired by*](https://www.graphitedigital.com/blog/build-your-own-basic-physics-engine-in-javascript)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpablopunk%2Fbuild-your-own-basic-physics-engine-in-javascript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpablopunk%2Fbuild-your-own-basic-physics-engine-in-javascript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpablopunk%2Fbuild-your-own-basic-physics-engine-in-javascript/lists"}