veloren/common/src/sys/phys.rs

218 lines
7.4 KiB
Rust
Raw Normal View History

use crate::{
comp::{Gliding, Jumping, MoveDir, OnGround, Ori, Pos, Rolling, Stats, Vel},
state::DeltaTime,
terrain::TerrainMap,
vol::{ReadVol, Vox},
};
2019-06-09 19:33:20 +00:00
use specs::{Entities, Join, Read, ReadExpect, ReadStorage, System, WriteStorage};
use vek::*;
const GRAVITY: f32 = 9.81 * 4.0;
const FRIC_GROUND: f32 = 0.15;
const FRIC_AIR: f32 = 0.015;
const HUMANOID_ACCEL: f32 = 70.0;
const HUMANOID_SPEED: f32 = 120.0;
const HUMANOID_AIR_ACCEL: f32 = 10.0;
const HUMANOID_AIR_SPEED: f32 = 100.0;
const HUMANOID_JUMP_ACCEL: f32 = 16.0;
const ROLL_ACCEL: f32 = 160.0;
const ROLL_SPEED: f32 = 550.0;
const GLIDE_ACCEL: f32 = 15.0;
const GLIDE_SPEED: f32 = 45.0;
// Gravity is 9.81 * 4, so this makes gravity equal to .15
const GLIDE_ANTIGRAV: f32 = 9.81 * 3.95;
2019-06-04 15:42:31 +00:00
// Integrates forces, calculates the new velocity based off of the old velocity
// dt = delta time
// lv = linear velocity
// damp = linear damping
// Friction is a type of damping.
fn integrate_forces(dt: f32, mut lv: Vec3<f32>, damp: f32) -> Vec3<f32> {
lv.z -= (GRAVITY * dt).max(-50.0);
let mut linear_damp = 1.0 - dt * damp;
if linear_damp < 0.0
// reached zero in the given time
{
linear_damp = 0.0;
}
lv *= linear_damp;
lv
}
/// This system applies forces and calculates new positions and velocities.
2019-06-09 19:33:20 +00:00
pub struct Sys;
impl<'a> System<'a> for Sys {
type SystemData = (
2019-06-09 19:33:20 +00:00
Entities<'a>,
ReadExpect<'a, TerrainMap>,
Read<'a, DeltaTime>,
ReadStorage<'a, MoveDir>,
ReadStorage<'a, Gliding>,
2019-06-11 04:08:55 +00:00
ReadStorage<'a, Stats>,
WriteStorage<'a, Jumping>,
2019-06-16 12:56:07 +00:00
WriteStorage<'a, Rolling>,
2019-06-13 18:09:50 +00:00
WriteStorage<'a, OnGround>,
WriteStorage<'a, Pos>,
WriteStorage<'a, Vel>,
WriteStorage<'a, Ori>,
);
2019-06-09 19:33:20 +00:00
fn run(
&mut self,
(
entities,
terrain,
dt,
move_dirs,
glidings,
stats,
mut jumpings,
2019-06-16 12:56:07 +00:00
mut rollings,
2019-06-13 18:09:50 +00:00
mut on_grounds,
mut positions,
mut velocities,
mut orientations,
): Self::SystemData,
2019-06-09 19:33:20 +00:00
) {
// Apply movement inputs
for (entity, stats, move_dir, gliding, mut pos, mut vel, mut ori) in (
2019-06-13 18:09:50 +00:00
&entities,
&stats,
move_dirs.maybe(),
glidings.maybe(),
&mut positions,
&mut velocities,
&mut orientations,
)
.join()
2019-06-09 19:33:20 +00:00
{
// Disable while dead TODO: Replace with client states?
if stats.is_dead {
continue;
}
// Move player according to move_dir
if let Some(move_dir) = move_dir {
vel.0 += Vec2::broadcast(dt.0)
* move_dir.0
2019-06-13 18:09:50 +00:00
* match (
on_grounds.get(entity).is_some(),
glidings.get(entity).is_some(),
rollings.get(entity).is_some(),
2019-06-13 18:09:50 +00:00
) {
(true, false, false) if vel.0.magnitude() < HUMANOID_SPEED => {
HUMANOID_ACCEL
}
(false, true, false) if vel.0.magnitude() < GLIDE_SPEED => GLIDE_ACCEL,
(false, false, false) if vel.0.magnitude() < HUMANOID_AIR_SPEED => {
HUMANOID_AIR_ACCEL
}
(true, false, true) if vel.0.magnitude() < ROLL_SPEED => ROLL_ACCEL,
_ => 0.0,
};
}
// Jump
if jumpings.get(entity).is_some() {
vel.0.z = HUMANOID_JUMP_ACCEL;
jumpings.remove(entity);
}
// Glide
if gliding.is_some() && vel.0.magnitude() < GLIDE_SPEED && vel.0.z < 0.0 {
let lift = GLIDE_ANTIGRAV + vel.0.z.powf(2.0) * 0.2;
vel.0.z += dt.0 * lift * Vec2::<f32>::from(vel.0 * 0.15).magnitude().min(1.0);
}
// Roll
2019-06-16 12:56:07 +00:00
if let Some(time) = rollings.get_mut(entity).map(|r| &mut r.time) {
*time += dt.0;
2019-06-16 15:54:02 +00:00
if *time > 0.55 {
2019-06-16 12:56:07 +00:00
rollings.remove(entity);
}
}
2019-06-11 04:08:55 +00:00
// Set direction based on velocity
if vel.0.magnitude_squared() != 0.0 {
ori.0 = vel.0.normalized() * Vec3::new(1.0, 1.0, 0.0);
}
2019-06-09 19:33:20 +00:00
// Movement
pos.0 += vel.0 * dt.0;
// Update OnGround component
if terrain
.get((pos.0 - Vec3::unit_z() * 0.1).map(|e| e.floor() as i32))
.map(|vox| !vox.is_empty())
.unwrap_or(false)
2019-06-09 19:33:20 +00:00
&& vel.0.z <= 0.0
{
2019-06-13 18:09:50 +00:00
on_grounds.insert(entity, OnGround);
2019-06-09 19:33:20 +00:00
} else {
2019-06-13 18:09:50 +00:00
on_grounds.remove(entity);
2019-06-09 19:33:20 +00:00
}
2019-06-04 15:42:31 +00:00
// Integrate forces
// Friction is assumed to be a constant dependent on location
2019-06-09 19:33:20 +00:00
let friction = 50.0
2019-06-13 18:09:50 +00:00
* if on_grounds.get(entity).is_some() {
2019-06-09 19:33:20 +00:00
FRIC_GROUND
} else {
FRIC_AIR
};
2019-06-04 15:42:31 +00:00
vel.0 = integrate_forces(dt.0, vel.0, friction);
// Basic collision with terrain
2019-06-25 16:13:30 +00:00
// Iterate through nearby blocks, prioritise closer ones
let near_iter = [0, -1, 1].into_iter()
.map(move |i| [0, -1, 1].into_iter()
.map(move |j| [0, 1, -1, 2].into_iter()
.map(move |k| (*i, *j, *k))))
.flatten()
.flatten();
// For every nearby block...
for (i, j, k) in near_iter {
let block_pos = pos.0.map(|e| e.floor() as i32) + Vec3::new(i, j, k);
// ...check to see whether it is solid...
if terrain
.get(block_pos)
.map(|vox| !vox.is_empty())
.unwrap_or(false)
{
// ...and calculate bounding boxes for both the player's body and the block.
let this_body_aabb = Aabb { min: pos.0 + Vec3::new(-0.4, -0.4, 1.0), max: pos.0 + Vec3::new(0.4, 0.4, 2.0) };
let other_aabb = Aabb { min: block_pos.map(|e| e as f32), max: block_pos.map(|e| e as f32) + 1.0 };
// If the bounding boxes collide, resolve the collision
if this_body_aabb.collides_with_aabb(other_aabb) {
let dir = this_body_aabb.collision_vector_with_aabb(other_aabb);
let max_axis = dir.map(|e| e.abs()).reduce_partial_min();
let resolve_dir = dir.map(|e| if e.abs() == max_axis { e } else { 0.0 });
pos.0 -= resolve_dir;
vel.0 = vel.0.map2(resolve_dir, |e, d| if d == 0.0 { e } else { 0.0 });
}
// Now, calculate a binding box for the player's feet...
let this_feet_aabb = Aabb { min: pos.0 + Vec3::new(-0.3, -0.3, 0.0), max: pos.0 + Vec3::new(0.3, 0.3, 1.0) };
// ...if it collides with the block, snap the player to the top of it...
if this_feet_aabb.collides_with_aabb(other_aabb) {
pos.0.z -= this_feet_aabb.collision_vector_with_aabb(other_aabb).z;
vel.0.z = vel.0.z.max(0.0);
}
}
}
}
}
}