Skip to main content

pite_physics/
shape.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2use rapier2d::prelude::*;
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6pub enum ShapeConfig {
7    Box { width: f32, height: f32 },
8    Circle { radius: f32 },
9    Capsule { radius: f32, height: f32 },
10}
11
12impl ShapeConfig {
13    pub fn to_shared_shape(&self) -> SharedShape {
14        match self {
15            Self::Box { width, height } => {
16                let hw = (width.abs() * 0.5).max(0.001);
17                let hh = (height.abs() * 0.5).max(0.001);
18                SharedShape::cuboid(hw, hh)
19            }
20            Self::Circle { radius } => {
21                let r = radius.abs().max(0.001);
22                SharedShape::ball(r)
23            }
24            Self::Capsule { radius, height } => {
25                let r = radius.abs().max(0.001);
26                let hh = (height.abs() * 0.5).max(0.001);
27                SharedShape::capsule_y(hh, r)
28            }
29        }
30    }
31}