bevy_serialization_physics 0.7.0

A crate for adding physics wrappers for bevy_serialization_extras
Documentation
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use std::any::type_name;

use bevy_log::warn;
// use bevy::{
//     ecs::query::QueryData,
//     prelude::{Component, Transform},
//     reflect::GetTypeRegistration,
// };
use bevy_rapier3d::prelude::ImpulseJoint;
use bevy_serialization_core::traits::{ChangeChecked, ComponentWrapper};
use bevy_utils::prelude::default;
//use urdf_rs::{Joint, Pose, Link, Visual};
use rapier3d::{
    dynamics::{GenericJoint, JointAxesMask, JointLimits, JointMotor, MotorModel},
    na::Isometry3,
};

use bevy_ecs::{component::StorageType, prelude::*, query::QueryData};
use bevy_math::Vec3;
use bevy_reflect::prelude::*;
use bevy_transform::prelude::*;

#[derive(Component, Default, Reflect)]
pub struct JointBounded;

#[derive(Component, Clone, Copy, Default, Reflect)]
pub struct GeometryShiftMarked;

/// flags entity geometry as already shifted to account for urdf origin
#[derive(Component, Clone, Copy, Default, Reflect)]
pub struct GeometryShifted;

// /// the "super-structure" that this entity is a part of. This is collecting related "parts" into their monolithic/object-oriented equivilent.
// #[derive(Reflect, Component, Clone)]
// pub struct StructureFlag {
//     pub name: String,
// }

#[derive(Default, PartialEq, Debug, Reflect, Clone)]
pub struct Dynamics {
    pub damping: f64,
    pub friction: f64,
}

#[derive(Debug, PartialEq, Reflect, Clone)]
pub struct JointLimitWrapper {
    pub lower: f64,
    pub upper: f64,
    pub effort: f64,
    pub velocity: f64,
}

// default movement should be unrestrained.
impl Default for JointLimitWrapper {
    fn default() -> Self {
        Self {
            lower: f64::MIN,
            upper: f64::MAX,
            effort: f64::MAX,
            velocity: f64::MAX,
        }
    }
}

/// Recieves joint movements from joint sender flag
#[derive(Component, Reflect)]
#[reflect(Component)]
pub struct JointRecieverFlag {
    pub id: String,
}

#[derive(QueryData)]
pub struct Linkage {
    entity: Entity,
    // It is required that all reference lifetimes are explicitly annotated, just like in any
    // struct. Each lifetime should be 'static.
    //link: LinkQuery,
    joint: &'static JointFlag,
}

impl ChangeChecked for Linkage {
    type ChangeCheckedComp = JointFlag;
}

// /// enum for convinience functions from converting to different cordinate systems
// pub enum CordBasis {
//     Bevy(Transform), //x right, y up, z backwards
//     Urdf(Transform), // right hand rule
// }

#[derive(Component, Reflect)]
#[reflect(Component)]
pub struct LinkFlag {
    pub geom_offset: Vec3,
}

// /// Request a joint by name
// #[derive(Component)]
// pub struct JointRequest(pub String);

impl From<&JointFlag> for GenericJoint {
    fn from(value: &JointFlag) -> Self {
        //GenericJoint::(locked_axes)
        let joint_limit = JointLimits {
            max: value.joint.limit.upper as f32,
            min: value.joint.limit.lower as f32,
            impulse: value.joint.limit.velocity as f32,
        };
        //FIXME: this is probably wrong...
        let joint_motors = &value.joint.motors;
        Self {
            local_frame1: Isometry3 {
                translation: value.joint.local_frame1.translation.into(),
                rotation: value.joint.local_frame1.rotation.into(),
            },
            local_frame2: Isometry3 {
                translation: value.joint.local_frame2.translation.into(),
                rotation: value.joint.local_frame2.rotation.into(),
            },
            locked_axes: JointAxesMask::from_bits_truncate(value.joint.locked_axes.bits()),
            limit_axes: JointAxesMask::from_bits_truncate(value.joint.limit_axes.bits()),
            motor_axes: JointAxesMask::from_bits_truncate(value.joint.motor_axes.bits()),
            coupled_axes: JointAxesMask::from_bits_truncate(value.joint.coupled_axes.bits()),
            //FIXME: this is probably wrong...
            limits: [
                joint_limit,
                joint_limit,
                joint_limit,
                joint_limit,
                joint_limit,
                joint_limit,
            ],
            //FIXME: this is probably wrong...
            motors: [
                (&joint_motors[0]).into(),
                (&joint_motors[1]).into(),
                (&joint_motors[2]).into(),
                (&joint_motors[3]).into(),
                (&joint_motors[4]).into(),
                (&joint_motors[5]).into(),
            ],
            contacts_enabled: value.joint.contacts_enabled,
            //FIXME: fix jointflag to have a proper enum for this later
            enabled: rapier3d::dynamics::JointEnabled::Enabled,
            //FIXME: figure out what this is for?
            user_data: 0,
        }
    }
}

