Correcting InheritableVariable::visit and physics integration parameters dt (#844)

This commit is contained in:
b-guild
2025-09-30 14:13:02 +03:00
committed by GitHub
parent dadf078092
commit 7a71aa9bb5
5 changed files with 62 additions and 12 deletions
+2 -4
View File
@@ -211,10 +211,6 @@ impl GameScene {
scene_content_root,
);
// Freeze physics simulation in while editing scene by setting time step to zero.
scene.graph.physics.integration_parameters.dt = Some(0.0);
scene.graph.physics2d.integration_parameters.dt = Some(0.0);
GameScene {
editor_objects_root,
scene_content_root,
@@ -224,6 +220,8 @@ impl GameScene {
clipboard: Default::default(),
preview_camera: Default::default(),
graph_switches: GraphUpdateSwitches {
// Freeze physics simulation while editing scene by setting time step to zero.
physics_dt: false,
physics2d: true,
physics: true,
// Prevent engine to update lifetime of the nodes and to delete "dead" nodes. Otherwise
+9 -2
View File
@@ -337,8 +337,15 @@ where
if visitor.is_reading() {
// Try to visit inner value first, this is very useful if user decides to make their
// variable inheritable, but still keep backward compatibility.
visited = self.value.visit(name, visitor).is_ok();
self.flags.get_mut().insert(VariableFlags::MODIFIED);
let has_flags = if let Ok(mut region) = visitor.enter_region(name) {
self.flags.get_mut().0.visit("Flags", &mut region).is_ok()
} else {
false
};
if !has_flags {
visited = self.value.visit(name, visitor).is_ok();
self.flags.get_mut().insert(VariableFlags::MODIFIED);
}
}
if !visited {
+23 -2
View File
@@ -618,12 +618,33 @@ impl PhysicsWorld {
}
}
pub(crate) fn update(&mut self, dt: f32) {
/// Update the physics pipeline with a timestep of the given length.
///
/// * `dt`: The amount of time that has passed since the previous update.
/// This may be overriden by [`PhysicsWorld::integration_parameters`] if
/// `integration_parameters.dt` is `Some`, in which case that value is used
/// instead of this argument.
/// * `dt_enabled`: If this is true then `dt` is used as usual, but if this is false
/// then both the `dt` argument and `integration_parameters.dt` are ignored and
/// a `dt` of zero is used instead, freezing all physics. This corresponds to the
/// [`GraphUpdateSwitches::physics_dt`](crate::scene::graph::GraphUpdateSwitches::physics_dt).
pub(crate) fn update(&mut self, dt: f32, dt_enabled: bool) {
let time = instant::Instant::now();
let parameter_dt = self.integration_parameters.dt;
let parameter_dt = if parameter_dt == Some(0.0) {
None
} else {
parameter_dt
};
let dt = if dt_enabled {
parameter_dt.unwrap_or(dt)
} else {
0.0
};
if *self.enabled {
let integration_parameters = rapier2d::dynamics::IntegrationParameters {
dt: self.integration_parameters.dt.unwrap_or(dt),
dt,
min_ccd_dt: self.integration_parameters.min_ccd_dt,
contact_damping_ratio: self.integration_parameters.contact_damping_ratio,
contact_natural_frequency: self.integration_parameters.contact_natural_frequency,
+5 -2
View File
@@ -270,6 +270,8 @@ fn clear_links(mut node: Node) -> Node {
/// A set of switches that allows you to disable a particular step of graph update pipeline.
#[derive(Clone, PartialEq, Eq)]
pub struct GraphUpdateSwitches {
/// Enables the physics update to have a non-zero `dt`.
pub physics_dt: bool,
/// Enables or disables update of the 2D physics.
pub physics2d: bool,
/// Enables or disables update of the 3D physics.
@@ -287,6 +289,7 @@ pub struct GraphUpdateSwitches {
impl Default for GraphUpdateSwitches {
fn default() -> Self {
Self {
physics_dt: true,
physics2d: true,
physics: true,
node_overrides: Default::default(),
@@ -1200,13 +1203,13 @@ impl Graph {
if switches.physics {
self.physics.performance_statistics.reset();
self.physics.update(dt);
self.physics.update(dt, switches.physics_dt);
self.performance_statistics.physics = self.physics.performance_statistics.clone();
}
if switches.physics2d {
self.physics2d.performance_statistics.reset();
self.physics2d.update(dt);
self.physics2d.update(dt, switches.physics_dt);
self.performance_statistics.physics2d = self.physics2d.performance_statistics.clone();
}
+23 -2
View File
@@ -1101,12 +1101,33 @@ impl PhysicsWorld {
}
}
pub(super) fn update(&mut self, dt: f32) {
/// Update the physics pipeline with a timestep of the given length.
///
/// * `dt`: The amount of time that has passed since the previous update.
/// This may be overriden by [`PhysicsWorld::integration_parameters`] if
/// `integration_parameters.dt` is `Some`, in which case that value is used
/// instead of this argument.
/// * `dt_enabled`: If this is true then `dt` is used as usual, but if this is false
/// then both the `dt` argument and `integration_parameters.dt` are ignored and
/// a `dt` of zero is used instead, freezing all physics. This corresponds to the
/// [`GraphUpdateSwitches::physics_dt`](crate::scene::graph::GraphUpdateSwitches::physics_dt).
pub(super) fn update(&mut self, dt: f32, dt_enabled: bool) {
let time = instant::Instant::now();
let parameter_dt = self.integration_parameters.dt;
let parameter_dt = if parameter_dt == Some(0.0) {
None
} else {
parameter_dt
};
let dt = if dt_enabled {
parameter_dt.unwrap_or(dt)
} else {
0.0
};
if *self.enabled {
let integration_parameters = rapier3d::dynamics::IntegrationParameters {
dt: self.integration_parameters.dt.unwrap_or(dt),
dt,
min_ccd_dt: self.integration_parameters.min_ccd_dt,
contact_damping_ratio: self.integration_parameters.contact_damping_ratio,
contact_natural_frequency: self.integration_parameters.contact_natural_frequency,