Merge pull request #14 from griffi-gh/client-physics

Client physics (wip)
This commit is contained in:
griffi-gh 2024-02-17 23:59:12 +03:00 committed by GitHub
commit 4cae827233
9 changed files with 1069 additions and 839 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);
height_noise.set_fractal_type(FractalType::FBM);
height_noise.set_fractal_octaves(4);

View file

@ -1,49 +1,185 @@
//TODO client-side physics
//TODO move this to shared
use glam::{Mat4, Vec3};
use kubi_shared::transform::Transform;
use glam::{vec3, IVec3, Mat4, Vec3, Vec3Swizzles};
use shipyard::{track, AllStoragesView, Component, IntoIter, Unique, UniqueView, View, ViewMut};
use crate::delta_time::DeltaTime;
use kubi_shared::{block::{Block, CollisionType}, transform::Transform};
use crate::{delta_time::DeltaTime, world::ChunkStorage};
#[derive(Unique)]
pub struct GlobalClPhysicsConfig {
pub gravity: Vec3,
///XXX: currenly unused:
pub iterations: usize,
}
impl Default for GlobalClPhysicsConfig {
fn default() -> Self {
Self {
gravity: Vec3::new(0., -9.8, 0.),
iterations: 10,
}
}
}
//TODO: actors should be represented by a vertical line, not a point.
//XXX: maybe a capsule? (or configurable hull?)
//TODO: per block friction
#[derive(Component)]
pub struct ClPhysicsActor {
pub disable: bool,
pub offset: Vec3,
pub forces: Vec3,
pub frame_velocity: Vec3,
pub velocity: Vec3,
pub terminal_velocity: f32,
//TODO: this should be configurable per block
pub friction_agains_ground: f32,
pub decel: Vec3,
pub gravity_scale: f32,
pub max_velocity: (Option<f32>, Option<f32>, Option<f32>),
pub hack_xz_circular: bool,
flag_ground: bool,
flag_collision: bool,
}
impl ClPhysicsActor {
pub fn apply_force(&mut self, force: Vec3) {
self.forces += force;
}
pub fn add_frame_velocity(&mut self, force: Vec3) {
self.frame_velocity += force;
}
pub fn on_ground(&self) -> bool {
self.flag_ground
}
}
impl Default for ClPhysicsActor {
fn default() -> Self {
Self {
//HACK: for player
disable: false,
offset: vec3(0., 1.5, 0.),
forces: Vec3::ZERO,
frame_velocity: Vec3::ZERO,
velocity: Vec3::ZERO,
terminal_velocity: 40.,
friction_agains_ground: 0.5,
//constant deceleration, in ratio per second. e.g. value of 1 should stop the actor in 1 second.
decel: vec3(1., 0., 1.),
gravity_scale: 1.,
max_velocity: (Some(20.), None, Some(20.)),
hack_xz_circular: true,
flag_ground: false,
flag_collision: false,
}
}
}
trait BlockCollisionExt {
fn collision_type(&self) -> CollisionType;
fn is_solid(&self) -> bool {
self.collision_type() == CollisionType::Solid
}
}
impl BlockCollisionExt for Option<Block> {
fn collision_type(&self) -> CollisionType {
self.unwrap_or(Block::Air).descriptor().collision
}
}
impl BlockCollisionExt for Block {
fn collision_type(&self) -> CollisionType {
self.descriptor().collision
}
}
pub fn init_client_physics(
storages: AllStoragesView,
) {
storages.add_unique(GlobalClPhysicsConfig {
gravity: Vec3::new(0., -9.8, 0.),
});
storages.add_unique(GlobalClPhysicsConfig::default());
}
pub fn update_client_physics_late(
controllers: View<ClPhysicsActor>,
mut actors: ViewMut<ClPhysicsActor>,
mut transforms: ViewMut<Transform, track::All>,
conf: UniqueView<GlobalClPhysicsConfig>,
world: UniqueView<ChunkStorage>,
dt: UniqueView<DeltaTime>,
phy_conf: UniqueView<GlobalClPhysicsConfig>,
) {
for (mut actor, mut transform) in (&mut actors, &mut transforms).iter() {
if actor.disable {
actor.forces = Vec3::ZERO;
continue;
}
//apply forces
let actor_forces = actor.forces;
actor.velocity += (actor_forces + conf.gravity) * dt.0.as_secs_f32();
actor.forces = Vec3::ZERO;
//get position
let (scale, rotation, mut actor_position) = transform.0.to_scale_rotation_translation();
actor_position -= actor.offset;
//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_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();
actor.flag_ground = actor.flag_collision || actor_block_below.is_solid();
//push actor back out of the block
if actor.flag_collision {
//first, compute restitution, based on position inside the block
// let block_center = actor_block_pos.as_f32() + Vec3::ONE * 0.5;
// let to_block_center = actor_position - block_center;
//then, based on normal:
//push the actor back
//actor_position += normal * 0.5;
//cancel out velocity in the direction of the normal
// let dot = actor.velocity.dot(normal);
// if dot > 0. {
// //actor.velocity -= normal * dot;
// actor.velocity = Vec3::ZERO;
// }
//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 = actor.velocity.y.max(0.);
}
}
//clamp velocity
let max_velocity = actor.max_velocity;
if actor.hack_xz_circular && actor.max_velocity.0.is_some() && (actor.max_velocity.0 == actor.max_velocity.2) {
actor.velocity.y = actor.velocity.y.clamp(-max_velocity.1.unwrap_or(f32::MAX), max_velocity.1.unwrap_or(f32::MAX));
let clamped = actor.velocity.xz().clamp_length_max(actor.max_velocity.0.unwrap_or(f32::MAX));
actor.velocity.x = clamped.x;
actor.velocity.z = clamped.y;
} else {
actor.velocity = vec3(
actor.velocity.x.clamp(-max_velocity.0.unwrap_or(f32::MAX), max_velocity.0.unwrap_or(f32::MAX)),
actor.velocity.y.clamp(-max_velocity.1.unwrap_or(f32::MAX), max_velocity.1.unwrap_or(f32::MAX)),
actor.velocity.z.clamp(-max_velocity.2.unwrap_or(f32::MAX), max_velocity.2.unwrap_or(f32::MAX)),
);
}
//Apply velocity
actor_position += (actor.velocity + actor.frame_velocity) * dt.0.as_secs_f32();
actor.frame_velocity = Vec3::ZERO;
actor_position += actor.offset;
transform.0 = Mat4::from_scale_rotation_translation(scale, rotation.normalize(), actor_position);
//Apply "friction"
let actor_velocity = actor.velocity;
let actor_decel = actor.decel;
actor.velocity -= actor_velocity * actor_decel * dt.0.as_secs_f32();
}
// for (_, mut transform) in (&controllers, &mut transforms).iter() {
// let (scale, rotation, mut translation) = transform.0.to_scale_rotation_translation();
// translation.y -= dt.0.as_secs_f32() * 100.;

