Skip to content

Commit

Permalink
Fix Capsule2d::sample_interior (#15191)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
Jondolf authored Sep 14, 2024
1 parent 583e034 commit b36443b
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion crates/bevy_math/src/sampling/shape_sampling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b36443b

Please sign in to comment.