1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! A simple plugin and bundle for a basic flying camera in Bevy.
//! Keybinds are identical to Minecraft:
//! - W / A / S / D - Move along the horizontal plane
//! - Shift - Move downward
//! - Space - Move upward
//!
//! # Example
//! ```rust
//! use bevy::prelude::*;
//! use bevy_fly_camera::{FlyCamera, FlyCameraPlugin};
//!
//! fn setup(mut commands: Commands) {
//!		commands.spawn(FlyCamera::default());
//! }
//! fn main() {
//!		App::build()
//! 		.add_default_plugins()
//! 		.add_startup_system(setup.system())
//! 		.add_plugin(FlyCameraPlugin)
//! 		.run();
//! }
//! ```
//!
//! There's also a basic piece of example code included in `/examples/basic.rs`
use bevy::input::mouse::MouseMotion;
use bevy::prelude::*;
use bevy_render::camera::{Camera, PerspectiveProjection, VisibleEntities};
use bevy_render::render_graph::base;

/// A set of options for initializing a FlyCamera.
pub struct FlyCameraOptions {
	/// The speed the FlyCamera moves at.
	pub speed: f32,
	/// The sensitivity of the FlyCamera's motion based on mouse movement.
	pub sensitivity: f32,
	/// The current pitch of the FlyCamera. This value is always up-to-date, enforced by [FlyCameraPlugin](struct.FlyCameraPlugin.html)
	pub pitch: f32,
	/// The current pitch of the FlyCamera. This value is always up-to-date, enforced by [FlyCameraPlugin](struct.FlyCameraPlugin.html)
	pub yaw: f32,
}
impl Default for FlyCameraOptions {
	fn default() -> Self {
		Self {
			speed: 10.0,
			sensitivity: 3.0,
			pitch: 0.0,
			yaw: 0.0,
		}
	}
}

/**
The FlyCamera bundle.
Spawn this to instantiate a flying camera.

```
fn setup(mut commands: Commands) {
	commands.spawn(FlyCamera::default());
}
```

This is based off [`Camera3dComponents`](https://docs.rs/bevy/0.1.2/bevy/prelude/struct.Camera3dComponents.html) in the base package, with added options and systems.
**/
#[derive(Bundle)]
pub struct FlyCamera {
	pub options: FlyCameraOptions,
	pub camera: Camera,
	pub perspective_projection: PerspectiveProjection,
	pub visible_entities: VisibleEntities,
	pub transform: Transform,
	pub translation: Translation,
	pub rotation: Rotation,
	pub scale: Scale,
}

impl Default for FlyCamera {
	fn default() -> Self {
		Self {
			options: FlyCameraOptions::default(),
			camera: Camera {
				name: Some(base::camera::CAMERA3D.to_string()),
				..Default::default()
			},
			perspective_projection: Default::default(),
			visible_entities: Default::default(),
			transform: Default::default(),
			translation: Default::default(),
			rotation: Default::default(),
			scale: Default::default(),
		}
	}
}

fn forward_vector(rotation: &Rotation) -> Vec3 {
	rotation.mul_vec3(Vec3::unit_z()).normalize()
}

fn forward_walk_vector(rotation: &Rotation) -> Vec3 {
	let f = forward_vector(rotation);
	let f_flattened = Vec3::new(f.x(), 0.0, f.z()).normalize();
	f_flattened
}

fn strafe_vector(rotation: &Rotation) -> Vec3 {
	// Rotate it 90 degrees to get the strafe direction
	Rotation::from_rotation_y(90.0f32.to_radians())
		.mul_vec3(forward_walk_vector(rotation))
		.normalize()
}

fn movement_axis(
	input: &Res<Input<KeyCode>>,
	plus: KeyCode,
	minus: KeyCode,
) -> f32 {
	let mut axis = 0.0;
	if input.pressed(plus) {
		axis += 1.0;
	}
	if input.pressed(minus) {
		axis -= 1.0;
	}
	axis
}

fn camera_movement_system(
	time: Res<Time>,
	keyboard_input: Res<Input<KeyCode>>,
	mut query: Query<(&FlyCameraOptions, &mut Translation, &Rotation)>,
) {
	let axis_h = movement_axis(&keyboard_input, KeyCode::D, KeyCode::A);
	let axis_v = movement_axis(&keyboard_input, KeyCode::S, KeyCode::W);

	let axis_float =
		movement_axis(&keyboard_input, KeyCode::Space, KeyCode::LShift);

	for (options, mut translation, rotation) in &mut query.iter() {
		let delta_f = forward_walk_vector(rotation)
			* axis_v
			* options.speed
			* time.delta_seconds;

		let delta_strafe =
			strafe_vector(rotation) * axis_h * options.speed * time.delta_seconds;

		let delta_float =
			Vec3::unit_y() * axis_float * options.speed * time.delta_seconds;

		translation.0 += delta_f + delta_strafe + delta_float;
		// println!("{:#?}", camera.projection_matrix);
	}
}

#[derive(Default)]
struct State {
	mouse_motion_event_reader: EventReader<MouseMotion>,
}

fn mouse_motion_system(
	time: Res<Time>,
	mut state: ResMut<State>,
	mouse_motion_events: Res<Events<MouseMotion>>,
	mut query: Query<(&mut FlyCameraOptions, &mut Rotation)>,
) {
	let mut delta: Vec2 = Vec2::zero();
	for event in state.mouse_motion_event_reader.iter(&mouse_motion_events) {
		delta += event.delta;
	}
	if delta == Vec2::zero() {
		return;
	}

	for (mut options, mut rotation) in &mut query.iter() {
		options.yaw -= delta.x() * options.sensitivity * time.delta_seconds;
		options.pitch += delta.y() * options.sensitivity * time.delta_seconds;

		if options.pitch > 89.9 {
			options.pitch = 89.9;
		}
		if options.pitch < -89.9 {
			options.pitch = -89.9;
		}
		// println!("pitch: {}, yaw: {}", options.pitch, options.yaw);

		let yaw_radians = options.yaw.to_radians();
		let pitch_radians = options.pitch.to_radians();

		rotation.0 = Quat::from_axis_angle(Vec3::unit_y(), yaw_radians)
			* Quat::from_axis_angle(-Vec3::unit_x(), pitch_radians);
	}
}

/**
Include this plugin to add the systems for the FlyCamera bundle.

```
fn main() {
	App::build().add_plugin(FlyCameraPlugin);
}
```

**/

pub struct FlyCameraPlugin;

impl Plugin for FlyCameraPlugin {
	fn build(&self, app: &mut AppBuilder) {
		app
			.init_resource::<State>()
			.add_system(camera_movement_system.system())
			.add_system(mouse_motion_system.system());
	}
}