apply smooth torque rather than "teleport" when rotating player

This commit is contained in:
yuni 2024-03-31 03:09:14 +02:00
parent ac3b17badf
commit 2411430799
3 changed files with 24 additions and 26 deletions

View file

@ -25,11 +25,6 @@ impl Plugin for CameraControllerPlugin {
} }
} }
// Based on Valorant's default sensitivity, not entirely sure why it is exactly 1.0 / 180.0,
// but I'm guessing it is a misunderstanding between degrees/radians and then sticking with
// it because it felt nice.
pub const RADIANS_PER_DOT: f32 = 1.0 / 180.0;
pub fn setup_camera( pub fn setup_camera(
mut commands: Commands, mut commands: Commands,
) { ) {
@ -131,10 +126,11 @@ fn apply_input_to_player(
ion_sound_controller: Query<&AudioSink, With<audio::ComponentIonSound>>, ion_sound_controller: Query<&AudioSink, With<audio::ComponentIonSound>>,
electricmotor_sound_controller: Query<&AudioSink, With<audio::ComponentElectricMotorSound>>, electricmotor_sound_controller: Query<&AudioSink, With<audio::ComponentElectricMotorSound>>,
mut q_playercam: Query<( mut q_playercam: Query<(
&mut Transform, &Transform,
&mut actor::Engine, &mut actor::Engine,
&mut AngularVelocity, &mut AngularVelocity,
&mut LinearVelocity, &mut LinearVelocity,
&mut ExternalTorque,
), (With<actor::PlayerCamera>, Without<Camera>)>, ), (With<actor::PlayerCamera>, Without<Camera>)>,
) { ) {
let dt = time.delta_seconds(); let dt = time.delta_seconds();
@ -146,17 +142,7 @@ fn apply_input_to_player(
focused = window_result.unwrap().focused; focused = window_result.unwrap().focused;
} }
if let Ok((mut player_transform, mut engine, mut angularvelocity, mut v)) = q_playercam.get_single_mut() { if let Ok((player_transform, mut engine, mut angularvelocity, mut v, mut torque)) = q_playercam.get_single_mut() {
if angularvelocity.length_squared() > 0.0001 {
angularvelocity.x *= 0.98;
angularvelocity.y *= 0.98;
angularvelocity.z *= 0.98;
}
else {
angularvelocity.0 = Vec3::splat(0.0);
}
// Handle key input // Handle key input
let mut axis_input = Vec3::ZERO; let mut axis_input = Vec3::ZERO;
if focused { if focused {
@ -231,7 +217,7 @@ fn apply_input_to_player(
let mut play_reactionwheel_sound = false; let mut play_reactionwheel_sound = false;
let mut mouse_delta = Vec2::ZERO; let mut mouse_delta = Vec2::ZERO;
let mut pitch_yaw_rot = Vec3::ZERO; let mut pitch_yaw_rot = Vec3::ZERO;
let mouseless_sensitivity = 8.0; let mouseless_sensitivity = 40.0;
if key_input.pressed(settings.key_mouseup) { if key_input.pressed(settings.key_mouseup) {
pitch_yaw_rot[0] -= mouseless_sensitivity; pitch_yaw_rot[0] -= mouseless_sensitivity;
} }
@ -239,8 +225,7 @@ fn apply_input_to_player(
pitch_yaw_rot[0] += mouseless_sensitivity; pitch_yaw_rot[0] += mouseless_sensitivity;
} }
if key_input.pressed(settings.key_mouseleft) { if key_input.pressed(settings.key_mouseleft) {
pitch_yaw_rot[1] += mouseless_sensitivity; pitch_yaw_rot[1] += mouseless_sensitivity; }
}
if key_input.pressed(settings.key_mouseright) { if key_input.pressed(settings.key_mouseright) {
pitch_yaw_rot[1] -= mouseless_sensitivity; pitch_yaw_rot[1] -= mouseless_sensitivity;
} }
@ -263,9 +248,18 @@ fn apply_input_to_player(
} }
if pitch_yaw_rot.length_squared() > 0.0001 { if pitch_yaw_rot.length_squared() > 0.0001 {
play_reactionwheel_sound = true; play_reactionwheel_sound = true;
pitch_yaw_rot *= RADIANS_PER_DOT * settings.mouse_sensitivity; pitch_yaw_rot *= settings.mouse_sensitivity * engine.reaction_wheels;
player_transform.rotation *= Quat::from_euler(EulerRot::ZYX, torque.apply_torque(player_transform.rotation * Vec3::new(
pitch_yaw_rot[2], pitch_yaw_rot[1], pitch_yaw_rot[0]).normalize(); 2.0*pitch_yaw_rot[0], pitch_yaw_rot[1], pitch_yaw_rot[2]));
angularvelocity.0 *= 0.98;
}
else {
if angularvelocity.length_squared() > 0.0000001 {
angularvelocity.0 *= 0.98;
}
else {
angularvelocity.0 = Vec3::splat(0.0);
}
} }
// Play sound effects // Play sound effects

View file

@ -5,8 +5,8 @@ actor 0 0 0 suit
oxygen 0.008 oxygen 0.008
health 0.3 health 0.3
angularmomentum 0 0 0 angularmomentum 0 0 0
collider mesh collider capsule 2 1
thrust 1.2 1 1 1 1.5 thrust 1.2 1 1 100 1.5
rotationy 0.65 rotationy 0.65
engine monopropellant engine monopropellant
@ -134,7 +134,7 @@ actor -3300 10 0 pizzeria
actor 10 -30 20 MeteorAceGT actor 10 -30 20 MeteorAceGT
scale 5 scale 5
vehicle yes vehicle yes
thrust 70 13.7 9.4 0.5 20 thrust 70 13.7 9.4 50000 20
engine ion engine ion
collider sphere 1.5 collider sphere 1.5
camdistance 50 camdistance 50

View file

@ -706,6 +706,10 @@ fn spawn_entities(
actor.insert(actor::Player); actor.insert(actor::Player);
actor.insert(actor::PlayerCamera); actor.insert(actor::PlayerCamera);
} }
if state.is_player || state.is_vehicle {
// used to apply mouse movement to actor rotation
actor.insert(ExternalTorque::ZERO.with_persistence(false));
}
if state.is_lifeform { if state.is_lifeform {
actor.insert(actor::LifeForm::default()); actor.insert(actor::LifeForm::default());
actor.insert(actor::Suit { actor.insert(actor::Suit {