Skip to content

Commit

Permalink
Merge pull request #9 from shnewto/expose-march-edges
Browse files Browse the repository at this point in the history
make march_edges symbol public
  • Loading branch information
shnewto committed Dec 2, 2023
2 parents 560317a + 0ed27be commit 9839de7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
41 changes: 22 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# bevy_rapier_collider_gen
[![Crates.io](https://img.shields.io/crates/v/bevy_rapier_collider_gen.svg)](https://crates.io/crates/bevy_rapier_collider_gen)
[![Crates.io](https://img.shields.io/crates/d/bevy_rapier_collider_gen.svg)](https://crates.io/crates/bevy_rapier_collider_gen)
[![MIT/Apache 2.0](https://img.shields.io/badge/license-MIT%2FApache-blue.svg)](https://github.com/shnewto/bevy_rapier_collider_gen#license)

[![Crates.io](<https://img.shields.io/crates/v/bevy_rapier_collider_gen.svg>)](<https://crates.io/crates/bevy_rapier_collider_gen>)
[![Crates.io](<https://img.shields.io/crates/d/bevy_rapier_collider_gen.svg>)](<https://crates.io/crates/bevy_rapier_collider_gen>)
[![MIT/Apache 2.0](<https://img.shields.io/badge/license-MIT%2FApache-blue.svg>)](<https://github.com/shnewto/bevy_rapier_collider_gen#license>)

a library for generating bevy_rapier2d colliders, for bevy apps, from images with transparency

## example

![](https://github.com/shnewto/bevy_rapier_collider_gen/blob/main/img/example-default.png?raw=true)
![example with a car, terrain, and boulders](<https://github.com/shnewto/bevy_rapier_collider_gen/blob/main/img/example-default.png?raw=true>)

to see this in action you can run the example, with no args it generates a scene with various colliders using pngs in the `assets/sprite` directory

```sh
cargo run --example colliders
```
Expand All @@ -18,14 +20,14 @@ you can also specify a path to an image yourself the example will attempt to gen

## about / why

i was looking for a way to iterate on some 2d scenes with colliders on things with more sophisticated shapes than simple
geometry, i figured there should be enough info in an image with transparency to generate colliders, and... there is! so i
packaged up my approach here in case anyone else could benefit.
i was looking for a way to iterate on some 2d scenes with colliders on things with more sophisticated shapes than simple
geometry, i figured there should be enough info in an image with transparency to generate colliders, and... there is! so i
packaged up my approach here in case anyone else could benefit.

## how it works

i was inspired by [a coding train (or, coding in the cabana rather) on an implementation of "marching squares"](https://youtu.be/0ZONMNUKTfU).
so this crate takes a "march through all the values" approach to find edges, i.e. pixels with at least 1 empty neighboring pixel, but
i was inspired by [a coding train (or, coding in the cabana rather) on an implementation of "marching squares"](<https://youtu.be/0ZONMNUKTfU>).
so this crate takes a "march through all the values" approach to find edges, i.e. pixels with at least 1 empty neighboring pixel, but
instead of drawing a contour in place, it just keeps track of all the actual pixel coordinates. to determine "empty" I bitwise
or all the bytes for each pixel and, in images with transparency, "empty" is a zero value for the pixel.

Expand All @@ -39,29 +41,28 @@ from any pixel in an existing group, that pixel begins a new group.
- there's no reason we couldn't generate colliders / geometry without transparency, it's just not implemented. if you've got a compelling case, raise an issue! or even better, create a pr!
- i imagine for generating things at a larger scale, i.e. colliders for sets of sprites bigger than pixel counts in the hundreds, this implementation won't be performant to do at runtime. i'll suggest serializing the colliders you like and deserializing in your app instead of doing all the number crunching on load when you need a performance boost


## examples of colliders generated for assets/sprite/car.png
## examples of colliders generated for assets/sprite/car.png

(as in pictures of the sort of thing you can expect, not the runnable bevy app example. that's a couple headings up)

### convex polyline

![](https://github.com/shnewto/bevy_rapier_collider_gen/blob/main/img/convex-polyline.png?raw=true)
![](<<https://github.com/shnewto/bevy_rapier_collider_gen/blob/main/img/convex-polyline.png?raw=true>)

### polyline

![](https://github.com/shnewto/bevy_rapier_collider_gen/blob/main/img/polyline.png?raw=true)
![](<<https://github.com/shnewto/bevy_rapier_collider_gen/blob/main/img/polyline.png?raw=true>)

### convex hull

![](https://github.com/shnewto/bevy_rapier_collider_gen/blob/main/img/convex-hull.png?raw=true)
![](<<https://github.com/shnewto/bevy_rapier_collider_gen/blob/main/img/convex-hull.png?raw=true>)

### heightfield

the current implementation does best if the image you're generating a heightfield from is either centered in the image
or spans the entire width of the image...

![](https://github.com/shnewto/bevy_rapier_collider_gen/blob/main/img/heightfield.png?raw=true)
![](<<https://github.com/shnewto/bevy_rapier_collider_gen/blob/main/img/heightfield.png?raw=true>)

### convex decomposition

Expand All @@ -85,11 +86,13 @@ for coords in edge_coordinate_groups {
}
```

![](https://github.com/shnewto/bevy_rapier_collider_gen/blob/main/img/convex-decomposition.png?raw=true)
![](<<https://github.com/shnewto/bevy_rapier_collider_gen/blob/main/img/convex-decomposition.png?raw=true>)

## license

all code in this repository is dual-licensed under either:
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)

at your option.
- MIT License (LICENSE-MIT or <<http://opensource.org/licenses/MIT>)
- Apache License, Version 2.0 (LICENSE-APACHE or <<http://www.apache.org/licenses/LICENSE-2.0>)

at your option.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub use crate::collider::single_heightfield_collider_translated;
pub use crate::collider::single_polyline_collider_raw;
pub use crate::collider::single_polyline_collider_translated;
pub use crate::edge::image_to_edges;
pub use crate::edge::march_edges;
pub use crate::edge::multi_image_edge_translated;
pub use crate::edge::multi_image_edges_raw;
pub use crate::edge::single_image_edge_raw;
Expand Down

0 comments on commit 9839de7

Please sign in to comment.