Skip to content

Latest commit

 

History

History
49 lines (39 loc) · 1.52 KB

shapes_with_transformation.md

File metadata and controls

49 lines (39 loc) · 1.52 KB

Shapes With Transformation

All the shapes in bevy::prelude::shape can apply Transform to change their positions, rotations and sizes.

We use Transform in ColorMesh2dBundle to do the transformation.

use bevy::{
    app::{App, Startup},
    asset::Assets,
    core_pipeline::core_2d::Camera2dBundle,
    ecs::system::{Commands, ResMut},
    math::Quat,
    prelude::default,
    render::mesh::{shape::RegularPolygon, Mesh},
    sprite::ColorMesh2dBundle,
    transform::components::Transform,
    DefaultPlugins,
};

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

fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
    commands.spawn(Camera2dBundle::default());

    commands.spawn(ColorMesh2dBundle {
        mesh: meshes.add(RegularPolygon::new(50., 5).into()).into(),
        transform: Transform {
            translation: (50., 100., 0.).into(),
            rotation: Quat::from_rotation_z(0.78),
            scale: (2., 1., 1.).into(),
        },
        ..default()
    });
}

Result:

Shapes With Transformation

➡️ Next: Colors

📘 Back: Table of contents