{"id":15722561,"url":"https://github.com/thinknathan/defold-emath","last_synced_at":"2025-10-14T19:30:57.196Z","repository":{"id":171165047,"uuid":"647527046","full_name":"thinknathan/defold-emath","owner":"thinknathan","description":"Defold native extension with math utility functions including clamp, round, sign, radian and degree conversion, measure 2d and 3d distances. ","archived":true,"fork":false,"pushed_at":"2023-12-29T04:06:24.000Z","size":27,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-25T09:48:28.650Z","etag":null,"topics":["defold","defold-game-engine","defold-library","defold-native-extension","gamedev"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc0-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thinknathan.png","metadata":{"files":{"readme":"README.md","changelog":null,"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}},"created_at":"2023-05-31T01:24:19.000Z","updated_at":"2024-01-26T17:59:10.000Z","dependencies_parsed_at":"2023-12-20T23:59:06.423Z","dependency_job_id":null,"html_url":"https://github.com/thinknathan/defold-emath","commit_stats":null,"previous_names":["thinknathan/defold-emath"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinknathan%2Fdefold-emath","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinknathan%2Fdefold-emath/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinknathan%2Fdefold-emath/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinknathan%2Fdefold-emath/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thinknathan","download_url":"https://codeload.github.com/thinknathan/defold-emath/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":236508345,"owners_count":19160336,"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":["defold","defold-game-engine","defold-library","defold-native-extension","gamedev"],"created_at":"2024-10-03T22:08:24.323Z","updated_at":"2025-10-14T19:30:51.883Z","avatar_url":"https://github.com/thinknathan.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# defold-emath\n\nDefold native extension with math functions for use in Lua scripts.\n\nYou may instead prefer [DefMath](https://github.com/subsoap/defmath) for a most robust implementation.\n\n## Functions\n\n- `emath.clamp(number, lower, upper)`\n- Restricts a number to be within a specified range.\n\n- `emath.round(number)`\n- Rounds a number to the nearest integer.\n\n- `emath.sign(number)`\n- Returns the sign of a number (-1 for negative, 1 for positive, 0 for zero).\n\n- `emath.deg_to_rad(degrees)`\n- Converts degrees to radians.\n\n- `emath.rad_to_deg(radians)`\n- Converts radians to degrees.\n\n- `emath.distance2d(x1, y1, x2, y2)`\n- Calculates the 2D distance between two points.\n\n- `emath.distance3d(x1, y1, z1, x2, y2, z2)`\n- Calculates the 3D distance between two points.\n\n## Installation\n\nThis extension includes types for use with [TypeScript + Defold](https://ts-defold.dev/).\n\n1. Edit game.project\n2. Add dependency `https://github.com/thinknathan/defold-emath/archive/main.zip` for the current version\n   - Or add a specific [release](https://github.com/thinknathan/defold-emath/releases)\n\n### TypeScript Definitions\n\n1. Install these types\n\n```bash\nyarn add git+https://git@github.com/thinknathan/defold-emath.git#^2.0.0 -D\n# or\nnpm install git+https://git@github.com/thinknathan/defold-emath.git#^2.0.0 --save-dev\n```\n\n2. Add `defold-emath` to `types` in `tsconfig.json`\n\n```diff\n{\n\t\"compilerOptions\": {\n\t\t\"types\": [\n+\t\t\t\"defold-emath\",\n\t\t],\n\t}\n}\n```\n\n3. Add `node_modules/@types` to `typeRoots` in `tsconfig.json` if it's not already there\n\n```diff\n{\n\t\"compilerOptions\": {\n\t\t\"typeRoots\": [\n+\t\t\t\"node_modules/@types\",\n\t\t],\n\t}\n}\n```\n\n## Usage\n\n```lua\n-- clamp\n-- Restricts a number to be within a specified range.\nlocal clampedValue = emath.clamp(25, 10, 20)\nprint(clampedValue)  -- Output: 20\n\n-- round\n-- Rounds a number to the nearest integer.\nlocal roundedValue = emath.round(15.75)\nprint(roundedValue)  -- Output: 16\n\n-- sign\n-- Returns the sign of a number (-1 for negative, 1 for positive, 0 for zero).\nlocal signValue = emath.sign(-7)\nprint(signValue)  -- Output: -1\n\n-- deg_to_rad\n-- Converts degrees to radians.\nlocal radians = emath.deg_to_rad(90)\nprint(radians)  -- Output: 1.5708 (approximately)\n\n-- rad_to_deg\n-- Converts radians to degrees.\nlocal degrees = emath.rad_to_deg(3.14159)\nprint(degrees)  -- Output: 180\n\n-- distance2d\n-- Calculates the 2D distance between two points.\nlocal distance = emath.distance2d(0, 0, 3, 4)\nprint(distance)  -- Output: 5\n\n-- distance3d\n-- Calculates the 3D distance between two points.\nlocal distance = emath.distance3d(0, 0, 0, 3, 4, 5)\nprint(distance)  -- Output: 7.0711 (approximately)\n```\n\n## Background\n\nThis project is an experiment with generating a Defold extension using Chat-GPT 3.5. The prompt was as follows:\n\n```\ncreate a defold extension that exposes a module to lua, use c++ that does not use any features newer than 2009, and does not use the standard library. the name of the module is emath. it should have a function called clamp exposed to lua that receives a number from lua, as well as an upper and lower bound from lua, and if the first number is larger than the upper it will be made the same as the upper, or if it is smaller than the lower it will be made the same as the lower, the final number will be returned to lua. it should have a function called round exposed to lua that receives a number, rounds it to the nearest integer, and returns it to lua.\n```\n\nFurther adjustments were made for v2.0.0.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthinknathan%2Fdefold-emath","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthinknathan%2Fdefold-emath","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthinknathan%2Fdefold-emath/lists"}