bevy_rapier3d/utils/
mod.rs

1use bevy::prelude::Transform;
2use rapier::math::{Isometry, Real};
3
4/// Converts a Rapier isometry to a Bevy transform.
5#[cfg(feature = "dim2")]
6pub fn iso_to_transform(iso: &Isometry<Real>) -> Transform {
7    Transform {
8        translation: iso.translation.vector.push(0.0).into(),
9        rotation: bevy::prelude::Quat::from_rotation_z(iso.rotation.angle()),
10        ..Default::default()
11    }
12}
13
14/// Converts a Rapier isometry to a Bevy transform.
15#[cfg(feature = "dim3")]
16pub fn iso_to_transform(iso: &Isometry<Real>) -> Transform {
17    Transform {
18        translation: iso.translation.vector.into(),
19        rotation: iso.rotation.into(),
20        ..Default::default()
21    }
22}
23
24/// Converts a Bevy transform to a Rapier isometry.
25#[cfg(feature = "dim2")]
26pub(crate) fn transform_to_iso(transform: &Transform) -> Isometry<Real> {
27    use bevy::math::Vec3Swizzles;
28    Isometry::new(
29        transform.translation.xy().into(),
30        transform.rotation.to_scaled_axis().z,
31    )
32}
33
34/// Converts a Bevy transform to a Rapier isometry.
35#[cfg(feature = "dim3")]
36pub(crate) fn transform_to_iso(transform: &Transform) -> Isometry<Real> {
37    Isometry::from_parts(transform.translation.into(), transform.rotation.into())
38}
39
40#[cfg(test)]
41#[cfg(feature = "dim3")]
42mod tests {
43    use super::*;
44    use bevy::prelude::Transform;
45
46    #[test]
47    fn convert_back_to_equal_transform() {
48        let transform = Transform {
49            translation: bevy::prelude::Vec3::new(-2.1855694e-8, 0.0, 0.0),
50            rotation: bevy::prelude::Quat::from_xyzw(0.99999994, 0.0, 1.6292068e-7, 0.0)
51                .normalize(),
52            ..Default::default()
53        };
54        let converted_transform = iso_to_transform(&transform_to_iso(&transform));
55        assert_eq!(converted_transform, transform);
56    }
57}