Skip to content

Commit

Permalink
Colliders from primitives (#326)
Browse files Browse the repository at this point in the history
# Objective

Support creating colliders from the new geometric primitives in Bevy 0.13.

## Solution

Add an `IntoCollider` trait and implement it for primitives. For now, tori and conical frusta are not supported, as they aren't as trivial to implement.

Some new collider shapes are also supported: ellipses and regular polygons. These are implemented using custom shapes, as Parry doesn't have them built-in.

`Collider::ball` is now also `Collider::circle` in 2D and `Collider::sphere` in 3D. `Collider::cuboid` is `Collider::rectangle` in 2D. This makes the naming consistent with Bevy. 

---

## Migration Guide

- Replace `Collider::ball` with `Collider::circle` in 2D and `Collider::sphere` in 3D
- Replace `Collider::cuboid` with `Collider::rectangle` in 2D
  • Loading branch information
Jondolf authored Feb 18, 2024
1 parent bbd0b2b commit 3e3c0bd
Show file tree
Hide file tree
Showing 44 changed files with 1,185 additions and 702 deletions.
2 changes: 1 addition & 1 deletion crates/benches_common_3d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ version = "0.1.0"
edition = "2021"

[dependencies]
bevy = { git = "https://github.com/bevyengine/bevy.git", default-features = false }
bevy = { version = "0.13", default-features = false }
bevy_xpbd_3d = { path = "../bevy_xpbd_3d", default-features = false }
criterion = "0.5"
12 changes: 5 additions & 7 deletions crates/bevy_xpbd_2d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ required-features = ["2d"]

[dependencies]
bevy_xpbd_derive = { path = "../bevy_xpbd_derive", version = "0.1" }
bevy = { git = "https://github.com/bevyengine/bevy.git", default-features = false }
bevy_math = { git = "https://github.com/bevyengine/bevy.git" }
bevy = { version = "0.13", default-features = false }
bevy_math = "0.13"
parry2d = { git = "https://github.com/Jondolf/parry.git", branch = "update-nalgebra", optional = true }
parry2d-f64 = { git = "https://github.com/Jondolf/parry.git", branch = "update-nalgebra", optional = true }
nalgebra = { git = "https://github.com/Jondolf/nalgebra.git", features = [
nalgebra = { git = "https://github.com/dimforge/nalgebra", features = [
"convert-glam025",
], branch = "glam025" }
] }
serde = { version = "1", features = ["derive"], optional = true }
derive_more = "0.99"
indexmap = "2.0.0"
Expand All @@ -53,9 +53,7 @@ itertools = "0.12"

[dev-dependencies]
examples_common_2d = { path = "../examples_common_2d" }
bevy_math = { git = "https://github.com/bevyengine/bevy.git", features = [
"approx",
] }
bevy_math = { version = "0.13", features = ["approx"] }
approx = "0.5"
insta = "1.0"

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_xpbd_2d/examples/chain_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn setup(
let current_particle = commands
.spawn((
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::ball(particle_radius), 1.0),
MassPropertiesBundle::new_computed(&Collider::circle(particle_radius), 1.0),
MaterialMesh2dBundle {
mesh: particle_mesh.clone(),
material: particle_material.clone(),
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_xpbd_2d/examples/collision_layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(500.0, 25.0),
Collider::rectangle(500.0, 25.0),
CollisionLayers::new([Layer::Blue], [Layer::Blue]),
));

Expand All @@ -55,7 +55,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(500.0, 25.0),
Collider::rectangle(500.0, 25.0),
CollisionLayers::new([Layer::Red], [Layer::Red]),
));

Expand All @@ -78,7 +78,7 @@ fn setup(
..default()
},
RigidBody::Dynamic,
Collider::ball(marble_radius as Scalar),
Collider::circle(marble_radius as Scalar),
CollisionLayers::new([Layer::Blue], [Layer::Blue]),
));
}
Expand All @@ -100,7 +100,7 @@ fn setup(
..default()
},
RigidBody::Dynamic,
Collider::ball(marble_radius as Scalar),
Collider::circle(marble_radius as Scalar),
CollisionLayers::new([Layer::Red], [Layer::Red]),
));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_xpbd_2d/examples/distance_joint_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn setup(mut commands: Commands) {
..default()
},
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::cuboid(50.0, 50.0), 1.0),
MassPropertiesBundle::new_computed(&Collider::rectangle(50.0, 50.0), 1.0),
))
.id();

Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_xpbd_2d/examples/dynamic_character_2d/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn setup(
..default()
},
RigidBody::Dynamic,
Collider::cuboid(30.0, 30.0),
Collider::rectangle(30.0, 30.0),
));

// Platforms
Expand All @@ -91,7 +91,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(1100.0, 50.0),
Collider::rectangle(1100.0, 50.0),
));
commands.spawn((
SpriteBundle {
Expand All @@ -104,7 +104,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(300.0, 25.0),
Collider::rectangle(300.0, 25.0),
));
commands.spawn((
SpriteBundle {
Expand All @@ -117,7 +117,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(300.0, 25.0),
Collider::rectangle(300.0, 25.0),
));
commands.spawn((
SpriteBundle {
Expand All @@ -130,7 +130,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(150.0, 80.0),
Collider::rectangle(150.0, 80.0),
));
commands.spawn((
SpriteBundle {
Expand All @@ -143,7 +143,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(150.0, 80.0),
Collider::rectangle(150.0, 80.0),
));

// Ramps
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_xpbd_2d/examples/fixed_joint_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn setup(mut commands: Commands) {
..default()
},
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::cuboid(50.0, 50.0), 1.0),
MassPropertiesBundle::new_computed(&Collider::rectangle(50.0, 50.0), 1.0),
))
.id();

Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_xpbd_2d/examples/kinematic_character_2d/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn setup(
..default()
},
RigidBody::Dynamic,
Collider::cuboid(30.0, 30.0),
Collider::rectangle(30.0, 30.0),
));