// impl From<&LinkageItem<'_>> for ImpulseJoint {
//     fn from(value: &LinkageItem) -> Self {
//         let joint = GenericJoint::from(value.joint);
//         let bevy_rapier_joint = bevy_rapier3d::dynamics::GenericJoint { raw: joint };
//         Self {
//             parent: value.joint.parent,
//             data: bevy_rapier3d::prelude::TypedJoint::GenericJoint(bevy_rapier_joint),
//         }
//     }
// }

impl From<&JointFlag> for ImpulseJoint {
    fn from(value: &JointFlag) -> Self {
        let joint = GenericJoint::from(value);
        let bevy_rapier_joint = bevy_rapier3d::dynamics::GenericJoint { raw: joint };
        Self {
            parent: value.parent,
            data: bevy_rapier3d::prelude::TypedJoint::GenericJoint(bevy_rapier_joint),
        }
    }
}

impl From<&ImpulseJoint> for JointFlag {
    fn from(value: &ImpulseJoint) -> Self {
        //return Self::from(value.data.raw);

        //let joint = value.data.raw;
        let joint = match value.data {
            bevy_rapier3d::prelude::TypedJoint::FixedJoint(joint) => joint.data.raw,
            bevy_rapier3d::prelude::TypedJoint::GenericJoint(joint) => joint.raw,
            bevy_rapier3d::prelude::TypedJoint::PrismaticJoint(joint) => joint.data.raw,
            bevy_rapier3d::prelude::TypedJoint::RevoluteJoint(joint) => joint.data.raw,
            bevy_rapier3d::prelude::TypedJoint::RopeJoint(joint) => joint.data.raw,
            bevy_rapier3d::prelude::TypedJoint::SphericalJoint(joint) => joint.data.raw,
            bevy_rapier3d::prelude::TypedJoint::SpringJoint(joint) => joint.data.raw,
        };

        // let joint_limit = JointLimitWrapper {
        //     lower: joint.limits[0].min.into(),
        //     upper: joint.limits[0].max.into(),
        //     effort: Default::default(),
        //     velocity: joint.limits[0].impulse.into(),
        // };
        let joint_limit_rapier =
            joint
                .limits(rapier3d::prelude::JointAxis::AngX)
                .unwrap_or(&JointLimits {
                    min: 99999.0,
                    max: 99999.0,
                    impulse: 999999.0,
                });
        let joint_limit = JointLimitWrapper {
            lower: joint_limit_rapier.min as f64,
            upper: joint_limit_rapier.max as f64,
            effort: Default::default(),
            velocity: joint_limit_rapier.impulse as f64,
        };
        Self {
            parent: value.parent,
            joint: JointInfo {
                limit: joint_limit,
                //FIXME: implement this properly
                dynamics: Default::default(),
                local_frame1: Transform {
                    translation: joint.local_frame1.translation.into(),
                    rotation: joint.local_frame1.rotation.into(),
                    //FIXME: implement this properly
                    scale: default(),
                },
                local_frame2: Transform {
                    translation: joint.local_frame2.translation.into(),
                    rotation: joint.local_frame2.rotation.into(),
                    //FIXME: implement this properly
                    scale: default(),
                },
                locked_axes: JointAxesMaskWrapper::from_bits_truncate(joint.locked_axes.bits()),
                limit_axes: JointAxesMaskWrapper::from_bits_truncate(joint.limit_axes.bits()),

                motor_axes: JointAxesMaskWrapper::from_bits_truncate(joint.motor_axes.bits()),
                motors: [
                    (&joint.motors[0]).into(),
                    (&joint.motors[1]).into(),
                    (&joint.motors[2]).into(),
                    (&joint.motors[3]).into(),
                    (&joint.motors[4]).into(),
                    (&joint.motors[5]).into(),
                ],

                coupled_axes: JointAxesMaskWrapper::from_bits_truncate(joint.coupled_axes.bits()),
                contacts_enabled: joint.contacts_enabled,
                enabled: joint.is_enabled(),
            },
        }
    }
}

