2021-07-18 00:13:36 +00:00
|
|
|
use super::{Fluid, Ori};
|
2021-11-04 12:45:08 +00:00
|
|
|
use crate::{
|
|
|
|
comp::body::ship::figuredata::VoxelCollider, consts::WATER_DENSITY, terrain::Block, 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;
|
2021-11-04 12:45:08 +00:00
|
|
|
use std::sync::Arc;
|
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)]
|
2021-07-18 00:13:36 +00:00
|
|
|
pub struct PosVelOriDefer {
|
2021-03-16 18:04:28 +00:00
|
|
|
pub pos: Option<Pos>,
|
|
|
|
pub vel: Option<Vel>,
|
2021-07-18 00:13:36 +00:00
|
|
|
pub ori: Option<Ori>,
|
2021-03-16 07:40:31 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 00:13:36 +00:00
|
|
|
impl Component for PosVelOriDefer {
|
2021-03-16 07:40:31 +00:00
|
|
|
// 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-09-15 21:27:48 +00:00
|
|
|
/// Approximate radius of cylinder of collider.
|
2021-02-22 17:55:10 +00:00
|
|
|
pub scaled_radius: f32,
|
2021-09-15 21:27:48 +00:00
|
|
|
/// Radius of stadium of collider.
|
2021-09-15 15:11:41 +00:00
|
|
|
pub neighborhood_radius: f32,
|
2021-09-15 21:27:48 +00:00
|
|
|
/// relative p0 and p1 of collider's statium, None if cylinder.
|
|
|
|
pub origins: Option<(Vec2<f32>, Vec2<f32>)>,
|
2021-09-17 21:34:28 +00:00
|
|
|
pub pos: Option<Pos>,
|
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
|
2021-11-04 12:45:08 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2020-04-26 14:37:13 +00:00
|
|
|
pub enum Collider {
|
2021-11-04 12:45:08 +00:00
|
|
|
/// A volume based on an existing voxel asset.
|
2021-09-11 12:06:13 +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,
|
|
|
|
},
|
2021-11-04 12:45:08 +00:00
|
|
|
/// A mutable volume.
|
|
|
|
Volume(Arc<VoxelCollider>),
|
2021-09-11 12:06:13 +00:00
|
|
|
/// Capsule prism with line segment from p0 to p1
|
|
|
|
CapsulePrism {
|
|
|
|
p0: Vec2<f32>,
|
|
|
|
p1: Vec2<f32>,
|
|
|
|
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 {
|
2021-11-04 12:45:08 +00:00
|
|
|
pub fn is_voxel(&self) -> bool { matches!(self, Collider::Voxel { .. } | Collider::Volume(_)) }
|
|
|
|
|
2021-09-16 16:45:17 +00:00
|
|
|
pub fn bounding_radius(&self) -> f32 {
|
2020-08-24 17:24:44 +00:00
|
|
|
match self {
|
2021-11-04 12:45:08 +00:00
|
|
|
Collider::Voxel { .. } | Collider::Volume(_) => 1.0,
|
2021-09-15 15:11:41 +00:00
|
|
|
Collider::CapsulePrism { radius, p0, p1, .. } => {
|
|
|
|
let a = p0.distance(*p1);
|
|
|
|
a / 2.0 + *radius
|
|
|
|
},
|
2020-08-24 17:24:44 +00:00
|
|
|
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-11-04 12:45:08 +00:00
|
|
|
Collider::Voxel { .. } | Collider::Volume(_) => (0.0, 1.0),
|
2021-09-11 12:06:13 +00:00
|
|
|
Collider::CapsulePrism { 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
|
|
|
}
|
|
|
|
|
2022-02-28 19:20:37 +00:00
|
|
|
#[derive(Copy, Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Immovable;
|
|
|
|
|
|
|
|
impl Component for Immovable {
|
|
|
|
type Storage = DerefFlaggedStorage<Self, NullStorage<Self>>;
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-06-20 03:51:04 +00:00
|
|
|
pub on_ground: Option<Block>,
|
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
|
2021-06-20 03:51:04 +00:00
|
|
|
.map(|_| -Vec3::unit_z())
|
2020-07-05 14:06:01 +00:00
|
|
|
.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>;
|
|
|
|
}
|