Skip to content

Commit

Permalink
add example for interpolation using bevy_transform_interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Nov 25, 2024
1 parent a57d80c commit 8e616d9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ rust.unexpected_cfgs = { level = "warn", check-cfg = [

[profile.dev]
# Use slightly better optimization by default, as examples otherwise seem laggy.
opt-level = 1
#opt-level = 1

[profile.release]
codegen-units = 1
Expand All @@ -27,3 +27,4 @@ codegen-units = 1
#rapier3d = { git = "https://github.com/dimforge/rapier", branch = "character-controller" }
bevy_egui = { git = "https://github.com/Vrixyz/bevy_egui", branch = "bevy_main" }
bevy-inspector-egui = { git = "https://github.com/Vrixyz/bevy-inspector-egui", branch = "bevy_0.15" }
# bevy_transform_interpolation = { path = "../bevy_transform_interpolation" }
42 changes: 34 additions & 8 deletions bevy_rapier2d/examples/interpolation2.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{fs::File, io::Write};

use bevy::prelude::*;
use bevy::{color::palettes, prelude::*};
use bevy_mod_debugdump::{schedule_graph, schedule_graph_dot};
use bevy_rapier2d::prelude::*;
use bevy_transform_interpolation::{
use bevy_transform_interpolation::prelude::{
RotationInterpolation, TransformInterpolationPlugin, TranslationInterpolation,
};

Expand All @@ -19,14 +19,18 @@ fn main() {
time_scale: 1f32,
substeps: 10,
})
.insert_resource(Time::<Fixed>::from_seconds(0.4))
.insert_resource(Time::<Fixed>::from_hz(5.0))
.add_plugins((
DefaultPlugins,
TransformInterpolationPlugin::default(),
RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0).in_fixed_schedule(),
RapierDebugRenderPlugin::default(),
))
.add_systems(Startup, (setup_graphics, setup_physics));
app.add_systems(
PostUpdate,
debug_with_transform_info.after(TransformSystem::TransformPropagate),
);
let mut debugdump_settings = schedule_graph::Settings::default();
// Filter out some less relevant systems.
debugdump_settings.include_system =
Expand All @@ -50,6 +54,8 @@ fn main() {

app.run();
}
#[derive(Component, Clone)]
pub struct VisualBallDebug;

pub fn setup_graphics(mut commands: Commands) {
commands.spawn((Camera2d::default(), Transform::from_xyz(0.0, 20.0, 0.0)));
Expand All @@ -67,24 +73,44 @@ pub fn setup_physics(mut commands: Commands) {
Collider::cuboid(ground_size, ground_height),
));

commands.spawn((
Transform::from_xyz(-40.0, 200.0, 0.0),
let interpolated_ball = (
Transform::from_xyz(-0.0, 200.0, 0.0),
RigidBody::Dynamic,
Collider::ball(20.0),
Restitution {
coefficient: 1.0,
coefficient: 0.99,
combine_rule: CoefficientCombineRule::Max,
},
TranslationInterpolation,
RotationInterpolation,
VisualBallDebug,
);
commands.spawn(interpolated_ball.clone());
commands.spawn(interpolated_ball).insert((
ColliderDebug::NeverRender,
Transform::from_xyz(-80.0, 200.0, 0.0),
));
commands.spawn((
Transform::from_xyz(40.0, 200.0, 0.0),
Transform::from_xyz(80.0, 200.0, 0.0),
RigidBody::Dynamic,
Collider::ball(20.0),
Restitution {
coefficient: 1.0,
coefficient: 0.99,
combine_rule: CoefficientCombineRule::Max,
},
VisualBallDebug,
));
}

pub fn debug_with_transform_info(
mut gizmos: Gizmos,
entities: Query<(&Transform, &Collider), With<VisualBallDebug>>,
) {
for (transform, collider) in entities.iter() {
gizmos.circle(
transform.translation,
collider.as_ball().unwrap().radius(),
palettes::basic::RED,
);
}
}
14 changes: 14 additions & 0 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ impl<'world, 'state, 'world2, 'state2, 'a, 'b, 'c, 'v, 'p>
};
collider_debug == ColliderDebug::AlwaysRender
}
DebugRenderObject::RigidBody(h, ..) => {
let Some(collider) = self.context.bodies.get(h) else {
return false;
};
let entity = Entity::from_bits(collider.user_data as u64);

let collider_debug =
if let Ok(collider_override) = self.override_visibility.get(entity) {
*collider_override
} else {
self.default_collider_debug
};
collider_debug == ColliderDebug::AlwaysRender
}
_ => true,
}
}
Expand Down

0 comments on commit 8e616d9

Please sign in to comment.