concinnity_physics/sim/character/
capsule.rs1use crate::ColliderShape;
10
11#[derive(Debug, Clone, Copy, PartialEq)]
14pub struct CharacterCapsule {
15 half_height: f32,
16 radius: f32,
17}
18
19impl CharacterCapsule {
20 pub fn new(half_height: f32, radius: f32) -> Self {
23 CharacterCapsule {
24 half_height,
25 radius,
26 }
27 }
28
29 pub fn resize(&mut self, half_height: f32, radius: f32) {
31 self.half_height = half_height;
32 self.radius = radius;
33 }
34
35 pub fn half_height(&self) -> f32 {
37 self.half_height
38 }
39
40 pub fn radius(&self) -> f32 {
42 self.radius
43 }
44
45 pub(crate) fn shape(&self) -> ColliderShape {
48 ColliderShape::Capsule {
49 half_height: self.half_height,
50 radius: self.radius,
51 }
52 }
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn the_capsule_carries_the_dimensions_it_was_built_from() {
61 let capsule = CharacterCapsule::new(0.6, 0.3);
62 assert_eq!(capsule.half_height(), 0.6);
63 assert_eq!(capsule.radius(), 0.3);
64 assert_eq!(
65 capsule.shape(),
66 ColliderShape::Capsule {
67 half_height: 0.6,
68 radius: 0.3,
69 }
70 );
71 }
72
73 #[test]
74 fn resizing_replaces_both_dimensions() {
75 let mut capsule = CharacterCapsule::new(0.6, 0.3);
76 capsule.resize(0.6, 0.3);
77 assert_eq!(capsule, CharacterCapsule::new(0.6, 0.3));
78 capsule.resize(0.9, 0.4);
79 assert_eq!(capsule.half_height(), 0.9);
80 assert_eq!(capsule.radius(), 0.4);
81 }
82}