Skip to content

Commit 4013d7d

Browse files
authored
Merge pull request dimforge#248 from dimforge/fix-scale
Mitigate rounding errors due to scaling extraction from the global transform
2 parents d5d3cac + 470ac88 commit 4013d7d

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/geometry/collider_impl.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,15 +511,28 @@ impl Collider {
511511
/// with a non-uniform scale results in an ellipse which isn’t supported),
512512
/// the shape is approximated by a convex polygon/convex polyhedron using
513513
/// `num_subdivisions` subdivisions.
514-
pub fn set_scale(&mut self, scale: Vect, num_subdivisions: u32) {
514+
pub fn set_scale(&mut self, mut scale: Vect, num_subdivisions: u32) {
515+
/// We restrict the scaling increment to 1.0e-4, to avoid numerical jitter
516+
/// due to the extraction of scaling factor from the GlobalTransform matrix.
517+
fn snap_value(new: &mut f32) {
518+
const PRECISION: f32 = 1.0e4;
519+
*new = (*new * PRECISION).round() / PRECISION;
520+
}
521+
522+
snap_value(&mut scale.x);
523+
snap_value(&mut scale.y);
524+
#[cfg(feature = "dim3")]
525+
snap_value(&mut scale.z);
526+
515527
if scale == self.scale {
516528
// Nothing to do.
529+
return;
517530
}
518531

519532
if scale == Vect::ONE {
520533
// Trivial case.
521534
self.raw = self.unscaled.clone();
522-
self.scale = scale;
535+
self.scale = Vect::ONE;
523536
return;
524537
}
525538

0 commit comments

Comments
 (0)