create a spinning cube using bevy in rust

To create a spinning cube using Bevy in Rust, you can follow these steps:

  1. Set up a new Bevy project and add the necessary dependencies to your Cargo.toml:
[dependencies]
bevy = "0.5.0"
bevy_render = "0.5.0"
bevy_pbr = "0.5.0"
71 chars
5 lines
  1. Create a new Bevy app and add a cube mesh:
main.rs
use bevy::prelude::*;
use bevy::render::mesh::Indices;

fn add_cube_mesh(commands: &mut Commands, meshes: &mut ResMut<Assets<Mesh>>) {
    // Define cube mesh vertices
    let cube_verts = vec![
        // front face
        ([0.5, 0.5, 0.5], [0.0, 0.0], [0.0, 0.0, 1.0]), // top right
        ([-0.5, 0.5, 0.5], [1.0, 0.0], [0.0, 0.0, 1.0]), // top left
        ([0.5, -0.5, 0.5], [0.0, 1.0], [0.0, 0.0, 1.0]), // bottom right
        ([-0.5, -0.5, 0.5], [1.0, 1.0], [0.0, 0.0, 1.0]), // bottom left
        // back face
        ([0.5, 0.5, -0.5], [0.0, 0.0], [0.0, 0.0, -1.0]), // top right
        ([0.5, -0.5, -0.5], [0.0, 1.0], [0.0, 0.0, -1.0]), // bottom right
        ([-0.5, 0.5, -0.5], [1.0, 0.0], [0.0, 0.0, -1.0]), // top left
        ([-0.5, -0.5, -0.5], [1.0, 1.0], [0.0, 0.0, -1.0]), // bottom left
        // top face
        ([0.5, 0.5, 0.5], [0.0, 0.0], [0.0, 1.0, 0.0]), // top right
        ([0.5, 0.5, -0.5], [0.0, 1.0], [0.0, 1.0, 0.0]), // bottom right
        ([-0.5, 0.5, 0.5], [1.0, 0.0], [0.0, 1.0, 0.0]), // top left
        ([-0.5, 0.5, -0.5], [1.0, 1.0], [0.0, 1.0, 0.0]), // bottom left
        // bottom face
        ([0.5, -0.5, 0.5], [0.0, 0.0], [0.0, -1.0, 0.0]), // top right
        ([-0.5, -0.5, 0.5], [1.0, 0.0], [0.0, -1.0, 0.0]), // top left
        ([0.5, -0.5, -0.5], [0.0, 1.0], [0.0, -1.0, 0.0]), // bottom right
        ([-0.5, -0.5, -0.5], [1.0, 1.0], [0.0, -1.0, 0.0]), // bottom left
        // right face
        ([0.5, 0.5, 0.5], [1.0, 0.0], [1.0, 0.0, 0.0]), // top back
        ([0.5, -0.5, 0.5], [1.0, 1.0], [1.0, 0.0, 0.0]), // bottom back
        ([0.5, 0.5, -0.5], [0.0, 0.0], [1.0, 0.0, 0.0]), // top front
        ([0.5, -0.5, -0.5], [0.0, 1.0], [1.0, 0.0, 0.0]), // bottom front
        // left face
        ([-0.5, 0.5, -0.5], [1.0, 0.0], [-1.0, 0.0, 0.0]), // top back
        ([-0.5, -0.5, -0.5], [1.0, 1.0], [-1.0, 0.0, 0.0]), // bottom back
        ([-0.5, 0.5, 0.5], [0.0, 0.0], [-1.0, 0.0, 0.0]), // top front
        ([-0.5, -0.5, 0.5], [0.0, 1.0], [-1.0, 0.0, 0.0]), // bottom front
    ];
    // Define cube mesh indices 
    let cube_indices = vec![
        0u32, 1, 2, // front face
        2, 1, 3,
        4, 5, 6, // back face
        6, 5, 7,
        8, 9, 10, // top face
        10, 9, 11,
        12, 13, 14, // bottom face
        14, 13, 15,
        16, 17, 18, // right face
        18, 17, 19,
        20, 21, 22, // left face
        22, 21, 23,
    ];
    // Create the cube mesh
    let cube_mesh = Mesh::new(PrimitiveTopology::TriangleList, cube_verts, None, None, cube_indices, None);
    // Add the mesh to the asset manager
    let cube_handle = meshes.add(cube_mesh);
    // Spawn an entity with the cube mesh as a component
    commands.spawn_bundle(PbrBundle {
        mesh: cube_handle,
        ..Default::default()
    });
}
2820 chars
63 lines
  1. Set up an update loop that rotates the cube:
main.rs
fn rotate_cube(time: Res<Time>, mut query: Query<&mut Transform>) {
    for mut transform in query.iter_mut() {
        transform.rotate(Quat::from_rotation_y(time.delta_seconds()));
    }
}
191 chars
6 lines
  1. Run the Bevy app:
main.rs
fn main() {
    App::build()
        .add_plugins(DefaultPlugins)
        .add_startup_system(add_cube_mesh.system())
        .add_system(rotate_cube.system())
        .run();
}
178 chars
8 lines

When you run the app, you should see a spinning cube on your screen. Congratulations, you've created a spinning cube using Bevy in Rust!

gistlibby LogSnag