Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix delta_time being 0 resulting in incorrect simulation #660

Merged
merged 7 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/dynamics/integration_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,13 @@ impl IntegrationParameters {
/// This parameter is computed automatically from [`Self::joint_natural_frequency`],
/// [`Self::joint_damping_ratio`] and the substep length.
pub fn joint_cfm_coeff(&self) -> Real {
let joint_erp = self.joint_erp();
if joint_erp == 0.0 {
return 0.0;
}
// Compute CFM assuming a critically damped spring multiplied by the damping ratio.
// The logic is similar to `Self::cfm_factor`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should a similar correction be applied to cfm_factor ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, the correct function referred to is probably contact_cfm_factor ?

let inv_erp_minus_one = 1.0 / self.joint_erp() - 1.0;
let inv_erp_minus_one = 1.0 / joint_erp - 1.0;
inv_erp_minus_one * inv_erp_minus_one
/ ((1.0 + inv_erp_minus_one)
* 4.0
Expand Down
2 changes: 1 addition & 1 deletion src/dynamics/rigid_body_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl BodyPair {
}

#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[derive(Clone, Default)]
#[derive(Clone, Default, Debug)]
Vrixyz marked this conversation as resolved.
Show resolved Hide resolved
/// A set of rigid bodies that can be handled by a physics pipeline.
pub struct RigidBodySet {
// NOTE: the pub(crate) are needed by the broad phase
Expand Down
68 changes: 67 additions & 1 deletion src/pipeline/physics_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,14 +662,16 @@ impl PhysicsPipeline {

#[cfg(test)]
mod test {
use na::point;

use crate::dynamics::{
CCDSolver, ImpulseJointSet, IntegrationParameters, IslandManager, RigidBodyBuilder,
RigidBodySet,
};
use crate::geometry::{BroadPhaseMultiSap, ColliderBuilder, ColliderSet, NarrowPhase};
use crate::math::Vector;
use crate::pipeline::PhysicsPipeline;
use crate::prelude::{MultibodyJointSet, RigidBodyType};
use crate::prelude::{MultibodyJointSet, RevoluteJointBuilder, RigidBodyType};

#[test]
fn kinematic_and_fixed_contact_crash() {
Expand Down Expand Up @@ -937,4 +939,68 @@ mod test {
// Expect body to now be in active_dynamic_set
assert!(islands.active_dynamic_set.contains(&h));
}

#[test]
fn joint_step_delta_time_0() {
let mut colliders = ColliderSet::new();
let mut impulse_joints = ImpulseJointSet::new();
let mut multibody_joints = MultibodyJointSet::new();
let mut pipeline = PhysicsPipeline::new();
let mut bf = BroadPhaseMultiSap::new();
let mut nf = NarrowPhase::new();
let mut islands = IslandManager::new();

let mut bodies = RigidBodySet::new();

// Initialize bodies
let rb = RigidBodyBuilder::fixed().additional_mass(1.0).build();
let h = bodies.insert(rb.clone());
let rb_dynamic = RigidBodyBuilder::dynamic().additional_mass(1.0).build();
let h_dynamic = bodies.insert(rb_dynamic.clone());

// Add joint
#[cfg(feature = "dim2")]
let joint = RevoluteJointBuilder::new()
.local_anchor1(point![0.0, 1.0].into())
.local_anchor2(point![0.0, -3.0].into());
Vrixyz marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "dim3")]
let joint = RevoluteJointBuilder::new(Vector::z_axis())
.local_anchor1(point![0.0, 1.0, 0.0].into())
.local_anchor2(point![0.0, -3.0, 0.0].into());
Vrixyz marked this conversation as resolved.
Show resolved Hide resolved
impulse_joints.insert(h, h_dynamic, joint, true);

let mut parameters = IntegrationParameters::default();
parameters.dt = 0f32.into();
Vrixyz marked this conversation as resolved.
Show resolved Hide resolved
// Step once
let gravity = Vector::y() * -9.81;
pipeline.step(
&gravity,
&parameters,
&mut islands,
&mut bf,
&mut nf,
&mut bodies,
&mut colliders,
&mut impulse_joints,
&mut multibody_joints,
&mut CCDSolver::new(),
None,
&(),
&(),
);
let translation = bodies.get(h_dynamic).unwrap().translation();
let rotation = bodies.get(h_dynamic).unwrap().rotation();
Vrixyz marked this conversation as resolved.
Show resolved Hide resolved
assert!(translation.x.is_finite());
assert!(translation.y.is_finite());
#[cfg(feature = "dim2")]
assert!(rotation.is_finite());
#[cfg(feature = "dim3")]
(|| {
Vrixyz marked this conversation as resolved.
Show resolved Hide resolved
assert!(translation.z.is_finite());
let rotation = rotation.scaled_axis();
assert!(rotation.x.is_finite());
assert!(rotation.y.is_finite());
assert!(rotation.z.is_finite());
Vrixyz marked this conversation as resolved.
Show resolved Hide resolved
})();
}
}