{"id":15138009,"url":"https://github.com/shnewto/bevy_collider_gen","last_synced_at":"2025-04-12T22:36:23.278Z","repository":{"id":75083609,"uuid":"578389739","full_name":"shnewto/bevy_collider_gen","owner":"shnewto","description":"a library for generating 2D colliders for bevy apps from images","archived":false,"fork":false,"pushed_at":"2025-02-04T17:23:40.000Z","size":2901,"stargazers_count":91,"open_issues_count":4,"forks_count":6,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-04T02:08:43.213Z","etag":null,"topics":["2d","bevy","collider","crates-io","gamedev","images","png","rapier","rapier2d","rust","transparency","xpbd","xpbd2d"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shnewto.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","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":"2022-12-15T00:01:21.000Z","updated_at":"2025-02-20T08:30:34.000Z","dependencies_parsed_at":"2024-11-16T18:42:08.484Z","dependency_job_id":"737dd49f-5dc5-4f29-9527-c43308039bab","html_url":"https://github.com/shnewto/bevy_collider_gen","commit_stats":{"total_commits":37,"total_committers":10,"mean_commits":3.7,"dds":0.2702702702702703,"last_synced_commit":"ca41771525025a49910d8e709c666a5d339efb0d"},"previous_names":["shnewto/bevy_collider_gen"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shnewto%2Fbevy_collider_gen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shnewto%2Fbevy_collider_gen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shnewto%2Fbevy_collider_gen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shnewto%2Fbevy_collider_gen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shnewto","download_url":"https://codeload.github.com/shnewto/bevy_collider_gen/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248642819,"owners_count":21138352,"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":["2d","bevy","collider","crates-io","gamedev","images","png","rapier","rapier2d","rust","transparency","xpbd","xpbd2d"],"created_at":"2024-09-26T07:04:17.616Z","updated_at":"2025-04-12T22:36:23.252Z","avatar_url":"https://github.com/shnewto.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bevy_collider_gen\n\n[![Crates.io](https://img.shields.io/crates/v/bevy_collider_gen.svg)](https://crates.io/crates/bevy_collider_gen)\n[![Crates.io](https://img.shields.io/crates/d/bevy_collider_gen.svg)](https://crates.io/crates/bevy_collider_gen)\n[![MIT/Apache 2.0](https://img.shields.io/badge/license-MIT%2FApache-blue.svg)](https://github.com/shnewto/bevy_collider_gen#license)\n\nA library for generating 2d colliders, for bevy apps, from images with transparency\n\n## Specifying your dependency\n\nBy default, both bevy_rapier2d and avian2d (formerly bevy_xpbd_2d) are enabled.\nThis is to help with the out of box experience, specifically,\nbeing able to run both examples and tinker.\n\nBut you'll probably only want to just use one of the physics engines supported\nso when you use it in your own crate fill in in the `bevy_collider_gen`\ndependencies with something like this for `bevy_rapier2d`\n\n```toml\n[dependencies.bevy_collider_gen]\n# replace \"*\" with the most recent version of bevy_collider_gen\nversion = \"*\"\n```\n\nOr this for `avian2d`\n\n```toml\n[dependencies.bevy_collider_gen]\n# replace \"*\" with the most recent version of bevy_collider_gen\nversion = \"*\"\nfeatures = [\"avian2d\", \"parallel\"]\ndefault-features = false\n```\n\n## Example\n\n![example with a car, terrain, and boulders](https://github.com/shnewto/bevy_collider_gen/blob/main/img/example-default.png?raw=true)\n\nTo see this in action you can run the example, with no arguments\nit generates a scene with various colliders using PNG's in the `assets/sprite` directory\n\n### bevy_rapier2d\n\n```sh\ncargo run --example rapier2d_colliders\n```\n\n### avian2d\n\n```sh\ncargo run --example avian2d_colliders -F avian2d\n```\n\nYou can also specify a path to an image yourself the example will attempt to\ngenerate one or more `convex_polyline` colliders for the objects it finds\n\n## About / why\n\nI was looking for a way to iterate on some 2d scenes with colliders\non things with more sophisticated shapes than simple\ngeometry, I figured there should be enough info in an image with\ntransparency to generate colliders, and... there is! So i\npackaged up my approach here in case anyone else could benefit.\n\n## How it works\n\n😄 head on over to the edges crate to learn more \u003chttps://github.com/shnewto/edges\u003e\n\n## Caveats\n\n- as mentioned here and there in these docs, this implementation requires\n  images to have transparency in order to distinguish object from non-object :)\n- i imagine for generating things at a larger scale, i.e. colliders\n  for sets of sprites bigger than pixel counts in the hundreds, this implementation\n  won't be performant to do at runtime. I'll suggest serializing the colliders\n  you like and deserializing in your app instead of doing all the number crunching\n  on load when you need a performance boost\n\n## Examples of colliders generated for assets/sprite/car.png\n\n(as in pictures of the sort of thing you can expect,\nnot the runnable bevy app example. That's a couple headings up)\n\n### convex polyline (bevy_raiper2d only)\n\n![convex polyline collider on an upside down car sprite](https://github.com/shnewto/bevy_collider_gen/blob/main/img/convex-polyline.png?raw=true)\n\n### polyline\n\n![polyline collider on an upside down car sprite](https://github.com/shnewto/bevy_collider_gen/blob/main/img/polyline.png?raw=true)\n\n### convex hull\n\n![convex hull collider on an upside down car sprite](https://github.com/shnewto/bevy_collider_gen/blob/main/img/convex-hull.png?raw=true)\n\n### heightfield\n\nThe current implementation does best if the image\nyou're generating a heightfield from is either centered in the image\nor spans the entire width of the image...\n\n![heightfield collider on an upside down car sprite](https://github.com/shnewto/bevy_collider_gen/blob/main/img/heightfield.png?raw=true)\n\n### convex decomposition\n\nI didn't add support for convex decomposition directly because when\nsprites were small, and collisions were forceful, they were sort of unreliable\n(occasional panics because of bounds indexing in rapier's dependencies 💀).\nBut if you wanted to use convex decomposition colliders you could construct\nthem with the edge coordinates from your image with something like this\n\n```rust\nlet sprite_image = image_assets.get(sprite_handle.unwrap()).unwrap();\nlet edges = Edges::try_from(sprite_image).unwrap();\nlet edge_coordinate_groups = edges.multi_translated();\nfor coords in edge_coordinate_groups {\n    let indices: Vec\u003c[u32; 2]\u003e = (0..coords.len()).map(|i| [i as u32, i as u32]).collect();\n    let collider = Collider::convex_decomposition(\u0026coords, \u0026indices);\n    commands.spawn((\n        collider,\n        RigidBody::Fixed,\n        SpriteBundle {\n            texture: sprite_handle.unwrap().clone(),\n            ..default()\n        },\n    ));\n}\n```\n\n![convex decomposition collider on a car sprite](https://github.com/shnewto/bevy_collider_gen/blob/main/img/convex-decomposition.png?raw=true)\n\n## License\n\nAll code in this repository is dual-licensed under either:\n\n- MIT License (LICENSE-MIT or \u003chttp://opensource.org/licenses/MIT\u003e)\n- Apache License, Version 2.0 (LICENSE-APACHE or \u003chttp://www.apache.org/licenses/LICENSE-2.0\u003e)\n\nAt your option.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshnewto%2Fbevy_collider_gen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshnewto%2Fbevy_collider_gen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshnewto%2Fbevy_collider_gen/lists"}