Giter Club home page Giter Club logo

smooth-bevy-cameras's Introduction

smooth-bevy-cameras

crates.io docs.rs

A collection of exponentially-smoothed camera controllers for the Bevy Engine.

Look Transform

All controllers are based on a LookTransform component, which is just an eye point that looks at a target point. By modifying this component, the scene graph Transform will automatically be synchronized.

Any entities with {Transform, LookTransform, Smoother} components will automatically have their Transform smoothed. Smoothing will have no effect on the LookTransform, only the final Transform in the scene graph.

use bevy::prelude::*;
use smooth_bevy_cameras::{LookTransform, LookTransformBundle, LookTransformPlugin, Smoother};

fn main() {
    App::build()
        .add_plugins(DefaultPlugins)
        // Enables the system that synchronizes your `Transform`s and `LookTransform`s.
        .add_plugin(LookTransformPlugin)
        .add_startup_system(setup.system())
        .add_system(move_camera_system.system());
}

fn setup(mut commands: Commands) {
    let eye = Vec3::default();
    let target = Vec3::default();

    commands
        .spawn_bundle(LookTransformBundle {
            transform: LookTransform { eye, target },
            smoother: Smoother::new(0.9), // Value between 0.0 and 1.0, higher is smoother.
        })
        .insert_bundle(PerspectiveCameraBundle<C>::default());

}

fn move_camera_system(mut cameras: Query<&mut LookTransform>) {
    // Later, another system will update the `Transform` and apply smoothing automatically.
    for mut c in cameras.iter_mut() { c.target += Vec3::new(1.0, 1.0, 1.0); }
}

Look Angles

When implementing a camera controller, it's often useful to work directly with the angles (pitch and yaw) of your look direction. You can do this with the LookAngles type:

use bevy::prelude::*;
use smooth_bevy_cameras::{
    LookAngles,
    LookTransform
};

fn look_angles(mut transform: LookTransform, delta: Vec2) {
    let mut angles = LookAngles::from_vector(transform.look_direction());
    angles.add_pitch(delta.y);
    angles.add_yaw(delta.x);
    transform.target = transform.target + 1.0 * transform.radius() * angles.unit_vector();
}

This is how the built-in controllers implement rotation controls.

Built-In Controllers

These plugins depend on the LookTransformPlugin:

  • FpsCameraPlugin + FpsCameraBundle
    • WASD: Translate on the XZ plane
    • Shift/Space: Translate along the Y axis
    • Mouse: Rotate camera
  • OrbitCameraPlugin + OrbitCameraBundle
    • CTRL + mouse drag: Rotate camera
    • Right mouse drag: Pan camera
    • Mouse wheel: Zoom
  • UnrealCameraPlugin + UnrealCameraBundle
    • Left mouse drag: Locomotion
    • Right mouse drag: Rotate camera
    • Left and Right mouse drag: Pan camera

License: MIT

smooth-bevy-cameras's People

Contributors

bonsairobo avatar scrblue avatar blackphlox avatar dtaralla avatar rivertam avatar dekuraan avatar rezural avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.