dae_parser/physics/
material.rs

1use crate::*;
2
3/// Defines the physical properties of an object.
4///
5/// It contains a technique/profile with parameters.
6/// The `COMMON` profile defines the built-in names, such as `static_friction`.
7#[derive(Clone, Default, Debug)]
8pub struct PhysicsMaterial {
9    /// A text string containing the unique identifier of the element.
10    pub id: Option<String>,
11    /// The text string name of this element.
12    pub name: Option<String>,
13    /// Asset management information about this element.
14    pub asset: Option<Box<Asset>>,
15    /// Specifies physics-material information for the common
16    /// profile that all COLLADA implementations must support.
17    pub common: PhysicsMaterialCommon,
18    /// Declares the information used to process some portion of the content. (optional)
19    pub technique: Vec<Technique>,
20    /// Provides arbitrary additional information about this element.
21    pub extra: Vec<Extra>,
22}
23
24impl PhysicsMaterial {
25    /// Build a `PhysicsMaterial` with default options.
26    pub fn new(id: impl Into<String>) -> Self {
27        Self {
28            id: Some(id.into()),
29            name: None,
30            asset: None,
31            common: Default::default(),
32            technique: vec![],
33            extra: vec![],
34        }
35    }
36}
37
38impl XNode for PhysicsMaterial {
39    const NAME: &'static str = "physics_material";
40    fn parse(element: &Element) -> Result<Self> {
41        debug_assert_eq!(element.name(), Self::NAME);
42        let mut it = element.children().peekable();
43        Ok(PhysicsMaterial {
44            id: element.attr("id").map(Into::into),
45            name: element.attr("name").map(Into::into),
46            asset: Asset::parse_opt_box(&mut it)?,
47            common: parse_one(Technique::COMMON, &mut it, PhysicsMaterialCommon::parse)?,
48            technique: Technique::parse_list(&mut it)?,
49            extra: Extra::parse_many(it)?,
50        })
51    }
52}
53
54impl XNodeWrite for PhysicsMaterial {
55    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
56        let mut e = Self::elem();
57        e.opt_attr("id", &self.id);
58        e.opt_attr("name", &self.name);
59        let e = e.start(w)?;
60        self.asset.write_to(w)?;
61        let common = ElemBuilder::new(Technique::COMMON).start(w)?;
62        self.common.write_to(w)?;
63        common.end(w)?;
64        self.technique.write_to(w)?;
65        self.extra.write_to(w)?;
66        e.end(w)
67    }
68}
69
70/// Specifies physics-material information for the common
71/// profile that all COLLADA implementations must support.
72#[derive(Clone, Copy, Default, Debug)]
73pub struct PhysicsMaterialCommon {
74    /// Contains a floating-point number that specifies the dynamic friction coefficient.
75    pub dynamic_friction: f32,
76    /// Contains a floating-point number that is the proportion
77    /// of the kinetic energy preserved in the impact (typically ranges from 0.0 to 1.0).
78    /// Also known as "bounciness" or "elasticity."
79    pub restitution: f32,
80    /// Contains a floating-point number that specifies the static friction coefficient.
81    pub static_friction: f32,
82}
83
84impl PhysicsMaterialCommon {
85    fn parse(e: &Element) -> Result<Self> {
86        let mut it = e.children().peekable();
87        let res = PhysicsMaterialCommon {
88            dynamic_friction: parse_opt("dynamic_friction", &mut it, parse_elem)?.unwrap_or(0.),
89            restitution: parse_opt("restitution", &mut it, parse_elem)?.unwrap_or(0.),
90            static_friction: parse_opt("static_friction", &mut it, parse_elem)?.unwrap_or(0.),
91        };
92        finish(res, it)
93    }
94}
95
96impl XNodeWrite for PhysicsMaterialCommon {
97    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
98        ElemBuilder::def_print("dynamic_friction", self.dynamic_friction, 0., w)?;
99        ElemBuilder::def_print("restitution", self.restitution, 0., w)?;
100        ElemBuilder::def_print("static_friction", self.static_friction, 0., w)
101    }
102}