#[derive(Reflect, Debug, PartialEq, Eq, Clone, Default)]
pub struct JointAxesMaskWrapper(u8);

bitflags::bitflags! {
    impl JointAxesMaskWrapper: u8 {
        /// The translational degree of freedom along the local X axis of a joint.
        const X = 1 << 0;
        /// The translational degree of freedom along the local Y axis of a joint.
        const Y = 1 << 1;
        /// The translational degree of freedom along the local Z axis of a joint.
        const Z = 1 << 2;
        /// The angular degree of freedom along the local X axis of a joint.
        const ANG_X = 1 << 3;
        /// The angular degree of freedom along the local Y axis of a joint.
        const ANG_Y = 1 << 4;
        /// The angular degree of freedom along the local Z axis of a joint.
        const ANG_Z = 1 << 5;
        /// The set of degrees of freedom locked by a revolute joint.
        const LOCKED_REVOLUTE_AXES = Self::X.bits() | Self::Y.bits() | Self::Z.bits() | Self::ANG_Y.bits() | Self::ANG_Z.bits();
        /// The set of degrees of freedom locked by a prismatic joint.
        const LOCKED_PRISMATIC_AXES = Self::Y.bits()| Self::Z.bits()| Self::ANG_X.bits()| Self::ANG_Y.bits()| Self::ANG_Z.bits();
        /// The set of degrees of freedom locked by a fixed joint.
        const LOCKED_FIXED_AXES = Self::X.bits()| Self::Y.bits()| Self::Z.bits()| Self::ANG_X.bits()| Self::ANG_Y.bits()| Self::ANG_Z.bits();
        /// The set of degrees of freedom locked by a spherical joint.
        const LOCKED_SPHERICAL_AXES = Self::X.bits()| Self::Y.bits()| Self::Z.bits();
        /// The set of degrees of freedom left free by a revolute joint.
        const FREE_REVOLUTE_AXES = Self::ANG_X.bits();
        /// The set of degrees of freedom left free by a prismatic joint.
        const FREE_PRISMATIC_AXES = Self::X.bits();
        /// The set of degrees of freedom left free by a fixed joint.
        const FREE_FIXED_AXES = 0;
        /// The set of degrees of freedom left free by a spherical joint.
        const FREE_SPHERICAL_AXES = Self::ANG_X.bits()| Self::ANG_Y.bits()| Self::ANG_Z.bits();
        /// The set of all translational degrees of freedom.
        const LIN_AXES = Self::X.bits() | Self::Y.bits() | Self::Z.bits();
        /// The set of all angular degrees of freedom.
        const ANG_AXES = Self::ANG_X.bits() | Self::ANG_Y.bits() | Self::ANG_Z.bits();
    }
}

/// serializable wrapper for physics joints.
#[derive(Debug, PartialEq, Reflect, Clone)]
#[reflect(Component)]
pub struct JointFlag {
    // removed. local_frame1 serves the same purpose.
    //pub offset: Transform,

    // //name of the parent "link" of the joint. Some joints may not have named parents, so this is optional
    // pub parent_name: Option<String>,
    //the parent entity of this joint. Some joint parents may be referenced by name only, so this has have to be populated later down
    //the deserialization pipeline.
    pub parent: Entity,

    pub joint: JointInfo,
}

impl ComponentWrapper for JointFlag {
    type WrapperTarget = ImpulseJoint;
}

#[derive(Default, Debug, PartialEq, Reflect, Clone)]
pub struct JointInfo {
    pub limit: JointLimitWrapper,
    pub dynamics: Dynamics,
    //pub mimic: Option<Mimic>
    //pub safety_controller: Option<SafetyController>,
    /// The joint’s frame, expressed in the first rigid-body’s local-space.
    pub local_frame1: Transform,
    /// The joint’s frame, expressed in the second rigid-body’s local-space.
    pub local_frame2: Transform,
    /// The degrees-of-freedoms locked by this joint.
    pub locked_axes: JointAxesMaskWrapper,
    /// The degrees-of-freedoms limited by this joint.
    pub limit_axes: JointAxesMaskWrapper,
    /// The degrees-of-freedoms motorised by this joint.
    pub motor_axes: JointAxesMaskWrapper,
    /// The coupled degrees of freedom of this joint.
    pub coupled_axes: JointAxesMaskWrapper,
    // The limits, along each degrees of freedoms of this joint.
    ///
    /// The motors, along each degrees of freedoms of this joint.
    ///
    /// Note that the mostor must also be explicitly enabled by the `motors` bitmask.
    pub motors: [JointMotorWrapper; 6],
    /// Are contacts between the attached rigid-bodies enabled?
    pub contacts_enabled: bool,
    /// Whether or not the joint is enabled.
    pub enabled: bool,
}

