Giter Club home page Giter Club logo

bevy_collider_gen's Introduction

bevy_collider_gen

Crates.io Crates.io MIT/Apache 2.0

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

specifying your dependency

by default, both bevy_rapier2d and bevy_xpbd_2d are enabled. this is to help with the out of box experience, specifically, being able to run both examples and tinker.

but you'll probably only want to just use one of the physics engines supported so when you use it in your own crate fill in in the bevy_collider_gen dependencies with something like this for bevy_rapier2d

[dependencies.bevy_collider_gen]
# replace "*" with the most recent version of bevy_collider_gen
version = "*"
features = ["rapier2d"]
default-features = false

or this for bevy_xpbd_2d

[dependencies.bevy_collider_gen]
# replace "*" with the most recent version of bevy_collider_gen
version = "*"
features = ["xpbd_2d"]
default-features = false

example

example with a car, terrain, and boulders

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

bevy_rapier2d

note that you must have the rapier2d feature enabled

cargo run --example rapier2d_colliders

bevy_xpbd_2d

note that you must have the xpbd_2d feature enabled

cargo run --example xpbd_2d_colliders

you can also specify a path to an image yourself the example will attempt to generate one or more convex_polyline colliders for the objects it finds

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.

how it works

๐Ÿ˜„ head on over to the edges crate to learn more https://github.com/shnewto/edges

caveats

  • as mentioned here and there in these docs, this implementation requires images to have transparency in order to distinguish object from non-object :)
  • 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

(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 (bevy_raiper2d only)

convex polyline collider on an upside down car sprite

polyline

polyline collider on an upside down car sprite

convex hull

convex hull collider on an upside down car sprite

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...

heightfield collider on an upside down car sprite

convex decomposition

I didn't add support for convex decomposition directly because when sprites were small, and collisions were forceful, they were sort of unreliable (occasional panics because of bounds indexing in rapier's dependencies ๐Ÿ’€). But if you wanted to use convex decomposition colliders you could construct them with the edge coordinates from your image with something like this

let sprite_image = image_assets.get(sprite_handle.unwrap()).unwrap();
let edges = Edges::from(sprite_image)
let edge_coordinate_groups = edges.multi_image_edge_translated();
for coords in edge_coordinate_groups {
    let indices: Vec<[u32; 2]> = (0..coords.len()).map(|i| [i as u32, i as u32]).collect();
    let collider = Collider::convex_decomposition(&coords, &indices);
    commands.spawn((
        collider,
        RigidBody::Fixed,
        SpriteBundle {
            texture: sprite_handle.unwrap().clone(),
            ..default()
        },
    ));
}

convex decomposition collider on a car sprite

license

all code in this repository is dual-licensed under either:

at your option.

bevy_collider_gen's People

Contributors

ettoreleandrotognoli avatar jmgr avatar shnewto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

bevy_collider_gen's Issues

bevy_xpbd collider gen?

I've been noticing that there are plenty of other things (beyond rapier colliders ๐Ÿ˜„) that could be useful once we have what this crate does under the hood, i.e. provide the edges of a sprite / image. one of those things is colliders for other physics engines like bevy_xpbd.

My naming of this project / crate wasn't very forward thinking in that regard, but maybe if there's an opportunity to extend to things outside the world of simply rapier colliders, I can figure some renaming to be more appropriate for what this whole thing does ๐Ÿ˜…

The "rapier collider" bit of this crate maybe a misnomer anyway, it does do that, but it doesn't have to only do that. the implementation isn't specific to the rapier engine afaik. it was just a convenient (to me) library that gave me what I was wanting at the time (rapier colliders). I think we could provide the same thing using this crate's edge module and whatever library accepts a collection of points.

Another issue that I saw mentioned in the Bevy discord is that pixel perfect colliders may not always be ideal for large sprites, I'd be glad to hear about and try to support alternative collections of points, or maybe just a convenient / smart ways to filter the points after they're generated.

@Jondolf if any of my ramblings sound interesting to you, I'd be glad for your input!

I'm going to tag this issue as "help wanted" in case anyone feels inspired to tackle some of this, specifically xpbd support before I find some time to try and wrangle it myself.

Expose `march_edges` function

Hello, I wanted to say that the library is awesome! But I'd like to extend the flexibility of the library by exposing march_edges function, so we can have custom behavior rather than having only images to generate collisions from.

bevy_ecs_tilemap generation?

This looks really interesting. Would it be within the scope of the project to generate colliders from bevy_ecs_tilemap tilemaps? Could perhaps be added behind a feature flag?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.