Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor of code #22

Merged
merged 26 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0978fe1
add: lint parameters
salam99823 Nov 11, 2024
298a383
fix: linter warnings
salam99823 Nov 11, 2024
8b12b5e
upd: thiserror dependency
salam99823 Nov 11, 2024
d2079cb
dependency `bevy` made dev and replaced with `bevy_math` and `bevy_re…
salam99823 Nov 11, 2024
1019ba6
add: rayon dependency for faster
salam99823 Nov 11, 2024
e4c501f
updated `edges` version
salam99823 Nov 12, 2024
1ef8422
`initial_xyz` variable renamed to `INITIAL_POSITION` and makes const
salam99823 Nov 12, 2024
3a51cc0
`ImagePlugin` type changed to `default_nearest`
salam99823 Nov 12, 2024
43b2e2c
changed: pixels per meter parameter from 1 to 100 for larger mass of …
salam99823 Nov 12, 2024
881b24d
inverted camera controls for convenience
salam99823 Nov 12, 2024
554404b
refactor: functions to generate colliders and its docs simplified, in…
salam99823 Nov 12, 2024
594e767
add: `parallel` feature
salam99823 Nov 12, 2024
42c750e
removed `bevy_render` dependency
salam99823 Nov 12, 2024
2f1f35b
removed `thiserror` dependency
salam99823 Nov 12, 2024
b607358
fixed argument naming
salam99823 Nov 13, 2024
618b5f7
replaced: small functions with long names
salam99823 Nov 13, 2024
f000104
avian2d remove from default features
salam99823 Nov 14, 2024
783b380
default features of rapier2d and avian 2d disabled
salam99823 Nov 14, 2024
2798f00
add: doc for `ColliderType`
salam99823 Nov 14, 2024
afa1f9c
refactor: conversion to collider, moved outside of generation functions
salam99823 Nov 14, 2024
56f3a2f
renamed public generate functions
salam99823 Nov 14, 2024
f8682d3
renamed `translate` argument
salam99823 Nov 14, 2024
5161d53
add: doc for public functions
salam99823 Nov 14, 2024
e1af25d
updated README.md
salam99823 Nov 14, 2024
b11b3bb
Merge pull request #1 from salam99823/dev
salam99823 Nov 14, 2024
aba0383
fix: rapier2d build
salam99823 Nov 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading