Skip to main content

Transform

Struct Transform 

Source
pub struct Transform {
    pub translation: Vec3,
    pub rotation: Quat,
    pub scale: Vec3,
}
Expand description

表示 3D 空间中位置、旋转和缩放的变换组件。

Transform 是 AnvilKit 中用于表示对象空间变换的基础类型。 它支持 2D 和 3D 使用场景,对于 2D 对象,通常将 Z 分量设置为 0。

§内存布局

该结构体使用紧凑的内存布局,总大小为 40 字节:

  • translation: 12 字节 (3 × f32)
  • rotation: 16 字节 (4 × f32)
  • scale: 12 字节 (3 × f32)

§线程安全

Transform 实现了 SendSync,可以安全地在线程间传递。

Fields§

§translation: Vec3

世界空间中的位置

§rotation: Quat

四元数表示的旋转

§scale: Vec3

各轴的缩放因子

Implementations§

Source§

impl Transform

Source

pub const IDENTITY: Transform

单位变换(无平移、旋转或缩放)

Source

pub fn new(translation: Vec3, rotation: Quat, scale: Vec3) -> Transform

创建一个新的变换实例

§参数
  • translation: 位置向量
  • rotation: 旋转四元数
  • scale: 缩放向量
§示例
use anvilkit_core::math::Transform;
use glam::{Vec3, Quat};
 
let transform = Transform::new(
    Vec3::new(1.0, 2.0, 3.0),
    Quat::IDENTITY,
    Vec3::ONE
);
Source

pub fn from_translation(translation: Vec3) -> Transform

从位置创建变换

§示例
use anvilkit_core::math::Transform;
use glam::Vec3;
 
let transform = Transform::from_translation(Vec3::new(1.0, 2.0, 3.0));
assert_eq!(transform.translation, Vec3::new(1.0, 2.0, 3.0));
Source

pub fn from_rotation(rotation: Quat) -> Transform

从旋转创建变换

§示例
use anvilkit_core::math::Transform;
use glam::Quat;
 
let rotation = Quat::from_rotation_y(std::f32::consts::PI / 4.0);
let transform = Transform::from_rotation(rotation);
assert_eq!(transform.rotation, rotation);
Source

pub fn from_scale(scale: Vec3) -> Transform

从缩放创建变换

§示例
use anvilkit_core::math::Transform;
use glam::Vec3;
 
let transform = Transform::from_scale(Vec3::splat(2.0));
assert_eq!(transform.scale, Vec3::splat(2.0));
Source

pub fn from_xyz(x: f32, y: f32, z: f32) -> Transform

从 XYZ 坐标创建变换

§示例
use anvilkit_core::math::Transform;
 
let transform = Transform::from_xyz(1.0, 2.0, 3.0);
assert_eq!(transform.translation.x, 1.0);
assert_eq!(transform.translation.y, 2.0);
assert_eq!(transform.translation.z, 3.0);
Source

pub fn from_xy(x: f32, y: f32) -> Transform

从 XY 坐标创建 2D 变换(Z = 0)

§示例
use anvilkit_core::math::Transform;
 
let transform = Transform::from_xy(1.0, 2.0);
assert_eq!(transform.translation.z, 0.0);
Source

pub fn with_translation(self, translation: Vec3) -> Transform

设置位置(链式调用)

§示例
use anvilkit_core::math::Transform;
use glam::Vec3;
 
let transform = Transform::IDENTITY
    .with_translation(Vec3::new(1.0, 2.0, 3.0));
Source

pub fn with_rotation(self, rotation: Quat) -> Transform

设置旋转(链式调用)

Source

pub fn with_scale(self, scale: Vec3) -> Transform

设置缩放(链式调用)

Source

pub fn looking_at( eye: Vec3, target: Vec3, up: Vec3, ) -> Result<Transform, AnvilKitError>

创建朝向目标的变换

§参数
  • eye: 观察者位置
  • target: 目标位置
  • up: 上方向向量
§示例
use anvilkit_core::math::Transform;
use glam::Vec3;
 
let transform = Transform::looking_at(
    Vec3::new(0.0, 0.0, 5.0),  // 相机位置
    Vec3::ZERO,                // 看向原点
    Vec3::Y                    // 上方向
);
Source

