Skip to content

Commit

Permalink
Merge branch 'master' into RapierContext_Component
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Aug 12, 2024
2 parents aeab8c9 + c2bed72 commit 07eefbb
Show file tree
Hide file tree
Showing 12 changed files with 364 additions and 17 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Modified

- Update from rapier `0.21` to rapier `0.22`,
see [rapier's changelog](https://github.com/dimforge/rapier/blob/master/CHANGELOG.md).

### Fix

- Fix a crash when using `TimestepMode::Interpolated` and removing colliders
Expand All @@ -12,6 +17,9 @@ during a frame which would not run a simulation step.
- Added a `TriMeshFlags` parameter for `ComputedColliderShape`,
its default value is `TriMeshFlags::MERGE_DUPLICATE_VERTICES`,
which was its hardcoded behaviour.
- Added a way to configure which colliders should be debug rendered: `global` parameter for both
`RapierDebugColliderPlugin` and `DebugRenderContext`, as well as individual collider setup via
a `ColliderDebug` component.

### Modified

Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
members = ["bevy_rapier2d", "bevy_rapier3d", "bevy_rapier_benches3d"]
resolver = "2"

[workspace.lints]
rust.unexpected_cfgs = { level = "warn", check-cfg = [
'cfg(feature, values("dim2", "dim3"))',
] }

[profile.dev]
# Use slightly better optimization by default, as examples otherwise seem laggy.
opt-level = 1
Expand Down
5 changes: 4 additions & 1 deletion bevy_rapier2d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ edition = "2021"
path = "../src/lib.rs"
required-features = ["dim2"]

[lints]
workspace = true

[features]
default = ["dim2", "async-collider", "debug-render-2d"]
dim2 = []
Expand Down Expand Up @@ -46,7 +49,7 @@ async-collider = ["bevy/bevy_asset", "bevy/bevy_scene"]
[dependencies]
bevy = { version = "0.14", default-features = false }
nalgebra = { version = "0.33", features = ["convert-glam027"] }
rapier2d = "0.21"
rapier2d = "0.22"
bitflags = "2.4"
log = "0.4"
serde = { version = "1", features = ["derive"], optional = true }
Expand Down
117 changes: 117 additions & 0 deletions bevy_rapier2d/examples/debug_toggle2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use std::time::Duration;

pub use bevy::input::common_conditions::input_just_pressed;
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;

fn main() {
App::new()
.insert_resource(ClearColor(Color::srgb(
0xF9 as f32 / 255.0,
0xF9 as f32 / 255.0,
0xFF as f32 / 255.0,
)))
.add_plugins((
DefaultPlugins,
RapierPhysicsPlugin::<NoUserData>::default(),
RapierDebugRenderPlugin::default(),
))
.add_systems(Startup, (setup_graphics, setup_physics))
.add_systems(
Update,
(
toggle_debug,
(|mut debug_render_context: ResMut<DebugRenderContext>| {
debug_render_context.enabled = !debug_render_context.enabled;
})
.run_if(input_just_pressed(KeyCode::KeyV)),
),
)
.run();
}

pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-30.0, 30.0, 100.0)
.looking_at(Vec3::new(0.0, 10.0, 0.0), Vec3::Y),
..Default::default()
});
}

#[derive(Component)]
pub struct DebugCooldown(pub Timer);

pub fn setup_physics(mut commands: Commands) {
/*
* Ground
*/
let ground_size = 200.1;
let ground_height = 0.1;

commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, -ground_height, 0.0)),
Collider::cuboid(ground_size, ground_height),
));

/*
* Create the cubes
*/
let num = 8;
let rad = 1.0;

let shift = rad * 2.0 + rad;
let centerx = shift * (num / 2) as f32;
let centery = shift / 2.0;
let centerz = shift * (num / 2) as f32;

let mut offset = -(num as f32) * (rad * 2.0 + rad) * 0.5;
let mut color = 0;
let colors = [
Hsla::hsl(220.0, 1.0, 0.3),
Hsla::hsl(180.0, 1.0, 0.3),
Hsla::hsl(260.0, 1.0, 0.7),
];

