Skip to content

Commit

Permalink
Add cone shape
Browse files Browse the repository at this point in the history
Closes jcornaz#64
  • Loading branch information
nicopap committed Nov 27, 2021
1 parent 7c5508f commit 60ac987
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ all-features = true
[features]
default = []
2d = ["heron_rapier/2d"]
3d = ["heron_rapier/3d"]
3d = ["heron_rapier/3d", "heron_core/3d"]
debug-2d = ["2d", "heron_debug/2d"]
debug-3d = ["3d", "heron_debug/3d"]

Expand Down
1 change: 0 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ fn main() {
dim2: { all(feature = "2d", not(feature = "3d")) },
// 3D feature takes precedence over 2D feature
dim3: { all(feature = "3d") },
// debug-3d doesn't exist yet, but include it for when it does
debug: { any(feature = "debug-2d", feature = "debug-3d") }
}
}
10 changes: 10 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ license = "MIT"
description = "Core components and resources to use Heron"
repository = "https://github.com/jcornaz/heron/"

[package.metadata.docs.rs]
all-features = true

[features]
default = []
3d = []

[dependencies]
bevy = { version = "0.5.0", default-features = false }
duplicate = "0.3.0"

[dev-dependencies]
rstest = "0.7"
bevy = { version = "0.5.0", default-features = false, features = ["render"] }

[build-dependencies]
cfg_aliases = "0.1.1"
8 changes: 8 additions & 0 deletions core/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
cfg_aliases::cfg_aliases! {
// 2D feature is only enabled if 3D is not enabled
dim2: { all(feature = "2d", not(feature = "3d")) },
// 3D feature takes precedence over 2D feature
dim3: { all(feature = "3d") }
}
}
13 changes: 13 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub fn should_run(
/// .insert(RigidBody::Dynamic) // Create a dynamic rigid body
/// .insert(CollisionShape::Sphere { radius: 1.0 }); // Attach a collision shape
/// }
/// ```
#[derive(Debug, Clone, Reflect)]
pub enum CollisionShape {
/// A sphere (or circle in 2d) shape defined by its radius
Expand Down Expand Up @@ -163,6 +164,18 @@ pub enum CollisionShape {
/// inner `Vec`, any other element will be ignored.
heights: Vec<Vec<f32>>,
},

/// A Cone shape, like a traffic cone, with a circular base
///
/// This shape is exclusive to the 3d API, you must enable the "3d" flag to use it.
/// For the 2d equivalent, look at [`Sphere`](CollisionShape::Sphere).
#[cfg(dim3)]
Cone {
/// Half of the height from the base of the cone to the top point
half_height: f32,
/// The radius of the base circle
radius: f32,
},
}

impl Default for CollisionShape {
Expand Down
9 changes: 8 additions & 1 deletion debug/src/dim3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use bevy_prototype_debug_lines::DebugLines;
use heron_core::{CollisionShape, RigidBody, SensorShape};

use crate::shape3d_wireframe::{
add_capsule, add_convex_hull, add_cuboid, add_height_field, add_rounded_cuboid, add_sphere,
add_capsule, add_cone, add_convex_hull, add_cuboid, add_height_field, add_rounded_cuboid,
add_sphere,
};

use super::DebugColor;
Expand Down Expand Up @@ -56,6 +57,12 @@ fn add_shape_outlines(
CollisionShape::HeightField { size, heights } => {
add_height_field(origin, orient, *size, heights, color, &mut lines);
}
CollisionShape::Cone {
half_height,
radius,
} => {
add_cone(origin, orient, *half_height, *radius, color, &mut lines);
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions debug/src/shape3d_wireframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ fn add_circle(origin: Vec3, orient: Quat, radius: f32, color: Color, lines: &mut
add_semicircle(origin, orient, radius, color, lines);
add_semicircle(origin, orient * x_rotate, radius, color, lines);
}
pub(crate) fn add_cone(
origin: Vec3,
orient: Quat,
half_height: f32,
radius: f32,
color: Color,
lines: &mut DebugLines,
) {
let cone_base = orient * (Vec3::Y * -half_height) + origin;
let cone_top = orient * (Vec3::Y * half_height) + origin;
let x_rotate = Quat::from_rotation_x(FRAC_PI_2);
let on_base_edge = |axis: Vec3, dir: f32| cone_base + dir * (orient * radius * axis);
add_circle(cone_base, orient * x_rotate, radius, color, lines);
lines.line_colored(on_base_edge(Vec3::Z, 1.0), cone_top, 0.0, color);
lines.line_colored(on_base_edge(Vec3::Z, -1.0), cone_top, 0.0, color);
lines.line_colored(on_base_edge(Vec3::X, 1.0), cone_top, 0.0, color);
lines.line_colored(on_base_edge(Vec3::X, -1.0), cone_top, 0.0, color);
}
pub(crate) fn add_sphere(
origin: Vec3,
orient: Quat,
Expand Down
22 changes: 22 additions & 0 deletions examples/debug_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,28 @@ fn setup(
.insert(RigidBody::Dynamic)
.insert(CollisionShape::Sphere { radius: 1.0 });

// Cone
commands
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Capsule {
radius: 0.5,
depth: 2.0,
..Default::default()
})),
material: materials.add(Color::RED.into()),
..Default::default()
})
.insert(Transform {
translation: Vec3::new(5., 15., -7.),
..Default::default()
})
.insert(GlobalTransform::identity())
.insert(RigidBody::Dynamic)
.insert(CollisionShape::Cone {
half_height: 2.0,
radius: 1.0,
});

// Capsule
commands
.spawn_bundle(PbrBundle {
Expand Down
4 changes: 4 additions & 0 deletions rapier/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ impl ColliderFactory for CollisionShape {
border_radius,
} => convex_hull_builder(points.as_slice(), *border_radius),
CollisionShape::HeightField { size, heights } => heightfield_builder(*size, heights),
CollisionShape::Cone {
half_height,
radius,
} => ColliderBuilder::cone(*half_height, *radius),
}
// General all types of collision events
.active_events(ActiveEvents::all())
Expand Down

0 comments on commit 60ac987

Please sign in to comment.