
Bevy là game engine mã nguồn mở được xây dựng bằng Rust, tập trung vào khả năng mở rộng (data-driven) và hiệu năng. Với kiến trúc ECS (Entity Component System) mạnh mẽ, Bevy cho phép developer xây dựng game 2D hoặc 3D mà không phải chiến đấu với engine phức tạp như Unity hoặc Unreal.
ECS là gì? Tại sao Bevy dùng ECS?
ECS chia game thành 3 khối:
- Entity: Định danh duy nhất (ID số), không có logic
- Component: Dữ liệu thuần (Transform, Velocity, Sprite, Health)
- System: Logic xử lý nhóm Component (MoveSystem chạy trên [Transform, Velocity])
Ưu điểm ECS: cache-friendly, dễ parallelize, tránh “spaghetti inheritance” của OOP truyền thống.
ECS trong thực tế: Query và Archetype
Bevy dùng Archetype để nhóm Entity có cùng set Component. Mỗi Archetype lưu dữ liệu liên tục trong memory (SoA – Structure of Arrays), cho phép CPU cache line fetch tối ưu. Khi System query Query, Bevy chỉ iterate qua Archetype chứa cả Transform và Velocity — bỏ qua Entity chỉ có một trong hai. Đây là lý do ECS nhanh hơn OOP khi có nhiều Entity.
Ví dụ System song song:
fn move_system(mut query: Query<&mut Transform, With>) {
for mut transform in query.iter_mut() {
// xử lý mỗi entity độc lập, Bevy tự động chia thread
}
}

Cài đặt Bevy
Bevy hỗ trợ Rust edition 2021. Tạo project mới:
cargo new my_game --name my_game
cd my_game
cargo add [email protected]
main.rs tối thiểu:
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "My Game".into(),
resolution: (1280., 720.).into(),
..default()
}),
..default()
}))
.add_systems(Startup, setup)
.add_systems(Update, move_player)
.run();
}
fn setup(mut commands: Commands, asset_server: Res) {
commands.spawn(Camera2dBundle::default());
commands.spawn(SpriteBundle {
texture: asset_server.load("player.png"),
transform: Transform::from_xyz(0.0, 0.0, 0.0),
..default()
});
}
fn move_player(
keys: Res<Input>,
mut query: Query<&mut Transform, With>,
) {
let mut transform = query.single_mut();
let speed = 5.0;
if keys.pressed(KeyCode::Left) { transform.translation.x -= speed; }
if keys.pressed(KeyCode::Right) { transform.translation.x += speed; }
if keys.pressed(KeyCode::Up) { transform.translation.y += speed; }
if keys.pressed(KeyCode::Down) { transform.translation.y -= speed; }
}
So sánh Bevy với Godot, Unity
| Tiêu chí | Bevy | Godot 4 | Unity |
|---|---|---|---|
| Ngôn ngữ | Rust | GDScript, C# | C# |
| Hiệu năng | Cực cao (native) | Cao | Trung bình |
| 2D | Rất tốt | Xuất sắc | Tốt |
| 3D | Đang phát triển nhanh | Tốt | Xuất sắc |
| ECS native | Có (bắt buộc) | Không | Entities/DOTS (optional) |
| Kích thước binary | ~5-10 MB | ~20-50 MB | ~100 MB+ |
| Learning curve | Cao (Rust + ECS) | Thấp | Trung bình |
| Community | Đang tăng nhanh | Lớn | Rất lớn |
Render 2D sprite và animation
fn setup_scene(mut commands: Commands, asset_server: Res) {
commands.spawn(Camera2dBundle::default());
let texture = asset_server.load("sprites/character.png");
let mut atlas = TextureAtlas::new_empty(texture, Vec2::new(32.0, 32.0));
atlas.add_texture(Rect::new(0.0, 0.0, 32.0, 32.0)); // frame 0
atlas.add_texture(Rect::new(32.0, 0.0, 64.0, 32.0)); // frame 1
commands.spawn((
SpriteSheetBundle {
texture_atlas: atlas,
transform: Transform::from_scale(Vec3::splat(4.0)),
..default()
},
AnimationTimer {
timer: Timer::from_seconds(0.1, TimerMode::Repeating),
},
));
}

Audio và input
fn play_sound(keys: Res<Input>, asset_server: Res, audio: Res
Bevy UI
Bevy có UI system riêng (bevy_ui) dựa trên Flexbox — tạo HUD, menu, inventory mà không cần thư viện ngoài. Các node UI cũng là Entity với Component (Style, Text, Image), nên có thể animate cùng game world.
commands.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
background_color: Color::rgba(0.2, 0.2, 0.2, 0.8).into(),
..default()
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Game Over",
TextStyle { font_size: 64.0, color: Color::WHITE, ..default() },
));
});
Physics và WebAssembly
Bevy tích hợp bevy_xpbd (Position-Based Dynamics) cho 2D/3D physics. Ngoài ra, Bevy compile ra WASM để chơi game trong browser — chỉ cần cargo build --target wasm32-unknown-unknown và serve file .wasm.
Performance profiling
Dùng bevy_dev_tools để xem FPS, entity count, system execution time. Kết hợp perf (Linux) hoặc Instruments (macOS) để tìm bottleneck trong system. ECS giúp dễ dàng parallelize system bằng ParallelSystemDescriptor.
Khi nào dùng Bevy?
- Bạn yêu Rust và muốn ownership pattern trong game logic
- Game 2D pixel art, roguelike, tower defense — Bevy rất phù hợp
- Cần ECS để scale lên nhiều hệ thống phức tạp
- Muốn binary nhỏ, không phụ thuộc runtime nặng
- Prototype nhanh mà không cần visual editor
Khi nào KHÔNG dùng Bevy?
- 3D AAA — Unity hoặc Unreal vẫn tốt hơn
- Team thiếu kinh nghiệm Rust — onboarding lâu
- Cần asset pipeline phức tạp, animation retargeting — Godot có editor trực quan hơn
- Mobile game cần IAP, ads, analytics SDK — ecosystem còn non trẻ
