---
# System prepended metadata

title: Bevy Engine
tags: [rust, game dev, bevy]

---

# Bevy Engine

{%hackmd @RintarouTW/About %}

<img src="https://i.imgur.com/JwbJE6j.png" style="filter:invert(.9)"></img>

https://bevyengine.org/

[Bevy Book](https://bevyengine.org/learn/book/introduction/)


## App & AppBuilder

App indicate the application which control the life time of the app.

AppBuilder just like a helper to add plugins, resources, systems into the stage, and the systems (game logics) 
would be triggered every frame to update the entities on stage(scene), finally the renderer would render the scence.

## Window

via **Winit** to cross platform(native window)

## ECS

- entities(as character instances) with components(a bundle of properties)
- systems (for game logic)

system can query the components to manipulate the properties, ex:
- sprite animation system
- render system

add_system()
- add the system into stage, triggered every frame.

### other systems
- audio
- text
- font
- network
- ui
- input
- graphics(2d/3d)
- phsyics

### Component

Registered components must implement the `Properties` and `FromResources` traits.

The `Properties` trait enables serialization, deserialization, dynamic property access, and change detection.

`Properties` enable a bunch of cool behaviors, so its worth checking out the dedicated `properties.rs` example.

The `FromResources` trait determines how your component is constructed when it loads. For simple use cases you can just implement the `Default` trait (which automatically implements FromResources). The simplest registered component just needs these two derives:

```!rust
#[derive(Properties, Default)]
struct ComponentA {
    pub x: f32,
    pub y: f32,
}
```

Some components have fields that cannot (or should not) be written to scene files. These can be ignored with the #[property(ignore)] attribute. This is also generally where the `FromResources` trait comes into play.


```!rust
#[derive(Properties)]
struct ComponentB {
    pub value: String,
    #[property(ignore)]
    pub time_since_startup: Duration,
}
```

`FromResources` gives you access to your App's current ECS `Resources` when you construct your component.

### Resource

```
Res<T> : Shared borrow of an entity's component.
ResMut<T> : Unique borrow of an entity's component.
Resources : A collection of resource instances identified by their type.
```

## Compile Time

```
cargo build --release
```

is much faster then

```
cargo build (debug)
```


## spawn/despwan

world.rs
commands.rs

## Custom Game Loop

```!rust
fn game_loop() {
    ...
}

App::build()
    .set_runner(game_loop)
```
## Math

bevy use `glam` for SIMD support.
https://docs.rs/glam/0.11.0/glam/

## Misc

- Assets<T>
```
Assets<ColorMaterial>
```
- bevy::ecs/core/bundle.rs(DynamicBundle)
    - SpriteBundle
    - Camera2dBundle
- Commands.spawn().with()
- Handle { handleId, ...}
- Cameras

## Perspective Projections

- near, far : distance from the camera (>= 0)
- right-hand side rule: x -> right, y -> top, z is negative in front of the camera.
- z value transform(from (-near, -far) normalized to -1~+1) is non-linear since we want more precision close to the camera and less precision far away from the camera. 

<img src="http://learnwebgl.brown37.net/_images/non_linear_mapping.png" style="filter:invert(.9)"></img>

:::info
also the floating pointer number is more precision within -1~+1
:::

## Reference

[Perspective Projections](http://learnwebgl.brown37.net/08_projections/projections_perspective.html)

###### tags: `game dev` `rust` `bevy`