Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enable_input so input control can be toggled. #5

Merged
merged 2 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 44 additions & 35 deletions examples/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ fn setup(
// The other is a "render" player that is what is displayed to the user
// This distinction is useful for later on if you want to add multiplayer,
// where often time these two ideas are not exactly synced up
commands.spawn()
commands
.spawn()
.insert(Collider::capsule(Vec3::Y * 0.5, Vec3::Y * 1.5, 0.5))
.insert(ActiveEvents::COLLISION_EVENTS)
.insert(Velocity::zero())
Expand All @@ -63,49 +64,50 @@ fn setup(
yaw: TAU * 5.0 / 8.0,
..default()
})
.insert(FpsController {
..default()
});
commands.spawn_bundle(Camera3dBundle::default())
.insert(FpsController { ..default() });
commands
.spawn_bundle(Camera3dBundle::default())
.insert(RenderPlayer(0));

// World
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Box {
min_x: -20.0,
max_x: 20.0,
min_y: -0.25,
max_y: 0.25,
min_z: -20.0,
max_z: 20.0,
})),
material: materials.add(StandardMaterial {
base_color: Color::hex("E6EED6").unwrap(),
commands
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Box {
min_x: -20.0,
max_x: 20.0,
min_y: -0.25,
max_y: 0.25,
min_z: -20.0,
max_z: 20.0,
})),
material: materials.add(StandardMaterial {
base_color: Color::hex("E6EED6").unwrap(),
..default()
}),
transform: Transform::identity(),
..default()
}),
transform: Transform::identity(),
..default()
})
})
.insert(Collider::cuboid(20.0, 0.25, 20.0))
.insert(RigidBody::Fixed)
.insert(Transform::identity());

commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Box {
min_x: -1.0,
max_x: 1.0,
min_y: -1.0,
max_y: 1.0,
min_z: -1.0,
max_z: 1.0,
})),
material: materials.add(StandardMaterial {
base_color: Color::hex("DDE2C6").unwrap(),
commands
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Box {
min_x: -1.0,
max_x: 1.0,
min_y: -1.0,
max_y: 1.0,
min_z: -1.0,
max_z: 1.0,
})),
material: materials.add(StandardMaterial {
base_color: Color::hex("DDE2C6").unwrap(),
..default()
}),
transform: Transform::identity(),
..default()
}),
transform: Transform::identity(),
..default()
})
})
.insert(Collider::cuboid(1.0, 1.0, 1.0))
.insert(RigidBody::Fixed)
.insert(Transform::from_xyz(4.0, 1.0, 4.0));
Expand All @@ -115,14 +117,21 @@ pub fn manage_cursor(
mut windows: ResMut<Windows>,
btn: Res<Input<MouseButton>>,
key: Res<Input<KeyCode>>,
mut controllers: Query<&mut FpsController>,
) {
let window = windows.get_primary_mut().unwrap();
if btn.just_pressed(MouseButton::Left) {
window.set_cursor_lock_mode(true);
window.set_cursor_visibility(false);
for mut controller in &mut controllers {
controller.enable_input = true;
}
}
if key.just_pressed(KeyCode::Escape) {
window.set_cursor_lock_mode(false);
window.set_cursor_visibility(true);
for mut controller in &mut controllers {
controller.enable_input = false;
}
}
}
98 changes: 69 additions & 29 deletions src/controller.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
use std::f32::consts::*;

use bevy::{
math::Vec3Swizzles,
prelude::*,
};
use bevy::input::mouse::MouseMotion;
use bevy::{math::Vec3Swizzles, prelude::*};
use bevy_rapier3d::prelude::*;

pub struct FpsControllerPlugin;

impl Plugin for FpsControllerPlugin {
fn build(&self, app: &mut App) {
// TODO: these need to be sequential (exclusive system set)
app
.add_system(fps_controller_input)
app.add_system(fps_controller_input)
.add_system(fps_controller_look)
.add_system(fps_controller_move)
.add_system(fps_controller_render);
Expand Down Expand Up @@ -66,6 +62,7 @@ pub struct FpsController {
pub ground_tick: u8,
pub stop_speed: f32,
pub sensitivity: f32,
pub enable_input: bool,
pub key_forward: KeyCode,
pub key_back: KeyCode,
pub key_left: KeyCode,
Expand Down Expand Up @@ -102,6 +99,7 @@ impl Default for FpsController {
ground_tick: 0,
stop_speed: 1.0,
jump_speed: 8.5,
enable_input: true,
key_forward: KeyCode::W,
key_back: KeyCode::S,
key_left: KeyCode::A,
Expand Down Expand Up @@ -130,9 +128,12 @@ pub fn fps_controller_input(
key_input: Res<Input<KeyCode>>,
mut windows: ResMut<Windows>,
mut mouse_events: EventReader<MouseMotion>,
mut query: Query<(&FpsController, &mut FpsControllerInput)>)
{
mut query: Query<(&FpsController, &mut FpsControllerInput)>,
) {
for (controller, mut input) in query.iter_mut() {
if !controller.enable_input {
continue;
}
let window = windows.get_primary_mut().unwrap();
if window.is_focused() {
let mut mouse_delta = Vec2::ZERO;
Expand All @@ -141,10 +142,8 @@ pub fn fps_controller_input(
}
mouse_delta *= controller.sensitivity;

input.pitch = (input.pitch - mouse_delta.y).clamp(
-FRAC_PI_2 + ANGLE_EPSILON,
FRAC_PI_2 - ANGLE_EPSILON,
);
input.pitch = (input.pitch - mouse_delta.y)
.clamp(-FRAC_PI_2 + ANGLE_EPSILON, FRAC_PI_2 - ANGLE_EPSILON);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of this is kind of ugly but it's okay

input.yaw = input.yaw - mouse_delta.x;
}

Expand All @@ -160,9 +159,7 @@ pub fn fps_controller_input(
}
}

pub fn fps_controller_look(
mut query: Query<(&mut FpsController, &FpsControllerInput)>,
) {
pub fn fps_controller_look(mut query: Query<(&mut FpsController, &FpsControllerInput)>) {
for (mut controller, input) in query.iter_mut() {
controller.pitch = input.pitch;
controller.yaw = input.yaw;
Expand All @@ -173,8 +170,12 @@ pub fn fps_controller_move(
time: Res<Time>,
physics_context: Res<RapierContext>,
mut query: Query<(
Entity, &FpsControllerInput, &mut FpsController,
&Collider, &mut Transform, &mut Velocity
Entity,
&FpsControllerInput,
&mut FpsController,
&Collider,
&mut Transform,
&mut Velocity,
)>,
) {
let dt = time.delta_seconds();
Expand All @@ -183,7 +184,7 @@ pub fn fps_controller_move(
if input.fly {
controller.move_mode = match controller.move_mode {
MoveMode::Noclip => MoveMode::Ground,
MoveMode::Ground => MoveMode::Noclip
MoveMode::Ground => MoveMode::Noclip,
}
}

Expand Down Expand Up @@ -223,21 +224,32 @@ pub fn fps_controller_move(
// Capsule cast downwards to find ground
// Better than single raycast as it handles when you are near the edge of a surface
let mut ground_hit = None;
let cast_capsule = Collider::capsule(capsule.segment.a.into(), capsule.segment.b.into(), capsule.radius * 1.0625);
let cast_capsule = Collider::capsule(
capsule.segment.a.into(),
capsule.segment.b.into(),
capsule.radius * 1.0625,
);
let cast_velocity = Vec3::Y * -1.0;
let max_distance = 0.125;
// Avoid self collisions
let groups = QueryFilter::default().exclude_rigid_body(entity);

if let Some((_handle, hit)) = physics_context.cast_shape(
position, orientation, cast_velocity, &cast_capsule, max_distance, groups,
position,
orientation,
cast_velocity,
&cast_capsule,
max_distance,
groups,
) {
ground_hit = Some(hit);
}

let mut wish_direction = input.movement.z * controller.forward_speed * forward + input.movement.x * controller.side_speed * right;
let mut wish_direction = input.movement.z * controller.forward_speed * forward
+ input.movement.x * controller.side_speed * right;
let mut wish_speed = wish_direction.length();
if wish_speed > 1e-6 { // Avoid division by zero
if wish_speed > 1e-6 {
// Avoid division by zero
wish_direction /= wish_speed; // Effectively normalize, avoid length computation twice
}

Expand All @@ -253,14 +265,26 @@ pub fn fps_controller_move(
// Only apply friction after at least one tick, allows b-hopping without losing speed
if controller.ground_tick >= 1 {
if lateral_speed > controller.friction_cutoff {
friction(lateral_speed, controller.friction, controller.stop_speed, dt, &mut end_velocity);
friction(
lateral_speed,
controller.friction,
controller.stop_speed,
dt,
&mut end_velocity,
);
} else {
end_velocity.x = 0.0;
end_velocity.z = 0.0;
}
end_velocity.y = 0.0;
}
accelerate(wish_direction, wish_speed, controller.accel, dt, &mut end_velocity);
accelerate(
wish_direction,
wish_speed,
controller.accel,
dt,
&mut end_velocity,
);
if input.jump {
// Simulate one update ahead, since this is an instant velocity change
start_velocity.y = controller.jump_speed;
Expand All @@ -271,7 +295,13 @@ pub fn fps_controller_move(
} else {
controller.ground_tick = 0;
wish_speed = f32::min(wish_speed, controller.air_speed_cap);
accelerate(wish_direction, wish_speed, controller.air_acceleration, dt, &mut end_velocity);
accelerate(
wish_direction,
wish_speed,
controller.air_acceleration,
dt,
&mut end_velocity,
);
end_velocity.y -= controller.gravity * dt;
let air_speed = end_velocity.xz().length();
if air_speed > controller.max_air_speed {
Expand Down Expand Up @@ -313,7 +343,9 @@ fn friction(lateral_speed: f32, friction: f32, stop_speed: f32, dt: f32, velocit
fn accelerate(wish_dir: Vec3, wish_speed: f32, accel: f32, dt: f32, velocity: &mut Vec3) {
let velocity_projection = Vec3::dot(*velocity, wish_dir);
let add_speed = wish_speed - velocity_projection;
if add_speed <= 0.0 { return; }
if add_speed <= 0.0 {
return;
}

let accel_speed = f32::min(accel * wish_speed * dt, add_speed);
let wish_direction = wish_dir * accel_speed;
Expand All @@ -322,7 +354,11 @@ fn accelerate(wish_dir: Vec3, wish_speed: f32, accel: f32, dt: f32, velocity: &m
}

fn get_pressed(key_input: &Res<Input<KeyCode>>, key: KeyCode) -> f32 {
if key_input.pressed(key) { 1.0 } else { 0.0 }
if key_input.pressed(key) {
1.0
} else {
0.0
}
}

fn get_axis(key_input: &Res<Input<KeyCode>>, key_pos: KeyCode, key_neg: KeyCode) -> f32 {
Expand All @@ -337,7 +373,10 @@ fn get_axis(key_input: &Res<Input<KeyCode>>, key_pos: KeyCode, key_neg: KeyCode)
// ╚═╝ ╚═╝╚══════╝╚═╝ ╚═══╝╚═════╝ ╚══════╝╚═╝ ╚═╝

pub fn fps_controller_render(
logical_query: Query<(&Transform, &Collider, &FpsController, &LogicalPlayer), With<LogicalPlayer>>,
logical_query: Query<
(&Transform, &Collider, &FpsController, &LogicalPlayer),
With<LogicalPlayer>,
>,
mut render_query: Query<(&mut Transform, &RenderPlayer), Without<LogicalPlayer>>,
) {
// TODO: inefficient O(N^2) loop, use hash map?
Expand All @@ -349,7 +388,8 @@ pub fn fps_controller_render(
}
// TODO: let this be more configurable
let camera_height = capsule.segment().b().y + capsule.radius() * 0.75;
render_transform.translation = logical_transform.translation + Vec3::Y * camera_height;
render_transform.translation =
logical_transform.translation + Vec3::Y * camera_height;
render_transform.rotation = look_quat(controller.pitch, controller.yaw);
}
}
Expand Down