// Platforms
Expand All @@ -84,7 +84,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(1100.0, 50.0),
Collider::rectangle(1100.0, 50.0),
));
commands.spawn((
SpriteBundle {
Expand All @@ -97,7 +97,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(300.0, 25.0),
Collider::rectangle(300.0, 25.0),
));
commands.spawn((
SpriteBundle {
Expand All @@ -110,7 +110,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(300.0, 25.0),
Collider::rectangle(300.0, 25.0),
));
commands.spawn((
SpriteBundle {
Expand All @@ -123,7 +123,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(150.0, 80.0),
Collider::rectangle(150.0, 80.0),
));
commands.spawn((
SpriteBundle {
Expand All @@ -136,7 +136,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(150.0, 80.0),
Collider::rectangle(150.0, 80.0),
));

// Ramps
Expand Down
18 changes: 9 additions & 9 deletions crates/bevy_xpbd_2d/examples/many_shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(50.0, 50.0),
Collider::rectangle(50.0, 50.0),
));
// Floor
commands.spawn((
Expand All @@ -51,7 +51,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(50.0, 50.0),
Collider::rectangle(50.0, 50.0),
));
// Left wall
commands.spawn((
Expand All @@ -62,7 +62,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(50.0, 50.0),
Collider::rectangle(50.0, 50.0),
));
// Right wall
commands.spawn((
Expand All @@ -73,16 +73,16 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(50.0, 50.0),
Collider::rectangle(50.0, 50.0),
));

let ball = (
Collider::ball(7.5),
let circle = (
Collider::circle(7.5),
meshes.add(shape::Circle::new(7.5)).into(),
materials.add(Color::rgb(0.29, 0.33, 0.64)),
);
let cuboid = (
Collider::cuboid(15.0, 15.0),
let rectangle = (
Collider::rectangle(15.0, 15.0),
meshes.add(shape::Box::new(15.0, 15.0, 15.0)).into(),
materials.add(Color::rgb(0.47, 0.58, 0.8)),
);
Expand All @@ -109,7 +109,7 @@ fn setup(
meshes.add(shape::RegularPolygon::new(10.0, 3)).into(),
materials.add(Color::rgb(0.77, 0.87, 0.97)),
);
let shapes = [ball, cuboid, capsule, triangle];
let shapes = [circle, rectangle, capsule, triangle];

for x in -12_i32..12 {
for y in -8_i32..8 {
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_xpbd_2d/examples/move_marbles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(50.0, 50.0),
Collider::rectangle(50.0, 50.0),
));
// Floor
commands.spawn((
Expand All @@ -51,7 +51,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(50.0, 50.0),
Collider::rectangle(50.0, 50.0),
));
// Left wall
commands.spawn((
Expand All @@ -62,7 +62,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(50.0, 50.0),
Collider::rectangle(50.0, 50.0),
));
// Right wall
commands.spawn((
Expand All @@ -73,7 +73,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(50.0, 50.0),
Collider::rectangle(50.0, 50.0),
));

let marble_radius = 5.0;
Expand All @@ -95,7 +95,7 @@ fn setup(
..default()
},
RigidBody::Dynamic,
Collider::ball(marble_radius as Scalar),
Collider::circle(marble_radius as Scalar),
Marble,
));
}
Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_xpbd_2d/examples/one_way_platform_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(50.0, 50.0),
Collider::rectangle(50.0, 50.0),
));
// Floor
commands.spawn((
Expand All @@ -77,7 +77,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(50.0, 50.0),
Collider::rectangle(50.0, 50.0),
));
// Left wall
commands.spawn((
Expand All @@ -88,7 +88,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(50.0, 50.0),
Collider::rectangle(50.0, 50.0),
));
// Right wall
commands.spawn((
Expand All @@ -99,7 +99,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(50.0, 50.0),
Collider::rectangle(50.0, 50.0),
));

// For one-way platforms
Expand All @@ -119,7 +119,7 @@ fn setup(
..default()
},
RigidBody::Static,
Collider::cuboid(50.0, 50.0),
Collider::rectangle(50.0, 50.0),
OneWayPlatform::default(),
));
}
Expand All @@ -137,7 +137,7 @@ fn setup(
RigidBody::Dynamic,
LockedAxes::ROTATION_LOCKED,
Restitution::ZERO.with_combine_rule(CoefficientCombine::Min),
Collider::cuboid(actor_size.x, actor_size.y),
Collider::rectangle(actor_size.x, actor_size.y),
Actor,
PassThroughOneWayPlatform::ByNormal,
MovementSpeed(250.0),
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_xpbd_2d/examples/prismatic_joint_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn setup(mut commands: Commands) {
..default()
},
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::cuboid(50.0, 50.0), 1.0),
MassPropertiesBundle::new_computed(&Collider::rectangle(50.0, 50.0), 1.0),
))
.id();

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_xpbd_2d/examples/ray_caster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn setup(
),
..default()
},
Collider::ball(radius as Scalar),
Collider::circle(radius as Scalar),
));
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_xpbd_2d/examples/revolute_joint_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn setup(mut commands: Commands) {
..default()
},
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::cuboid(50.0, 50.0), 1.0),
MassPropertiesBundle::new_computed(&Collider::rectangle(50.0, 50.0), 1.0),
))
.id();

Expand Down
Loading

0 comments on commit 3e3c0bd

Please sign in to comment.