box2d_rs/b2_shape.rs
1use crate::b2_collision::*;
2use crate::b2_math::{B2Transform, B2vec2};
3use crate::shapes::b2rs_to_derived_shape::*;
4use std::rc::Rc;
5use std::cell::RefCell;
6
7#[cfg(feature="serde_support")]
8use serde::{Serialize, Deserialize};
9
10/// This holds the mass data computed for a shape.
11#[derive(Default, Clone, Copy, Debug)]
12pub struct B2massData {
13 /// The mass of the shape, usually in kilograms.
14 pub mass: f32,
15
16 /// The position of the shape's centroid relative to the shape's origin.
17 pub center: B2vec2,
18
19 /// The rotational inertia of the shape about the local origin.
20 pub i: f32,
21}
22
23#[derive(Clone, Copy, Debug, PartialEq)]
24#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
25pub enum B2ShapeType {
26 ECircle = 0,
27 EEdge = 1,
28 EPolygon = 2,
29 EChain = 3,
30 ETypeCount = 4,
31}
32
33impl Default for B2ShapeType {
34 fn default() -> Self {
35 B2ShapeType::ECircle
36 }
37}
38
39/// A shape is used for collision detection. You can create a shape however you like.
40/// Shapes used for simulation in B2world are created automatically when a B2fixture
41/// is created. Shapes may encapsulate a one or more child shapes.
42#[derive(Default, Clone, Copy, Debug)]
43#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
44pub struct B2Shape {
45 pub m_type: B2ShapeType,
46
47 /// Radius of a shape. For polygonal shapes this must be B2_POLYGON_RADIUS. There is no support for
48 /// making rounded polygons.
49 pub m_radius: f32,
50}
51
52impl B2Shape {
53 /// Get the type of this shape. You can use this to down cast to the concrete shape.
54 ///
55 /// @return the shape type.
56 pub fn get_type(self) -> B2ShapeType {
57 return self.m_type;
58 }
59}
60
61pub type ShapeDefPtr = Rc<RefCell<dyn B2shapeDynTrait>>;
62pub(crate) type ShapePtr = Rc<dyn B2shapeDynTrait>;
63
64pub trait B2shapeDynTrait: ToDerivedShape {
65 fn get_base(&self) -> &B2Shape;
66 /// Get the type of this shape. You can use this to down cast to the concrete shape.
67 ///
68 /// @return the shape type.
69 fn get_type(&self) -> B2ShapeType;
70 /// Clone the concrete shape using the provided allocator.
71 fn clone_box(&self) -> Box<dyn B2shapeDynTrait>;
72 fn clone_rc(&self) -> ShapePtr;
73
74 /// Get the number of child primitives.
75 fn get_child_count(&self) -> usize;
76
77 /// Test a point for containment in this shape. This only works for convex shapes.
78 /// * `xf` - the shape world transform.
79 /// * `p` - a point in world coordinates.
80 fn test_point(&self, xf: B2Transform, p: B2vec2) -> bool;
81
82 /// Cast a ray against a child shape.
83 /// * `output` - the ray-cast results.
84 /// * `input` - the ray-cast input parameters.
85 /// * `transform` - the transform to be applied to the shape.
86 /// * `child_index` - the child shape index
87 fn ray_cast(
88 &self,
89 output: &mut B2rayCastOutput,
90 input: &B2rayCastInput,
91 transform: B2Transform,
92 child_index: usize,
93 ) -> bool;
94
95 /// Given a transform, compute the associated axis aligned bounding box for a child shape.
96 /// * `aabb` - returns the axis aligned box.
97 /// * `xf` - the world transform of the shape.
98 /// * `child_index` - the child shape
99 fn compute_aabb(&self, aabb: &mut B2AABB, xf: B2Transform, child_index: usize);
100
101 /// Compute the mass properties of this shape using its dimensions and density.
102 /// The inertia tensor is computed about the local origin.
103 /// * `mass_data` - returns the mass data for this shape.
104 /// * `density` - the density in kilograms per meter squared.
105 fn compute_mass(&self, mass_data: &mut B2massData, density: f32);
106}