2020-02-01 20:39:39 +00:00
|
|
|
use crate::{
|
2020-08-03 21:54:33 +00:00
|
|
|
comp::{
|
2020-11-06 01:22:26 +00:00
|
|
|
BeamSegment, CharacterState, Collider, Gravity, Mass, Mounting, Ori, PhysicsState, Pos,
|
|
|
|
PreviousVelDtCache, Projectile, Scale, Shockwave, Sticky, Vel,
|
2020-08-03 21:54:33 +00:00
|
|
|
},
|
2020-02-01 20:39:39 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2020-10-20 16:31:19 +00:00
|
|
|
metrics::{PhysicsMetrics, SysMetrics},
|
2020-08-26 06:11:56 +00:00
|
|
|
span,
|
2020-02-01 20:39:39 +00:00
|
|
|
state::DeltaTime,
|
2020-09-19 23:35:14 +00:00
|
|
|
sync::Uid,
|
2020-09-20 09:09:29 +00:00
|
|
|
terrain::{Block, TerrainGrid},
|
2020-02-01 20:39:39 +00:00
|
|
|
vol::ReadVol,
|
2019-03-02 03:48:30 +00:00
|
|
|
};
|
2020-08-13 08:12:14 +00:00
|
|
|
use rayon::iter::ParallelIterator;
|
2020-10-20 16:31:19 +00:00
|
|
|
use specs::{
|
|
|
|
Entities, Join, ParJoin, Read, ReadExpect, ReadStorage, System, WriteExpect, WriteStorage,
|
|
|
|
};
|
2020-08-11 11:13:18 +00:00
|
|
|
use std::ops::Range;
|
2020-08-12 14:10:12 +00:00
|
|
|
use vek::*;
|
2019-03-02 03:48:30 +00:00
|
|
|
|
2020-03-10 18:12:16 +00:00
|
|
|
pub const GRAVITY: f32 = 9.81 * 5.0;
|
2020-11-12 21:31:28 +00:00
|
|
|
pub const BOUYANCY: f32 = 1.0;
|
2019-09-05 10:24:38 +00:00
|
|
|
// Friction values used for linear damping. They are unitless quantities. The
|
|
|
|
// value of these quantities must be between zero and one. They represent the
|
2020-08-25 12:21:25 +00:00
|
|
|
// amount an object will slow down within 1/60th of a second. Eg. if the
|
|
|
|
// friction is 0.01, and the speed is 1.0, then after 1/60th of a second the
|
|
|
|
// speed will be 0.99. after 1 second the speed will be 0.54, which is 0.99 ^
|
|
|
|
// 60.
|
2020-11-12 21:31:28 +00:00
|
|
|
pub const FRIC_GROUND: f32 = 0.15;
|
|
|
|
pub const FRIC_AIR: f32 = 0.0125;
|
2020-11-14 16:03:01 +00:00
|
|
|
pub const FRIC_FLUID: f32 = 0.4;
|
2019-06-29 20:11:21 +00:00
|
|
|
|
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.
|
2019-07-26 13:42:36 +00:00
|
|
|
fn integrate_forces(dt: f32, mut lv: Vec3<f32>, grav: f32, damp: f32) -> Vec3<f32> {
|
2019-09-05 10:24:38 +00:00
|
|
|
// this is not linear damping, because it is proportional to the original
|
|
|
|
// velocity this "linear" damping in in fact, quite exponential. and thus
|
|
|
|
// must be interpolated accordingly
|
2019-09-09 19:11:40 +00:00
|
|
|
let linear_damp = (1.0 - damp.min(1.0)).powf(dt * 60.0);
|
2019-06-04 15:42:31 +00:00
|
|
|
|
2020-11-11 08:50:02 +00:00
|
|
|
// TODO: investigate if we can have air friction provide the neccessary limits
|
|
|
|
// here
|
|
|
|
lv.z = (lv.z - grav * dt).max(-80.0).min(lv.z);
|
2019-06-27 14:21:41 +00:00
|
|
|
lv * linear_damp
|
2019-06-04 15:42:31 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 19:28:25 +00:00
|
|
|
/// This system applies forces and calculates new positions and velocities.
|
2019-06-09 19:33:20 +00:00
|
|
|
pub struct Sys;
|
2019-04-16 21:06:33 +00:00
|
|
|
impl<'a> System<'a> for Sys {
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2019-03-02 03:48:30 +00:00
|
|
|
type SystemData = (
|
2019-06-09 19:33:20 +00:00
|
|
|
Entities<'a>,
|
2019-09-21 12:43:24 +00:00
|
|
|
ReadStorage<'a, Uid>,
|
common: Rework volume API
See the doc comments in `common/src/vol.rs` for more information on
the API itself.
The changes include:
* Consistent `Err`/`Error` naming.
* Types are named `...Error`.
* `enum` variants are named `...Err`.
* Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation
to an upcoming change where a “map” in the game related sense will
be added.
* Add volume iterators. There are two types of them:
* _Position_ iterators obtained from the trait `IntoPosIterator`
using the method
`fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `Vec3<i32>`.
* _Volume_ iterators obtained from the trait `IntoVolIterator`
using the method
`fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `(Vec3<i32>, &Self::Vox)`.
Those traits will usually be implemented by references to volume
types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some
type which usually implements several volume traits, such as `Chunk`).
* _Position_ iterators iterate over the positions valid for that
volume.
* _Volume_ iterators do the same but return not only the position
but also the voxel at that position, in each iteration.
* Introduce trait `RectSizedVol` for the use case which we have with
`Chonk`: A `Chonk` is sized only in x and y direction.
* Introduce traits `RasterableVol`, `RectRasterableVol`
* `RasterableVol` represents a volume that is compile-time sized and has
its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen
because such a volume can be used with `VolGrid3d`.
* `RectRasterableVol` represents a volume that is compile-time sized at
least in x and y direction and has its lower bound at `(0, 0, z)`.
There's no requirement on he lower bound or size in z direction.
The name `RectRasterableVol` was chosen because such a volume can be
used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
|
|
|
ReadExpect<'a, TerrainGrid>,
|
2019-03-02 03:48:30 +00:00
|
|
|
Read<'a, DeltaTime>,
|
2020-09-14 12:56:05 +00:00
|
|
|
ReadExpect<'a, SysMetrics>,
|
2020-10-20 16:31:19 +00:00
|
|
|
WriteExpect<'a, PhysicsMetrics>,
|
2019-10-08 16:50:26 +00:00
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
2019-08-03 11:26:05 +00:00
|
|
|
ReadStorage<'a, Scale>,
|
2019-10-02 19:28:35 +00:00
|
|
|
ReadStorage<'a, Sticky>,
|
2019-09-25 20:22:39 +00:00
|
|
|
ReadStorage<'a, Mass>,
|
2020-04-26 14:37:13 +00:00
|
|
|
ReadStorage<'a, Collider>,
|
2019-10-17 20:59:36 +00:00
|
|
|
ReadStorage<'a, Gravity>,
|
2019-08-23 10:11:37 +00:00
|
|
|
WriteStorage<'a, PhysicsState>,
|
2019-06-13 18:09:50 +00:00
|
|
|
WriteStorage<'a, Pos>,
|
|
|
|
WriteStorage<'a, Vel>,
|
|
|
|
WriteStorage<'a, Ori>,
|
2020-11-04 10:02:45 +00:00
|
|
|
WriteStorage<'a, PreviousVelDtCache>,
|
2019-09-09 19:11:40 +00:00
|
|
|
ReadStorage<'a, Mounting>,
|
2020-08-03 21:54:33 +00:00
|
|
|
ReadStorage<'a, Projectile>,
|
2020-09-24 02:02:30 +00:00
|
|
|
ReadStorage<'a, BeamSegment>,
|
2020-10-08 00:32:30 +00:00
|
|
|
ReadStorage<'a, Shockwave>,
|
2020-11-06 01:22:26 +00:00
|
|
|
ReadStorage<'a, CharacterState>,
|
2019-03-02 03:48:30 +00:00
|
|
|
);
|
|
|
|
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::or_fun_call)] // TODO: Pending review in #587
|
2020-06-23 06:52:04 +00:00
|
|
|
#[allow(clippy::blocks_in_if_conditions)] // TODO: Pending review in #587
|
2019-06-09 19:33:20 +00:00
|
|
|
fn run(
|
|
|
|
&mut self,
|
2019-06-11 19:28:25 +00:00
|
|
|
(
|
|
|
|
entities,
|
2019-09-21 12:43:24 +00:00
|
|
|
uids,
|
2019-06-11 19:28:25 +00:00
|
|
|
terrain,
|
|
|
|
dt,
|
2020-09-14 12:56:05 +00:00
|
|
|
sys_metrics,
|
2020-10-20 16:31:19 +00:00
|
|
|
mut physics_metrics,
|
2019-08-07 15:39:16 +00:00
|
|
|
event_bus,
|
2019-08-03 11:26:05 +00:00
|
|
|
scales,
|
2019-10-02 19:28:35 +00:00
|
|
|
stickies,
|
2019-09-25 20:22:39 +00:00
|
|
|
masses,
|
2020-04-26 14:37:13 +00:00
|
|
|
colliders,
|
2019-10-17 20:59:36 +00:00
|
|
|
gravities,
|
2019-08-23 10:11:37 +00:00
|
|
|
mut physics_states,
|
2019-06-13 18:09:50 +00:00
|
|
|
mut positions,
|
|
|
|
mut velocities,
|
|
|
|
mut orientations,
|
2020-11-04 10:02:45 +00:00
|
|
|
mut previous_velocities_times_dt,
|
2019-09-09 19:11:40 +00:00
|
|
|
mountings,
|
2020-08-03 21:54:33 +00:00
|
|
|
projectiles,
|
2020-09-13 05:32:50 +00:00
|
|
|
beams,
|
2020-10-08 00:32:30 +00:00
|
|
|
shockwaves,
|
2020-11-06 01:22:26 +00:00
|
|
|
char_states,
|
2019-06-11 19:28:25 +00:00
|
|
|
): Self::SystemData,
|
2019-06-09 19:33:20 +00:00
|
|
|
) {
|
2020-09-14 12:56:05 +00:00
|
|
|
let start_time = std::time::Instant::now();
|
2020-09-07 04:59:16 +00:00
|
|
|
span!(_guard, "run", "phys::Sys::run");
|
2019-08-07 15:39:16 +00:00
|
|
|
let mut event_emitter = event_bus.emitter();
|
|
|
|
|
2020-08-24 17:24:44 +00:00
|
|
|
// Add/reset physics state components
|
2020-08-26 08:11:31 +00:00
|
|
|
span!(guard, "Add/reset physics state components");
|
2020-08-25 10:01:17 +00:00
|
|
|
for (entity, _, _, _, _) in (
|
2020-08-13 08:12:14 +00:00
|
|
|
&entities,
|
|
|
|
&colliders,
|
|
|
|
&positions,
|
|
|
|
&velocities,
|
|
|
|
&orientations,
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2020-08-25 10:01:17 +00:00
|
|
|
let _ = physics_states
|
|
|
|
.entry(entity)
|
|
|
|
.map(|e| e.or_insert_with(Default::default));
|
2020-08-24 17:24:44 +00:00
|
|
|
}
|
2020-08-26 08:11:31 +00:00
|
|
|
drop(guard);
|
2020-08-24 17:24:44 +00:00
|
|
|
|
|
|
|
// Apply pushback
|
|
|
|
//
|
|
|
|
// Note: We now do this first because we project velocity ahead. This is slighty
|
|
|
|
// imperfect and implies that we might get edge-cases where entities
|
|
|
|
// standing right next to the edge of a wall may get hit by projectiles
|
|
|
|
// fired into the wall very close to them. However, this sort of thing is
|
|
|
|
// already possible with poorly-defined hitboxes anyway so it's not too
|
|
|
|
// much of a concern.
|
|
|
|
//
|
|
|
|
// If this situation becomes a problem, this code should be integrated with the
|
|
|
|
// terrain collision code below, although that's not trivial to do since
|
|
|
|
// it means the step needs to take into account the speeds of both
|
|
|
|
// entities.
|
2020-11-04 10:02:45 +00:00
|
|
|
span!(guard, "Maintain pushback cache");
|
|
|
|
//Add PreviousVelDtCache for all relevant entities
|
|
|
|
for entity in (
|
2020-10-21 10:23:34 +00:00
|
|
|
&entities,
|
|
|
|
&velocities,
|
|
|
|
&positions,
|
2020-11-04 10:02:45 +00:00
|
|
|
!&previous_velocities_times_dt,
|
2020-10-21 10:23:34 +00:00
|
|
|
!&mountings,
|
|
|
|
!&beams,
|
|
|
|
!&shockwaves,
|
|
|
|
)
|
|
|
|
.join()
|
2020-11-04 10:02:45 +00:00
|
|
|
.map(|(e, _, _, _, _, _, _)| e)
|
|
|
|
.collect::<Vec<_>>()
|
2020-10-21 10:23:34 +00:00
|
|
|
{
|
2020-11-04 10:02:45 +00:00
|
|
|
let _ = previous_velocities_times_dt.insert(entity, PreviousVelDtCache(Vec3::zero()));
|
|
|
|
}
|
|
|
|
|
|
|
|
//Update PreviousVelDtCache
|
|
|
|
for (_, vel, _, mut vel_dt, _, _, _) in (
|
|
|
|
&entities,
|
|
|
|
&velocities,
|
|
|
|
&positions,
|
|
|
|
&mut previous_velocities_times_dt,
|
|
|
|
!&mountings,
|
|
|
|
!&beams,
|
|
|
|
!&shockwaves,
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
|
|
|
vel_dt.0 = vel.0 * dt.0;
|
2020-10-21 10:23:34 +00:00
|
|
|
}
|
|
|
|
drop(guard);
|
2020-08-26 08:11:31 +00:00
|
|
|
span!(guard, "Apply pushback");
|
2020-10-20 16:31:19 +00:00
|
|
|
let metrics = (
|
2020-08-24 17:24:44 +00:00
|
|
|
&entities,
|
|
|
|
&positions,
|
2020-10-21 10:23:34 +00:00
|
|
|
&mut velocities,
|
2020-11-04 10:02:45 +00:00
|
|
|
&previous_velocities_times_dt,
|
2020-08-24 17:24:44 +00:00
|
|
|
scales.maybe(),
|
|
|
|
masses.maybe(),
|
|
|
|
colliders.maybe(),
|
|
|
|
!&mountings,
|
|
|
|
stickies.maybe(),
|
|
|
|
&mut physics_states,
|
|
|
|
// TODO: if we need to avoid collisions for other things consider moving whether it
|
|
|
|
// should interact into the collider component or into a separate component
|
|
|
|
projectiles.maybe(),
|
2020-11-06 01:22:26 +00:00
|
|
|
char_states.maybe(),
|
2020-08-24 17:24:44 +00:00
|
|
|
)
|
2020-10-20 16:31:19 +00:00
|
|
|
.par_join()
|
2020-11-06 01:22:26 +00:00
|
|
|
.filter(|(_, _, _, _, _, _, _, _, sticky, physics, _, _)| {
|
2020-09-30 00:47:11 +00:00
|
|
|
sticky.is_none() || (physics.on_wall.is_none() && !physics.on_ground)
|
2020-08-24 17:24:44 +00:00
|
|
|
})
|
2020-11-06 01:22:26 +00:00
|
|
|
.map(|(e, p, v, vd, s, m, c, _, _, ph, pr, c_s)| (e, p, v, vd, s, m, c, ph, pr, c_s))
|
|
|
|
.fold(
|
|
|
|
PhysicsMetrics::default,
|
|
|
|
|mut metrics,
|
|
|
|
(
|
|
|
|
entity,
|
|
|
|
pos,
|
|
|
|
vel,
|
|
|
|
vel_dt,
|
|
|
|
scale,
|
|
|
|
mass,
|
|
|
|
collider,
|
|
|
|
physics,
|
|
|
|
projectile,
|
|
|
|
char_state_maybe,
|
|
|
|
)| {
|
2020-10-20 16:31:19 +00:00
|
|
|
let scale = scale.map(|s| s.0).unwrap_or(1.0);
|
|
|
|
let radius = collider.map(|c| c.get_radius()).unwrap_or(0.5);
|
2020-11-06 01:22:26 +00:00
|
|
|
let modifier = if char_state_maybe.map_or(false, |c_s| c_s.is_dodge()) {
|
|
|
|
0.5
|
|
|
|
} else {
|
|
|
|
1.0
|
|
|
|
};
|
|
|
|
let z_limits = collider
|
|
|
|
.map(|c| c.get_z_limits(modifier))
|
|
|
|
.unwrap_or((-0.5 * modifier, 0.5 * modifier));
|
2020-10-20 16:31:19 +00:00
|
|
|
let mass = mass.map(|m| m.0).unwrap_or(scale);
|
2020-08-24 17:24:44 +00:00
|
|
|
|
2020-10-20 16:31:19 +00:00
|
|
|
// Resets touch_entities in physics
|
|
|
|
physics.touch_entities.clear();
|
2020-09-11 02:17:33 +00:00
|
|
|
|
2020-10-20 16:31:19 +00:00
|
|
|
let is_projectile = projectile.is_some();
|
2020-08-24 17:24:44 +00:00
|
|
|
|
2020-10-20 16:31:19 +00:00
|
|
|
let mut vel_delta = Vec3::zero();
|
2020-08-25 10:01:17 +00:00
|
|
|
|
2020-10-20 16:31:19 +00:00
|
|
|
for (
|
|
|
|
entity_other,
|
|
|
|
other,
|
|
|
|
pos_other,
|
2020-11-04 10:02:45 +00:00
|
|
|
vel_dt_other,
|
2020-10-20 16:31:19 +00:00
|
|
|
scale_other,
|
|
|
|
mass_other,
|
|
|
|
collider_other,
|
|
|
|
_,
|
|
|
|
_,
|
|
|
|
_,
|
|
|
|
_,
|
2020-11-06 01:22:26 +00:00
|
|
|
char_state_other_maybe,
|
2020-10-20 16:31:19 +00:00
|
|
|
) in (
|
|
|
|
&entities,
|
|
|
|
&uids,
|
|
|
|
&positions,
|
2020-11-04 10:02:45 +00:00
|
|
|
&previous_velocities_times_dt,
|
2020-10-20 16:31:19 +00:00
|
|
|
scales.maybe(),
|
|
|
|
masses.maybe(),
|
|
|
|
colliders.maybe(),
|
|
|
|
!&projectiles,
|
|
|
|
!&mountings,
|
|
|
|
!&beams,
|
|
|
|
!&shockwaves,
|
2020-11-06 01:22:26 +00:00
|
|
|
char_states.maybe(),
|
2020-10-20 16:31:19 +00:00
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
|
|
|
if entity == entity_other {
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-11 13:59:45 +00:00
|
|
|
|
2020-10-20 16:31:19 +00:00
|
|
|
let scale_other = scale_other.map(|s| s.0).unwrap_or(1.0);
|
|
|
|
let radius_other = collider_other.map(|c| c.get_radius()).unwrap_or(0.5);
|
2020-08-24 17:24:44 +00:00
|
|
|
|
2020-10-20 16:31:19 +00:00
|
|
|
let collision_dist = scale * radius + scale_other * radius_other;
|
2020-08-24 17:24:44 +00:00
|
|
|
|
2020-11-04 23:49:06 +00:00
|
|
|
// Sanity check: skip colliding entities that are too far from each other
|
2020-11-06 01:22:26 +00:00
|
|
|
if (pos.0 - pos_other.0).xy().magnitude()
|
|
|
|
> (vel_dt.0 - vel_dt_other.0).xy().magnitude() + collision_dist
|
|
|
|
{
|
2020-10-20 16:31:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-08-24 17:24:44 +00:00
|
|
|
|
2020-11-06 01:22:26 +00:00
|
|
|
let modifier_other =
|
|
|
|
if char_state_other_maybe.map_or(false, |c_s| c_s.is_dodge()) {
|
|
|
|
0.5
|
|
|
|
} else {
|
|
|
|
1.0
|
|
|
|
};
|
|
|
|
|
2020-10-20 16:31:19 +00:00
|
|
|
let z_limits_other = collider_other
|
2020-11-06 01:22:26 +00:00
|
|
|
.map(|c| c.get_z_limits(modifier_other))
|
|
|
|
.unwrap_or((-0.5 * modifier_other, 0.5 * modifier_other));
|
2020-10-20 16:31:19 +00:00
|
|
|
let mass_other = mass_other.map(|m| m.0).unwrap_or(scale_other);
|
|
|
|
//This check after the pos check, as we currently don't have that many
|
|
|
|
// massless entites [citation needed]
|
|
|
|
if mass_other == 0.0 {
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-21 10:23:34 +00:00
|
|
|
|
2020-10-20 16:31:19 +00:00
|
|
|
metrics.entity_entity_collision_checks += 1;
|
2020-10-21 10:23:34 +00:00
|
|
|
|
2020-10-20 16:31:19 +00:00
|
|
|
const MIN_COLLISION_DIST: f32 = 0.3;
|
2020-11-06 01:22:26 +00:00
|
|
|
let increments = ((vel_dt.0 - vel_dt_other.0).magnitude()
|
|
|
|
/ MIN_COLLISION_DIST)
|
2020-10-20 16:31:19 +00:00
|
|
|
.max(1.0)
|
|
|
|
.ceil() as usize;
|
|
|
|
let step_delta = 1.0 / increments as f32;
|
|
|
|
let mut collided = false;
|
2020-08-24 17:24:44 +00:00
|
|
|
|
2020-10-20 16:31:19 +00:00
|
|
|
for i in 0..increments {
|
|
|
|
let factor = i as f32 * step_delta;
|
2020-11-04 10:02:45 +00:00
|
|
|
let pos = pos.0 + vel_dt.0 * factor;
|
|
|
|
let pos_other = pos_other.0 + vel_dt_other.0 * factor;
|
2020-08-24 17:24:44 +00:00
|
|
|
|
2020-10-20 16:31:19 +00:00
|
|
|
let diff = pos.xy() - pos_other.xy();
|
2020-08-24 17:24:44 +00:00
|
|
|
|
2020-11-04 23:49:06 +00:00
|
|
|
if diff.magnitude_squared() <= collision_dist.powi(2)
|
2020-10-20 16:31:19 +00:00
|
|
|
&& pos.z + z_limits.1 * scale
|
|
|
|
>= pos_other.z + z_limits_other.0 * scale_other
|
|
|
|
&& pos.z + z_limits.0 * scale
|
|
|
|
<= pos_other.z + z_limits_other.1 * scale_other
|
|
|
|
{
|
|
|
|
if !collided {
|
|
|
|
physics.touch_entities.push(*other);
|
|
|
|
metrics.entity_entity_collisions += 1;
|
|
|
|
}
|
2020-08-24 17:24:44 +00:00
|
|
|
|
2020-10-20 16:31:19 +00:00
|
|
|
// Don't apply repulsive force to projectiles
|
|
|
|
if diff.magnitude_squared() > 0.0 && !is_projectile {
|
|
|
|
let force =
|
|
|
|
400.0 * (collision_dist - diff.magnitude()) * mass_other
|
|
|
|
/ (mass + mass_other);
|
2020-08-24 17:24:44 +00:00
|
|
|
|
2020-10-20 16:31:19 +00:00
|
|
|
vel_delta += Vec3::from(diff.normalized()) * force * step_delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
collided = true;
|
|
|
|
}
|
|
|
|
}
|
2020-08-24 17:24:44 +00:00
|
|
|
}
|
2020-08-25 10:01:17 +00:00
|
|
|
|
2020-10-20 16:31:19 +00:00
|
|
|
// Change velocity
|
|
|
|
vel.0 += vel_delta * dt.0;
|
|
|
|
metrics
|
|
|
|
},
|
|
|
|
)
|
2020-11-06 01:22:26 +00:00
|
|
|
.reduce(PhysicsMetrics::default, |old, new| PhysicsMetrics {
|
|
|
|
entity_entity_collision_checks: old.entity_entity_collision_checks
|
|
|
|
+ new.entity_entity_collision_checks,
|
|
|
|
entity_entity_collisions: old.entity_entity_collisions
|
|
|
|
+ new.entity_entity_collisions,
|
2020-10-20 16:31:19 +00:00
|
|
|
});
|
|
|
|
physics_metrics.entity_entity_collision_checks = metrics.entity_entity_collision_checks;
|
|
|
|
physics_metrics.entity_entity_collisions = metrics.entity_entity_collisions;
|
2020-08-26 08:11:31 +00:00
|
|
|
drop(guard);
|
2020-08-13 08:12:14 +00:00
|
|
|
|
2019-06-11 19:28:25 +00:00
|
|
|
// Apply movement inputs
|
2020-08-26 08:11:31 +00:00
|
|
|
span!(guard, "Apply movement and terrain collision");
|
2020-08-13 08:12:14 +00:00
|
|
|
let land_on_grounds = (
|
2019-06-13 18:09:50 +00:00
|
|
|
&entities,
|
2019-08-03 11:26:05 +00:00
|
|
|
scales.maybe(),
|
2019-10-02 19:28:35 +00:00
|
|
|
stickies.maybe(),
|
2020-04-26 14:37:13 +00:00
|
|
|
&colliders,
|
2019-06-13 18:09:50 +00:00
|
|
|
&mut positions,
|
|
|
|
&mut velocities,
|
|
|
|
&mut orientations,
|
2020-08-13 08:12:14 +00:00
|
|
|
&mut physics_states,
|
2019-09-09 19:11:40 +00:00
|
|
|
!&mountings,
|
2019-06-13 18:09:50 +00:00
|
|
|
)
|
2020-08-13 08:12:14 +00:00
|
|
|
.par_join()
|
|
|
|
.fold(Vec::new, |
|
|
|
|
mut land_on_grounds,
|
|
|
|
(entity, _scale, sticky, collider, mut pos, mut vel, _ori, mut physics_state, _),
|
|
|
|
| {
|
2020-07-05 14:06:01 +00:00
|
|
|
if sticky.is_some() && physics_state.on_surface().is_some() {
|
2020-04-26 14:51:08 +00:00
|
|
|
vel.0 = Vec3::zero();
|
2020-08-13 08:12:14 +00:00
|
|
|
return land_on_grounds;
|
2019-10-02 19:28:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 13:51:21 +00:00
|
|
|
// TODO: Use this
|
|
|
|
//let scale = scale.map(|s| s.0).unwrap_or(1.0);
|
2019-08-03 11:26:05 +00:00
|
|
|
|
2019-09-06 06:22:58 +00:00
|
|
|
let old_vel = *vel;
|
2019-09-05 10:24:38 +00:00
|
|
|
// Integrate forces
|
|
|
|
// Friction is assumed to be a constant dependent on location
|
2019-09-09 19:11:40 +00:00
|
|
|
let friction = FRIC_AIR
|
|
|
|
.max(if physics_state.on_ground {
|
|
|
|
FRIC_GROUND
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
})
|
2020-11-06 15:33:20 +00:00
|
|
|
.max(if physics_state.in_liquid.is_some() {
|
2019-09-09 19:11:40 +00:00
|
|
|
FRIC_FLUID
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
});
|
2020-07-18 11:08:38 +00:00
|
|
|
let in_loaded_chunk = terrain
|
|
|
|
.get_key(terrain.pos_key(pos.0.map(|e| e.floor() as i32)))
|
|
|
|
.is_some();
|
|
|
|
let downward_force = if !in_loaded_chunk {
|
|
|
|
0.0 // No gravity in unloaded chunks
|
2020-08-12 14:10:12 +00:00
|
|
|
} else if physics_state
|
2020-11-06 15:33:20 +00:00
|
|
|
.in_liquid
|
2020-08-12 14:10:12 +00:00
|
|
|
.map(|depth| depth > 0.75)
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2019-09-09 19:11:40 +00:00
|
|
|
(1.0 - BOUYANCY) * GRAVITY
|
2019-09-05 10:24:38 +00:00
|
|
|
} else {
|
2019-09-09 19:11:40 +00:00
|
|
|
GRAVITY
|
2019-10-17 20:59:36 +00:00
|
|
|
} * gravities.get(entity).map(|g| g.0).unwrap_or_default();
|
2019-09-09 19:11:40 +00:00
|
|
|
vel.0 = integrate_forces(dt.0, vel.0, downward_force, friction);
|
2019-09-05 10:24:38 +00:00
|
|
|
|
|
|
|
// Don't move if we're not in a loaded chunk
|
2020-07-18 11:08:38 +00:00
|
|
|
let mut pos_delta = if in_loaded_chunk {
|
2019-09-06 04:04:20 +00:00
|
|
|
// this is an approximation that allows most framerates to
|
|
|
|
// behave in a similar manner.
|
2020-06-01 20:33:20 +00:00
|
|
|
let dt_lerp = 0.2;
|
|
|
|
(vel.0 * dt_lerp + old_vel.0 * (1.0 - dt_lerp)) * dt.0
|
2019-09-05 10:24:38 +00:00
|
|
|
} else {
|
|
|
|
Vec3::zero()
|
|
|
|
};
|
|
|
|
|
2020-08-25 10:01:17 +00:00
|
|
|
match *collider {
|
2020-04-26 14:44:03 +00:00
|
|
|
Collider::Box {
|
|
|
|
radius,
|
|
|
|
z_min,
|
|
|
|
z_max,
|
|
|
|
} => {
|
2020-04-26 14:37:13 +00:00
|
|
|
// Scale collider
|
2020-08-24 19:52:18 +00:00
|
|
|
// TODO: Use scale & actual proportions when pathfinding is good enough to manage irregular entity
|
|
|
|
// sizes
|
|
|
|
let radius = radius.min(0.45); // * scale;
|
2020-08-24 17:24:44 +00:00
|
|
|
let z_min = z_min; // * scale;
|
2020-08-24 20:53:17 +00:00
|
|
|
let z_max = z_max.clamped(1.2, 1.95); // * scale;
|
2020-04-26 14:37:13 +00:00
|
|
|
|
|
|
|
// Probe distances
|
|
|
|
let hdist = radius.ceil() as i32;
|
|
|
|
// Neighbouring blocks iterator
|
|
|
|
let near_iter = (-hdist..hdist + 1)
|
|
|
|
.map(move |i| {
|
|
|
|
(-hdist..hdist + 1).map(move |j| {
|
2020-09-20 09:09:29 +00:00
|
|
|
(1 - Block::MAX_HEIGHT.ceil() as i32 + z_min.floor() as i32
|
2020-04-26 14:44:03 +00:00
|
|
|
..z_max.ceil() as i32 + 1)
|
|
|
|
.map(move |k| (i, j, k))
|
2020-04-26 14:37:13 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.flatten();
|
|
|
|
|
2020-08-12 14:10:12 +00:00
|
|
|
// Function for iterating over the blocks the player at a specific position
|
|
|
|
// collides with
|
2020-08-11 11:13:18 +00:00
|
|
|
fn collision_iter<'a>(
|
|
|
|
pos: Vec3<f32>,
|
|
|
|
terrain: &'a TerrainGrid,
|
2020-08-13 08:12:14 +00:00
|
|
|
hit: &'a impl Fn(&Block) -> bool,
|
2020-09-26 14:53:49 +00:00
|
|
|
height: &'a impl Fn(&Block) -> f32,
|
2020-08-12 14:10:12 +00:00
|
|
|
near_iter: impl Iterator<Item = (i32, i32, i32)> + 'a,
|
2020-08-11 11:13:18 +00:00
|
|
|
radius: f32,
|
|
|
|
z_range: Range<f32>,
|
2020-08-12 14:10:12 +00:00
|
|
|
) -> impl Iterator<Item = Aabb<f32>> + 'a {
|
|
|
|
near_iter.filter_map(move |(i, j, k)| {
|
|
|
|
let block_pos = pos.map(|e| e.floor() as i32) + Vec3::new(i, j, k);
|
|
|
|
|
|
|
|
if let Some(block) = terrain.get(block_pos).ok().copied().filter(hit) {
|
|
|
|
let player_aabb = Aabb {
|
|
|
|
min: pos + Vec3::new(-radius, -radius, z_range.start),
|
|
|
|
max: pos + Vec3::new(radius, radius, z_range.end),
|
|
|
|
};
|
|
|
|
let block_aabb = Aabb {
|
|
|
|
min: block_pos.map(|e| e as f32),
|
|
|
|
max: block_pos.map(|e| e as f32)
|
2020-09-26 14:53:49 +00:00
|
|
|
+ Vec3::new(1.0, 1.0, height(&block)),
|
2020-08-12 14:10:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if player_aabb.collides_with_aabb(block_aabb) {
|
|
|
|
return Some(block_aabb);
|
2020-08-11 11:13:18 +00:00
|
|
|
}
|
2020-08-12 14:10:12 +00:00
|
|
|
}
|
2020-08-11 11:13:18 +00:00
|
|
|
|
2020-08-12 14:10:12 +00:00
|
|
|
None
|
|
|
|
})
|
2020-08-11 11:13:18 +00:00
|
|
|
};
|
|
|
|
|
2020-08-13 08:12:14 +00:00
|
|
|
let z_range = z_min..z_max;
|
2020-04-26 14:37:13 +00:00
|
|
|
// Function for determining whether the player at a specific position collides
|
2020-08-11 11:13:18 +00:00
|
|
|
// with blocks with the given criteria
|
2020-08-13 08:12:14 +00:00
|
|
|
fn collision_with<'a>(
|
|
|
|
pos: Vec3<f32>,
|
|
|
|
terrain: &'a TerrainGrid,
|
2020-09-26 14:53:49 +00:00
|
|
|
hit: impl Fn(&Block) -> bool,
|
2020-08-13 08:12:14 +00:00
|
|
|
near_iter: impl Iterator<Item = (i32, i32, i32)> + 'a,
|
|
|
|
radius: f32,
|
|
|
|
z_range: Range<f32>,
|
|
|
|
) -> bool {
|
2020-09-26 14:53:49 +00:00
|
|
|
collision_iter(pos, terrain, &|block| block.is_solid() && hit(block), &Block::solid_height, near_iter, radius, z_range).count()
|
2020-08-12 14:10:12 +00:00
|
|
|
> 0
|
2019-06-26 21:43:47 +00:00
|
|
|
};
|
2019-06-25 16:13:30 +00:00
|
|
|
|
2020-04-26 14:37:13 +00:00
|
|
|
let was_on_ground = physics_state.on_ground;
|
|
|
|
physics_state.on_ground = false;
|
|
|
|
|
|
|
|
let mut on_ground = false;
|
|
|
|
let mut on_ceiling = false;
|
|
|
|
let mut attempts = 0; // Don't loop infinitely here
|
|
|
|
|
|
|
|
// Don't jump too far at once
|
|
|
|
let increments = (pos_delta.map(|e| e.abs()).reduce_partial_max() / 0.3)
|
|
|
|
.ceil()
|
|
|
|
.max(1.0);
|
|
|
|
let old_pos = pos.0;
|
2020-09-26 14:53:49 +00:00
|
|
|
fn block_true(_: &Block) -> bool { true }
|
2020-04-26 14:37:13 +00:00
|
|
|
for _ in 0..increments as usize {
|
|
|
|
pos.0 += pos_delta / increments;
|
|
|
|
|
|
|
|
const MAX_ATTEMPTS: usize = 16;
|
|
|
|
|
|
|
|
// While the player is colliding with the terrain...
|
2020-09-26 14:53:49 +00:00
|
|
|
while collision_with(pos.0, &terrain, block_true, near_iter.clone(), radius, z_range.clone())
|
2020-04-26 14:37:13 +00:00
|
|
|
&& attempts < MAX_ATTEMPTS
|
|
|
|
{
|
|
|
|
// Calculate the player's AABB
|
|
|
|
let player_aabb = Aabb {
|
|
|
|
min: pos.0 + Vec3::new(-radius, -radius, z_min),
|
|
|
|
max: pos.0 + Vec3::new(radius, radius, z_max),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Determine the block that we are colliding with most (based on minimum
|
|
|
|
// collision axis)
|
|
|
|
let (_block_pos, block_aabb, block_height) = near_iter
|
|
|
|
.clone()
|
|
|
|
// Calculate the block's position in world space
|
|
|
|
.map(|(i, j, k)| pos.0.map(|e| e.floor() as i32) + Vec3::new(i, j, k))
|
|
|
|
// Make sure the block is actually solid
|
|
|
|
.filter_map(|block_pos| {
|
|
|
|
if let Some(block) = terrain
|
|
|
|
.get(block_pos)
|
|
|
|
.ok()
|
|
|
|
.filter(|block| block.is_solid())
|
|
|
|
{
|
|
|
|
// Calculate block AABB
|
|
|
|
Some((
|
|
|
|
block_pos,
|
|
|
|
Aabb {
|
|
|
|
min: block_pos.map(|e| e as f32),
|
2020-09-20 09:09:29 +00:00
|
|
|
max: block_pos.map(|e| e as f32) + Vec3::new(1.0, 1.0, block.solid_height()),
|
2020-04-26 14:37:13 +00:00
|
|
|
},
|
2020-09-20 09:09:29 +00:00
|
|
|
block.solid_height(),
|
2020-04-26 14:37:13 +00:00
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// Determine whether the block's AABB collides with the player's AABB
|
|
|
|
.filter(|(_, block_aabb, _)| block_aabb.collides_with_aabb(player_aabb))
|
|
|
|
// Find the maximum of the minimum collision axes (this bit is weird, trust me that it works)
|
|
|
|
.min_by_key(|(_, block_aabb, _)| {
|
|
|
|
((block_aabb.center() - player_aabb.center() - Vec3::unit_z() * 0.5)
|
|
|
|
.map(|e| e.abs())
|
|
|
|
.sum()
|
|
|
|
* 1_000_000.0) as i32
|
|
|
|
})
|
|
|
|
.expect("Collision detected, but no colliding blocks found!");
|
|
|
|
|
|
|
|
// Find the intrusion vector of the collision
|
|
|
|
let dir = player_aabb.collision_vector_with_aabb(block_aabb);
|
|
|
|
|
2020-04-26 14:44:03 +00:00
|
|
|
// Determine an appropriate resolution vector (i.e: the minimum distance
|
|
|
|
// needed to push out of the block)
|
2020-04-26 14:37:13 +00:00
|
|
|
let max_axis = dir.map(|e| e.abs()).reduce_partial_min();
|
|
|
|
let resolve_dir = -dir.map(|e| {
|
|
|
|
if e.abs().to_bits() == max_axis.to_bits() {
|
|
|
|
e
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-26 14:44:03 +00:00
|
|
|
// When the resolution direction is pointing upwards, we must be on the
|
|
|
|
// ground
|
2020-04-26 14:37:13 +00:00
|
|
|
if resolve_dir.z > 0.0 && vel.0.z <= 0.0 {
|
|
|
|
on_ground = true;
|
|
|
|
|
|
|
|
if !was_on_ground {
|
2020-08-13 08:12:14 +00:00
|
|
|
land_on_grounds.push((entity, *vel));
|
2020-04-26 14:37:13 +00:00
|
|
|
}
|
|
|
|
} else if resolve_dir.z < 0.0 && vel.0.z >= 0.0 {
|
|
|
|
on_ceiling = true;
|
|
|
|
}
|
|
|
|
|
2020-04-26 14:44:03 +00:00
|
|
|
// When the resolution direction is non-vertical, we must be colliding
|
|
|
|
// with a wall If the space above is free...
|
2020-09-26 14:53:49 +00:00
|
|
|
if !collision_with(Vec3::new(pos.0.x, pos.0.y, (pos.0.z + 0.1).ceil()), &terrain, block_true, near_iter.clone(), radius, z_range.clone())
|
2020-04-26 14:37:13 +00:00
|
|
|
// ...and we're being pushed out horizontally...
|
|
|
|
&& resolve_dir.z == 0.0
|
|
|
|
// ...and the vertical resolution direction is sufficiently great...
|
|
|
|
&& -dir.z > 0.1
|
|
|
|
// ...and we're falling/standing OR there is a block *directly* beneath our current origin (note: not hitbox)...
|
|
|
|
&& (vel.0.z <= 0.0 || terrain
|
|
|
|
.get((pos.0 - Vec3::unit_z() * 0.1).map(|e| e.floor() as i32))
|
|
|
|
.map(|block| block.is_solid())
|
|
|
|
.unwrap_or(false))
|
|
|
|
// ...and there is a collision with a block beneath our current hitbox...
|
|
|
|
&& collision_with(
|
2020-06-01 20:33:20 +00:00
|
|
|
pos.0 + resolve_dir - Vec3::unit_z() * 1.05,
|
2020-08-13 08:12:14 +00:00
|
|
|
&terrain,
|
2020-09-26 14:53:49 +00:00
|
|
|
block_true,
|
2020-04-26 14:37:13 +00:00
|
|
|
near_iter.clone(),
|
2020-08-13 08:12:14 +00:00
|
|
|
radius,
|
|
|
|
z_range.clone(),
|
2020-04-26 14:37:13 +00:00
|
|
|
)
|
2020-04-18 20:26:43 +00:00
|
|
|
{
|
2020-04-26 14:37:13 +00:00
|
|
|
// ...block-hop!
|
|
|
|
pos.0.z = (pos.0.z + 0.1).floor() + block_height;
|
|
|
|
vel.0.z = 0.0;
|
|
|
|
on_ground = true;
|
|
|
|
break;
|
2020-04-18 20:26:43 +00:00
|
|
|
} else {
|
2020-04-26 14:37:13 +00:00
|
|
|
// Correct the velocity
|
2020-04-26 14:44:03 +00:00
|
|
|
vel.0 = vel.0.map2(resolve_dir, |e, d| {
|
|
|
|
if d * e.signum() < 0.0 { 0.0 } else { e }
|
|
|
|
});
|
2020-06-01 20:33:20 +00:00
|
|
|
pos_delta *= resolve_dir.map(|e| if e != 0.0 { 0.0 } else { 1.0 });
|
2020-04-18 20:26:43 +00:00
|
|
|
}
|
2019-06-25 16:13:30 +00:00
|
|
|
|
2020-04-26 14:37:13 +00:00
|
|
|
// Resolve the collision normally
|
|
|
|
pos.0 += resolve_dir;
|
2019-06-25 16:13:30 +00:00
|
|
|
|
2020-04-26 14:37:13 +00:00
|
|
|
attempts += 1;
|
2019-07-01 20:42:43 +00:00
|
|
|
}
|
2019-06-26 09:40:07 +00:00
|
|
|
|
2020-04-26 14:37:13 +00:00
|
|
|
if attempts == MAX_ATTEMPTS {
|
2020-07-09 14:46:00 +00:00
|
|
|
vel.0 = Vec3::zero();
|
2020-04-26 14:37:13 +00:00
|
|
|
pos.0 = old_pos;
|
|
|
|
break;
|
2019-08-07 09:50:49 +00:00
|
|
|
}
|
2019-06-26 21:43:47 +00:00
|
|
|
}
|
2019-06-26 09:43:03 +00:00
|
|
|
|
2020-04-26 14:37:13 +00:00
|
|
|
if on_ceiling {
|
|
|
|
physics_state.on_ceiling = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if on_ground {
|
|
|
|
physics_state.on_ground = true;
|
|
|
|
// If the space below us is free, then "snap" to the ground
|
|
|
|
} else if collision_with(
|
|
|
|
pos.0 - Vec3::unit_z() * 1.05,
|
2020-08-13 08:12:14 +00:00
|
|
|
&terrain,
|
2020-09-26 14:53:49 +00:00
|
|
|
block_true,
|
2020-04-26 14:37:13 +00:00
|
|
|
near_iter.clone(),
|
2020-08-13 08:12:14 +00:00
|
|
|
radius,
|
|
|
|
z_range.clone(),
|
2020-04-26 14:37:13 +00:00
|
|
|
) && vel.0.z < 0.0
|
|
|
|
&& vel.0.z > -1.5
|
|
|
|
&& was_on_ground
|
|
|
|
&& !collision_with(
|
|
|
|
pos.0 - Vec3::unit_z() * 0.05,
|
2020-08-13 08:12:14 +00:00
|
|
|
&terrain,
|
2020-09-26 14:53:49 +00:00
|
|
|
|block| block.solid_height() >= (pos.0.z - 0.05).rem_euclid(1.0),
|
2019-06-29 21:42:20 +00:00
|
|
|
near_iter.clone(),
|
2020-08-13 08:12:14 +00:00
|
|
|
radius,
|
|
|
|
z_range.clone(),
|
2019-06-29 21:42:20 +00:00
|
|
|
)
|
2019-06-26 21:43:47 +00:00
|
|
|
{
|
2020-04-26 14:37:13 +00:00
|
|
|
let snap_height = terrain
|
2020-04-26 14:44:03 +00:00
|
|
|
.get(
|
|
|
|
Vec3::new(pos.0.x, pos.0.y, pos.0.z - 0.05)
|
|
|
|
.map(|e| e.floor() as i32),
|
|
|
|
)
|
2020-04-26 14:37:13 +00:00
|
|
|
.ok()
|
|
|
|
.filter(|block| block.is_solid())
|
2020-09-20 09:09:29 +00:00
|
|
|
.map(|block| block.solid_height())
|
2020-04-26 14:37:13 +00:00
|
|
|
.unwrap_or(0.0);
|
|
|
|
pos.0.z = (pos.0.z - 0.05).floor() + snap_height;
|
|
|
|
physics_state.on_ground = true;
|
2019-06-26 21:43:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 14:37:13 +00:00
|
|
|
let dirs = [
|
|
|
|
Vec3::unit_x(),
|
|
|
|
Vec3::unit_y(),
|
|
|
|
-Vec3::unit_x(),
|
|
|
|
-Vec3::unit_y(),
|
|
|
|
];
|
2019-08-06 15:00:14 +00:00
|
|
|
|
2020-04-26 14:44:03 +00:00
|
|
|
if let (wall_dir, true) =
|
|
|
|
dirs.iter().fold((Vec3::zero(), false), |(a, hit), dir| {
|
|
|
|
if collision_with(
|
|
|
|
pos.0 + *dir * 0.01,
|
2020-08-13 08:12:14 +00:00
|
|
|
&terrain,
|
2020-09-26 14:53:49 +00:00
|
|
|
block_true,
|
2020-04-26 14:44:03 +00:00
|
|
|
near_iter.clone(),
|
2020-08-13 08:12:14 +00:00
|
|
|
radius,
|
|
|
|
z_range.clone(),
|
2020-04-26 14:44:03 +00:00
|
|
|
) {
|
|
|
|
(a + dir, true)
|
|
|
|
} else {
|
|
|
|
(a, hit)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
{
|
2020-04-26 14:37:13 +00:00
|
|
|
physics_state.on_wall = Some(wall_dir);
|
|
|
|
} else {
|
|
|
|
physics_state.on_wall = None;
|
|
|
|
}
|
2019-08-23 10:11:37 +00:00
|
|
|
|
2020-04-26 14:37:13 +00:00
|
|
|
// Figure out if we're in water
|
2020-11-06 15:33:20 +00:00
|
|
|
physics_state.in_liquid = collision_iter(
|
2020-08-12 14:10:12 +00:00
|
|
|
pos.0,
|
|
|
|
&terrain,
|
2020-09-20 09:09:29 +00:00
|
|
|
&|block| block.is_liquid(),
|
2020-09-26 14:53:49 +00:00
|
|
|
// The liquid part of a liquid block always extends 1 block high.
|
|
|
|
&|_block| 1.0,
|
2020-08-12 14:10:12 +00:00
|
|
|
near_iter.clone(),
|
|
|
|
radius,
|
|
|
|
z_min..z_max,
|
|
|
|
)
|
|
|
|
.max_by_key(|block_aabb| (block_aabb.max.z * 100.0) as i32)
|
|
|
|
.map(|block_aabb| block_aabb.max.z - pos.0.z);
|
2020-04-26 14:37:13 +00:00
|
|
|
},
|
|
|
|
Collider::Point => {
|
2020-09-19 09:16:06 +00:00
|
|
|
let (dist, block) = terrain.ray(pos.0, pos.0 + pos_delta)
|
2020-11-19 00:23:13 +00:00
|
|
|
.until(|block: &Block| block.is_filled())
|
2020-09-19 09:16:06 +00:00
|
|
|
.ignore_error().cast();
|
2020-04-26 14:37:13 +00:00
|
|
|
|
|
|
|
pos.0 += pos_delta.try_normalized().unwrap_or(Vec3::zero()) * dist;
|
|
|
|
|
2020-09-19 09:16:06 +00:00
|
|
|
// Can't fail since we do ignore_error above
|
2020-04-26 14:44:03 +00:00
|
|
|
if block.unwrap().is_some() {
|
2020-04-26 14:51:08 +00:00
|
|
|
let block_center = pos.0.map(|e| e.floor()) + 0.5;
|
|
|
|
let block_rpos = (pos.0 - block_center)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec3::zero());
|
|
|
|
|
|
|
|
// See whether we're on the top/bottom of a block, or the side
|
|
|
|
if block_rpos.z.abs()
|
|
|
|
> block_rpos.xy().map(|e| e.abs()).reduce_partial_max()
|
|
|
|
{
|
|
|
|
if block_rpos.z > 0.0 {
|
|
|
|
physics_state.on_ground = true;
|
2020-04-26 14:37:13 +00:00
|
|
|
} else {
|
2020-04-26 14:51:08 +00:00
|
|
|
physics_state.on_ceiling = true;
|
2020-04-26 14:37:13 +00:00
|
|
|
}
|
2020-04-26 14:52:45 +00:00
|
|
|
vel.0.z = 0.0;
|
2020-04-26 14:51:08 +00:00
|
|
|
} else {
|
|
|
|
physics_state.on_wall =
|
|
|
|
Some(if block_rpos.x.abs() > block_rpos.y.abs() {
|
2020-04-26 14:52:45 +00:00
|
|
|
vel.0.x = 0.0;
|
2020-04-26 14:51:08 +00:00
|
|
|
Vec3::unit_x() * -block_rpos.x.signum()
|
|
|
|
} else {
|
2020-04-26 14:52:45 +00:00
|
|
|
vel.0.y = 0.0;
|
2020-04-26 14:51:08 +00:00
|
|
|
Vec3::unit_y() * -block_rpos.y.signum()
|
|
|
|
});
|
2020-04-26 14:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-19 09:16:06 +00:00
|
|
|
|
2020-11-06 15:33:20 +00:00
|
|
|
physics_state.in_liquid = terrain.get(pos.0.map(|e| e.floor() as i32))
|
2020-09-19 09:16:06 +00:00
|
|
|
.ok()
|
2020-09-20 10:56:57 +00:00
|
|
|
.and_then(|vox| vox.is_liquid().then_some(1.0));
|
2020-04-26 14:37:13 +00:00
|
|
|
},
|
2019-09-09 19:11:40 +00:00
|
|
|
}
|
|
|
|
|
2020-08-13 08:12:14 +00:00
|
|
|
land_on_grounds
|
|
|
|
}).reduce(Vec::new, |mut land_on_grounds_a, mut land_on_grounds_b| {
|
|
|
|
land_on_grounds_a.append(&mut land_on_grounds_b);
|
|
|
|
land_on_grounds_a
|
|
|
|
});
|
2020-08-26 08:11:31 +00:00
|
|
|
drop(guard);
|
2020-08-13 08:12:14 +00:00
|
|
|
|
|
|
|
land_on_grounds.into_iter().for_each(|(entity, vel)| {
|
|
|
|
event_emitter.emit(ServerEvent::LandOnGround { entity, vel: vel.0 });
|
|
|
|
});
|
2020-09-14 12:56:05 +00:00
|
|
|
sys_metrics.phys_ns.store(
|
|
|
|
start_time.elapsed().as_nanos() as i64,
|
|
|
|
std::sync::atomic::Ordering::Relaxed,
|
|
|
|
);
|
2019-03-02 03:48:30 +00:00
|
|
|
}
|
|
|
|
}
|