Skip to content

Commit

Permalink
replaced: small functions with long names
Browse files Browse the repository at this point in the history
  • Loading branch information
salam99823 committed Nov 13, 2024
1 parent b607358 commit 618b5f7
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 406 deletions.
16 changes: 7 additions & 9 deletions examples/avian2d_colliders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ use bevy::{
},
};
use bevy_collider_gen::{
avian2d::{
multi_convex_polyline_collider_translated, single_convex_polyline_collider_translated,
single_heightfield_collider_translated,
},
Edges,
avian2d::{multi_collider, single_collider},
edges::Edges,
ColliderType,
};
use bevy_prototype_lyon::{
prelude::{Fill, GeometryBuilder, ShapePlugin},
Expand Down Expand Up @@ -46,7 +44,7 @@ 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 = multi_collider(sprite_image, ColliderType::ConvexPolyline, true);
for collider in colliders {
commands.spawn((
collider.unwrap(),
Expand Down Expand Up @@ -77,7 +75,7 @@ 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 = single_collider(sprite_image, ColliderType::ConvexPolyline, true).unwrap();
commands.spawn((
collider,
SpriteBundle {
Expand All @@ -103,7 +101,7 @@ fn terrain_spawn(
return;
}
let sprite_image = image_assets.get(sprite_handle.unwrap()).unwrap();
let collider = single_heightfield_collider_translated(sprite_image);
let collider = single_collider(sprite_image, ColliderType::Heightfield, true).unwrap();
commands.spawn((
collider,
RigidBody::Static,
Expand Down Expand Up @@ -131,7 +129,7 @@ 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 = multi_collider(sprite_image, ColliderType::ConvexPolyline, true);

for (coords, collider) in coord_group.iter().zip(colliders.into_iter()) {
let shape = shapes::Polygon {
Expand Down
16 changes: 7 additions & 9 deletions examples/rapier2d_colliders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ use bevy::{
},
};
use bevy_collider_gen::{
rapier2d::{
multi_convex_polyline_collider_translated, single_convex_polyline_collider_translated,
single_heightfield_collider_translated,
},
Edges,
edges::Edges,
rapier2d::{multi_collider, single_collider},
ColliderType,
};
use bevy_prototype_lyon::{
prelude::{Fill, GeometryBuilder, ShapePlugin},
Expand Down Expand Up @@ -45,7 +43,7 @@ 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 = multi_collider(sprite_image, ColliderType::ConvexPolyline, true);
for collider in colliders {
commands.spawn((
collider.unwrap(),
Expand Down Expand Up @@ -93,7 +91,7 @@ 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 = single_collider(sprite_image, ColliderType::ConvexPolyline, true).unwrap();
commands.spawn((
collider,
RigidBody::Dynamic,
Expand All @@ -119,7 +117,7 @@ fn terrain_spawn(
return;
}
let sprite_image = image_assets.get(sprite_handle.unwrap()).unwrap();
let collider = single_heightfield_collider_translated(sprite_image);
let collider = single_collider(sprite_image, ColliderType::Heightfield, true).unwrap();
commands.spawn((
collider,
RigidBody::Fixed,
Expand All @@ -146,7 +144,7 @@ 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 = multi_collider(sprite_image, ColliderType::ConvexPolyline, true);

for (coords, collider) in coord_group.iter().zip(colliders.into_iter()) {
let shape = shapes::Polygon {
Expand Down
241 changes: 80 additions & 161 deletions src/avian2d.rs
Original file line number Diff line number Diff line change
@@ -1,166 +1,85 @@
use edges::Edges;

use crate::utils::{
avian2d::{convex_hull_collider, convex_polyline_collider, heightfield_collider, Collider},
generate_collider, generate_multi_collider,
use avian2d::{
parry::{math::Point, shape::SharedShape},
prelude::Collider,
};
use edges::Edges;

/// Generate a single polyline collider from the image,
/// coordinates translated to either side of (0, 0)
#[must_use]
pub fn single_polyline_collider_translated<I>(image: I) -> Collider
where
Edges: From<I>,
{
generate_collider(image, |points| Collider::polyline(points, None), true)
}

/// Generate a single polyline collider from the image,
/// coordinates left alone and all in positive x and y
#[must_use]
pub fn single_polyline_collider_raw<I>(image: I) -> Collider
where
Edges: From<I>,
{
generate_collider(image, |points| Collider::polyline(points, None), false)
}

/// Generate a single `convex_polyline` collider from the image,
/// coordinates translated to either side of (0, 0)
#[must_use]
pub fn single_convex_polyline_collider_translated<I>(image: I) -> Option<Collider>
where
Edges: From<I>,
{
generate_collider(image, convex_polyline_collider, true)
}

/// Generate a single `convex_polyline` collider from the image,
/// coordinates left alone and all in positive x and y
#[must_use]
pub fn single_convex_polyline_collider_raw<I>(image: I) -> Option<Collider>
where
Edges: From<I>,
{
generate_collider(image, convex_polyline_collider, false)
}

/// Generate a single `convex_hull` collider from the image,
/// coordinates translated to either side of (0, 0)
#[must_use]
pub fn single_convex_hull_collider_translated<I>(image: I) -> Option<Collider>
where
Edges: From<I>,
{
generate_collider(image, convex_hull_collider, true)
}

/// Generate a single `convex_hull` collider from the image,
/// coordinates left alone and all in positive x and y
#[must_use]
pub fn single_convex_hull_collider_raw<I>(image: I) -> Option<Collider>
where
Edges: From<I>,
{
generate_collider(image, convex_hull_collider, false)
}

/// Generate a single heightfield collider from the image,
/// coordinates translated to either side of (0, 0)
#[must_use]
pub fn single_heightfield_collider_translated<I>(image: I) -> Collider
where
Edges: From<I>,
{
generate_collider(image, heightfield_collider, true)
}

/// Generate a single heightfield collider from the image,
/// coordinates left alone and all in positive x and y
#[must_use]
pub fn single_heightfield_collider_raw<I>(image: I) -> Collider
where
Edges: From<I>,
{
generate_collider(image, heightfield_collider, false)
}

/// Generate as many polyline colliders as it can find in the image,
/// coordinates translated to either side of (0, 0)
#[must_use]
pub fn multi_polyline_collider_translated<I>(image: I) -> Vec<Collider>
where
Edges: From<I>,
{
generate_multi_collider(image, |points| Collider::polyline(points, None), true)
}

/// Generate as many polyline colliders as it can find in the image,
/// coordinates left alone and all in positive x and y
#[must_use]
pub fn multi_polyline_collider_raw<I>(image: I) -> Vec<Collider>
where
Edges: From<I>,
{
generate_multi_collider(image, |points| Collider::polyline(points, None), false)
}

/// Generate as many `convex_polyline` colliders as it can find in the image,
/// coordinates translated to either side of (0, 0)
#[must_use]
pub fn multi_convex_polyline_collider_translated<I>(image: I) -> Vec<Option<Collider>>
where
Edges: From<I>,
{
generate_multi_collider(image, convex_polyline_collider, true)
}

/// Generate as many `convex_polyline` colliders as it can find in the image,
/// coordinates left alone and all in positive x and y
#[must_use]
pub fn multi_convex_polyline_collider_raw<I>(image: I) -> Vec<Option<Collider>>
where
Edges: From<I>,
{
generate_multi_collider(image, convex_polyline_collider, false)
}

/// Generate as many heightfield colliders as it can find in the image,
/// coordinates translated to either side of (0, 0)
#[must_use]
pub fn multi_heightfield_collider_translated<I>(image: I) -> Vec<Collider>
where
Edges: From<I>,
{
generate_multi_collider(image, heightfield_collider, true)
}

/// Generate as many heightfield colliders as it can find in the image,
/// coordinates left alone and all in positive x and y
#[must_use]
pub fn multi_heightfield_collider_raw<I>(image: I) -> Vec<Collider>
where
Edges: From<I>,
{
generate_multi_collider(image, heightfield_collider, false)
}
#[cfg(feature = "parallel")]
use rayon::prelude::*;

/// Generate as many `convex_hull` colliders as it can find in the image,
/// coordinates translated to either side of (0, 0)
#[must_use]
pub fn multi_convex_hull_collider_translated<I>(image: I) -> Vec<Option<Collider>>
where
Edges: From<I>,
{
generate_multi_collider(image, convex_hull_collider, true)
}
use crate::{
utils::{generate_collider, generate_multi_collider, heights_and_scale},
ColliderType,
};

/// Generate as many `convex_hull` colliders as it can find in the image,
/// coordinates left alone and all in positive x and y
#[must_use]
pub fn multi_convex_hull_collider_raw<I>(image: I) -> Vec<Option<Collider>>
where
Edges: From<I>,
{
generate_multi_collider(image, convex_hull_collider, false)
/// Generate a single collider from the image.
#[must_use]
pub fn single_collider<I>(
image: I,
collider_type: ColliderType,
translated: bool,
) -> Option<Collider>
where
Edges: From<I>,
{
let collider_fn = match collider_type {
ColliderType::Polyline => |vertices| Some(Collider::polyline(vertices, None)),
ColliderType::ConvexPolyline => |points: Vec<_>| {
SharedShape::convex_polyline(
{
#[cfg(not(feature = "parallel"))]
let iterator = points.into_iter();

#[cfg(feature = "parallel")]
let iterator = points.into_par_iter();
iterator
}
.map(Point::from)
.collect(),
)
.map(Collider::from)
},
ColliderType::ConvexHull => |points| Collider::convex_hull(points),
ColliderType::Heightfield => |points| {
let (heights, scale) = heights_and_scale(points);
Some(Collider::heightfield(heights, scale))
},
};
generate_collider(image, collider_fn, translated)
}

/// Generate as many colliders as it can find in the image.
#[must_use]
pub fn multi_collider<I>(
image: I,
collider_type: ColliderType,
translated: bool,
) -> Vec<Option<Collider>>
where
Edges: From<I>,
{
let collider_fn = match collider_type {
ColliderType::Polyline => |vertices| Some(Collider::polyline(vertices, None)),
ColliderType::ConvexPolyline => |points: Vec<_>| {
SharedShape::convex_polyline(
{
#[cfg(not(feature = "parallel"))]
let iterator = points.into_iter();

#[cfg(feature = "parallel")]
let iterator = points.into_par_iter();
iterator
}
.map(Point::from)
.collect(),
)
.map(Collider::from)
},
ColliderType::ConvexHull => |points| Collider::convex_hull(points),
ColliderType::Heightfield => |points| {
let (heights, scale) = heights_and_scale(points);
Some(Collider::heightfield(heights, scale))
},
};
generate_multi_collider(image, collider_fn, translated)
}
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#![doc = include_str!("../README.md")]

pub use edges::Edges;
pub extern crate edges;

#[cfg(feature = "avian2d")]
pub mod avian2d;
#[cfg(feature = "rapier2d")]
pub mod rapier2d;
mod utils;

#[derive(Clone, Copy, Debug)]
pub enum ColliderType {
Polyline,
ConvexPolyline,
ConvexHull,
Heightfield,
}
Loading

0 comments on commit 618b5f7

Please sign in to comment.