Skip to content

Latest commit

 

History

History
62 lines (49 loc) · 1.98 KB

rotation.md

File metadata and controls

62 lines (49 loc) · 1.98 KB

Rotation

In addition to move an image, Transform is also able to rotate an image. We can use the function Transform::from_rotation to rotate the image. The function receives a struct Quat, which describes how to rotate an object.

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn(Camera2dBundle::default());

    commands.spawn(SpriteBundle {
        texture: asset_server.load("bevy_bird_dark.png"),
        transform: Transform::from_rotation(Quat::from_rotation_z(1.57)),
        ..default()
    });
}

The function Quat::from_rotation_z rotates objects based on the z axis, resulting in the objects rotated on the x-y plane. Its parameter describes the angle of the rotation. The angle is in radians and is for counter-clockwise rotations when the angle is positive.

The full code is as follows:

use bevy::{
    app::{App, Startup},
    asset::AssetServer,
    core_pipeline::core_2d::Camera2dBundle,
    ecs::system::{Commands, Res},
    math::Quat,
    sprite::SpriteBundle,
    transform::components::Transform,
    utils::default,
    DefaultPlugins,
};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn(Camera2dBundle::default());

    commands.spawn(SpriteBundle {
        texture: asset_server.load("bevy_bird_dark.png"),
        transform: Transform::from_rotation(Quat::from_rotation_z(1.57)),
        ..default()
    });
}

Result:

Rotation

➡️ Next: Scale

📘 Back: Table of contents