for j in 0usize..20 {
for i in 0..num {
for k in 0usize..num {
let x = i as f32 * shift - centerx + offset;
let y = j as f32 * shift + centery + 3.0;
let z = k as f32 * shift - centerz + offset;
color += 1;

commands
.spawn(TransformBundle::from(Transform::from_rotation(
Quat::from_rotation_x(0.2),
)))
.with_children(|child| {
child.spawn((
TransformBundle::from(Transform::from_xyz(x, y, z)),
RigidBody::Dynamic,
Collider::cuboid(rad, rad),
ColliderDebugColor(colors[color % 3]),
ColliderDebug::AlwaysRender,
DebugCooldown(Timer::new(
Duration::from_secs_f32(0.4f32 + (i % 3 + (j + 1) % 3) as f32),
TimerMode::Repeating,
)),
));
});
}
}

offset -= 0.05 * rad * (num as f32 - 1.0);
}
}

pub fn toggle_debug(time: Res<Time>, mut query: Query<(&mut ColliderDebug, &mut DebugCooldown)>) {
for (mut debug, mut cooldown) in query.iter_mut() {
cooldown.0.tick(time.delta());
if cooldown.0.just_finished() {
*debug = match *debug {
ColliderDebug::AlwaysRender => ColliderDebug::NeverRender,
ColliderDebug::NeverRender => ColliderDebug::AlwaysRender,
}
}
}
}
19 changes: 19 additions & 0 deletions bevy_rapier2d/examples/testbed2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod boxes2;
mod debug_despawn2;
mod debug_toggle2;
mod despawn2;
mod events2;
mod joints2;
Expand All @@ -21,6 +22,7 @@ pub enum Examples {
#[default]
None,
Boxes2,
DebugToggle2,
RopeJoint2,
DebugDespawn2,
Despawn2,
Expand Down Expand Up @@ -90,6 +92,23 @@ fn main() {
)
.add_systems(OnExit(Examples::Boxes2), cleanup)
//
// Debug toggle
.add_systems(
OnEnter(Examples::DebugToggle2),
(debug_toggle2::setup_graphics, debug_toggle2::setup_physics),
)
.add_systems(
Update,
(
debug_toggle2::toggle_debug,
(|mut debug_render_context: ResMut<DebugRenderContext>| {
debug_render_context.enabled = !debug_render_context.enabled;
})
.run_if(debug_toggle2::input_just_pressed(KeyCode::KeyV)),
)
.run_if(in_state(Examples::DebugToggle2)),
)
//
// rope joint
.add_systems(
OnEnter(Examples::RopeJoint2),
Expand Down
5 changes: 4 additions & 1 deletion bevy_rapier3d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ edition = "2021"
path = "../src/lib.rs"
required-features = ["dim3"]

[lints]
workspace = true

[features]
default = ["dim3", "async-collider", "debug-render-3d"]
dim3 = []
Expand Down Expand Up @@ -47,7 +50,7 @@ async-collider = ["bevy/bevy_asset", "bevy/bevy_scene"]
[dependencies]
bevy = { version = "0.14", default-features = false }
nalgebra = { version = "0.33", features = ["convert-glam027"] }
rapier3d = "0.21"
rapier3d = "0.22"
bitflags = "2.4"
log = "0.4"
serde = { version = "1", features = ["derive"], optional = true }
Expand Down
117 changes: 117 additions & 0 deletions bevy_rapier3d/examples/debug_toggle3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use std::time::Duration;

pub use bevy::input::common_conditions::input_just_pressed;
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;

fn main() {
App::new()
.insert_resource(ClearColor(Color::srgb(
0xF9 as f32 / 255.0,
0xF9 as f32 / 255.0,
0xFF as f32 / 255.0,
)))
.add_plugins((
DefaultPlugins,
RapierPhysicsPlugin::<NoUserData>::default(),
RapierDebugRenderPlugin::default(),
))
.add_systems(Startup, (setup_graphics, setup_physics))
.add_systems(
Update,
(
toggle_debug,
(|mut debug_render_context: ResMut<DebugRenderContext>| {
debug_render_context.enabled = !debug_render_context.enabled;
})
.run_if(input_just_pressed(KeyCode::KeyV)),
),
)
.run();
}

pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-30.0, 30.0, 100.0)
.looking_at(Vec3::new(0.0, 10.0, 0.0), Vec3::Y),
..Default::default()
});
}

