dae_parser/physics/
scene.rs

1use crate::*;
2
3/// Specifies an environment in which physical objects are instantiated and simulated.
4#[derive(Clone, Default, Debug)]
5pub struct PhysicsScene {
6    /// A text string containing the unique identifier of the element.
7    pub id: Option<String>,
8    /// The text string name of this element.
9    pub name: Option<String>,
10    /// Asset management information about this element.
11    pub asset: Option<Box<Asset>>,
12    /// Instantiates a [`ForceField`] element to influence this physics scene.
13    pub instance_force_field: Vec<Instance<ForceField>>,
14    /// Instantiates a [`PhysicsModel`] element and
15    /// allows for overriding some or all of its children.
16    pub instance_physics_model: Vec<Instance<PhysicsModel>>,
17    /// Specifies physics-scene information for the common profile
18    /// that all COLLADA implementations must support.
19    pub common: PhysicsSceneCommon,
20    /// Declares the information used to process some portion of the content. (optional)
21    pub technique: Vec<Technique>,
22    /// Provides arbitrary additional information about this element.
23    pub extra: Vec<Extra>,
24}
25
26impl XNode for PhysicsScene {
27    const NAME: &'static str = "physics_scene";
28    fn parse(element: &Element) -> Result<Self> {
29        debug_assert_eq!(element.name(), Self::NAME);
30        let mut it = element.children().peekable();
31        Ok(PhysicsScene {
32            id: element.attr("id").map(Into::into),
33            name: element.attr("name").map(Into::into),
34            asset: Asset::parse_opt_box(&mut it)?,
35            instance_force_field: Instance::parse_list(&mut it)?,
36            instance_physics_model: Instance::parse_list(&mut it)?,
37            common: parse_one(Technique::COMMON, &mut it, PhysicsSceneCommon::parse)?,
38            technique: Technique::parse_list(&mut it)?,
39            extra: Extra::parse_many(it)?,
40        })
41    }
42}
43
44impl XNodeWrite for PhysicsScene {
45    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
46        let mut e = Self::elem();
47        e.opt_attr("id", &self.id);
48        e.opt_attr("name", &self.name);
49        let e = e.start(w)?;
50        self.asset.write_to(w)?;
51        self.instance_force_field.write_to(w)?;
52        self.instance_physics_model.write_to(w)?;
53        let common = ElemBuilder::new(Technique::COMMON).start(w)?;
54        self.common.write_to(w)?;
55        common.end(w)?;
56        self.technique.write_to(w)?;
57        self.extra.write_to(w)?;
58        e.end(w)
59    }
60}
61
62/// Specifies physics-scene information for the common profile
63/// that all COLLADA implementations must support.
64#[derive(Clone, Default, Debug)]
65pub struct PhysicsSceneCommon {
66    /// A vector representation of the scene’s gravity force field.
67    /// It is given as a denormalized direction vector of three
68    /// floating-point values that indicate both the magnitude
69    /// and direction of acceleration caused by the field.
70    pub gravity: Option<Box<[f32; 3]>>,
71    /// The integration time step, measured in seconds, of the physics scene.
72    /// This value is engine-specific. If omitted, the physics engine’s default is used.
73    pub time_step: Option<f32>,
74}
75
76impl PhysicsSceneCommon {
77    /// Parse a [`PhysicsSceneCommon`] from a
78    /// `<physics_scene>/<technique_common>` XML element.
79    pub fn parse(e: &Element) -> Result<Self> {
80        let mut it = e.children().peekable();
81        let res = Self {
82            gravity: parse_opt("gravity", &mut it, parse_array_n)?,
83            time_step: parse_opt("time_step", &mut it, parse_elem)?,
84        };
85        finish(res, it)
86    }
87}
88
89impl XNodeWrite for PhysicsSceneCommon {
90    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
91        opt(&self.gravity, |e| {
92            ElemBuilder::print_arr("gravity", &**e, w)
93        })?;
94        ElemBuilder::opt_print("time_step", &self.time_step, w)
95    }
96}
97
98/// Provides a general container for force fields.
99///
100/// At the moment, it contains only techniques and extra elements.
101#[derive(Clone, Default, Debug)]
102pub struct ForceField {
103    /// A text string containing the unique identifier of the element.
104    pub id: Option<String>,
105    /// The text string name of this element.
106    pub name: Option<String>,
107    /// Asset management information about this element.
108    pub asset: Option<Box<Asset>>,
109    /// Declares the information used to process some portion of the content.
110    /// This field is always nonempty, because the spec provides no common data
111    /// for `force_field` elements.
112    pub technique: Vec<Technique>,
113    /// Provides arbitrary additional information about this element.
114    pub extra: Vec<Extra>,
115}
116
117impl XNode for ForceField {
118    const NAME: &'static str = "force_field";
119    fn parse(element: &Element) -> Result<Self> {
120        debug_assert_eq!(element.name(), Self::NAME);
121        let mut it = element.children().peekable();
122        Ok(ForceField {
123            id: element.attr("id").map(Into::into),
124            name: element.attr("name").map(Into::into),
125            asset: Asset::parse_opt_box(&mut it)?,
126            technique: Technique::parse_list_n::<1>(&mut it)?,
127            extra: Extra::parse_many(it)?,
128        })
129    }
130}
131
132impl XNodeWrite for ForceField {
133    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
134        let mut e = Self::elem();
135        e.opt_attr("id", &self.id);
136        e.opt_attr("name", &self.name);
137        let e = e.start(w)?;
138        self.asset.write_to(w)?;
139        self.technique.write_to(w)?;
140        self.extra.write_to(w)?;
141        e.end(w)
142    }
143}
144
145/// Defines an attachment frame, to a rigid body or a node, within a rigid constraint.
146#[derive(Clone, Debug)]
147pub struct Attachment {
148    /// A URI reference to a [`RigidBody`] or [`Node`]. This must refer to a
149    /// [`RigidBody`] in `attachment` or in `ref_attachment`; they cannot both be [`Node`]s.
150    pub rigid_body: Url,
151    /// Changes the position of the attachment point.
152    pub transform: Vec<RigidTransform>,
153    /// Provides arbitrary additional information about this element.
154    pub extra: Vec<Extra>,
155}
156
157impl Attachment {
158    /// Construct an attachment to a [`RigidBody`] or [`Node`].
159    pub fn new(rigid_body: Url) -> Self {
160        Self {
161            rigid_body,
162            transform: vec![],
163            extra: vec![],
164        }
165    }
166
167    /// The name of the `<ref_attachment>` element.
168    pub const REF: &'static str = "ref_attachment";
169
170    pub(crate) fn write_inner<W: Write>(
171        &self,
172        mut e: ElemBuilder,
173        w: &mut XWriter<W>,
174    ) -> Result<()> {
175        e.print_attr("rigid_body", &self.rigid_body);
176        let e = e.start(w)?;
177        self.transform.write_to(w)?;
178        self.extra.write_to(w)?;
179        e.end(w)
180    }
181}
182
183impl XNode for Attachment {
184    const NAME: &'static str = "attachment";
185    fn parse(element: &Element) -> Result<Self> {
186        debug_assert!(element.name() == Self::NAME || element.name() == Self::REF);
187        let mut it = element.children().peekable();
188        Ok(Attachment {
189            rigid_body: parse_attr(element.attr("rigid_body"))?.ok_or("missing rigid_body attr")?,
190            transform: parse_list_many(&mut it, RigidTransform::parse)?,
191            extra: Extra::parse_many(it)?,
192        })
193    }
194}
195
196impl XNodeWrite for Attachment {
197    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
198        self.write_inner(Self::elem(), w)
199    }
200}