From 6a96d6c3d3ef68d2b956b553d4909c7c23514ed4 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Thu, 15 Feb 2024 19:39:09 +0100 Subject: [PATCH] fun --- kubi/src/client_physics.rs | 16 ++++++++++++---- kubi/src/player_controller.rs | 12 ++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/kubi/src/client_physics.rs b/kubi/src/client_physics.rs index f23ba00..f2d9416 100644 --- a/kubi/src/client_physics.rs +++ b/kubi/src/client_physics.rs @@ -31,7 +31,7 @@ pub struct ClPhysicsActor { pub velocity: Vec3, pub terminal_velocity: f32, //TODO: this should be configurable per block - pub friction_agains_ground: f32, + pub ground_friction: f32, pub gravity_scale: f32, flag_ground: bool, flag_collision: bool, @@ -56,7 +56,7 @@ impl Default for ClPhysicsActor { forces: Vec3::ZERO, velocity: Vec3::ZERO, terminal_velocity: 40., - friction_agains_ground: 0.5, + ground_friction: 10., gravity_scale: 1., flag_ground: false, flag_collision: false, @@ -114,7 +114,8 @@ pub fn update_client_physics_late( //get grid-aligned pos and blocks let actor_block_pos = actor_position.floor().as_ivec3(); let actor_block = world.get_block(actor_block_pos); - let actor_block_below = world.get_block(actor_block_pos + IVec3::NEG_Y); + let actor_block_pos_slightly_below = (actor_position + Vec3::NEG_Y * 0.01).floor().as_ivec3(); + let actor_block_below = world.get_block(actor_block_pos_slightly_below); //update flags actor.flag_collision = actor_block.is_solid(); @@ -139,7 +140,7 @@ pub fn update_client_physics_late( //HACK: for now, just stop the vertical velocity if on ground altogether, //as we don't have proper collision velocity resolution yet (we need to compute dot product or sth) if actor.flag_ground { - actor.velocity.y = 0.; + actor.velocity.y = actor.velocity.y.max(0.); } } @@ -147,6 +148,13 @@ pub fn update_client_physics_late( actor_position += actor.velocity * dt.0.as_secs_f32(); actor_position += actor.offset; transform.0 = Mat4::from_scale_rotation_translation(scale, rotation.normalize(), actor_position); + + //Apply friction + // if actor.flag_ground { + // let actor_velocity = actor.velocity; + // let actor_friction = actor.ground_friction; + // actor.velocity -= (actor_velocity * actor_friction * dt.0.as_secs_f32()) * vec3(1., 0., 1.); + // } } // for (_, mut transform) in (&controllers, &mut transforms).iter() { // let (scale, rotation, mut translation) = transform.0.to_scale_rotation_translation(); diff --git a/kubi/src/player_controller.rs b/kubi/src/player_controller.rs index 3bd1ab8..aee4a49 100644 --- a/kubi/src/player_controller.rs +++ b/kubi/src/player_controller.rs @@ -2,7 +2,7 @@ use glam::{vec3, EulerRot, Mat4, Quat, Vec2, Vec2Swizzles, Vec3, Vec3Swizzles}; use shipyard::{track, Component, Get, IntoIter, IntoWithId, IntoWorkload, Unique, UniqueView, View, ViewMut, Workload}; use winit::keyboard::KeyCode; use std::f32::consts::PI; -use crate::{client_physics::ClPhysicsActor, delta_time::DeltaTime, input::{Inputs, RawKbmInputState}, settings::GameSettings, transform::Transform}; +use crate::{client_physics::ClPhysicsActor, delta_time::DeltaTime, input::{Inputs, PrevInputs, RawKbmInputState}, settings::GameSettings, transform::Transform}; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum PlayerControllerType { @@ -62,10 +62,12 @@ fn update_movement( mut transforms: ViewMut, mut actors: ViewMut, inputs: UniqueView, + prev_inputs: UniqueView, dt: UniqueView, ) { - if (inputs.movement == Vec2::ZERO) && !inputs.jump { return } - let movement = inputs.movement.extend(inputs.jump as u32 as f32).xzy(); + let jump = inputs.jump && !prev_inputs.0.jump; + if (inputs.movement == Vec2::ZERO) && !jump { return } + let movement = inputs.movement.extend(jump as u32 as f32).xzy(); for (id, (ctl, mut transform)) in (&controllers, &mut transforms).iter().with_id() { let (scale, rotation, mut translation) = transform.0.to_scale_rotation_translation(); let rotation_norm = rotation.normalize(); @@ -78,6 +80,7 @@ fn update_movement( }, PlayerControllerType::FpsCtl => { let mut actor = (&mut actors).get(id).unwrap(); + let actor_on_ground = actor.on_ground(); let euler = rotation_norm.to_euler(EulerRot::YZX); let right = Vec2::from_angle(-euler.0).extend(0.).xzy(); @@ -86,7 +89,8 @@ fn update_movement( actor.apply_force(ctl.speed * ( (forward * movement.z) + (right * movement.x) + - (Vec3::Y * movement.y) + //TODO: remove hardcoded jump force + (Vec3::Y * movement.y * 125. * (actor_on_ground as u8 as f32)) )); // translation += forward * movement.z * ctl.speed * dt.0.as_secs_f32();