#camera #scroll #2d #bevy

bevy_scrolling_2d_camera

A simple 2d camera plugin for bevy supporting scrolling with right mouse dragging

5 unstable releases

0.3.0 Aug 10, 2024
0.2.0 Jun 20, 2024
0.1.2 Jun 8, 2024
0.1.1 Jun 4, 2024
0.1.0 Jun 4, 2024

#734 in Game dev

Download history 208/week @ 2024-05-31 202/week @ 2024-06-07 117/week @ 2024-06-14 41/week @ 2024-06-21 2/week @ 2024-06-28 8/week @ 2024-07-05 115/week @ 2024-08-09 7/week @ 2024-08-16

122 downloads per month

MIT license

11KB
175 lines

Bevy scrolling 2d camera plugin

Here is a simple 2d camera plugin for bevy engine, supporting scrolling with mouse right dragging and zooming with mouse wheel.

It is suitable for RTS or Simulation 2D games.

Check the cargo project in example folder to see how it works.

bevy bevy_scrolling_2d_camera
0.14.1 0.3.0
0.13 ^0.1.*

Idea of the dragging function

By pressing down the right mouse button, the plugin records the cursor position. The vector from this record position to the current cursor position is used as the camera velocity. The camera updates its translation based on the calculated veclocity.

Things added to your project with this plugin

This plugin is minimum, but it adds the following things to your project.

  1. A resource holding the entity of the camera.
#[derive(Resource)]
pub struct ScrollingCamera{
    pub entity: Option<Entity>,
}
  1. A resource storing the velocity of the camera
#[derive(Resource)]
pub struct CameraVelocity{
    pub v: Vec3,
}
  1. A resource marking the cursor position when pressing down the right mouse button.
#[derive(Resource)]
pub struct CapturedMouseRightClickPosition{
    pub pos: Option<Vec2>,
}
  1. A resource confining the zoom scale. (Added from 0.2.0)
#[derive(Resource)]
pub struct ZoomBound{
    pub max: f32,
    pub min: f32,
}
  1. A state enum marking the current state of the camera.
#[derive(States, Debug, Clone, Copy, Eq, PartialEq, Hash, Default)]
pub enum CameraState {
    #[default]
    Idling,
    Scrolling,
}
  1. Four updating systems to control the movement of the camera.
pub fn camera_move(
    mut query: Query<&mut Transform, With<Camera>>,
    velocity: Res<CameraVelocity>,
    time: Res<Time>,
    scrolling_camera: Res<ScrollingCamera>,
) {/*...*/}
pub fn capture_mouse_right_click_for_scrolling(
    mut commands: Commands,
    windows: Query<&Window>,
    input:  Res<ButtonInput<MouseButton>>,
    mut click_pos : ResMut<CapturedMouseRightClickPosition>,
) {/*...*/}
pub fn control_camera_movment(
    mut commands: Commands,
    window_query: Query<&Window>,
    camera_query: Query<(&Camera, &GlobalTransform)>,
    input:  Res<ButtonInput<MouseButton>>,
    click_pos : Res<CapturedMouseRightClickPosition>,
    mut velocity: ResMut<CameraVelocity>,
    mut gizmos: Gizmos,
    scrolling_camera: Res<ScrollingCamera>,
){/*...*/}
pub fn camera_zoom(
    mut mouse_wheel_event: EventReader<MouseWheel>,
    mut query: Query<&mut OrthographicProjection>,
    time: Res<Time>,
    scrolling_camera: Res<ScrollingCamera>,
) {/*...*/}

Dependencies

~23MB
~424K SLoC