avian3d/collision/collider/collider_transform/
mod.rs

1//! Transform management and types for colliders.
2
3mod plugin;
4
5pub use plugin::ColliderTransformPlugin;
6
7use crate::prelude::*;
8use bevy::prelude::*;
9
10/// The transform of a collider relative to the rigid body it's attached to.
11/// This is in the local space of the body, not the collider itself.
12///
13/// This is used for computing things like contact positions and a body's center of mass
14/// without having to traverse deeply nested hierarchies. It's updated automatically,
15/// so you shouldn't modify it manually.
16#[derive(Reflect, Clone, Copy, Component, Debug, PartialEq)]
17#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
18#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
19#[reflect(Debug, Component, PartialEq)]
20pub struct ColliderTransform {
21    /// The translation of a collider in a rigid body's frame of reference.
22    pub translation: Vector,
23    /// The rotation of a collider in a rigid body's frame of reference.
24    pub rotation: Rotation,
25    /// The global scale of a collider. Equivalent to the `GlobalTransform` scale.
26    pub scale: Vector,
27}
28
29impl ColliderTransform {
30    /// Transforms a given point by applying the translation, rotation and scale of
31    /// this [`ColliderTransform`].
32    pub fn transform_point(&self, mut point: Vector) -> Vector {
33        point *= self.scale;
34        point = self.rotation * point;
35        point += self.translation;
36        point
37    }
38}
39
40impl Default for ColliderTransform {
41    fn default() -> Self {
42        Self {
43            translation: Vector::ZERO,
44            rotation: Rotation::default(),
45            scale: Vector::ONE,
46        }
47    }
48}
49
50impl From<Transform> for ColliderTransform {
51    fn from(value: Transform) -> Self {
52        Self {
53            #[cfg(feature = "2d")]
54            translation: value.translation.truncate().adjust_precision(),
55            #[cfg(feature = "3d")]
56            translation: value.translation.adjust_precision(),
57            rotation: Rotation::from(value.rotation.adjust_precision()),
58            #[cfg(feature = "2d")]
59            scale: value.scale.truncate().adjust_precision(),
60            #[cfg(feature = "3d")]
61            scale: value.scale.adjust_precision(),
62        }
63    }
64}