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
use bevy::prelude::Transform;
use rapier::math::{Isometry, Real};
#[cfg(feature = "dim2")]
pub fn iso_to_transform(iso: &Isometry<Real>, physics_scale: Real) -> Transform {
Transform {
translation: (iso.translation.vector.push(0.0) * physics_scale).into(),
rotation: bevy::prelude::Quat::from_rotation_z(iso.rotation.angle()),
..Default::default()
}
}
#[cfg(feature = "dim3")]
pub fn iso_to_transform(iso: &Isometry<Real>, physics_scale: Real) -> Transform {
Transform {
translation: (iso.translation.vector * physics_scale).into(),
rotation: iso.rotation.into(),
..Default::default()
}
}
#[cfg(feature = "dim2")]
pub(crate) fn transform_to_iso(transform: &Transform, physics_scale: Real) -> Isometry<Real> {
use bevy::math::Vec3Swizzles;
Isometry::new(
(transform.translation / physics_scale).xy().into(),
transform.rotation.to_scaled_axis().z,
)
}
#[cfg(feature = "dim3")]
pub(crate) fn transform_to_iso(transform: &Transform, physics_scale: Real) -> Isometry<Real> {
Isometry::from_parts(
(transform.translation / physics_scale).into(),
transform.rotation.into(),
)
}
#[cfg(test)]
#[cfg(feature = "dim3")]
mod tests {
use super::*;
use bevy::prelude::Transform;
#[test]
fn convert_back_to_equal_transform() {
let transform = Transform {
translation: bevy::prelude::Vec3::new(-2.1855694e-8, 0.0, 0.0),
rotation: bevy::prelude::Quat::from_xyzw(0.99999994, 0.0, 1.6292068e-7, 0.0)
.normalize(),
..Default::default()
};
let converted_transform = iso_to_transform(&transform_to_iso(&transform, 1.0), 1.0);
assert_eq!(converted_transform, transform);
}
}