Skip to content

Commit

Permalink
Merge pull request #22 from salam99823/main
Browse files Browse the repository at this point in the history
Refactor of code
  • Loading branch information
shnewto authored Nov 19, 2024
2 parents ca41771 + aba0383 commit 2077b1a
Show file tree
Hide file tree
Showing 11 changed files with 396 additions and 478 deletions.
32 changes: 24 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,44 @@ description = "a library for generating colliders, for bevy apps, from images wi
keywords = ["bevy", "rapier", "png", "collider", "2d"]
readme = "README.md"

[lints.clippy]
cast_precision_loss = { level = "allow", priority = 1 }
pedantic = { level = "warn", priority = 0 }

[features]
default = ["avian2d", "rapier2d"]
default = ["rapier2d", "parallel"]
avian2d = ["dep:avian2d"]
rapier2d = ["dep:bevy_rapier2d"]
parallel = ["dep:rayon", "avian2d/parallel", "bevy_rapier2d/parallel"]

[dependencies]
bevy = "0.14"
bevy_rapier2d = { version = "0.27.0", optional = true }
avian2d = { version = "0.1.0", optional = true }
edges = { version = "0.3.4", features = ["bevy"] }
thiserror = "1.0.57"
edges = "0.4.0"
bevy_math = { version = "0.14", default-features = false }
rayon = { version = "1.10.0", optional = true }

[dependencies.bevy_rapier2d]
version = "0.27"
optional = true
default-features = false
features = ["dim2", "headless"]

[dependencies.avian2d]
version = "0.1"
optional = true
default-features = false
features = ["2d", "parry-f32"]

[dev-dependencies]
bevy = "0.14"
bevy_prototype_lyon = "0.12.0"
indoc = "2.0.4"

[[example]]
name = "avian2d_colliders"
path = "examples/avian2d_colliders.rs"
required-features = ["avian2d"]
required-features = ["avian2d", "avian2d/debug-plugin"]

[[example]]
name = "rapier2d_colliders"
path = "examples/rapier2d_colliders.rs"
required-features = ["rapier2d"]
required-features = ["rapier2d", "bevy_rapier2d/debug-render-2d"]
26 changes: 12 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# bevy_collider_gen

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

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

Expand All @@ -16,8 +16,6 @@ but you'll probably only want to just use one of the physics engines supported s
[dependencies.bevy_collider_gen]
# replace "*" with the most recent version of bevy_collider_gen
version = "*"
features = ["rapier2d"]
default-features = false
```

or this for `avian2d`
Expand All @@ -26,13 +24,13 @@ or this for `avian2d`
[dependencies.bevy_collider_gen]
# replace "*" with the most recent version of bevy_collider_gen
version = "*"
features = ["avian2d"]
features = ["avian2d", "parallel"]
default-features = false
```

## example

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

Expand All @@ -41,15 +39,15 @@ to see this in action you can run the example, with no args it generates a scene
#### note that you must have the rapier2d feature enabled

```sh
cargo run --example rapier2d_colliders
cargo run --example rapier2d_colliders -F "bevy_rapier2d/debug-render-2d"
```

### avian2d

#### note that you must have the avian2d feature enabled

```sh
cargo run --example avian2d_colliders
cargo run --example avian2d_colliders -F "avian2d, avian2d/debug-plugin"
```

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
Expand All @@ -75,22 +73,22 @@ packaged up my approach here in case anyone else could benefit.

### convex polyline (bevy_raiper2d only)

