Compare commits

...

3 commits

Author SHA1 Message Date
griffi-gh 6a96d6c3d3 fun 2024-02-15 19:39:09 +01:00
griffi-gh 778c2b279e use force for plr 2024-02-15 15:51:06 +01:00
griffi-gh 89ccd595ac fps ctl test impl 2024-02-15 14:49:41 +01:00
5 changed files with 423 additions and 351 deletions

View file

@ -52,6 +52,14 @@ pub fn generate_world(chunk_position: IVec3, seed: u64) -> (BlockData, Vec<Queue
} }
}; };
//STICK
if chunk_position.x == 0 && chunk_position.y == 5 {
for z in 0..CHUNK_SIZE {
blocks[0][0][z] = Block::Stone;
}
}
//
let mut height_noise = FastNoise::seeded(seed); let mut height_noise = FastNoise::seeded(seed);
height_noise.set_fractal_type(FractalType::FBM); height_noise.set_fractal_type(FractalType::FBM);
height_noise.set_fractal_octaves(4); height_noise.set_fractal_octaves(4);

View file

@ -25,12 +25,14 @@ impl Default for GlobalClPhysicsConfig {
//XXX: maybe a capsule? (or configurable hull?) //XXX: maybe a capsule? (or configurable hull?)
#[derive(Component)] #[derive(Component)]
pub struct ClPhysicsActor { pub struct ClPhysicsActor {
pub disable: bool,
pub offset: Vec3, pub offset: Vec3,
pub forces: Vec3, pub forces: Vec3,
pub velocity: Vec3, pub velocity: Vec3,
pub terminal_velocity: f32, pub terminal_velocity: f32,
//TODO: this should be configurable per block //TODO: this should be configurable per block
pub friction_agains_ground: f32, pub ground_friction: f32,
pub gravity_scale: f32,
flag_ground: bool, flag_ground: bool,
flag_collision: bool, flag_collision: bool,
} }
@ -49,11 +51,13 @@ impl Default for ClPhysicsActor {
fn default() -> Self { fn default() -> Self {
Self { Self {
//HACK: for player //HACK: for player
disable: false,
offset: vec3(0., 1.5, 0.), offset: vec3(0., 1.5, 0.),
forces: Vec3::ZERO, forces: Vec3::ZERO,
velocity: Vec3::ZERO, velocity: Vec3::ZERO,
terminal_velocity: 40., terminal_velocity: 40.,
friction_agains_ground: 0.5, ground_friction: 10.,
gravity_scale: 1.,
flag_ground: false, flag_ground: false,
flag_collision: false, flag_collision: false,
} }
@ -93,6 +97,11 @@ pub fn update_client_physics_late(
dt: UniqueView<DeltaTime>, dt: UniqueView<DeltaTime>,
) { ) {
for (mut actor, mut transform) in (&mut actors, &mut transforms).iter() { for (mut actor, mut transform) in (&mut actors, &mut transforms).iter() {
if actor.disable {
actor.forces = Vec3::ZERO;
continue;
}
//apply forces //apply forces
let actor_forces = actor.forces; let actor_forces = actor.forces;
actor.velocity += (actor_forces + conf.gravity) * dt.0.as_secs_f32(); actor.velocity += (actor_forces + conf.gravity) * dt.0.as_secs_f32();
@ -105,7 +114,8 @@ pub fn update_client_physics_late(
//get grid-aligned pos and blocks //get grid-aligned pos and blocks
let actor_block_pos = actor_position.floor().as_ivec3(); let actor_block_pos = actor_position.floor().as_ivec3();
let actor_block = world.get_block(actor_block_pos); 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 //update flags
actor.flag_collision = actor_block.is_solid(); actor.flag_collision = actor_block.is_solid();
@ -130,14 +140,21 @@ pub fn update_client_physics_late(
//HACK: for now, just stop the vertical velocity if on ground altogether, //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) //as we don't have proper collision velocity resolution yet (we need to compute dot product or sth)
if actor.flag_ground { if actor.flag_ground {
actor.velocity.y = 0.; actor.velocity.y = actor.velocity.y.max(0.);
} }
} }
//Apply velocity //Apply velocity
actor_position += actor.velocity * dt.0.as_secs_f32(); actor_position += actor.velocity * dt.0.as_secs_f32();
actor_position += actor.offset; actor_position += actor.offset;
transform.0 = Mat4::from_scale_rotation_translation(scale, rotation, actor_position); 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() { // for (_, mut transform) in (&controllers, &mut transforms).iter() {
// let (scale, rotation, mut translation) = transform.0.to_scale_rotation_translation(); // let (scale, rotation, mut translation) = transform.0.to_scale_rotation_translation();

View file

@ -187,7 +187,7 @@ fn update_input_state (
inputs.look += raw_inputs.mouse_delta.as_vec2(); inputs.look += raw_inputs.mouse_delta.as_vec2();
inputs.action_a |= raw_inputs.button_state[0]; inputs.action_a |= raw_inputs.button_state[0];
inputs.action_b |= raw_inputs.button_state[1]; inputs.action_b |= raw_inputs.button_state[1];
inputs.jump |= raw_inputs.button_state[2]; inputs.jump |= raw_inputs.keyboard_state.contains(KeyCode::Space as u32);
} }
fn update_input_state_gamepad ( fn update_input_state_gamepad (

View file

@ -57,7 +57,7 @@ use events::{
player_actions::generate_move_events, player_actions::generate_move_events,
}; };
use input::{init_input, process_inputs}; use input::{init_input, process_inputs};
use player_controller::update_player_controllers; use player_controller::{debug_switch_ctl_type, update_player_controllers};
use rendering::{ use rendering::{
Renderer, Renderer,
RenderTarget, RenderTarget,
@ -133,6 +133,7 @@ fn update() -> Workload {
update_loaded_world_around_player, update_loaded_world_around_player,
).into_sequential_workload().run_if(is_ingame_or_loading), ).into_sequential_workload().run_if(is_ingame_or_loading),
( (
debug_switch_ctl_type,
update_player_controllers, update_player_controllers,
update_client_physics_late, update_client_physics_late,
generate_move_events, generate_move_events,

View file

@ -1,7 +1,8 @@
use glam::{Vec3, Mat4, Quat, EulerRot, Vec2}; use glam::{vec3, EulerRot, Mat4, Quat, Vec2, Vec2Swizzles, Vec3, Vec3Swizzles};
use shipyard::{Component, View, ViewMut, IntoIter, UniqueView, Workload, IntoWorkload, track}; use shipyard::{track, Component, Get, IntoIter, IntoWithId, IntoWorkload, Unique, UniqueView, View, ViewMut, Workload};
use winit::keyboard::KeyCode;
use std::f32::consts::PI; use std::f32::consts::PI;
use crate::{transform::Transform, input::Inputs, settings::GameSettings, delta_time::DeltaTime}; use crate::{client_physics::ClPhysicsActor, delta_time::DeltaTime, input::{Inputs, PrevInputs, RawKbmInputState}, settings::GameSettings, transform::Transform};
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PlayerControllerType { pub enum PlayerControllerType {
@ -18,7 +19,7 @@ pub struct PlayerController {
impl PlayerController { impl PlayerController {
pub const DEFAULT_FLY_CAM: Self = Self { pub const DEFAULT_FLY_CAM: Self = Self {
control_type: PlayerControllerType::FlyCam, control_type: PlayerControllerType::FlyCam,
speed: 30., speed: 50.,
}; };
pub const DEFAULT_FPS_CTL: Self = Self { pub const DEFAULT_FPS_CTL: Self = Self {
@ -59,16 +60,61 @@ fn update_look(
fn update_movement( fn update_movement(
controllers: View<PlayerController>, controllers: View<PlayerController>,
mut transforms: ViewMut<Transform, track::All>, mut transforms: ViewMut<Transform, track::All>,
mut actors: ViewMut<ClPhysicsActor>,
inputs: UniqueView<Inputs>, inputs: UniqueView<Inputs>,
prev_inputs: UniqueView<PrevInputs>,
dt: UniqueView<DeltaTime>, dt: UniqueView<DeltaTime>,
) { ) {
if inputs.movement == Vec2::ZERO { return } let jump = inputs.jump && !prev_inputs.0.jump;
let movement = inputs.movement * dt.0.as_secs_f32(); if (inputs.movement == Vec2::ZERO) && !jump { return }
for (ctl, mut transform) in (&controllers, &mut transforms).iter() { 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 (scale, rotation, mut translation) = transform.0.to_scale_rotation_translation();
let rotation_norm = rotation.normalize(); let rotation_norm = rotation.normalize();
translation += (rotation_norm * Vec3::NEG_Z).normalize() * movement.y * ctl.speed; match ctl.control_type {
translation += (rotation_norm * Vec3::X).normalize() * movement.x * ctl.speed; PlayerControllerType::FlyCam => {
translation += (rotation_norm * Vec3::NEG_Z).normalize() * movement.z * ctl.speed * dt.0.as_secs_f32();
translation += (rotation_norm * Vec3::X).normalize() * movement.x * ctl.speed * dt.0.as_secs_f32();
translation += Vec3::Y * movement.y * ctl.speed * dt.0.as_secs_f32();
transform.0 = Mat4::from_scale_rotation_translation(scale, rotation_norm, translation); transform.0 = Mat4::from_scale_rotation_translation(scale, rotation_norm, translation);
},
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();
let forward = Vec2::from_angle(-(euler.0 + PI/2.)).extend(0.).xzy();
actor.apply_force(ctl.speed * (
(forward * movement.z) +
(right * movement.x) +
//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();
// translation += right * movement.x * ctl.speed * dt.0.as_secs_f32();
// translation += Vec3::Y * movement.y * ctl.speed * dt.0.as_secs_f32();
// transform.0 = Mat4::from_scale_rotation_translation(scale, rotation_norm, translation);
}
}
}
}
pub fn debug_switch_ctl_type(
mut controllers: ViewMut<PlayerController>,
mut actors: ViewMut<ClPhysicsActor>,
kbm_state: UniqueView<RawKbmInputState>,
) {
for (mut controller, mut actor) in (&mut controllers, &mut actors).iter() {
if kbm_state.keyboard_state.contains(KeyCode::F4 as u32) {
*controller = PlayerController::DEFAULT_FPS_CTL;
actor.disable = false;
} else if kbm_state.keyboard_state.contains(KeyCode::F5 as u32) {
*controller = PlayerController::DEFAULT_FLY_CAM;
actor.disable = true;
}
} }
} }