2021-03-23 09:51:53 +00:00
|
|
|
use super::Fluid;
|
|
|
|
use crate::{consts::WATER_DENSITY, uid::Uid};
|
2021-04-03 18:50:06 +00:00
|
|
|
use hashbrown::HashSet;
|
2020-07-06 14:23:08 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-01-07 20:25:12 +00:00
|
|
|
use specs::{Component, DerefFlaggedStorage, NullStorage};
|
2020-07-06 05:56:02 +00:00
|
|
|
use specs_idvs::IdvStorage;
|
2019-07-29 19:54:58 +00:00
|
|
|
use vek::*;
|
2019-01-02 19:22:01 +00:00
|
|
|
|
2021-03-16 07:40:31 +00:00
|
|
|
/// Position
|
2019-07-29 16:19:08 +00:00
|
|
|
#[derive(Copy, Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
2019-01-02 19:22:01 +00:00
|
|
|
pub struct Pos(pub Vec3<f32>);
|
|
|
|
|
|
|
|
impl Component for Pos {
|
2021-03-16 07:40:31 +00:00
|
|
|
// TODO: why not regular vec storage????
|
|
|
|
// TODO: component occupancy metrics
|
2020-07-06 05:56:02 +00:00
|
|
|
type Storage = IdvStorage<Self>;
|
2019-01-02 19:22:01 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 07:40:31 +00:00
|
|
|
/// Velocity
|
2019-07-29 16:19:08 +00:00
|
|
|
#[derive(Copy, Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
2019-01-02 19:22:01 +00:00
|
|
|
pub struct Vel(pub Vec3<f32>);
|
|
|
|
|
2021-03-23 09:51:53 +00:00
|
|
|
impl Vel {
|
|
|
|
pub fn zero() -> Self { Vel(Vec3::zero()) }
|
|
|
|
}
|
|
|
|
|
2019-01-02 19:22:01 +00:00
|
|
|
impl Component for Vel {
|
2021-03-16 07:40:31 +00:00
|
|
|
// TODO: why not regular vec storage????
|
|
|
|
type Storage = IdvStorage<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Used to defer writes to Pos/Vel in nested join loops
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct PosVelDefer {
|
2021-03-16 18:04:28 +00:00
|
|
|
pub pos: Option<Pos>,
|
|
|
|
pub vel: Option<Vel>,
|
2021-03-16 07:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for PosVelDefer {
|
|
|
|
// TODO: why not regular vec storage????
|
2020-07-06 05:56:02 +00:00
|
|
|
type Storage = IdvStorage<Self>;
|
2019-01-02 19:22:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 10:02:45 +00:00
|
|
|
/// Cache of Velocity (of last tick) * dt (of curent tick)
|
|
|
|
/// It's updated and read in physics sys to speed up entity<->entity collisions
|
|
|
|
/// no need to send it via network
|
|
|
|
#[derive(Copy, Clone, Default, Debug, PartialEq)]
|
2021-02-14 17:09:52 +00:00
|
|
|
pub struct PreviousPhysCache {
|
2021-02-22 17:55:10 +00:00
|
|
|
pub velocity_dt: Vec3<f32>,
|
2021-03-13 16:21:02 +00:00
|
|
|
/// Center of bounding sphere that encompasses the entity along its path for
|
|
|
|
/// this tick
|
2021-02-22 17:55:10 +00:00
|
|
|
pub center: Vec3<f32>,
|
|
|
|
/// Calculates a Sphere over the Entity for quick boundary checking
|
|
|
|
pub collision_boundary: f32,
|
2021-02-14 17:09:52 +00:00
|
|
|
pub scale: f32,
|
2021-02-22 17:55:10 +00:00
|
|
|
pub scaled_radius: f32,
|
2021-03-13 22:17:53 +00:00
|
|
|
pub ori: Quaternion<f32>,
|
2021-02-14 17:09:52 +00:00
|
|
|
}
|
2020-11-04 10:02:45 +00:00
|
|
|
|
2021-02-14 17:09:52 +00:00
|
|
|
impl Component for PreviousPhysCache {
|
2021-03-16 07:40:31 +00:00
|
|
|
// TODO: why not regular vec storage????
|
2020-11-04 10:02:45 +00:00
|
|
|
type Storage = IdvStorage<Self>;
|
|
|
|
}
|
|
|
|
|
2019-08-02 18:56:37 +00:00
|
|
|
// Scale
|
|
|
|
#[derive(Copy, Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Scale(pub f32);
|
|
|
|
|
|
|
|
impl Component for Scale {
|
2021-01-07 20:25:12 +00:00
|
|
|
type Storage = DerefFlaggedStorage<Self, IdvStorage<Self>>;
|
2019-08-02 18:56:37 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 20:22:39 +00:00
|
|
|
// Mass
|
2021-03-23 09:51:53 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2019-09-25 20:22:39 +00:00
|
|
|
pub struct Mass(pub f32);
|
|
|
|
|
2021-03-23 09:51:53 +00:00
|
|
|
impl Default for Mass {
|
|
|
|
fn default() -> Mass { Mass(1.0) }
|
|
|
|
}
|
|
|
|
|
2019-09-25 20:22:39 +00:00
|
|
|
impl Component for Mass {
|
2021-01-07 20:25:12 +00:00
|
|
|
type Storage = DerefFlaggedStorage<Self, IdvStorage<Self>>;
|
2019-09-25 20:22:39 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 09:51:53 +00:00
|
|
|
/// The average density (specific mass) of an entity.
|
|
|
|
/// Units used for reference is kg/m³
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Density(pub f32);
|
|
|
|
|
|
|
|
impl Default for Density {
|
|
|
|
fn default() -> Density { Density(WATER_DENSITY) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for Density {
|
|
|
|
type Storage = DerefFlaggedStorage<Self, IdvStorage<Self>>;
|
|
|
|
}
|
|
|
|
|
2021-03-11 16:48:59 +00:00
|
|
|
// Collider
|
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2020-04-26 14:37:13 +00:00
|
|
|
pub enum Collider {
|
2021-03-11 16:48:59 +00:00
|
|
|
// TODO: pass the map from ids -> voxel data to get_radius and get_z_limits to compute a
|
|
|
|
// bounding cylinder
|
|
|
|
Voxel { id: String },
|
2020-04-26 14:44:03 +00:00
|
|
|
Box { radius: f32, z_min: f32, z_max: f32 },
|
2020-04-26 14:37:13 +00:00
|
|
|
Point,
|
|
|
|
}
|
|
|
|
|
2020-08-24 17:24:44 +00:00
|
|
|
impl Collider {
|
|
|
|
pub fn get_radius(&self) -> f32 {
|
|
|
|
match self {
|
2021-03-11 22:27:03 +00:00
|
|
|
Collider::Voxel { .. } => 1.0,
|
2020-08-24 17:24:44 +00:00
|
|
|
Collider::Box { radius, .. } => *radius,
|
|
|
|
Collider::Point => 0.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 09:51:53 +00:00
|
|
|
pub fn get_height(&self) -> f32 {
|
|
|
|
let (z_min, z_max) = self.get_z_limits(1.0);
|
|
|
|
z_max - z_min
|
|
|
|
}
|
|
|
|
|
2020-11-06 01:22:26 +00:00
|
|
|
pub fn get_z_limits(&self, modifier: f32) -> (f32, f32) {
|
2020-08-24 17:24:44 +00:00
|
|
|
match self {
|
2021-03-11 22:27:03 +00:00
|
|
|
Collider::Voxel { .. } => (0.0, 1.0),
|
2020-11-06 01:22:26 +00:00
|
|
|
Collider::Box { z_min, z_max, .. } => (*z_min * modifier, *z_max * modifier),
|
2020-08-24 17:24:44 +00:00
|
|
|
Collider::Point => (0.0, 0.0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 14:37:13 +00:00
|
|
|
impl Component for Collider {
|
2021-01-07 20:25:12 +00:00
|
|
|
type Storage = DerefFlaggedStorage<Self, IdvStorage<Self>>;
|
2020-04-26 14:37:13 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 19:28:35 +00:00
|
|
|
#[derive(Copy, Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Sticky;
|
|
|
|
|
|
|
|
impl Component for Sticky {
|
2021-01-07 20:25:12 +00:00
|
|
|
type Storage = DerefFlaggedStorage<Self, NullStorage<Self>>;
|
2019-10-02 19:28:35 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 10:11:37 +00:00
|
|
|
// PhysicsState
|
2020-08-24 17:24:44 +00:00
|
|
|
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
2019-08-23 10:11:37 +00:00
|
|
|
pub struct PhysicsState {
|
|
|
|
pub on_ground: bool,
|
2020-04-26 14:37:13 +00:00
|
|
|
pub on_ceiling: bool,
|
2019-09-09 19:11:40 +00:00
|
|
|
pub on_wall: Option<Vec3<f32>>,
|
2021-04-03 18:50:06 +00:00
|
|
|
pub touch_entities: HashSet<Uid>,
|
2021-03-23 09:51:53 +00:00
|
|
|
pub in_fluid: Option<Fluid>,
|
2021-03-12 13:26:02 +00:00
|
|
|
pub ground_vel: Vec3<f32>,
|
2019-08-23 10:11:37 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 14:06:01 +00:00
|
|
|
impl PhysicsState {
|
2020-08-25 10:01:17 +00:00
|
|
|
pub fn reset(&mut self) {
|
2020-08-24 17:24:44 +00:00
|
|
|
// Avoid allocation overhead!
|
|
|
|
let mut touch_entities = std::mem::take(&mut self.touch_entities);
|
|
|
|
touch_entities.clear();
|
|
|
|
*self = Self {
|
|
|
|
touch_entities,
|
2021-03-12 22:14:08 +00:00
|
|
|
ground_vel: self.ground_vel, /* Preserved, since it's the velocity of the last
|
|
|
|
* contact point */
|
2020-08-24 17:24:44 +00:00
|
|
|
..Self::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 14:06:01 +00:00
|
|
|
pub fn on_surface(&self) -> Option<Vec3<f32>> {
|
|
|
|
self.on_ground
|
|
|
|
.then_some(-Vec3::unit_z())
|
|
|
|
.or_else(|| self.on_ceiling.then_some(Vec3::unit_z()))
|
|
|
|
.or(self.on_wall)
|
|
|
|
}
|
2021-03-23 09:51:53 +00:00
|
|
|
|
|
|
|
pub fn in_liquid(&self) -> Option<f32> { self.in_fluid.and_then(|fluid| fluid.depth()) }
|
2020-07-05 14:06:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 10:11:37 +00:00
|
|
|
impl Component for PhysicsState {
|
2020-08-13 08:12:14 +00:00
|
|
|
type Storage = IdvStorage<Self>;
|
2019-08-23 10:11:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-28 01:33:45 +00:00
|
|
|
/// Used to forcefully update the position, velocity, and orientation of the
|
|
|
|
/// client
|
2019-07-30 05:24:36 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
2019-04-14 20:30:27 +00:00
|
|
|
pub struct ForceUpdate;
|
|
|
|
|
|
|
|
impl Component for ForceUpdate {
|
|
|
|
type Storage = NullStorage<Self>;
|
|
|
|
}
|