![convex polyline collider on an upside down car sprite](<https://github.com/shnewto/bevy_collider_gen/blob/main/img/convex-polyline.png?raw=true>)
![convex polyline collider on an upside down car sprite](https://github.com/shnewto/bevy_collider_gen/blob/main/img/convex-polyline.png?raw=true)

### polyline

![polyline collider on an upside down car sprite](<https://github.com/shnewto/bevy_collider_gen/blob/main/img/polyline.png?raw=true>)
![polyline collider on an upside down car sprite](https://github.com/shnewto/bevy_collider_gen/blob/main/img/polyline.png?raw=true)

### convex hull

![convex hull collider on an upside down car sprite](<https://github.com/shnewto/bevy_collider_gen/blob/main/img/convex-hull.png?raw=true>)
![convex hull collider on an upside down car sprite](https://github.com/shnewto/bevy_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...

![heightfield collider on an upside down car sprite](<https://github.com/shnewto/bevy_collider_gen/blob/main/img/heightfield.png?raw=true>)
![heightfield collider on an upside down car sprite](https://github.com/shnewto/bevy_collider_gen/blob/main/img/heightfield.png?raw=true)

### convex decomposition

Expand All @@ -115,7 +113,7 @@ for coords in edge_coordinate_groups {
}
```

![convex decomposition collider on a car sprite](<https://github.com/shnewto/bevy_collider_gen/blob/main/img/convex-decomposition.png?raw=true>)
![convex decomposition collider on a car sprite](https://github.com/shnewto/bevy_collider_gen/blob/main/img/convex-decomposition.png?raw=true)

## license

Expand Down
84 changes: 44 additions & 40 deletions examples/avian2d_colliders.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
use avian2d::math::Vector;
use avian2d::prelude::*;
use bevy::asset::LoadState;
use bevy::color::palettes::css;
use bevy::pbr::wireframe::WireframePlugin;
use bevy::prelude::*;
use bevy::render::settings::{RenderCreation, WgpuFeatures, WgpuSettings};
use bevy::render::RenderPlugin;
use bevy_collider_gen::avian2d::single_convex_polyline_collider_translated;
#![allow(clippy::needless_pass_by_value)]
use avian2d::{math::Vector, prelude::*};
use bevy::{
asset::LoadState,
color::palettes::css,
pbr::wireframe::WireframePlugin,
prelude::*,
render::{
settings::{RenderCreation, WgpuFeatures, WgpuSettings},
RenderPlugin,
},
};
use bevy_collider_gen::{
avian2d::{multi_convex_polyline_collider_translated, single_heightfield_collider_translated},
Edges,
avian2d::{generate_collider, generate_colliders},
edges::Edges,
ColliderType,
};
use bevy_prototype_lyon::{
prelude::{Fill, GeometryBuilder, ShapePlugin},
shapes,
};
use bevy_prototype_lyon::prelude::{Fill, GeometryBuilder, ShapePlugin};
use bevy_prototype_lyon::shapes;
use indoc::indoc;
use std::collections::HashMap;

Expand All @@ -25,9 +31,9 @@ use std::collections::HashMap;
/// w (zoom in)
/// d (zoom out)
/// Custom PNG: convex_polyline collider
/// Custom PNG: `convex_polyline` collider
/// from png path specified as cli argument
pub fn custom_png_spawn(
fn custom_png_spawn(
mut commands: Commands,
game_assets: Res<GameAsset>,
image_assets: Res<Assets<Image>>,
Expand All @@ -38,7 +44,7 @@ pub fn custom_png_spawn(
}
let sprite_image = image_assets.get(sprite_handle.unwrap()).unwrap();

let colliders = multi_convex_polyline_collider_translated(sprite_image);
let colliders = generate_colliders(sprite_image, ColliderType::ConvexPolyline, true);
for collider in colliders {
commands.spawn((
collider.unwrap(),
Expand All @@ -53,15 +59,12 @@ pub fn custom_png_spawn(
}
}

/// for the movement system
#[derive(Component, Resource)]
pub struct Car {
pub initial_xyz: Vec3,
}
#[derive(Component)]
pub struct Car;

/// Car: convex_polyline collider
/// Car: `convex_polyline` collider
/// from assets/sprite/car.png
pub fn car_spawn(
fn car_spawn(
mut commands: Commands,
game_assets: Res<GameAsset>,
image_assets: Res<Assets<Image>>,
Expand All @@ -72,23 +75,23 @@ pub fn car_spawn(
return;
}
let sprite_image = image_assets.get(sprite_handle.unwrap()).unwrap();
let collider = single_convex_polyline_collider_translated(sprite_image).unwrap();
let collider = generate_collider(sprite_image, ColliderType::ConvexPolyline, true).unwrap();
commands.spawn((
collider,
SpriteBundle {
texture: sprite_handle.unwrap().clone(),
transform: Transform::from_xyz(initial_xyz.x, initial_xyz.y, initial_xyz.z),
..default()
},
Car { initial_xyz },
Car,
RigidBody::Dynamic,
DebugRender::default().with_collider_color(css::VIOLET.into()),
));
}

/// Terrain: heightfield collider
/// from assets/sprite/terrain.png
pub fn terrain_spawn(
fn terrain_spawn(
mut commands: Commands,
game_assets: Res<GameAsset>,
image_assets: Res<Assets<Image>>,
Expand All @@ -98,7 +101,7 @@ pub fn terrain_spawn(
return;
}
let sprite_image = image_assets.get(sprite_handle.unwrap()).unwrap();
let collider = single_heightfield_collider_translated(sprite_image);
let collider = generate_collider(sprite_image, ColliderType::Heightfield, true).unwrap();
commands.spawn((
collider,
RigidBody::Static,
Expand All @@ -111,9 +114,9 @@ pub fn terrain_spawn(
}

/// Boulder: using groups of edge coordinates to create geometry to color fill
/// multiple convex_polyline colliders
/// multiple `convex_polyline` colliders
/// from assets/sprite/boulders.png
pub fn boulders_spawn(
fn boulders_spawn(
mut commands: Commands,
game_assets: Res<GameAsset>,
image_assets: Res<Assets<Image>>,
Expand All @@ -126,7 +129,7 @@ pub fn boulders_spawn(

let edges = Edges::from(sprite_image);
let coord_group = edges.multi_image_edge_translated();
let colliders = multi_convex_polyline_collider_translated(sprite_image);
let colliders = generate_colliders(sprite_image, ColliderType::ConvexPolyline, true);

for (coords, collider) in coord_group.iter().zip(colliders.into_iter()) {
let shape = shapes::Polygon {
Expand Down Expand Up @@ -169,6 +172,7 @@ fn main() {
App::new()
.add_plugins(
DefaultPlugins
.set(ImagePlugin::default_nearest())
.set(WindowPlugin {
primary_window: Some(Window {
title: "colliders".to_string(),
Expand Down Expand Up @@ -236,7 +240,7 @@ pub fn check_assets(
return;
}

state.set(AppState::Running)
state.set(AppState::Running);
}

pub fn camera_spawn(mut commands: Commands) {
Expand All @@ -247,20 +251,20 @@ pub fn camera_movement(
mut query: Query<(&Camera, &mut OrthographicProjection, &mut Transform)>,
keys: Res<ButtonInput<KeyCode>>,
) {
for (_, mut projection, mut transform) in query.iter_mut() {
for (_, mut projection, mut transform) in &mut query {
if keys.pressed(KeyCode::ArrowLeft) {
transform.translation.x += 10.0;
transform.translation.x -= 10.0;
}
if keys.pressed(KeyCode::ArrowRight) {
transform.translation.x -= 10.0;
transform.translation.x += 10.0;
}

if keys.pressed(KeyCode::ArrowUp) {
transform.translation.y -= 10.0;
transform.translation.y += 10.0;
}

if keys.pressed(KeyCode::ArrowDown) {
transform.translation.y += 10.0;
transform.translation.y -= 10.0;
}

if keys.pressed(KeyCode::KeyW) {
Expand Down Expand Up @@ -355,10 +359,10 @@ pub fn controls_text_spawn(mut commands: Commands, game_assets: Res<GameAsset>)
}

pub fn car_movement(
mut query: Query<(&Car, &mut LinearVelocity, &mut Transform)>,
mut query: Query<(&mut LinearVelocity, &mut Transform), With<Car>>,
keys: Res<ButtonInput<KeyCode>>,
) {
for (car, mut linear_velocity, mut transform) in query.iter_mut() {
for (mut linear_velocity, mut transform) in &mut query {
if keys.pressed(KeyCode::KeyD) {
linear_velocity.x += 30.0;
}
Expand All @@ -370,8 +374,8 @@ pub fn car_movement(
if keys.pressed(KeyCode::Digit1) {
linear_velocity.x = 0.0;
linear_velocity.y = 0.0;
*transform =
Transform::from_xyz(car.initial_xyz.x, car.initial_xyz.y, car.initial_xyz.z);
*transform = INITIAL_POSITION;
}
}
}
const INITIAL_POSITION: Transform = Transform::from_xyz(-200.0, 2.0, 0.0);
Loading

0 comments on commit 2077b1a

Please sign in to comment.