Skip to content

Latest commit

 

History

History
60 lines (46 loc) · 2.3 KB

circles.md

File metadata and controls

60 lines (46 loc) · 2.3 KB

Circles

To add basic shapes in bevy::prelude::shape, we can use ColorMesh2dBundle.

In the following example, we add a Circle to the app. The Circle component takes a radius in its new function. The Circle is added to the mesh component in ColorMesh2dBundle. This Circle must also be added to the resource Assets<Mesh> for working with the underlying engine.

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

    commands.spawn(ColorMesh2dBundle {
        mesh: meshes.add(Circle::new(50.).into()).into(),
        ..default()
    });
}

In the code, we use into() in several places to convert the structs to the correct types.

The full code is as follows:

use bevy::{
    app::{App, Startup},
    asset::Assets,
    core_pipeline::core_2d::Camera2dBundle,
    ecs::system::{Commands, ResMut},
    prelude::default,
    render::mesh::{shape::Circle, Mesh},
    sprite::ColorMesh2dBundle,
    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(Circle::new(50.).into()).into(),
        ..default()
    });
}

Result:

Circles

➡️ Next: Quads

📘 Back: Table of contents