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.
This commit is contained in:
Joona Aalto 2024-09-14 05:24:08 +03:00 committed by GitHub
parent 583e034796
commit b36443b6ed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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);