pub struct Transform {
pub translation: Vec3,
pub rotation: Quat,
pub scale: Vec3,
}Expand description
Fields§
§translation: Vec3世界空间中的位置
rotation: Quat四元数表示的旋转
scale: Vec3各轴的缩放因子
Implementations§
Source§impl Transform
impl Transform
Sourcepub fn from_translation(translation: Vec3) -> Transform
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));Sourcepub fn from_rotation(rotation: Quat) -> Transform
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);Sourcepub fn from_scale(scale: Vec3) -> Transform
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));Sourcepub fn from_xyz(x: f32, y: f32, z: f32) -> Transform
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);Sourcepub fn from_xy(x: f32, y: f32) -> Transform
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);Sourcepub fn with_translation(self, translation: Vec3) -> Transform
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));Sourcepub fn with_rotation(self, rotation: Quat) -> Transform
pub fn with_rotation(self, rotation: Quat) -> Transform
设置旋转(链式调用)
Sourcepub fn with_scale(self, scale: Vec3) -> Transform
pub fn with_scale(self, scale: Vec3) -> Transform
设置缩放(链式调用)
Sourcepub fn looking_at(
eye: Vec3,
target: Vec3,
up: Vec3,
) -> Result<Transform, AnvilKitError>
pub fn looking_at( eye: Vec3, target: Vec3, up: Vec3, ) -> Result<Transform, AnvilKitError>
Sourcepub fn compute_matrix(&self) -> Mat4
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));Sourcepub fn transform_point(&self, point: Vec3) -> Vec3
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));Sourcepub fn transform_vector(&self, vector: Vec3) -> Vec3
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));Sourcepub fn mul_transform(&self, other: &Transform) -> Transform
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));Sourcepub fn from_matrix(matrix: Mat4) -> Transform
pub fn from_matrix(matrix: Mat4) -> Transform
Sourcepub fn inverse(&self) -> Result<Transform, AnvilKitError>
pub fn inverse(&self) -> Result<Transform, AnvilKitError>
Trait Implementations§
Source§impl Component for Transform
impl Component for Transform
Source§const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::Table
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)
fn register_component_hooks(_hooks: &mut ComponentHooks)
Called when registering this component, allowing mutable access to its
ComponentHooks.Source§impl From<Transform> for GlobalTransform
impl From<Transform> for GlobalTransform
Source§fn from(transform: Transform) -> GlobalTransform
fn from(transform: Transform) -> GlobalTransform
Converts to this type from the input type.
impl Copy for Transform
impl StructuralPartialEq for Transform
Auto Trait Implementations§
impl Freeze for Transform
impl RefUnwindSafe for Transform
impl Send for Transform
impl Sync for Transform
impl Unpin for Transform
impl UnsafeUnpin for Transform
impl UnwindSafe for Transform
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<C> Bundle for Cwhere
C: Component,
impl<C> Bundle for Cwhere
C: Component,
fn component_ids( components: &mut Components, storages: &mut Storages, ids: &mut impl FnMut(ComponentId), )
unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> C
Source§fn get_component_ids(
components: &Components,
ids: &mut impl FnMut(Option<ComponentId>),
)
fn get_component_ids( components: &Components, ids: &mut impl FnMut(Option<ComponentId>), )
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
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>
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)
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)
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
impl<T> DowncastSync for T
Source§impl<C> DynamicBundle for Cwhere
C: Component,
impl<C> DynamicBundle for Cwhere
C: Component,
fn get_components(self, func: &mut impl FnMut(StorageType, OwningPtr<'_>))
Source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Creates
Self using data from the given World.