View file

@ -2,8 +2,7 @@ use shipyard::{Component, View, ViewMut, EntitiesViewMut, IntoIter, track};
use glam::{IVec3, Quat, Vec3};
use kubi_shared::block::Block;
use crate::{
player::MainPlayer,
transform::Transform
client_physics::ClPhysicsActor, player::MainPlayer, transform::Transform
};
use super::EventComponent;
@ -11,6 +10,8 @@ use super::EventComponent;
pub enum PlayerActionEvent {
PositionChanged {
position: Vec3,
//XXX: should this even be here?
velocity: Vec3,
direction: Quat
},
UpdatedBlock {
@ -22,14 +23,17 @@ pub enum PlayerActionEvent {
pub fn generate_move_events(
transforms: View<Transform, track::All>,
player: View<MainPlayer>,
actors: View<ClPhysicsActor>,
mut entities: EntitiesViewMut,
mut events: ViewMut<EventComponent>,
mut actions: ViewMut<PlayerActionEvent>,
) {
let Some((_, transform)) = (&player, transforms.inserted_or_modified()).iter().next() else { return };
let Some((_, transform, actor)) = (&player, transforms.inserted_or_modified(), &actors).iter().next() else { return };
let (_, direction, position) = transform.0.to_scale_rotation_translation();
//HACK: if the actor is disabled, the velocity is irrelevant, so we just set it to zero.
let velocity = if actor.disable { Vec3::ZERO } else { actor.velocity };
entities.add_entity(
(&mut events, &mut actions),
(EventComponent, PlayerActionEvent::PositionChanged { position, direction })
(EventComponent, PlayerActionEvent::PositionChanged { position, velocity, direction })
);
}

View file

@ -1,53 +0,0 @@
use glam::{Vec3, Mat4, Quat, EulerRot, Vec2};
use shipyard::{Component, View, ViewMut, IntoIter, UniqueView, Workload, IntoWorkload, track};
use std::f32::consts::PI;
use crate::{transform::Transform, input::Inputs, settings::GameSettings, delta_time::DeltaTime};
#[derive(Component)]
pub struct FlyController;
pub fn update_controllers() -> Workload {
(
update_look,
update_movement
).into_sequential_workload()
}
const MAX_PITCH: f32 = PI/2. - 0.05;
fn update_look(
controllers: View<FlyController>,
mut transforms: ViewMut<Transform, track::All>,
inputs: UniqueView<Inputs>,
settings: UniqueView<GameSettings>,
dt: UniqueView<DeltaTime>,
) {
let look = inputs.look * settings.mouse_sensitivity * dt.0.as_secs_f32();
if look == Vec2::ZERO { return }
for (_, mut transform) in (&controllers, &mut transforms).iter() {
let (scale, mut rotation, translation) = transform.0.to_scale_rotation_translation();
let (mut yaw, mut pitch, _roll) = rotation.to_euler(EulerRot::YXZ);
yaw -= look.x;
pitch -= look.y;
pitch = pitch.clamp(-MAX_PITCH, MAX_PITCH);
rotation = Quat::from_euler(EulerRot::YXZ, yaw, pitch, 0.).normalize();
transform.0 = Mat4::from_scale_rotation_translation(scale, rotation, translation);
}
}
fn update_movement(
controllers: View<FlyController>,
mut transforms: ViewMut<Transform, track::All>,
inputs: UniqueView<Inputs>,
dt: UniqueView<DeltaTime>,
) {
if inputs.movement == Vec2::ZERO { return }
let movement = inputs.movement * 30. * dt.0.as_secs_f32();
for (_, mut transform) in (&controllers, &mut transforms).iter() {
let (scale, rotation, mut translation) = transform.0.to_scale_rotation_translation();
let rotation_norm = rotation.normalize();
translation += (rotation_norm * Vec3::NEG_Z).normalize() * movement.y;
translation += (rotation_norm * Vec3::X).normalize() * movement.x;
transform.0 = Mat4::from_scale_rotation_translation(scale, rotation_norm, translation);
}
}

View file

@ -19,6 +19,7 @@ pub struct Inputs {
pub look: Vec2,
pub action_a: bool,
pub action_b: bool,
pub jump: bool,
}
#[derive(Unique, Clone, Copy, Default, Debug)]
@ -186,6 +187,7 @@ fn update_input_state (
inputs.look += raw_inputs.mouse_delta.as_vec2();
inputs.action_a |= raw_inputs.button_state[0];
inputs.action_b |= raw_inputs.button_state[1];
inputs.jump |= raw_inputs.keyboard_state.contains(KeyCode::Space as u32);
}
fn update_input_state_gamepad (
@ -198,9 +200,11 @@ fn update_input_state_gamepad (
let left_stick = vec2(gamepad.value(Axis::LeftStickX), gamepad.value(Axis::LeftStickY));
let right_stick = vec2(gamepad.value(Axis::RightStickX), -gamepad.value(Axis::RightStickY));
inputs.movement += left_stick;
inputs.look += right_stick;
inputs.action_a |= gamepad.is_pressed(Button::South);
//HACK: for now, we multiply look by 2 to make it feel more responsive
inputs.look += right_stick * 2.;
inputs.action_a |= gamepad.is_pressed(Button::West);
inputs.action_b |= gamepad.is_pressed(Button::East);
inputs.jump |= gamepad.is_pressed(Button::South);
}
}
}

View file

@ -23,7 +23,7 @@ pub(crate) mod settings;
pub(crate) mod camera;
pub(crate) mod events;
pub(crate) mod input;
pub(crate) mod fly_controller;
pub(crate) mod player_controller;
pub(crate) mod block_placement;
pub(crate) mod delta_time;
pub(crate) mod cursor_lock;
@ -57,7 +57,7 @@ use events::{
player_actions::generate_move_events,
};
use input::{init_input, process_inputs};
use fly_controller::update_controllers;
use player_controller::{debug_switch_ctl_type, update_player_controllers};
use rendering::{
Renderer,
RenderTarget,
@ -133,7 +133,8 @@ fn update() -> Workload {
update_loaded_world_around_player,
).into_sequential_workload().run_if(is_ingame_or_loading),
(
update_controllers,
debug_switch_ctl_type,
update_player_controllers,
update_client_physics_late,
generate_move_events,
update_raycasts,

View file

@ -26,13 +26,13 @@ pub fn send_player_movement_events(
mut client: UniqueViewMut<UdpClient>,
) {
for event in actions.iter() {
let PlayerActionEvent::PositionChanged { position, direction } = event else {
let PlayerActionEvent::PositionChanged { position, velocity, direction } = event else {
continue
};
client.0.send(
postcard::to_allocvec(&ClientToServerMessage::PositionChanged {
position: *position,
velocity: Vec3::ZERO,
velocity: *velocity,
direction: *direction
}).unwrap().into_boxed_slice(),
Channel::Move as usize,

View file

@ -12,7 +12,7 @@ use kubi_shared::{
use crate::{
camera::Camera,
client_physics::ClPhysicsActor,
fly_controller::FlyController,
player_controller::PlayerController,
transform::Transform,
world::raycast::LookingAtBlock
};
@ -31,7 +31,7 @@ pub fn spawn_player (
Health::new(PLAYER_HEALTH),
Transform::default(),
Camera::default(),
FlyController,
PlayerController::DEFAULT_FPS_CTL,
LookingAtBlock::default(),
PlayerHolding(Some(Block::Cobblestone)),
Username("LocalPlayer".into()),
@ -53,7 +53,7 @@ pub fn spawn_local_player_multiplayer (
init.health,
Transform(Mat4::from_rotation_translation(init.direction, init.position)),
Camera::default(),
FlyController,
PlayerController::DEFAULT_FPS_CTL,
LookingAtBlock::default(),
PlayerHolding::default(),
),(

View file

@ -0,0 +1,130 @@
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, PrevInputs, RawKbmInputState}, settings::GameSettings, transform::Transform};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PlayerControllerType {
FlyCam,
FpsCtl,
}
#[derive(Component)]
pub struct PlayerController {
pub control_type: PlayerControllerType,
pub speed: f32,
}
impl PlayerController {
pub const DEFAULT_FLY_CAM: Self = Self {
control_type: PlayerControllerType::FlyCam,
speed: 50.,
};
pub const DEFAULT_FPS_CTL: Self = Self {
control_type: PlayerControllerType::FpsCtl,
speed: 10.,
};
}
pub fn update_player_controllers() -> Workload {
(
update_look,
update_movement
).into_sequential_workload()
}
const MAX_PITCH: f32 = PI/2. - 0.05;
fn update_look(
controllers: View<PlayerController>,
mut transforms: ViewMut<Transform, track::All>,
inputs: UniqueView<Inputs>,
settings: UniqueView<GameSettings>,
dt: UniqueView<DeltaTime>,
) {
let look = inputs.look * settings.mouse_sensitivity * dt.0.as_secs_f32();
if look == Vec2::ZERO { return }
for (_, mut transform) in (&controllers, &mut transforms).iter() {
let (scale, mut rotation, translation) = transform.0.to_scale_rotation_translation();
let (mut yaw, mut pitch, _roll) = rotation.to_euler(EulerRot::YXZ);
yaw -= look.x;
pitch -= look.y;
pitch = pitch.clamp(-MAX_PITCH, MAX_PITCH);
rotation = Quat::from_euler(EulerRot::YXZ, yaw, pitch, 0.).normalize();
transform.0 = Mat4::from_scale_rotation_translation(scale, rotation, translation);
}
}
fn update_movement(
controllers: View<PlayerController>,
mut transforms: ViewMut<Transform, track::All>,
mut actors: ViewMut<ClPhysicsActor>,
inputs: UniqueView<Inputs>,
prev_inputs: UniqueView<PrevInputs>,
dt: UniqueView<DeltaTime>,
) {
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();
match ctl.control_type {
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);
},
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();
//TODO: remove hardcoded jump force
// actor.apply_constant_force(ctl.speed * (
// (forward * movement.z) +
// (right * movement.x)
// ));
actor.apply_force(
ctl.speed * (
(forward * movement.z) +
(right * movement.x)
) +
Vec3::Y * movement.y * 1250. * (actor_on_ground as u8 as f32)
);
// actor.decel =
// (right * (1. - inputs.movement.x.abs()) * 10.) +
// (forward * (1. - inputs.movement.y.abs()) * 10.);
// 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;
}
}
}