1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use crate::*;
#[derive(Clone, Debug)]
pub struct PhysicsModel {
pub id: Option<String>,
pub name: Option<String>,
pub asset: Option<Box<Asset>>,
pub rigid_body: Vec<RigidBody>,
pub rigid_constraint: Vec<RigidConstraint>,
pub instances: Vec<Instance<PhysicsModel>>,
pub extra: Vec<Extra>,
}
impl XNode for PhysicsModel {
const NAME: &'static str = "physics_model";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
Ok(PhysicsModel {
id: element.attr("id").map(Into::into),
name: element.attr("name").map(Into::into),
asset: Asset::parse_opt_box(&mut it)?,
rigid_body: RigidBody::parse_list(&mut it)?,
rigid_constraint: RigidConstraint::parse_list(&mut it)?,
instances: Instance::parse_list(&mut it)?,
extra: Extra::parse_many(it)?,
})
}
}
#[derive(Clone, Debug)]
pub struct InstancePhysicsModelData {
pub parent: Option<UrlRef<Node>>,
pub instance_force_field: Vec<Instance<ForceField>>,
pub instance_rigid_body: Vec<InstanceRigidBody>,
pub instance_rigid_constraint: Vec<InstanceRigidConstraint>,
}
impl Instantiate for PhysicsModel {
const INSTANCE: &'static str = "instance_physics_model";
type Data = InstancePhysicsModelData;
fn parse_data(e: &Element, it: &mut ElementIter<'_>) -> Result<Self::Data> {
Ok(InstancePhysicsModelData {
parent: parse_attr(e.attr("parent"))?,
instance_force_field: Instance::parse_list(it)?,
instance_rigid_body: InstanceRigidBody::parse_list(it)?,
instance_rigid_constraint: InstanceRigidConstraint::parse_list(it)?,
})
}
}
impl CollectLocalMaps for InstancePhysicsModelData {
fn collect_local_maps<'a>(&'a self, maps: &mut LocalMaps<'a>) {
self.instance_rigid_body.collect_local_maps(maps);
}
}
#[derive(Clone, Debug)]
pub struct RigidBody {
pub sid: Option<String>,
pub name: Option<String>,
pub common: RigidBodyCommon,
pub technique: Vec<Technique>,
pub extra: Vec<Extra>,
}
impl Deref for RigidBody {
type Target = RigidBodyCommon;
fn deref(&self) -> &Self::Target {
&self.common
}
}
impl XNode for RigidBody {
const NAME: &'static str = "rigid_body";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
Ok(RigidBody {
sid: element.attr("sid").map(Into::into),
name: element.attr("name").map(Into::into),
common: parse_one(Technique::COMMON, &mut it, |e| {
RigidBodyCommon::parse(e.children().peekable())
})?,
technique: Technique::parse_list(&mut it)?,
extra: Extra::parse_many(it)?,
})
}
}
impl CollectLocalMaps for RigidBody {
fn collect_local_maps<'a>(&'a self, maps: &mut LocalMaps<'a>) {
self.common.collect_local_maps(maps);
}
}
#[derive(Clone, Default, Debug)]
pub struct RigidBodyCommon {
pub dynamic: Option<bool>,
pub mass: Option<f32>,
pub mass_frame: Vec<RigidTransform>,
pub inertia: Option<Box<[f32; 3]>>,
pub physics_material: Option<Box<DefInstance<PhysicsMaterial>>>,
pub shape: Vec<Shape>,
}
impl RigidBodyCommon {
fn parse(mut it: ElementIter<'_>) -> Result<Self> {
let res = Self {
dynamic: parse_opt("dynamic", &mut it, parse_elem)?,
mass: parse_opt("mass", &mut it, parse_elem)?,
mass_frame: parse_opt("mass_frame", &mut it, |e| {
let mut it = e.children().peekable();
finish(parse_list_many(&mut it, RigidTransform::parse)?, it)
})?
.unwrap_or_default(),
inertia: parse_opt("inertia", &mut it, parse_array_n)?,
physics_material: parse_opt_many(&mut it, DefInstance::parse)?.map(Box::new),
shape: Shape::parse_list(&mut it)?,
};
finish(res, it)
}
}
impl CollectLocalMaps for RigidBodyCommon {
fn collect_local_maps<'a>(&'a self, maps: &mut LocalMaps<'a>) {
self.physics_material.collect_local_maps(maps);
self.shape.collect_local_maps(maps);
}
}
#[derive(Clone, Debug)]
pub struct InstanceRigidBody {
pub body: NameRef<RigidBody>,
pub target: UrlRef<Node>,
pub common: InstanceRigidBodyCommon,
pub technique: Vec<Technique>,
pub extra: Vec<Extra>,
}
impl Deref for InstanceRigidBody {
type Target = InstanceRigidBodyCommon;
fn deref(&self) -> &Self::Target {
&self.common
}
}
impl XNode for InstanceRigidBody {
const NAME: &'static str = "rigid_body";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
Ok(InstanceRigidBody {
body: Ref::new(element.attr("body").ok_or("missing body attribute")?.into()),
target: parse_attr(element.attr("target"))?.ok_or("missing url attribute")?,
common: parse_one(Technique::COMMON, &mut it, InstanceRigidBodyCommon::parse)?,
technique: Technique::parse_list(&mut it)?,
extra: Extra::parse_many(it)?,
})
}
}
impl CollectLocalMaps for InstanceRigidBody {
fn collect_local_maps<'a>(&'a self, maps: &mut LocalMaps<'a>) {
self.common.collect_local_maps(maps);
}
}
#[derive(Clone, Default, Debug)]
pub struct InstanceRigidBodyCommon {
pub angular_velocity: [f32; 3],
pub velocity: [f32; 3],
pub common: RigidBodyCommon,
}
impl Deref for InstanceRigidBodyCommon {
type Target = RigidBodyCommon;
fn deref(&self) -> &Self::Target {
&self.common
}
}
impl InstanceRigidBodyCommon {
pub fn parse(e: &Element) -> Result<Self> {
let mut it = e.children().peekable();
Ok(Self {
angular_velocity: parse_opt("angular_velocity", &mut it, parse_array_n)?
.map_or([0.; 3], |a| *a),
velocity: parse_opt("velocity", &mut it, parse_array_n)?.map_or([0.; 3], |a| *a),
common: RigidBodyCommon::parse(it)?,
})
}
}
impl CollectLocalMaps for InstanceRigidBodyCommon {
fn collect_local_maps<'a>(&'a self, maps: &mut LocalMaps<'a>) {
self.common.collect_local_maps(maps);
}
}
#[derive(Clone, Debug)]
pub struct RigidConstraint {
pub sid: Option<String>,
pub name: Option<String>,
pub ref_attachment: Attachment,
pub attachment: Attachment,
pub common: RigidConstraintCommon,
pub technique: Vec<Technique>,
pub extra: Vec<Extra>,
}
impl XNode for RigidConstraint {
const NAME: &'static str = "rigid_constraint";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
Ok(RigidConstraint {
sid: element.attr("sid").map(Into::into),
name: element.attr("name").map(Into::into),
ref_attachment: parse_one(Attachment::REF, &mut it, Attachment::parse)?,
attachment: Attachment::parse_one(&mut it)?,
common: parse_one(Technique::COMMON, &mut it, RigidConstraintCommon::parse)?,
technique: Technique::parse_list(&mut it)?,
extra: Extra::parse_many(it)?,
})
}
}
#[derive(Clone, Default, Debug)]
pub struct RigidConstraintCommon {
pub enabled: bool,
pub interpenetrate: bool,
pub swing_cone_and_twist: Limits,
pub linear: Limits,
pub spring_angular: Spring,
pub spring_linear: Spring,
}
impl RigidConstraintCommon {
pub fn parse(e: &Element) -> Result<Self> {
let mut it = e.children().peekable();
let enabled = parse_opt("enabled", &mut it, parse_elem)?.unwrap_or(true);
let interpenetrate = parse_opt("interpenetrate", &mut it, parse_elem)?.unwrap_or(false);
let (swing_cone_and_twist, linear) = parse_opt("limits", &mut it, |e| {
let mut it = e.children().peekable();
let scat =
parse_opt("swing_cone_and_twist", &mut it, Limits::parse)?.unwrap_or_default();
let lin = parse_opt("linear", &mut it, Limits::parse)?.unwrap_or_default();
finish((scat, lin), it)
})?
.unwrap_or_default();
let (spring_angular, spring_linear) = parse_opt("spring", &mut it, |e| {
let mut it = e.children().peekable();
let ang = parse_opt("angular", &mut it, Spring::parse)?.unwrap_or_default();
let lin = parse_opt("linear", &mut it, Spring::parse)?.unwrap_or_default();
finish((ang, lin), it)
})?
.unwrap_or_default();
let res = Self {
enabled,
interpenetrate,
swing_cone_and_twist,
linear,
spring_angular,
spring_linear,
};
finish(res, it)
}
}
#[derive(Clone, Debug)]
pub struct InstanceRigidConstraint {
pub constraint: NameRef<RigidConstraint>,
pub extra: Vec<Extra>,
}
impl XNode for InstanceRigidConstraint {
const NAME: &'static str = "instance_rigid_constraint";
fn parse(e: &Element) -> Result<Self> {
debug_assert_eq!(e.name(), Self::NAME);
let constraint = e.attr("constraint").ok_or("missing constraint attr")?;
Ok(InstanceRigidConstraint {
constraint: Ref::new(constraint.into()),
extra: Extra::parse_many(e.children())?,
})
}
}
#[derive(Clone, Default, Debug)]
pub struct Limits {
pub min: Option<Box<[f32; 3]>>,
pub max: Option<Box<[f32; 3]>>,
}
impl Limits {
pub fn parse(e: &Element) -> Result<Self> {
let mut it = e.children().peekable();
let res = Self {
min: parse_opt("min", &mut it, parse_array_n)?,
max: parse_opt("max", &mut it, parse_array_n)?,
};
finish(res, it)
}
}
#[derive(Clone, Copy, Debug)]
pub struct Spring {
pub stiffness: f32,
pub damping: f32,
pub target_value: f32,
}
impl Default for Spring {
fn default() -> Self {
Self {
stiffness: 1.,
damping: 0.,
target_value: 0.,
}
}
}
impl Spring {
pub fn parse(e: &Element) -> Result<Self> {
let mut it = e.children().peekable();
let res = Self {
stiffness: parse_opt("stiffness", &mut it, parse_elem)?.unwrap_or(1.),
damping: parse_opt("damping", &mut it, parse_elem)?.unwrap_or(0.),
target_value: parse_opt("target_value", &mut it, parse_elem)?.unwrap_or(0.),
};
finish(res, it)
}
}