impl Component for JointFlag {
    const STORAGE_TYPE: StorageType = StorageType::Table;

    fn register_component_hooks(_hooks: &mut bevy_ecs::component::ComponentHooks) {
        // keeps joint and Transform consistent with eachother to stop parts from flying off
        _hooks.on_add(|mut world, e, _| {
            // rapier joint positions affect transform, but do not affect transformation unless they're part of an active rigidbody.
            // to prevent rebound from joint being snapped on by joint, add transform onto this entity to automatically snap it to where its supposed to be
            let new_trans = {
                let comp = match world.entity(e).get::<Self>() {
                    Some(val) => val,
                    None => {
                        warn!("could not get {:#?} on: {:#}", type_name::<Self>(), e);
                        return;
                    }
                };
                let Some(parent_trans) = world.get::<Transform>(comp.parent) else {
                    warn!("parent {:#?} has no trans?", comp.parent);
                    return;
                };

                let new_translation = parent_trans.translation
                    + comp.joint.local_frame1.translation
                    - comp.joint.local_frame2.translation;

                let new_result = Transform::from_translation(new_translation)
                    .with_rotation(parent_trans.rotation);
                //println!("new result: {:#?}", new_result);
                new_result
                //parent_trans.translation + comp.joint.local_frame1.translation
            };

            world.commands().entity(e).insert(new_trans);

            // let Some(parent) =
            // let Some(parent_trans) = world.entity(comp.parent_id).get::<Tran
            //world.commands().entity(e).insert()
        });
    }
}

#[derive(Reflect, PartialEq, Clone, Debug)]
pub struct JointMotorWrapper {
    /// The target velocity of the motor.
    pub target_vel: f32,
    /// The target position of the motor.
    pub target_pos: f32,
    /// The stiffness coefficient of the motor’s spring-like equation.
    pub stiffness: f32,
    /// The damping coefficient of the motor’s spring-like equation.
    pub damping: f32,
    /// The maximum force this motor can deliver.
    pub max_force: f32,
    /// The impulse applied by this motor.
    pub impulse: f32,
    /// The spring-like model used for simulating this motor.
    pub model: MotorModelWrapper,
}

impl Default for JointMotorWrapper {
    fn default() -> Self {
        Self {
            max_force: f32::MAX,
            target_vel: 0.0,
            target_pos: 0.0,
            stiffness: 0.0,
            damping: 0.0,
            impulse: 0.0,
            model: MotorModelWrapper::default(),
        }
    }
}

impl From<&JointMotor> for JointMotorWrapper {
    fn from(value: &JointMotor) -> Self {
        Self {
            target_vel: value.target_vel,
            target_pos: value.target_pos,
            stiffness: value.stiffness,
            damping: value.damping,
            max_force: value.max_force,
            impulse: value.impulse,
            model: (&value.model).into(),
        }
    }
}

impl From<&JointMotorWrapper> for JointMotor {
    fn from(value: &JointMotorWrapper) -> Self {
        Self {
            target_vel: value.target_vel,
            target_pos: value.target_pos,
            stiffness: value.stiffness,
            damping: value.damping,
            max_force: value.max_force,
            impulse: value.impulse,
            model: (&value.model).into(),
        }
    }
}

/// The spring-like model used for constraints resolution.
#[derive(Reflect, Clone, Debug, PartialEq, Eq, Default)]
pub enum MotorModelWrapper {
    /// The solved spring-like equation is:
    /// `acceleration = stiffness * (pos - target_pos) + damping * (vel - target_vel)`
    #[default]
    AccelerationBased,
    /// The solved spring-like equation is:
    /// `force = stiffness * (pos - target_pos) + damping * (vel - target_vel)`
    ForceBased,
}

impl From<&MotorModel> for MotorModelWrapper {
    fn from(value: &MotorModel) -> Self {
        match value {
            MotorModel::AccelerationBased => Self::AccelerationBased,
            MotorModel::ForceBased => Self::ForceBased,
        }
    }
}

impl From<&MotorModelWrapper> for MotorModel {
    fn from(value: &MotorModelWrapper) -> Self {
        match value {
            MotorModelWrapper::AccelerationBased => Self::AccelerationBased,
            MotorModelWrapper::ForceBased => Self::ForceBased,
        }
    }
}