pub fn compute_matrix(&self) -> Mat4

将变换转换为 4x4 变换矩阵

矩阵的计算顺序为:缩放 → 旋转 → 平移

§示例
use anvilkit_core::math::Transform;
use glam::Vec3;
 
let transform = Transform::from_xyz(1.0, 2.0, 3.0);
let matrix = transform.compute_matrix();
 
// 验证平移部分
assert_eq!(matrix.w_axis.truncate(), Vec3::new(1.0, 2.0, 3.0));
Source

pub fn transform_point(&self, point: Vec3) -> Vec3

应用变换到点

§示例
use anvilkit_core::math::Transform;
use glam::Vec3;
 
let transform = Transform::from_xyz(1.0, 2.0, 3.0);
let point = Vec3::ZERO;
let transformed = transform.transform_point(point);
 
assert_eq!(transformed, Vec3::new(1.0, 2.0, 3.0));
Source

pub fn transform_vector(&self, vector: Vec3) -> Vec3

应用变换到向量(忽略平移)

§示例
use anvilkit_core::math::Transform;
use glam::Vec3;
 
let transform = Transform::from_scale(Vec3::splat(2.0));
let vector = Vec3::new(1.0, 1.0, 1.0);
let transformed = transform.transform_vector(vector);
 
assert_eq!(transformed, Vec3::new(2.0, 2.0, 2.0));
Source

pub fn mul_transform(&self, other: &Transform) -> Transform

组合两个变换(self * other)

§示例
use anvilkit_core::math::Transform;
use glam::Vec3;
 
let parent = Transform::from_xyz(1.0, 0.0, 0.0);
let child = Transform::from_xyz(0.0, 1.0, 0.0);
let combined = parent.mul_transform(&child);
 
assert_eq!(combined.translation, Vec3::new(1.0, 1.0, 0.0));
Source

pub fn from_matrix(matrix: Mat4) -> Transform

从变换矩阵创建变换

§注意

如果矩阵包含非均匀缩放或剪切,可能会丢失信息。

Source

pub fn inverse(&self) -> Result<Transform, AnvilKitError>

获取变换的逆变换

§错误

如果变换不可逆(例如缩放为零),返回错误。

§示例
use anvilkit_core::math::Transform;
use glam::Vec3;

let transform = Transform::from_xyz(1.0, 2.0, 3.0);
let inverse = transform.inverse().unwrap();
let identity = transform.mul_transform(&inverse);

// 结果应该接近单位变换
assert!((identity.translation.length() < 1e-5));
Source

pub fn is_finite(&self) -> bool

检查变换是否有效(不包含 NaN 或无穷大)

Trait Implementations§

Source§

impl Clone for Transform

Source§

fn clone(&self) -> Transform

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Component for Transform
where Transform: Send + Sync + 'static,

Source§

const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::Table

A constant indicating the storage type used for this component.
Source§

fn register_component_hooks(_hooks: &mut ComponentHooks)

Called when registering this component, allowing mutable access to its ComponentHooks.
Source§

impl Debug for Transform

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Transform

Source§

fn default() -> Transform

Returns the “default value” for a type. Read more
Source§

impl From<Transform> for GlobalTransform

Source§

fn from(transform: Transform) -> GlobalTransform

Converts to this type from the input type.
Source§

impl PartialEq for Transform

Source§

fn eq(&self, other: &Transform) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Transform

Source§

impl StructuralPartialEq for Transform

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<C> Bundle for C
where C: Component,

Source§

fn component_ids( components: &mut Components, storages: &mut Storages, ids: &mut impl FnMut(ComponentId), )

Source§

unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> C
where F: for<'a> FnMut(&'a mut T) -> OwningPtr<'a>,

Source§

fn get_component_ids( components: &Components, ids: &mut impl FnMut(Option<ComponentId>), )

Gets this Bundle’s component ids. This will be None if the component has not been registered.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<C> DynamicBundle for C
where C: Component,

Source§

fn get_components(self, func: &mut impl FnMut(StorageType, OwningPtr<'_>))

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromWorld for T
where T: Default,

Source§

fn from_world(_world: &mut World) -> T

Creates Self using data from the given World.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> TypeData for T
where T: 'static + Send + Sync + Clone,

Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ConditionalSend for T
where T: Send,

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,