From b36443b6ed8a6928097e6c1d33310c5d321db3e5 Mon Sep 17 00:00:00 2001 From: Joona Aalto Date: Sat, 14 Sep 2024 05:24:08 +0300 Subject: [PATCH] Fix `Capsule2d::sample_interior` (#15191) # Objective `Capsule2d::sample_interior` uses the radius of the capsule for the width of its rectangular section. It should be using two times the radius for the full width! I noticed this as I was getting incorrect results for angular inertia approximated from a point cloud of points sampled on the capsule. This hinted that something was wrong with the sampling. ## Solution Multiply the radius by two to get the full width of the rectangular section. With this, the sampling produces the correct result in my tests. --- crates/bevy_math/src/sampling/shape_sampling.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_math/src/sampling/shape_sampling.rs b/crates/bevy_math/src/sampling/shape_sampling.rs index 10f9e17f60dd5..10d99f8a8b16d 100644 --- a/crates/bevy_math/src/sampling/shape_sampling.rs +++ b/crates/bevy_math/src/sampling/shape_sampling.rs @@ -450,7 +450,7 @@ impl ShapeSample for Capsule2d { if capsule_area > 0.0 { // Check if the random point should be inside the rectangle if rng.gen_bool((rectangle_area / capsule_area) as f64) { - let rectangle = Rectangle::new(self.radius, self.half_length * 2.0); + let rectangle = Rectangle::new(self.radius * 2.0, self.half_length * 2.0); rectangle.sample_interior(rng) } else { let circle = Circle::new(self.radius);