#[derive(Component)]
pub struct DebugCooldown(pub Timer);

pub fn setup_physics(mut commands: Commands) {
/*
* Ground
*/
let ground_size = 200.1;
let ground_height = 0.1;

commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, -ground_height, 0.0)),
Collider::cuboid(ground_size, ground_height, ground_size),
));

/*
* Create the cubes
*/
let num = 8;
let rad = 1.0;

let shift = rad * 2.0 + rad;
let centerx = shift * (num / 2) as f32;
let centery = shift / 2.0;
let centerz = shift * (num / 2) as f32;

let mut offset = -(num as f32) * (rad * 2.0 + rad) * 0.5;
let mut color = 0;
let colors = [
Hsla::hsl(220.0, 1.0, 0.3),
Hsla::hsl(180.0, 1.0, 0.3),
Hsla::hsl(260.0, 1.0, 0.7),
];

for j in 0usize..20 {
for i in 0..num {
for k in 0usize..num {
let x = i as f32 * shift - centerx + offset;
let y = j as f32 * shift + centery + 3.0;
let z = k as f32 * shift - centerz + offset;
color += 1;

commands
.spawn(TransformBundle::from(Transform::from_rotation(
Quat::from_rotation_x(0.2),
)))
.with_children(|child| {
child.spawn((
TransformBundle::from(Transform::from_xyz(x, y, z)),
RigidBody::Dynamic,
Collider::cuboid(rad, rad, rad),
ColliderDebugColor(colors[color % 3]),
ColliderDebug::AlwaysRender,
DebugCooldown(Timer::new(
Duration::from_secs_f32(0.4f32 + (i % 3 + (j + 1) % 3) as f32),
TimerMode::Repeating,
)),
));
});
}
}

offset -= 0.05 * rad * (num as f32 - 1.0);
}
}

pub fn toggle_debug(time: Res<Time>, mut query: Query<(&mut ColliderDebug, &mut DebugCooldown)>) {
for (mut debug, mut cooldown) in query.iter_mut() {
cooldown.0.tick(time.delta());
if cooldown.0.just_finished() {
*debug = match *debug {
ColliderDebug::AlwaysRender => ColliderDebug::NeverRender,
ColliderDebug::NeverRender => ColliderDebug::AlwaysRender,
}
}
}
}
20 changes: 20 additions & 0 deletions bevy_rapier3d/examples/testbed3.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(dead_code)]

mod boxes3;
mod debug_toggle3;
mod despawn3;
mod events3;
mod joints3;
Expand All @@ -20,6 +21,7 @@ pub enum Examples {
#[default]
None,
Boxes3,
DebugToggle3,
Despawn3,
Events3,
Joints3,
Expand Down Expand Up @@ -69,6 +71,7 @@ fn main() {
.init_state::<Examples>()
.insert_resource(ExampleSet(vec![
(Examples::Boxes3, "Boxes3").into(),
(Examples::DebugToggle3, "DebugToggle3").into(),
(Examples::Despawn3, "Despawn3").into(),
(Examples::Events3, "Events3").into(),
(Examples::Joints3, "Joints3").into(),
Expand All @@ -87,6 +90,23 @@ fn main() {
)
.add_systems(OnExit(Examples::Boxes3), cleanup)
//
// Debug toggle
.add_systems(
OnEnter(Examples::DebugToggle3),
(debug_toggle3::setup_graphics, debug_toggle3::setup_physics),
)
.add_systems(
Update,
(
debug_toggle3::toggle_debug,
(|mut debug_render_context: ResMut<DebugRenderContext>| {
debug_render_context.enabled = !debug_render_context.enabled;
})
.run_if(debug_toggle3::input_just_pressed(KeyCode::KeyV)),
)
.run_if(in_state(Examples::DebugToggle3)),
)
//
// despawn
.init_resource::<despawn3::DespawnResource>()
.add_systems(
Expand Down
Loading

0 comments on commit 07eefbb

Please sign in to comment.