2020-12-01 00:28:00 +00:00
|
|
|
use common::{
|
2020-08-03 21:54:33 +00:00
|
|
|
comp::{
|
2021-03-17 03:32:03 +00:00
|
|
|
body::ship::figuredata::{VoxelCollider, VOXEL_COLLIDER_MANIFEST},
|
2021-06-16 21:40:18 +00:00
|
|
|
fluid_dynamics::{Fluid, LiquidKind, Wings},
|
2021-05-22 17:56:13 +00:00
|
|
|
BeamSegment, Body, CharacterState, Collider, Density, Mass, Mounting, Ori, PhysicsState,
|
2021-07-18 00:13:36 +00:00
|
|
|
Pos, PosVelOriDefer, PreviousPhysCache, Projectile, Scale, Shockwave, Stats, Sticky, Vel,
|
2020-08-03 21:54:33 +00:00
|
|
|
},
|
2021-03-23 09:51:53 +00:00
|
|
|
consts::{AIR_DENSITY, FRIC_GROUND, GRAVITY},
|
2020-02-01 20:39:39 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2021-03-28 23:29:48 +00:00
|
|
|
outcome::Outcome,
|
2020-12-01 00:28:00 +00:00
|
|
|
resources::DeltaTime,
|
2021-05-22 17:56:13 +00:00
|
|
|
states,
|
2021-06-20 05:37:22 +00:00
|
|
|
terrain::{Block, TerrainGrid},
|
2020-12-13 17:40:15 +00:00
|
|
|
uid::Uid,
|
2021-04-21 17:10:13 +00:00
|
|
|
util::{Projection, SpatialGrid},
|
2021-03-11 16:48:59 +00:00
|
|
|
vol::{BaseVol, ReadVol},
|
2019-03-02 03:48:30 +00:00
|
|
|
};
|
2021-03-13 06:48:30 +00:00
|
|
|
use common_base::{prof_span, span};
|
2021-03-08 22:40:02 +00:00
|
|
|
use common_ecs::{Job, Origin, ParMode, Phase, PhysicsMetrics, System};
|
2020-08-13 08:12:14 +00:00
|
|
|
use rayon::iter::ParallelIterator;
|
2021-03-11 16:48:59 +00:00
|
|
|
use specs::{
|
2021-03-11 22:27:03 +00:00
|
|
|
shred::{ResourceId, World},
|
2021-03-28 23:29:48 +00:00
|
|
|
Entities, Entity, Join, ParJoin, Read, ReadExpect, ReadStorage, SystemData, Write, WriteExpect,
|
2021-03-11 22:27:03 +00:00
|
|
|
WriteStorage,
|
2021-03-11 16:48:59 +00:00
|
|
|
};
|
2021-09-16 16:45:17 +00:00
|
|
|
use std::ops::Range;
|
2020-08-12 14:10:12 +00:00
|
|
|
use vek::*;
|
2019-03-02 03:48:30 +00:00
|
|
|
|
2021-03-23 09:51:53 +00:00
|
|
|
/// The density of the fluid as a function of submersion ratio in given fluid
|
|
|
|
/// where it is assumed that any unsubmersed part is is air.
|
2021-04-21 14:18:46 +00:00
|
|
|
// TODO: Better suited partial submersion curve?
|
2021-03-23 09:51:53 +00:00
|
|
|
fn fluid_density(height: f32, fluid: &Fluid) -> Density {
|
|
|
|
// If depth is less than our height (partial submersion), remove
|
|
|
|
// fluid density based on the ratio of displacement to full volume.
|
2021-04-21 14:18:46 +00:00
|
|
|
let immersion = fluid
|
|
|
|
.depth()
|
|
|
|
.map_or(1.0, |depth| (depth / height).clamp(0.0, 1.0));
|
2021-03-23 09:51:53 +00:00
|
|
|
|
|
|
|
Density(fluid.density().0 * immersion + AIR_DENSITY * (1.0 - immersion))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
fn integrate_forces(
|
|
|
|
dt: &DeltaTime,
|
|
|
|
mut vel: Vel,
|
2021-05-22 17:56:13 +00:00
|
|
|
(body, wings): (&Body, Option<&Wings>),
|
2021-03-23 09:51:53 +00:00
|
|
|
density: &Density,
|
|
|
|
mass: &Mass,
|
|
|
|
fluid: &Fluid,
|
|
|
|
gravity: f32,
|
|
|
|
) -> Vel {
|
|
|
|
let dim = body.dimensions();
|
|
|
|
let height = dim.z;
|
|
|
|
let rel_flow = fluid.relative_flow(&vel);
|
|
|
|
let fluid_density = fluid_density(height, fluid);
|
|
|
|
debug_assert!(mass.0 > 0.0);
|
|
|
|
debug_assert!(density.0 > 0.0);
|
|
|
|
|
|
|
|
// Aerodynamic/hydrodynamic forces
|
|
|
|
if !rel_flow.0.is_approx_zero() {
|
|
|
|
debug_assert!(!rel_flow.0.map(|a| a.is_nan()).reduce_or());
|
2021-05-22 17:56:13 +00:00
|
|
|
let impulse = dt.0 * body.aerodynamic_forces(&rel_flow, fluid_density.0, wings);
|
2021-03-23 09:51:53 +00:00
|
|
|
debug_assert!(!impulse.map(|a| a.is_nan()).reduce_or());
|
|
|
|
if !impulse.is_approx_zero() {
|
|
|
|
let new_v = vel.0 + impulse / mass.0;
|
|
|
|
// If the new velocity is in the opposite direction, it's because the forces
|
|
|
|
// involved are too high for the current tick to handle. We deal with this by
|
|
|
|
// removing the component of our velocity vector along the direction of force.
|
|
|
|
// This way we can only ever lose velocity and will never experience a reverse
|
|
|
|
// in direction from events such as falling into water at high velocities.
|
|
|
|
if new_v.dot(vel.0) < 0.0 {
|
2021-08-07 13:19:58 +00:00
|
|
|
// Multiply by a factor to prevent full stop,
|
|
|
|
// as this can cause things to get stuck in high-density medium
|
2021-05-22 17:56:13 +00:00
|
|
|
vel.0 -= vel.0.projected(&impulse) * 0.9;
|
2021-03-23 09:51:53 +00:00
|
|
|
} else {
|
|
|
|
vel.0 = new_v;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
debug_assert!(!vel.0.map(|a| a.is_nan()).reduce_or());
|
|
|
|
};
|
|
|
|
|
|
|
|
// Hydrostatic/aerostatic forces
|
|
|
|
// modify gravity to account for the effective density as a result of buoyancy
|
|
|
|
let down_force = dt.0 * gravity * (density.0 - fluid_density.0) / density.0;
|
|
|
|
vel.0.z -= down_force;
|
|
|
|
|
|
|
|
vel
|
2019-06-04 15:42:31 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 17:55:10 +00:00
|
|
|
fn calc_z_limit(
|
|
|
|
char_state_maybe: Option<&CharacterState>,
|
|
|
|
collider: Option<&Collider>,
|
|
|
|
) -> (f32, f32) {
|
2021-05-23 07:26:11 +00:00
|
|
|
let modifier = if char_state_maybe.map_or(false, |c_s| c_s.is_dodge() || c_s.is_glide()) {
|
2021-02-22 17:55:10 +00:00
|
|
|
0.5
|
|
|
|
} else {
|
|
|
|
1.0
|
|
|
|
};
|
|
|
|
collider
|
|
|
|
.map(|c| c.get_z_limits(modifier))
|
|
|
|
.unwrap_or((-0.5 * modifier, 0.5 * modifier))
|
|
|
|
}
|
|
|
|
|
2019-06-11 19:28:25 +00:00
|
|
|
/// This system applies forces and calculates new positions and velocities.
|
2021-03-04 14:00:16 +00:00
|
|
|
#[derive(Default)]
|
2019-06-09 19:33:20 +00:00
|
|
|
pub struct Sys;
|
2021-02-22 17:55:10 +00:00
|
|
|
|
2021-03-11 16:48:59 +00:00
|
|
|
#[derive(SystemData)]
|
2021-03-12 22:14:08 +00:00
|
|
|
pub struct PhysicsRead<'a> {
|
2021-03-11 16:48:59 +00:00
|
|
|
entities: Entities<'a>,
|
|
|
|
uids: ReadStorage<'a, Uid>,
|
|
|
|
terrain: ReadExpect<'a, TerrainGrid>,
|
|
|
|
dt: Read<'a, DeltaTime>,
|
|
|
|
event_bus: Read<'a, EventBus<ServerEvent>>,
|
|
|
|
scales: ReadStorage<'a, Scale>,
|
|
|
|
stickies: ReadStorage<'a, Sticky>,
|
|
|
|
masses: ReadStorage<'a, Mass>,
|
|
|
|
colliders: ReadStorage<'a, Collider>,
|
|
|
|
mountings: ReadStorage<'a, Mounting>,
|
|
|
|
projectiles: ReadStorage<'a, Projectile>,
|
|
|
|
beams: ReadStorage<'a, BeamSegment>,
|
|
|
|
shockwaves: ReadStorage<'a, Shockwave>,
|
|
|
|
char_states: ReadStorage<'a, CharacterState>,
|
2021-03-13 20:51:32 +00:00
|
|
|
bodies: ReadStorage<'a, Body>,
|
2021-03-14 23:33:54 +00:00
|
|
|
character_states: ReadStorage<'a, CharacterState>,
|
2021-03-23 09:51:53 +00:00
|
|
|
densities: ReadStorage<'a, Density>,
|
2021-05-24 00:45:22 +00:00
|
|
|
stats: ReadStorage<'a, Stats>,
|
2021-03-11 16:48:59 +00:00
|
|
|
}
|
2019-03-02 03:48:30 +00:00
|
|
|
|
2021-03-11 16:48:59 +00:00
|
|
|
#[derive(SystemData)]
|
2021-03-12 22:14:08 +00:00
|
|
|
pub struct PhysicsWrite<'a> {
|
2021-03-11 16:48:59 +00:00
|
|
|
physics_metrics: WriteExpect<'a, PhysicsMetrics>,
|
2021-04-21 17:10:13 +00:00
|
|
|
cached_spatial_grid: Write<'a, common::CachedSpatialGrid>,
|
2021-03-11 16:48:59 +00:00
|
|
|
physics_states: WriteStorage<'a, PhysicsState>,
|
|
|
|
positions: WriteStorage<'a, Pos>,
|
|
|
|
velocities: WriteStorage<'a, Vel>,
|
2021-07-18 00:13:36 +00:00
|
|
|
pos_vel_ori_defers: WriteStorage<'a, PosVelOriDefer>,
|
2021-03-11 16:48:59 +00:00
|
|
|
orientations: WriteStorage<'a, Ori>,
|
|
|
|
previous_phys_cache: WriteStorage<'a, PreviousPhysCache>,
|
2021-03-28 23:29:48 +00:00
|
|
|
outcomes: Write<'a, Vec<Outcome>>,
|
2021-03-11 16:48:59 +00:00
|
|
|
}
|
2021-03-04 14:00:16 +00:00
|
|
|
|
2021-03-11 16:48:59 +00:00
|
|
|
#[derive(SystemData)]
|
2021-03-12 22:14:08 +00:00
|
|
|
pub struct PhysicsData<'a> {
|
|
|
|
read: PhysicsRead<'a>,
|
|
|
|
write: PhysicsWrite<'a>,
|
2021-03-11 16:48:59 +00:00
|
|
|
}
|
2019-08-07 15:39:16 +00:00
|
|
|
|
2021-03-12 22:14:08 +00:00
|
|
|
impl<'a> PhysicsData<'a> {
|
2021-03-11 16:48:59 +00:00
|
|
|
/// Add/reset physics state components
|
|
|
|
fn reset(&mut self) {
|
2021-03-15 02:36:59 +00:00
|
|
|
span!(_guard, "Add/reset physics state components");
|
2020-08-25 10:01:17 +00:00
|
|
|
for (entity, _, _, _, _) in (
|
2021-03-12 22:14:08 +00:00
|
|
|
&self.read.entities,
|
|
|
|
&self.read.colliders,
|
|
|
|
&self.write.positions,
|
|
|
|
&self.write.velocities,
|
|
|
|
&self.write.orientations,
|
2020-08-13 08:12:14 +00:00
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2021-03-11 22:27:03 +00:00
|
|
|
let _ = self
|
2021-03-12 22:14:08 +00:00
|
|
|
.write
|
2021-03-11 22:27:03 +00:00
|
|
|
.physics_states
|
2020-08-25 10:01:17 +00:00
|
|
|
.entry(entity)
|
|
|
|
.map(|e| e.or_insert_with(Default::default));
|
2020-08-24 17:24:44 +00:00
|
|
|
}
|
2021-03-11 16:48:59 +00:00
|
|
|
}
|
2020-08-24 17:24:44 +00:00
|
|
|
|
2021-03-11 16:48:59 +00:00
|
|
|
fn maintain_pushback_cache(&mut self) {
|
2021-03-15 02:36:59 +00:00
|
|
|
span!(_guard, "Maintain pushback cache");
|
2021-03-16 05:13:52 +00:00
|
|
|
// Add PreviousPhysCache for all relevant entities
|
2020-11-04 10:02:45 +00:00
|
|
|
for entity in (
|
2021-03-12 22:14:08 +00:00
|
|
|
&self.read.entities,
|
|
|
|
&self.write.velocities,
|
|
|
|
&self.write.positions,
|
|
|
|
!&self.write.previous_phys_cache,
|
|
|
|
!&self.read.mountings,
|
|
|
|
!&self.read.beams,
|
|
|
|
!&self.read.shockwaves,
|
2020-10-21 10:23:34 +00:00
|
|
|
)
|
|
|
|
.join()
|
2020-11-04 10:02:45 +00:00
|
|
|
.map(|(e, _, _, _, _, _, _)| e)
|
|
|
|
.collect::<Vec<_>>()
|
2020-10-21 10:23:34 +00:00
|
|
|
{
|
2021-03-11 22:27:03 +00:00
|
|
|
let _ = self
|
2021-03-12 22:14:08 +00:00
|
|
|
.write
|
2021-03-11 22:27:03 +00:00
|
|
|
.previous_phys_cache
|
|
|
|
.insert(entity, PreviousPhysCache {
|
|
|
|
velocity_dt: Vec3::zero(),
|
|
|
|
center: Vec3::zero(),
|
|
|
|
collision_boundary: 0.0,
|
|
|
|
scale: 0.0,
|
|
|
|
scaled_radius: 0.0,
|
2021-09-15 15:11:41 +00:00
|
|
|
neighborhood_radius: 0.0,
|
2021-09-15 21:27:48 +00:00
|
|
|
origins: None,
|
2021-09-17 21:34:28 +00:00
|
|
|
pos: None,
|
2021-03-13 22:17:53 +00:00
|
|
|
ori: Quaternion::identity(),
|
2021-03-11 22:27:03 +00:00
|
|
|
});
|
2020-11-04 10:02:45 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 05:13:52 +00:00
|
|
|
// Update PreviousPhysCache
|
2021-09-15 15:11:41 +00:00
|
|
|
for (_, vel, position, ori, mut phys_cache, collider, scale, cs, _, _, _) in (
|
2021-03-12 22:14:08 +00:00
|
|
|
&self.read.entities,
|
|
|
|
&self.write.velocities,
|
|
|
|
&self.write.positions,
|
2021-09-15 15:11:41 +00:00
|
|
|
&self.write.orientations,
|
2021-03-12 22:14:08 +00:00
|
|
|
&mut self.write.previous_phys_cache,
|
|
|
|
self.read.colliders.maybe(),
|
|
|
|
self.read.scales.maybe(),
|
|
|
|
self.read.char_states.maybe(),
|
|
|
|
!&self.read.mountings,
|
|
|
|
!&self.read.beams,
|
|
|
|
!&self.read.shockwaves,
|
2020-11-04 10:02:45 +00:00
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2021-02-22 17:55:10 +00:00
|
|
|
let scale = scale.map(|s| s.0).unwrap_or(1.0);
|
|
|
|
let z_limits = calc_z_limit(cs, collider);
|
2021-09-16 00:08:54 +00:00
|
|
|
let (z_min, z_max) = z_limits;
|
|
|
|
let (z_min, z_max) = (z_min * scale, z_max * scale);
|
|
|
|
let half_height = (z_max - z_min) / 2.0;
|
2021-02-14 17:09:52 +00:00
|
|
|
|
2021-03-12 22:14:08 +00:00
|
|
|
phys_cache.velocity_dt = vel.0 * self.read.dt.0;
|
2021-09-16 00:08:54 +00:00
|
|
|
let entity_center = position.0 + Vec3::new(0.0, 0.0, z_min + half_height);
|
2021-09-16 16:45:17 +00:00
|
|
|
let flat_radius = collider.map_or(0.5, Collider::bounding_radius) * scale;
|
2021-09-16 00:08:54 +00:00
|
|
|
let radius = (flat_radius.powi(2) + half_height.powi(2)).sqrt();
|
|
|
|
|
|
|
|
// Move center to the middle between OLD and OLD+VEL_DT
|
|
|
|
// so that we can reduce the collision_boundary.
|
|
|
|
phys_cache.center = entity_center + phys_cache.velocity_dt / 2.0;
|
2021-09-16 10:42:07 +00:00
|
|
|
phys_cache.collision_boundary = radius + (phys_cache.velocity_dt / 2.0).magnitude();
|
2021-09-16 00:08:54 +00:00
|
|
|
phys_cache.scale = scale;
|
|
|
|
phys_cache.scaled_radius = flat_radius;
|
|
|
|
|
2021-09-15 15:11:41 +00:00
|
|
|
let neighborhood_radius = match collider {
|
|
|
|
Some(Collider::CapsulePrism { radius, .. }) => radius * scale,
|
2021-09-16 16:45:17 +00:00
|
|
|
Some(Collider::Voxel { .. } | Collider::Point) | None => flat_radius,
|
2021-09-15 15:11:41 +00:00
|
|
|
};
|
|
|
|
phys_cache.neighborhood_radius = neighborhood_radius;
|
|
|
|
|
|
|
|
let ori = ori.to_quat();
|
2021-09-15 21:27:48 +00:00
|
|
|
let origins = match collider {
|
2021-09-15 15:11:41 +00:00
|
|
|
Some(Collider::CapsulePrism { p0, p1, .. }) => {
|
|
|
|
let a = p1 - p0;
|
|
|
|
let len = a.magnitude();
|
2021-09-16 00:08:54 +00:00
|
|
|
// If origins are close enough, our capsule prism is cylinder
|
|
|
|
// with one origin which we don't even need to rotate.
|
|
|
|
//
|
|
|
|
// Other advantage of early-return is that we don't
|
|
|
|
// later divide by zero and return NaN
|
2021-09-16 16:45:17 +00:00
|
|
|
if len < std::f32::EPSILON * 10.0 {
|
2021-09-16 00:08:54 +00:00
|
|
|
Some((*p0, *p0))
|
|
|
|
} else {
|
|
|
|
// Apply orientation to origins of prism.
|
|
|
|
//
|
|
|
|
// We do this by building line between them,
|
|
|
|
// rotate it and then split back to origins.
|
|
|
|
// (Otherwise we will need to do the same with each
|
|
|
|
// origin).
|
|
|
|
//
|
|
|
|
// Cast it to 3d and then convert it back to 2d
|
|
|
|
// to apply quaternion.
|
|
|
|
let a = a.with_z(0.0);
|
|
|
|
let a = ori * a;
|
|
|
|
let a = a.xy();
|
|
|
|
// Previous operation could shrink x and y coordinates
|
|
|
|
// if orientation had Z parameter.
|
|
|
|
// Make sure we have the same length as before
|
|
|
|
// (and scale it, while we on it).
|
|
|
|
let a = a.normalized() * scale * len;
|
|
|
|
let p0 = -a / 2.0;
|
|
|
|
let p1 = a / 2.0;
|
|
|
|
|
|
|
|
Some((p0, p1))
|
|
|
|
}
|
2021-09-15 15:11:41 +00:00
|
|
|
},
|
2021-09-16 16:45:17 +00:00
|
|
|
Some(Collider::Voxel { .. } | Collider::Point) | None => None,
|
2021-09-15 15:11:41 +00:00
|
|
|
};
|
2021-09-15 21:27:48 +00:00
|
|
|
phys_cache.origins = origins;
|
2021-09-15 15:11:41 +00:00
|
|
|
phys_cache.ori = ori;
|
2020-10-21 10:23:34 +00:00
|
|
|
}
|
2021-03-11 16:48:59 +00:00
|
|
|
}
|
2021-03-11 22:27:03 +00:00
|
|
|
|
2021-03-16 05:13:52 +00:00
|
|
|
fn construct_spatial_grid(&mut self) -> SpatialGrid {
|
|
|
|
span!(_guard, "Construct spatial grid");
|
|
|
|
let PhysicsData {
|
|
|
|
ref read,
|
|
|
|
ref write,
|
|
|
|
} = self;
|
|
|
|
// NOTE: i32 places certain constraints on how far out collision works
|
|
|
|
// NOTE: uses the radius of the entity and their current position rather than
|
|
|
|
// the radius of their bounding sphere for the current frame of movement
|
|
|
|
// because the nonmoving entity is what is collided against in the inner
|
|
|
|
// loop of the pushback collision code
|
|
|
|
// TODO: maintain frame to frame? (requires handling deletion)
|
|
|
|
// TODO: if not maintaining frame to frame consider counting entities to
|
|
|
|
// preallocate?
|
|
|
|
// TODO: assess parallelizing (overhead might dominate here? would need to merge
|
|
|
|
// the vecs in each hashmap)
|
2021-03-16 06:31:58 +00:00
|
|
|
let lg2_cell_size = 5;
|
2021-03-16 05:13:52 +00:00
|
|
|
let lg2_large_cell_size = 6;
|
|
|
|
let radius_cutoff = 8;
|
|
|
|
let mut spatial_grid = SpatialGrid::new(lg2_cell_size, lg2_large_cell_size, radius_cutoff);
|
|
|
|
for (entity, pos, phys_cache, _, _, _, _, _) in (
|
|
|
|
&read.entities,
|
|
|
|
&write.positions,
|
|
|
|
&write.previous_phys_cache,
|
|
|
|
write.velocities.mask(),
|
|
|
|
!&read.projectiles, // Not needed because they are skipped in the inner loop below
|
|
|
|
!&read.mountings,
|
|
|
|
!&read.beams,
|
|
|
|
!&read.shockwaves,
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
|
|
|
// Note: to not get too fine grained we use a 2D grid for now
|
|
|
|
let radius_2d = phys_cache.scaled_radius.ceil() as u32;
|
|
|
|
let pos_2d = pos.0.xy().map(|e| e as i32);
|
|
|
|
const POS_TRUNCATION_ERROR: u32 = 1;
|
|
|
|
spatial_grid.insert(pos_2d, radius_2d + POS_TRUNCATION_ERROR, entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
spatial_grid
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_pushback(&mut self, job: &mut Job<Sys>, spatial_grid: &SpatialGrid) {
|
2021-03-15 02:36:59 +00:00
|
|
|
span!(_guard, "Apply pushback");
|
2021-03-04 14:00:16 +00:00
|
|
|
job.cpu_stats.measure(ParMode::Rayon);
|
2021-03-12 22:14:08 +00:00
|
|
|
let PhysicsData {
|
|
|
|
ref read,
|
|
|
|
ref mut write,
|
2021-03-11 22:27:03 +00:00
|
|
|
} = self;
|
2021-03-12 22:14:08 +00:00
|
|
|
let (positions, previous_phys_cache) = (&write.positions, &write.previous_phys_cache);
|
2020-10-20 16:31:19 +00:00
|
|
|
let metrics = (
|
2021-03-12 22:14:08 +00:00
|
|
|
&read.entities,
|
2021-03-11 16:48:59 +00:00
|
|
|
positions,
|
2021-03-12 22:14:08 +00:00
|
|
|
&mut write.velocities,
|
2021-03-11 16:48:59 +00:00
|
|
|
previous_phys_cache,
|
2021-03-23 09:51:53 +00:00
|
|
|
&read.masses,
|
2021-03-12 22:14:08 +00:00
|
|
|
read.colliders.maybe(),
|
|
|
|
!&read.mountings,
|
|
|
|
read.stickies.maybe(),
|
|
|
|
&mut write.physics_states,
|
2021-09-11 12:06:13 +00:00
|
|
|
// 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.
|
2021-03-12 22:14:08 +00:00
|
|
|
read.projectiles.maybe(),
|
|
|
|
read.char_states.maybe(),
|
2020-08-24 17:24:44 +00:00
|
|
|
)
|
2020-10-20 16:31:19 +00:00
|
|
|
.par_join()
|
2021-05-03 07:50:24 +00:00
|
|
|
.map(|(e, p, v, vd, m, c, _, sticky, ph, pr, c_s)| {
|
|
|
|
(e, p, v, vd, m, c, sticky, ph, pr, c_s)
|
2020-08-24 17:24:44 +00:00
|
|
|
})
|
2021-03-13 06:48:30 +00:00
|
|
|
.map_init(
|
|
|
|
|| {
|
|
|
|
prof_span!(guard, "physics e<>e rayon job");
|
|
|
|
guard
|
|
|
|
},
|
|
|
|
|_guard,
|
2020-11-06 01:22:26 +00:00
|
|
|
(
|
|
|
|
entity,
|
|
|
|
pos,
|
|
|
|
vel,
|
2021-02-14 17:09:52 +00:00
|
|
|
previous_cache,
|
2020-11-06 01:22:26 +00:00
|
|
|
mass,
|
|
|
|
collider,
|
2021-05-03 07:50:24 +00:00
|
|
|
sticky,
|
2021-09-15 21:27:48 +00:00
|
|
|
mut physics,
|
2020-11-06 01:22:26 +00:00
|
|
|
projectile,
|
|
|
|
char_state_maybe,
|
|
|
|
)| {
|
2021-05-03 07:50:24 +00:00
|
|
|
let is_sticky = sticky.is_some();
|
2021-08-13 20:16:38 +00:00
|
|
|
let is_mid_air = physics.on_surface().is_none();
|
2021-04-21 17:38:56 +00:00
|
|
|
let mut entity_entity_collision_checks = 0;
|
|
|
|
let mut entity_entity_collisions = 0;
|
|
|
|
|
2021-09-11 12:06:13 +00:00
|
|
|
// TODO: quick fix for bad performance. At extrememly high
|
|
|
|
// velocities use oriented rectangles at some threshold of
|
|
|
|
// displacement/radius to query the spatial grid and limit
|
|
|
|
// max displacement per tick somehow.
|
2021-04-21 17:38:56 +00:00
|
|
|
if previous_cache.collision_boundary > 128.0 {
|
|
|
|
return PhysicsMetrics {
|
|
|
|
entity_entity_collision_checks,
|
|
|
|
entity_entity_collisions,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-22 17:55:10 +00:00
|
|
|
let z_limits = calc_z_limit(char_state_maybe, collider);
|
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
|
|
|
|
2021-04-21 17:10:13 +00:00
|
|
|
let query_center = previous_cache.center.xy();
|
|
|
|
let query_radius = previous_cache.collision_boundary;
|
2021-03-16 06:31:58 +00:00
|
|
|
|
|
|
|
spatial_grid
|
2021-04-21 17:10:13 +00:00
|
|
|
.in_circle_aabr(query_center, query_radius)
|
2021-03-16 06:31:58 +00:00
|
|
|
.filter_map(|entity| {
|
2021-09-15 15:11:41 +00:00
|
|
|
let uid = read.uids.get(entity)?;
|
|
|
|
let pos = positions.get(entity)?;
|
|
|
|
let previous_cache = previous_phys_cache.get(entity)?;
|
|
|
|
let mass = read.masses.get(entity)?;
|
|
|
|
|
|
|
|
Some((
|
|
|
|
entity,
|
|
|
|
uid,
|
|
|
|
pos,
|
|
|
|
previous_cache,
|
|
|
|
mass,
|
|
|
|
read.colliders.get(entity),
|
|
|
|
read.char_states.get(entity),
|
|
|
|
))
|
2021-03-16 06:31:58 +00:00
|
|
|
})
|
|
|
|
.for_each(
|
|
|
|
|(
|
|
|
|
entity_other,
|
|
|
|
other,
|
|
|
|
pos_other,
|
|
|
|
previous_cache_other,
|
|
|
|
mass_other,
|
|
|
|
collider_other,
|
|
|
|
char_state_other_maybe,
|
|
|
|
)| {
|
|
|
|
let collision_boundary = previous_cache.collision_boundary
|
|
|
|
+ previous_cache_other.collision_boundary;
|
|
|
|
if previous_cache
|
|
|
|
.center
|
|
|
|
.distance_squared(previous_cache_other.center)
|
|
|
|
> collision_boundary.powi(2)
|
|
|
|
|| entity == entity_other
|
2021-03-11 22:27:03 +00:00
|
|
|
{
|
2021-03-16 06:31:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-03-16 05:13:52 +00:00
|
|
|
|
2021-03-16 06:31:58 +00:00
|
|
|
let z_limits_other =
|
|
|
|
calc_z_limit(char_state_other_maybe, collider_other);
|
|
|
|
|
|
|
|
entity_entity_collision_checks += 1;
|
|
|
|
|
|
|
|
const MIN_COLLISION_DIST: f32 = 0.3;
|
2021-09-16 00:08:54 +00:00
|
|
|
|
2021-03-16 06:31:58 +00:00
|
|
|
let increments = ((previous_cache.velocity_dt
|
|
|
|
- previous_cache_other.velocity_dt)
|
|
|
|
.magnitude()
|
|
|
|
/ MIN_COLLISION_DIST)
|
|
|
|
.max(1.0)
|
|
|
|
.ceil()
|
|
|
|
as usize;
|
|
|
|
let step_delta = 1.0 / increments as f32;
|
2021-09-15 15:11:41 +00:00
|
|
|
|
2021-08-28 22:31:44 +00:00
|
|
|
let mut collision_registered = false;
|
2021-03-16 06:31:58 +00:00
|
|
|
|
|
|
|
for i in 0..increments {
|
|
|
|
let factor = i as f32 * step_delta;
|
2021-09-16 16:45:17 +00:00
|
|
|
// We are not interested if collision succeed
|
|
|
|
// or no as of now.
|
|
|
|
// Collision reaction is done inside.
|
|
|
|
let _ = resolve_e2e_collision(
|
2021-09-15 21:27:48 +00:00
|
|
|
// utility variables for our entity
|
|
|
|
&mut collision_registered,
|
|
|
|
&mut entity_entity_collisions,
|
|
|
|
factor,
|
|
|
|
&mut physics,
|
2021-09-15 15:11:41 +00:00
|
|
|
char_state_maybe,
|
2021-09-15 21:27:48 +00:00
|
|
|
&mut vel_delta,
|
|
|
|
step_delta,
|
|
|
|
// physics flags
|
|
|
|
is_mid_air,
|
|
|
|
is_sticky,
|
|
|
|
is_projectile,
|
|
|
|
// entity we colliding with
|
|
|
|
*other,
|
|
|
|
// symetrical collider context
|
|
|
|
pos,
|
|
|
|
pos_other,
|
|
|
|
previous_cache,
|
|
|
|
previous_cache_other,
|
|
|
|
z_limits,
|
|
|
|
z_limits_other,
|
|
|
|
collider,
|
|
|
|
collider_other,
|
|
|
|
*mass,
|
|
|
|
*mass_other,
|
2021-09-16 16:45:17 +00:00
|
|
|
);
|
2021-03-16 05:13:52 +00:00
|
|
|
}
|
2021-03-16 06:31:58 +00:00
|
|
|
},
|
|
|
|
);
|
2020-08-25 10:01:17 +00:00
|
|
|
|
2020-10-20 16:31:19 +00:00
|
|
|
// Change velocity
|
2021-03-12 22:14:08 +00:00
|
|
|
vel.0 += vel_delta * read.dt.0;
|
2021-03-13 06:48:30 +00:00
|
|
|
|
|
|
|
// Metrics
|
|
|
|
PhysicsMetrics {
|
|
|
|
entity_entity_collision_checks,
|
|
|
|
entity_entity_collisions,
|
|
|
|
}
|
2020-10-20 16:31:19 +00:00
|
|
|
},
|
|
|
|
)
|
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
|
|
|
});
|
2021-03-12 22:14:08 +00:00
|
|
|
write.physics_metrics.entity_entity_collision_checks =
|
2021-03-11 22:27:03 +00:00
|
|
|
metrics.entity_entity_collision_checks;
|
2021-03-12 22:14:08 +00:00
|
|
|
write.physics_metrics.entity_entity_collisions = metrics.entity_entity_collisions;
|
2021-03-11 16:48:59 +00:00
|
|
|
}
|
2020-08-13 08:12:14 +00:00
|
|
|
|
2021-03-17 03:32:03 +00:00
|
|
|
fn construct_voxel_collider_spatial_grid(&mut self) -> SpatialGrid {
|
|
|
|
span!(_guard, "Construct voxel collider spatial grid");
|
|
|
|
let PhysicsData {
|
|
|
|
ref read,
|
|
|
|
ref write,
|
|
|
|
} = self;
|
|
|
|
// NOTE: i32 places certain constraints on how far out collision works
|
|
|
|
// NOTE: uses the radius of the entity and their current position rather than
|
|
|
|
// the radius of their bounding sphere for the current frame of movement
|
|
|
|
// because the nonmoving entity is what is collided against in the inner
|
|
|
|
// loop of the pushback collision code
|
|
|
|
// TODO: optimize these parameters (especially radius cutoff)
|
|
|
|
let lg2_cell_size = 7; // 128
|
|
|
|
let lg2_large_cell_size = 8; // 256
|
|
|
|
let radius_cutoff = 64;
|
|
|
|
let mut spatial_grid = SpatialGrid::new(lg2_cell_size, lg2_large_cell_size, radius_cutoff);
|
|
|
|
// TODO: give voxel colliders their own component type
|
|
|
|
for (entity, pos, collider, ori) in (
|
|
|
|
&read.entities,
|
|
|
|
&write.positions,
|
|
|
|
&read.colliders,
|
|
|
|
&write.orientations,
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
|
|
|
let voxel_id = match collider {
|
|
|
|
Collider::Voxel { id } => id,
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(voxel_collider) = VOXEL_COLLIDER_MANIFEST.read().colliders.get(&*voxel_id) {
|
|
|
|
let sphere = voxel_collider_bounding_sphere(voxel_collider, pos, ori);
|
|
|
|
let radius = sphere.radius.ceil() as u32;
|
|
|
|
let pos_2d = sphere.center.xy().map(|e| e as i32);
|
|
|
|
const POS_TRUNCATION_ERROR: u32 = 1;
|
|
|
|
spatial_grid.insert(pos_2d, radius + POS_TRUNCATION_ERROR, entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spatial_grid
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_movement_and_terrain(
|
|
|
|
&mut self,
|
|
|
|
job: &mut Job<Sys>,
|
|
|
|
voxel_collider_spatial_grid: &SpatialGrid,
|
|
|
|
) {
|
2021-03-12 22:14:08 +00:00
|
|
|
let PhysicsData {
|
|
|
|
ref read,
|
|
|
|
ref mut write,
|
2021-03-11 22:27:03 +00:00
|
|
|
} = self;
|
2021-03-16 07:40:31 +00:00
|
|
|
|
2021-07-18 00:13:36 +00:00
|
|
|
prof_span!(guard, "insert PosVelOriDefer");
|
2021-03-16 07:40:31 +00:00
|
|
|
// NOTE: keep in sync with join below
|
|
|
|
(
|
|
|
|
&read.entities,
|
|
|
|
read.colliders.mask(),
|
|
|
|
&write.positions,
|
|
|
|
&write.velocities,
|
2021-07-18 00:13:36 +00:00
|
|
|
&write.orientations,
|
2021-03-16 07:40:31 +00:00
|
|
|
write.orientations.mask(),
|
|
|
|
write.physics_states.mask(),
|
2021-07-18 00:13:36 +00:00
|
|
|
!&write.pos_vel_ori_defers, // This is the one we are adding
|
2021-03-16 07:40:31 +00:00
|
|
|
write.previous_phys_cache.mask(),
|
|
|
|
!&read.mountings,
|
|
|
|
)
|
|
|
|
.join()
|
2021-07-18 00:13:36 +00:00
|
|
|
.map(|t| (t.0, *t.2, *t.3, *t.4))
|
2021-03-16 07:40:31 +00:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.into_iter()
|
2021-07-18 00:13:36 +00:00
|
|
|
.for_each(|(entity, pos, vel, ori)| {
|
|
|
|
let _ = write.pos_vel_ori_defers.insert(entity, PosVelOriDefer {
|
2021-03-16 18:04:28 +00:00
|
|
|
pos: Some(pos),
|
|
|
|
vel: Some(vel),
|
2021-07-18 00:13:36 +00:00
|
|
|
ori: Some(ori),
|
2021-03-16 18:04:28 +00:00
|
|
|
});
|
2021-03-16 07:40:31 +00:00
|
|
|
});
|
|
|
|
drop(guard);
|
|
|
|
|
2019-06-11 19:28:25 +00:00
|
|
|
// Apply movement inputs
|
2021-05-29 00:25:57 +00:00
|
|
|
span!(guard, "Apply movement");
|
|
|
|
let (positions, velocities) = (&write.positions, &mut write.velocities);
|
2021-03-13 19:40:00 +00:00
|
|
|
|
|
|
|
// First pass: update velocity using air resistance and gravity for each entity.
|
|
|
|
// We do this in a first pass because it helps keep things more stable for
|
|
|
|
// entities that are anchored to other entities (such as airships).
|
|
|
|
(
|
|
|
|
positions,
|
|
|
|
velocities,
|
2021-04-22 19:12:35 +00:00
|
|
|
read.stickies.maybe(),
|
2021-03-23 09:51:53 +00:00
|
|
|
&read.bodies,
|
2021-04-27 14:41:48 +00:00
|
|
|
read.character_states.maybe(),
|
2021-03-13 19:40:00 +00:00
|
|
|
&write.physics_states,
|
2021-03-23 09:51:53 +00:00
|
|
|
&read.masses,
|
|
|
|
&read.densities,
|
2021-03-13 19:40:00 +00:00
|
|
|
!&read.mountings,
|
|
|
|
)
|
|
|
|
.par_join()
|
2021-03-16 07:40:31 +00:00
|
|
|
.for_each_init(
|
|
|
|
|| {
|
|
|
|
prof_span!(guard, "velocity update rayon job");
|
|
|
|
guard
|
|
|
|
},
|
2021-04-27 14:41:48 +00:00
|
|
|
|_guard,
|
|
|
|
(
|
|
|
|
pos,
|
|
|
|
vel,
|
|
|
|
sticky,
|
|
|
|
body,
|
|
|
|
character_state,
|
|
|
|
physics_state,
|
|
|
|
mass,
|
|
|
|
density,
|
|
|
|
_,
|
|
|
|
)| {
|
2021-03-16 07:40:31 +00:00
|
|
|
let in_loaded_chunk = read
|
|
|
|
.terrain
|
|
|
|
.get_key(read.terrain.pos_key(pos.0.map(|e| e.floor() as i32)))
|
|
|
|
.is_some();
|
2021-03-23 09:51:53 +00:00
|
|
|
|
|
|
|
// Apply physics only if in a loaded chunk
|
2021-04-22 19:12:35 +00:00
|
|
|
if in_loaded_chunk
|
|
|
|
// And not already stuck on a block (e.g., for arrows)
|
|
|
|
&& !(physics_state.on_surface().is_some() && sticky.is_some())
|
|
|
|
{
|
2021-09-11 12:06:13 +00:00
|
|
|
// Clamp dt to an effective 10 TPS, to prevent gravity
|
|
|
|
// from slamming the players into the floor when
|
|
|
|
// stationary if other systems cause the server
|
2021-03-23 09:51:53 +00:00
|
|
|
// to lag (as observed in the 0.9 release party).
|
|
|
|
let dt = DeltaTime(read.dt.0.min(0.1));
|
|
|
|
|
|
|
|
match physics_state.in_fluid {
|
|
|
|
None => {
|
|
|
|
vel.0.z -= dt.0 * GRAVITY;
|
|
|
|
},
|
|
|
|
Some(fluid) => {
|
2021-05-22 17:56:13 +00:00
|
|
|
let wings = match character_state {
|
|
|
|
Some(&CharacterState::Glide(states::glide::Data {
|
|
|
|
aspect_ratio,
|
|
|
|
planform_area,
|
|
|
|
ori,
|
|
|
|
..
|
|
|
|
})) => Some(Wings {
|
|
|
|
aspect_ratio,
|
|
|
|
planform_area,
|
|
|
|
ori,
|
|
|
|
}),
|
|
|
|
|
|
|
|
_ => None,
|
|
|
|
};
|
2021-03-23 09:51:53 +00:00
|
|
|
vel.0 = integrate_forces(
|
2021-04-27 14:41:48 +00:00
|
|
|
&dt,
|
|
|
|
*vel,
|
2021-05-22 17:56:13 +00:00
|
|
|
(body, wings.as_ref()),
|
2021-04-27 14:41:48 +00:00
|
|
|
density,
|
|
|
|
mass,
|
|
|
|
&fluid,
|
|
|
|
GRAVITY,
|
2021-03-23 09:51:53 +00:00
|
|
|
)
|
|
|
|
.0
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 07:40:31 +00:00
|
|
|
},
|
|
|
|
);
|
2021-05-29 00:25:57 +00:00
|
|
|
drop(guard);
|
|
|
|
job.cpu_stats.measure(ParMode::Single);
|
|
|
|
|
|
|
|
// Second pass: resolve collisions for terrain-like entities, this is required
|
|
|
|
// in order to update their positions before resolving collisions for
|
|
|
|
// non-terrain-like entities, since otherwise, collision is resolved
|
|
|
|
// based on where the terrain-like entity was in the previous frame.
|
|
|
|
Self::resolve_et_collision(job, read, write, voxel_collider_spatial_grid, true);
|
2021-03-13 19:40:00 +00:00
|
|
|
|
2021-05-29 00:25:57 +00:00
|
|
|
// Third pass: resolve collisions for non-terrain-like entities
|
|
|
|
Self::resolve_et_collision(job, read, write, voxel_collider_spatial_grid, false);
|
2021-07-18 00:13:36 +00:00
|
|
|
|
|
|
|
// Update cached 'old' physics values to the current values ready for the next
|
|
|
|
// tick
|
|
|
|
prof_span!(guard, "record ori into phys_cache");
|
2021-09-17 21:34:28 +00:00
|
|
|
for (pos, ori, previous_phys_cache, _) in (
|
|
|
|
&write.positions,
|
2021-07-18 00:13:36 +00:00
|
|
|
&write.orientations,
|
|
|
|
&mut write.previous_phys_cache,
|
|
|
|
&read.colliders,
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2021-07-29 07:08:43 +00:00
|
|
|
// Note: updating ori with the rest of the cache values above was attempted but
|
|
|
|
// it did not work (investigate root cause?)
|
2021-09-17 21:34:28 +00:00
|
|
|
previous_phys_cache.pos = Some(*pos);
|
2021-07-18 00:13:36 +00:00
|
|
|
previous_phys_cache.ori = ori.to_quat();
|
|
|
|
}
|
|
|
|
drop(guard);
|
2021-05-29 00:25:57 +00:00
|
|
|
}
|
2021-03-13 19:40:00 +00:00
|
|
|
|
2021-05-29 00:25:57 +00:00
|
|
|
fn resolve_et_collision(
|
|
|
|
job: &mut Job<Sys>,
|
|
|
|
read: &PhysicsRead,
|
|
|
|
write: &mut PhysicsWrite,
|
|
|
|
voxel_collider_spatial_grid: &SpatialGrid,
|
|
|
|
terrain_like_entities: bool,
|
|
|
|
) {
|
|
|
|
let (positions, velocities, previous_phys_cache, orientations) = (
|
|
|
|
&write.positions,
|
|
|
|
&write.velocities,
|
|
|
|
&write.previous_phys_cache,
|
|
|
|
&write.orientations,
|
|
|
|
);
|
|
|
|
span!(guard, "Apply terrain collision");
|
|
|
|
job.cpu_stats.measure(ParMode::Rayon);
|
2021-03-28 23:29:48 +00:00
|
|
|
let (land_on_grounds, mut outcomes) = (
|
2021-03-12 22:14:08 +00:00
|
|
|
&read.entities,
|
|
|
|
read.scales.maybe(),
|
|
|
|
read.stickies.maybe(),
|
|
|
|
&read.colliders,
|
2021-03-11 22:27:03 +00:00
|
|
|
positions,
|
2021-03-12 11:32:19 +00:00
|
|
|
velocities,
|
2021-03-12 04:42:53 +00:00
|
|
|
orientations,
|
2021-03-13 20:51:32 +00:00
|
|
|
read.bodies.maybe(),
|
2021-03-14 23:33:54 +00:00
|
|
|
read.character_states.maybe(),
|
2021-03-12 22:14:08 +00:00
|
|
|
&mut write.physics_states,
|
2021-07-18 00:13:36 +00:00
|
|
|
&mut write.pos_vel_ori_defers,
|
2021-03-11 22:27:03 +00:00
|
|
|
previous_phys_cache,
|
2021-03-12 22:14:08 +00:00
|
|
|
!&read.mountings,
|
2021-03-11 22:27:03 +00:00
|
|
|
)
|
|
|
|
.par_join()
|
2021-05-29 00:25:57 +00:00
|
|
|
.filter(|tuple| matches!(tuple.3, Collider::Voxel { .. }) == terrain_like_entities)
|
2021-03-16 07:40:31 +00:00
|
|
|
.map_init(
|
|
|
|
|| {
|
|
|
|
prof_span!(guard, "physics e<>t rayon job");
|
|
|
|
guard
|
|
|
|
},
|
|
|
|
|_guard,
|
2021-03-11 22:27:03 +00:00
|
|
|
(
|
|
|
|
entity,
|
|
|
|
scale,
|
|
|
|
sticky,
|
|
|
|
collider,
|
|
|
|
pos,
|
2021-03-12 11:32:19 +00:00
|
|
|
vel,
|
2021-07-18 00:13:36 +00:00
|
|
|
ori,
|
2021-03-13 20:51:32 +00:00
|
|
|
body,
|
2021-03-14 23:33:54 +00:00
|
|
|
character_state,
|
2021-03-11 22:27:03 +00:00
|
|
|
mut physics_state,
|
2021-07-18 00:13:36 +00:00
|
|
|
pos_vel_ori_defer,
|
2021-09-17 21:34:28 +00:00
|
|
|
previous_cache,
|
2021-03-11 22:27:03 +00:00
|
|
|
_,
|
|
|
|
)| {
|
2021-03-16 07:40:31 +00:00
|
|
|
let mut land_on_ground = None;
|
2021-03-28 23:29:48 +00:00
|
|
|
let mut outcomes = Vec::new();
|
2021-09-11 12:06:13 +00:00
|
|
|
// Defer the writes of positions, velocities and orientations
|
|
|
|
// to allow an inner loop over terrain-like entities.
|
2021-03-16 18:04:28 +00:00
|
|
|
let old_vel = *vel;
|
2021-03-12 11:32:19 +00:00
|
|
|
let mut vel = *vel;
|
2021-07-18 00:13:36 +00:00
|
|
|
let old_ori = *ori;
|
|
|
|
let mut ori = *ori;
|
2021-03-16 07:40:31 +00:00
|
|
|
|
2021-03-11 22:27:03 +00:00
|
|
|
let scale = if let Collider::Voxel { .. } = collider {
|
|
|
|
scale.map(|s| s.0).unwrap_or(1.0)
|
|
|
|
} else {
|
|
|
|
// TODO: Use scale & actual proportions when pathfinding is good
|
|
|
|
// enough to manage irregular entity sizes
|
|
|
|
1.0
|
|
|
|
};
|
|
|
|
|
2021-03-12 22:14:08 +00:00
|
|
|
let in_loaded_chunk = read
|
2021-03-11 22:27:03 +00:00
|
|
|
.terrain
|
2021-03-12 22:14:08 +00:00
|
|
|
.get_key(read.terrain.pos_key(pos.0.map(|e| e.floor() as i32)))
|
2021-03-11 22:27:03 +00:00
|
|
|
.is_some();
|
|
|
|
|
|
|
|
// Don't move if we're not in a loaded chunk
|
|
|
|
let pos_delta = if in_loaded_chunk {
|
2021-03-13 19:40:00 +00:00
|
|
|
vel.0 * read.dt.0
|
2021-03-11 22:27:03 +00:00
|
|
|
} else {
|
|
|
|
Vec3::zero()
|
|
|
|
};
|
|
|
|
|
2021-09-11 12:06:13 +00:00
|
|
|
// What's going on here?
|
|
|
|
// Because collisions need to be resolved against multiple
|
2021-03-13 16:14:13 +00:00
|
|
|
// colliders, this code takes the current position and
|
|
|
|
// propagates it forward according to velocity to find a
|
2021-09-11 12:06:13 +00:00
|
|
|
// 'target' position.
|
|
|
|
//
|
|
|
|
// This is where we'd ideally end up at the end of the tick,
|
2021-03-13 16:14:13 +00:00
|
|
|
// assuming no collisions. Then, we refine this target by
|
|
|
|
// stepping from the original position to the target for
|
2021-09-11 12:06:13 +00:00
|
|
|
// every obstacle, refining the target position as we go.
|
|
|
|
//
|
|
|
|
// It's not perfect, but it works pretty well in practice.
|
|
|
|
// Oddities can occur on the intersection between multiple
|
|
|
|
// colliders, but it's not like any game physics system
|
|
|
|
// resolves these sort of things well anyway.
|
|
|
|
// At the very least, we don't do things that result in glitchy
|
2021-03-13 16:14:13 +00:00
|
|
|
// velocities or entirely broken position snapping.
|
2021-03-13 15:36:56 +00:00
|
|
|
let mut tgt_pos = pos.0 + pos_delta;
|
|
|
|
|
2021-06-20 03:51:04 +00:00
|
|
|
let was_on_ground = physics_state.on_ground.is_some();
|
2021-04-27 14:41:48 +00:00
|
|
|
let block_snap =
|
|
|
|
body.map_or(false, |b| !matches!(b, Body::Object(_) | Body::Ship(_)));
|
2021-03-14 23:33:54 +00:00
|
|
|
let climbing =
|
2021-03-20 21:56:54 +00:00
|
|
|
character_state.map_or(false, |cs| matches!(cs, CharacterState::Climb(_)));
|
2021-03-12 18:53:01 +00:00
|
|
|
|
2021-03-12 22:14:08 +00:00
|
|
|
match &collider {
|
2021-03-11 22:27:03 +00:00
|
|
|
Collider::Voxel { .. } => {
|
2021-09-11 12:06:13 +00:00
|
|
|
// For now, treat entities with voxel colliders
|
|
|
|
// as their bounding cylinders for the purposes of
|
|
|
|
// colliding them with terrain.
|
|
|
|
//
|
|
|
|
// Additionally, multiply radius by 0.1 to make
|
|
|
|
// the cylinder smaller to avoid lag.
|
2021-09-16 16:45:17 +00:00
|
|
|
let radius = collider.bounding_radius() * scale * 0.1;
|
2021-09-16 18:59:52 +00:00
|
|
|
let (_, z_max) = collider.get_z_limits(scale);
|
|
|
|
let z_min = 0.0;
|
2021-03-11 22:27:03 +00:00
|
|
|
|
2021-03-13 16:43:01 +00:00
|
|
|
let mut cpos = *pos;
|
2021-03-11 22:27:03 +00:00
|
|
|
let cylinder = (radius, z_min, z_max);
|
2021-03-15 02:36:59 +00:00
|
|
|
box_voxel_collision(
|
2021-03-11 22:27:03 +00:00
|
|
|
cylinder,
|
2021-03-12 22:14:08 +00:00
|
|
|
&*read.terrain,
|
2021-03-11 22:27:03 +00:00
|
|
|
entity,
|
2021-03-13 15:36:56 +00:00
|
|
|
&mut cpos,
|
|
|
|
tgt_pos,
|
2021-03-12 11:32:19 +00:00
|
|
|
&mut vel,
|
2021-03-11 22:27:03 +00:00
|
|
|
&mut physics_state,
|
2021-03-12 13:26:02 +00:00
|
|
|
Vec3::zero(),
|
2021-03-12 22:14:08 +00:00
|
|
|
&read.dt,
|
2021-03-12 18:53:01 +00:00
|
|
|
was_on_ground,
|
2021-03-13 20:51:32 +00:00
|
|
|
block_snap,
|
2021-03-14 23:33:54 +00:00
|
|
|
climbing,
|
2021-03-16 07:40:31 +00:00
|
|
|
|entity, vel| land_on_ground = Some((entity, vel)),
|
2021-05-24 00:45:22 +00:00
|
|
|
read,
|
2021-03-11 22:27:03 +00:00
|
|
|
);
|
2021-03-13 15:36:56 +00:00
|
|
|
tgt_pos = cpos.0;
|
2021-03-11 22:27:03 +00:00
|
|
|
},
|
2021-09-11 12:06:13 +00:00
|
|
|
Collider::CapsulePrism {
|
2021-09-16 18:59:52 +00:00
|
|
|
z_min: _,
|
2021-09-11 12:06:13 +00:00
|
|
|
z_max,
|
2021-09-15 15:11:41 +00:00
|
|
|
p0: _,
|
|
|
|
p1: _,
|
|
|
|
radius: _,
|
2021-09-11 12:06:13 +00:00
|
|
|
} => {
|
2021-03-11 22:27:03 +00:00
|
|
|
// Scale collider
|
2021-09-16 16:45:17 +00:00
|
|
|
let radius = collider.bounding_radius().min(0.45) * scale;
|
2021-09-16 18:59:52 +00:00
|
|
|
let z_min = 0.0;
|
2021-03-11 22:27:03 +00:00
|
|
|
let z_max = z_max.clamped(1.2, 1.95) * scale;
|
|
|
|
|
|
|
|
let cylinder = (radius, z_min, z_max);
|
2021-03-13 16:43:01 +00:00
|
|
|
let mut cpos = *pos;
|
2021-03-15 02:36:59 +00:00
|
|
|
box_voxel_collision(
|
2021-03-11 22:27:03 +00:00
|
|
|
cylinder,
|
2021-03-12 22:14:08 +00:00
|
|
|
&*read.terrain,
|
2021-03-11 22:27:03 +00:00
|
|
|
entity,
|
2021-03-13 15:36:56 +00:00
|
|
|
&mut cpos,
|
|
|
|
tgt_pos,
|
2021-03-12 11:32:19 +00:00
|
|
|
&mut vel,
|
2021-03-11 22:27:03 +00:00
|
|
|
&mut physics_state,
|
2021-03-12 13:26:02 +00:00
|
|
|
Vec3::zero(),
|
2021-03-12 22:14:08 +00:00
|
|
|
&read.dt,
|
2021-03-12 18:53:01 +00:00
|
|
|
was_on_ground,
|
2021-03-13 20:51:32 +00:00
|
|
|
block_snap,
|
2021-03-14 23:33:54 +00:00
|
|
|
climbing,
|
2021-03-16 07:40:31 +00:00
|
|
|
|entity, vel| land_on_ground = Some((entity, vel)),
|
2021-05-24 00:45:22 +00:00
|
|
|
read,
|
2021-03-11 22:27:03 +00:00
|
|
|
);
|
2021-03-29 20:50:50 +00:00
|
|
|
|
|
|
|
// Sticky things shouldn't move when on a surface
|
|
|
|
if physics_state.on_surface().is_some() && sticky.is_some() {
|
|
|
|
vel.0 = physics_state.ground_vel;
|
|
|
|
}
|
|
|
|
|
2021-03-13 15:36:56 +00:00
|
|
|
tgt_pos = cpos.0;
|
2021-03-11 22:27:03 +00:00
|
|
|
},
|
|
|
|
Collider::Point => {
|
2021-03-13 16:43:01 +00:00
|
|
|
let mut pos = *pos;
|
2021-03-13 16:14:13 +00:00
|
|
|
|
2021-09-11 12:06:13 +00:00
|
|
|
// TODO: If the velocity is exactly 0,
|
|
|
|
// a raycast may not pick up the current block.
|
|
|
|
//
|
|
|
|
// Handle this.
|
2021-03-28 23:29:48 +00:00
|
|
|
let (dist, block) = if let Some(block) = read
|
2021-03-11 22:27:03 +00:00
|
|
|
.terrain
|
2021-03-28 23:29:48 +00:00
|
|
|
.get(pos.0.map(|e| e.floor() as i32))
|
|
|
|
.ok()
|
2021-08-30 15:02:13 +00:00
|
|
|
.filter(|b| b.is_solid())
|
2021-03-28 23:29:48 +00:00
|
|
|
{
|
|
|
|
(0.0, Some(block))
|
|
|
|
} else {
|
|
|
|
let (dist, block) = read
|
|
|
|
.terrain
|
|
|
|
.ray(pos.0, pos.0 + pos_delta)
|
2021-08-30 15:02:13 +00:00
|
|
|
.until(|block: &Block| block.is_solid())
|
2021-03-28 23:29:48 +00:00
|
|
|
.ignore_error()
|
|
|
|
.cast();
|
2021-09-11 12:06:13 +00:00
|
|
|
// Can't fail since we do ignore_error above
|
|
|
|
(dist, block.unwrap())
|
2021-03-28 23:29:48 +00:00
|
|
|
};
|
2021-03-11 22:27:03 +00:00
|
|
|
|
2021-03-12 22:14:08 +00:00
|
|
|
pos.0 += pos_delta.try_normalized().unwrap_or_else(Vec3::zero) * dist;
|
2021-03-11 22:27:03 +00:00
|
|
|
|
2021-03-28 23:29:48 +00:00
|
|
|
// TODO: Not all projectiles should count as sticky!
|
|
|
|
if sticky.is_some() {
|
|
|
|
if let Some((projectile, body)) = read
|
|
|
|
.projectiles
|
|
|
|
.get(entity)
|
|
|
|
.filter(|_| vel.0.magnitude_squared() > 1.0 && block.is_some())
|
|
|
|
.zip(read.bodies.get(entity).copied())
|
|
|
|
{
|
|
|
|
outcomes.push(Outcome::ProjectileHit {
|
2021-08-30 15:02:13 +00:00
|
|
|
pos: pos.0 + pos_delta * dist,
|
2021-03-28 23:29:48 +00:00
|
|
|
body,
|
|
|
|
vel: vel.0,
|
|
|
|
source: projectile.owner,
|
|
|
|
target: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if block.is_some() {
|
2021-03-11 22:27:03 +00:00
|
|
|
let block_center = pos.0.map(|e| e.floor()) + 0.5;
|
|
|
|
let block_rpos = (pos.0 - block_center)
|
|
|
|
.try_normalized()
|
2021-03-12 22:14:08 +00:00
|
|
|
.unwrap_or_else(Vec3::zero);
|
2021-03-11 22:27:03 +00:00
|
|
|
|
2021-08-10 16:53:39 +00:00
|
|
|
// See whether we're on the top/bottom of a block,
|
|
|
|
// or the side
|
2021-03-11 22:27:03 +00:00
|
|
|
if block_rpos.z.abs()
|
|
|
|
> block_rpos.xy().map(|e| e.abs()).reduce_partial_max()
|
|
|
|
{
|
|
|
|
if block_rpos.z > 0.0 {
|
2021-06-20 03:51:04 +00:00
|
|
|
physics_state.on_ground = block.copied();
|
2021-03-11 16:48:59 +00:00
|
|
|
} else {
|
2021-03-11 22:27:03 +00:00
|
|
|
physics_state.on_ceiling = true;
|
2021-03-11 16:48:59 +00:00
|
|
|
}
|
2021-03-11 22:27:03 +00:00
|
|
|
vel.0.z = 0.0;
|
|
|
|
} else {
|
|
|
|
physics_state.on_wall =
|
|
|
|
Some(if block_rpos.x.abs() > block_rpos.y.abs() {
|
|
|
|
vel.0.x = 0.0;
|
|
|
|
Vec3::unit_x() * -block_rpos.x.signum()
|
|
|
|
} else {
|
|
|
|
vel.0.y = 0.0;
|
|
|
|
Vec3::unit_y() * -block_rpos.y.signum()
|
|
|
|
});
|
2020-08-11 11:13:18 +00:00
|
|
|
}
|
2021-03-28 23:29:48 +00:00
|
|
|
|
|
|
|
// Sticky things shouldn't move
|
|
|
|
if sticky.is_some() {
|
2021-03-29 20:50:50 +00:00
|
|
|
vel.0 = physics_state.ground_vel;
|
2021-03-28 23:29:48 +00:00
|
|
|
}
|
2021-03-11 22:27:03 +00:00
|
|
|
}
|
2020-08-11 11:13:18 +00:00
|
|
|
|
2021-03-23 09:51:53 +00:00
|
|
|
physics_state.in_fluid = read
|
2021-03-11 22:27:03 +00:00
|
|
|
.terrain
|
|
|
|
.get(pos.0.map(|e| e.floor() as i32))
|
|
|
|
.ok()
|
2021-06-16 21:40:18 +00:00
|
|
|
.and_then(|vox| {
|
|
|
|
vox.liquid_kind().map(|kind| Fluid::Liquid {
|
|
|
|
kind,
|
|
|
|
depth: 1.0,
|
|
|
|
vel: Vel::zero(),
|
|
|
|
})
|
2021-03-23 09:51:53 +00:00
|
|
|
})
|
2021-04-27 14:41:48 +00:00
|
|
|
.or_else(|| match physics_state.in_fluid {
|
2021-06-16 21:40:18 +00:00
|
|
|
Some(Fluid::Liquid { .. }) | None => Some(Fluid::Air {
|
2021-03-23 09:51:53 +00:00
|
|
|
elevation: pos.0.z,
|
2021-04-27 14:41:48 +00:00
|
|
|
vel: Vel::default(),
|
|
|
|
}),
|
|
|
|
fluid => fluid,
|
2021-03-23 09:51:53 +00:00
|
|
|
});
|
2021-03-13 15:36:56 +00:00
|
|
|
|
|
|
|
tgt_pos = pos.0;
|
2021-03-11 22:27:03 +00:00
|
|
|
},
|
|
|
|
}
|
2020-08-11 11:13:18 +00:00
|
|
|
|
2021-08-10 16:53:39 +00:00
|
|
|
// Compute center and radius of tick path bounding sphere
|
|
|
|
// for the entity for broad checks of whether it will
|
|
|
|
// collide with a voxel collider
|
2021-03-17 03:34:49 +00:00
|
|
|
let path_sphere = {
|
2021-08-10 16:53:39 +00:00
|
|
|
// TODO: duplicated with maintain_pushback_cache,
|
|
|
|
// make a common function to call to compute all this info?
|
2021-03-17 03:34:49 +00:00
|
|
|
let z_limits = calc_z_limit(character_state, Some(collider));
|
|
|
|
let z_limits = (z_limits.0 * scale, z_limits.1 * scale);
|
|
|
|
let half_height = (z_limits.1 - z_limits.0) / 2.0;
|
|
|
|
|
|
|
|
let entity_center = pos.0 + (z_limits.0 + half_height) * Vec3::unit_z();
|
|
|
|
let path_center = entity_center + pos_delta / 2.0;
|
|
|
|
|
2021-09-16 16:45:17 +00:00
|
|
|
let flat_radius = collider.bounding_radius() * scale;
|
2021-03-17 03:34:49 +00:00
|
|
|
let radius = (flat_radius.powi(2) + half_height.powi(2)).sqrt();
|
|
|
|
let path_bounding_radius = radius + (pos_delta / 2.0).magnitude();
|
|
|
|
|
|
|
|
Sphere {
|
|
|
|
center: path_center,
|
|
|
|
radius: path_bounding_radius,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Collide with terrain-like entities
|
2021-04-21 17:10:13 +00:00
|
|
|
let query_center = path_sphere.center.xy();
|
|
|
|
let query_radius = path_sphere.radius;
|
2021-09-11 12:06:13 +00:00
|
|
|
|
2021-03-17 03:34:49 +00:00
|
|
|
voxel_collider_spatial_grid
|
2021-04-21 17:10:13 +00:00
|
|
|
.in_circle_aabr(query_center, query_radius)
|
2021-03-17 03:34:49 +00:00
|
|
|
.filter_map(|entity| {
|
|
|
|
positions
|
|
|
|
.get(entity)
|
2021-04-21 17:10:13 +00:00
|
|
|
.and_then(|l| velocities.get(entity).map(|r| (l, r)))
|
|
|
|
.and_then(|l| previous_phys_cache.get(entity).map(|r| (l, r)))
|
|
|
|
.and_then(|l| read.colliders.get(entity).map(|r| (l, r)))
|
|
|
|
.and_then(|l| orientations.get(entity).map(|r| (l, r)))
|
2021-03-17 03:34:49 +00:00
|
|
|
.map(|((((pos, vel), previous_cache), collider), ori)| {
|
|
|
|
(entity, pos, vel, previous_cache, collider, ori)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.for_each(
|
|
|
|
|(
|
|
|
|
entity_other,
|
|
|
|
pos_other,
|
|
|
|
vel_other,
|
|
|
|
previous_cache_other,
|
|
|
|
collider_other,
|
|
|
|
ori_other,
|
|
|
|
)| {
|
|
|
|
if entity == entity_other {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-15 02:36:59 +00:00
|
|
|
|
2021-03-17 03:34:49 +00:00
|
|
|
let voxel_id = if let Collider::Voxel { id } = collider_other {
|
|
|
|
id
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
2021-03-17 03:32:03 +00:00
|
|
|
|
2021-03-17 03:34:49 +00:00
|
|
|
// use bounding cylinder regardless of our collider
|
|
|
|
// TODO: extract point-terrain collision above to its own
|
|
|
|
// function
|
2021-09-16 16:45:17 +00:00
|
|
|
let radius = collider.bounding_radius();
|
2021-09-16 18:59:52 +00:00
|
|
|
let (_, z_max) = collider.get_z_limits(1.0);
|
2021-03-17 03:32:03 +00:00
|
|
|
|
2021-03-17 03:34:49 +00:00
|
|
|
let radius = radius.min(0.45) * scale;
|
2021-09-16 18:59:52 +00:00
|
|
|
let z_min = 0.0;
|
2021-03-17 03:34:49 +00:00
|
|
|
let z_max = z_max.clamped(1.2, 1.95) * scale;
|
2021-03-17 03:32:03 +00:00
|
|
|
|
2021-03-17 03:34:49 +00:00
|
|
|
if let Some(voxel_collider) =
|
|
|
|
VOXEL_COLLIDER_MANIFEST.read().colliders.get(voxel_id)
|
|
|
|
{
|
|
|
|
// TODO: cache/precompute sphere?
|
|
|
|
let voxel_sphere = voxel_collider_bounding_sphere(
|
|
|
|
voxel_collider,
|
|
|
|
pos_other,
|
|
|
|
ori_other,
|
|
|
|
);
|
|
|
|
// Early check
|
|
|
|
if voxel_sphere.center.distance_squared(path_sphere.center)
|
|
|
|
> (voxel_sphere.radius + path_sphere.radius).powi(2)
|
|
|
|
{
|
|
|
|
return;
|
2021-03-17 03:32:03 +00:00
|
|
|
}
|
2021-03-12 00:01:16 +00:00
|
|
|
|
2021-03-17 03:34:49 +00:00
|
|
|
let mut physics_state_delta = physics_state.clone();
|
|
|
|
// deliberately don't use scale yet here, because the
|
|
|
|
// 11.0/0.8 thing is
|
|
|
|
// in the comp::Scale for visual reasons
|
|
|
|
let mut cpos = *pos;
|
|
|
|
let wpos = cpos.0;
|
2021-03-11 16:48:59 +00:00
|
|
|
|
2021-03-17 03:34:49 +00:00
|
|
|
// TODO: Cache the matrices here to avoid recomputing
|
2021-03-17 03:32:03 +00:00
|
|
|
|
2021-09-17 21:34:28 +00:00
|
|
|
let transform_last_from = Mat4::<f32>::translation_3d(
|
|
|
|
previous_cache_other.pos.unwrap_or(*pos_other).0
|
|
|
|
- previous_cache.pos.unwrap_or(Pos(wpos)).0,
|
|
|
|
) * Mat4::from(
|
|
|
|
previous_cache_other.ori,
|
|
|
|
) * Mat4::<f32>::translation_3d(
|
|
|
|
voxel_collider.translation,
|
|
|
|
);
|
|
|
|
let transform_last_to = transform_last_from.inverted();
|
|
|
|
|
2021-03-17 03:34:49 +00:00
|
|
|
let transform_from =
|
|
|
|
Mat4::<f32>::translation_3d(pos_other.0 - wpos)
|
|
|
|
* Mat4::from(ori_other.to_quat())
|
|
|
|
* Mat4::<f32>::translation_3d(
|
|
|
|
voxel_collider.translation,
|
|
|
|
);
|
|
|
|
let transform_to = transform_from.inverted();
|
2021-09-17 21:34:28 +00:00
|
|
|
|
|
|
|
let ori_last_from = Mat4::from(previous_cache_other.ori);
|
|
|
|
let ori_last_to = ori_last_from.inverted();
|
|
|
|
|
2021-03-17 03:34:49 +00:00
|
|
|
let ori_from = Mat4::from(ori_other.to_quat());
|
|
|
|
|
|
|
|
// The velocity of the collider, taking into account
|
|
|
|
// orientation.
|
2021-07-18 00:13:36 +00:00
|
|
|
let pos_rel = (Mat4::<f32>::translation_3d(Vec3::zero())
|
2021-03-17 03:32:03 +00:00
|
|
|
* Mat4::from(ori_other.to_quat())
|
2021-03-17 03:34:49 +00:00
|
|
|
* Mat4::<f32>::translation_3d(voxel_collider.translation))
|
|
|
|
.inverted()
|
2021-07-18 00:13:36 +00:00
|
|
|
.mul_point(wpos - pos_other.0);
|
|
|
|
let rpos_last = (Mat4::<f32>::translation_3d(Vec3::zero())
|
2021-03-17 03:34:49 +00:00
|
|
|
* Mat4::from(previous_cache_other.ori)
|
|
|
|
* Mat4::<f32>::translation_3d(voxel_collider.translation))
|
2021-07-18 00:13:36 +00:00
|
|
|
.mul_point(pos_rel);
|
|
|
|
let vel_other = vel_other.0
|
|
|
|
+ (wpos - (pos_other.0 + rpos_last)) / read.dt.0;
|
2021-03-17 03:34:49 +00:00
|
|
|
|
2021-09-17 21:34:28 +00:00
|
|
|
cpos.0 = transform_last_to.mul_point(Vec3::zero());
|
|
|
|
vel.0 = ori_last_to.mul_direction(vel.0 - vel_other);
|
2021-03-17 03:34:49 +00:00
|
|
|
let cylinder = (radius, z_min, z_max);
|
|
|
|
box_voxel_collision(
|
|
|
|
cylinder,
|
|
|
|
&voxel_collider.dyna,
|
|
|
|
entity,
|
|
|
|
&mut cpos,
|
|
|
|
transform_to.mul_point(tgt_pos - wpos),
|
|
|
|
&mut vel,
|
|
|
|
&mut physics_state_delta,
|
2021-09-17 21:34:28 +00:00
|
|
|
ori_last_to.mul_direction(vel_other),
|
2021-03-17 03:34:49 +00:00
|
|
|
&read.dt,
|
|
|
|
was_on_ground,
|
|
|
|
block_snap,
|
|
|
|
climbing,
|
|
|
|
|entity, vel| {
|
|
|
|
land_on_ground =
|
|
|
|
Some((entity, Vel(ori_from.mul_direction(vel.0))));
|
|
|
|
},
|
2021-05-24 00:45:22 +00:00
|
|
|
read,
|
2021-03-17 03:34:49 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
cpos.0 = transform_from.mul_point(cpos.0) + wpos;
|
|
|
|
vel.0 = ori_from.mul_direction(vel.0) + vel_other;
|
|
|
|
tgt_pos = cpos.0;
|
|
|
|
|
|
|
|
// union in the state updates, so that the state isn't just
|
|
|
|
// based on the most
|
|
|
|
// recent terrain that collision was attempted with
|
2021-06-20 03:51:04 +00:00
|
|
|
if physics_state_delta.on_ground.is_some() {
|
2021-03-17 03:34:49 +00:00
|
|
|
physics_state.ground_vel = vel_other;
|
2021-07-18 00:13:36 +00:00
|
|
|
|
|
|
|
// Rotate if on ground
|
|
|
|
ori = ori.rotated(
|
|
|
|
ori_other.to_quat()
|
|
|
|
* previous_cache_other.ori.inverse(),
|
|
|
|
);
|
2021-03-17 03:34:49 +00:00
|
|
|
}
|
2021-06-20 03:51:04 +00:00
|
|
|
physics_state.on_ground =
|
|
|
|
physics_state.on_ground.or(physics_state_delta.on_ground);
|
2021-03-17 03:34:49 +00:00
|
|
|
physics_state.on_ceiling |= physics_state_delta.on_ceiling;
|
|
|
|
physics_state.on_wall = physics_state.on_wall.or_else(|| {
|
|
|
|
physics_state_delta
|
|
|
|
.on_wall
|
|
|
|
.map(|dir| ori_from.mul_direction(dir))
|
|
|
|
});
|
2021-03-23 09:51:53 +00:00
|
|
|
physics_state.in_fluid = match (
|
|
|
|
physics_state.in_fluid,
|
|
|
|
physics_state_delta.in_fluid,
|
2021-03-17 03:34:49 +00:00
|
|
|
) {
|
2021-03-23 09:51:53 +00:00
|
|
|
(Some(x), Some(y)) => x
|
|
|
|
.depth()
|
|
|
|
.and_then(|xh| {
|
|
|
|
y.depth()
|
|
|
|
.map(|yh| xh > yh)
|
|
|
|
.unwrap_or(true)
|
|
|
|
.then_some(x)
|
|
|
|
})
|
|
|
|
.or(Some(y)),
|
2021-03-17 03:34:49 +00:00
|
|
|
(x @ Some(_), _) => x,
|
|
|
|
(_, y @ Some(_)) => y,
|
|
|
|
_ => None,
|
|
|
|
};
|
2021-03-17 03:32:03 +00:00
|
|
|
}
|
2021-03-17 03:34:49 +00:00
|
|
|
},
|
|
|
|
);
|
2021-03-13 15:36:56 +00:00
|
|
|
|
2021-03-16 18:04:28 +00:00
|
|
|
if tgt_pos != pos.0 {
|
2021-07-18 00:13:36 +00:00
|
|
|
pos_vel_ori_defer.pos = Some(Pos(tgt_pos));
|
2021-03-16 18:04:28 +00:00
|
|
|
} else {
|
2021-07-18 00:13:36 +00:00
|
|
|
pos_vel_ori_defer.pos = None;
|
2021-03-16 18:04:28 +00:00
|
|
|
}
|
|
|
|
if vel != old_vel {
|
2021-07-18 00:13:36 +00:00
|
|
|
pos_vel_ori_defer.vel = Some(vel);
|
2021-03-16 18:04:28 +00:00
|
|
|
} else {
|
2021-07-18 00:13:36 +00:00
|
|
|
pos_vel_ori_defer.vel = None;
|
|
|
|
}
|
|
|
|
if ori != old_ori {
|
|
|
|
pos_vel_ori_defer.ori = Some(ori);
|
|
|
|
} else {
|
|
|
|
pos_vel_ori_defer.ori = None;
|
2021-03-16 18:04:28 +00:00
|
|
|
}
|
2020-04-26 14:37:13 +00:00
|
|
|
|
2021-03-28 23:29:48 +00:00
|
|
|
(land_on_ground, outcomes)
|
2021-03-16 07:40:31 +00:00
|
|
|
},
|
|
|
|
)
|
2021-03-28 23:29:48 +00:00
|
|
|
.fold(
|
|
|
|
|| (Vec::new(), Vec::new()),
|
|
|
|
|(mut land_on_grounds, mut all_outcomes), (land_on_ground, mut outcomes)| {
|
|
|
|
land_on_ground.map(|log| land_on_grounds.push(log));
|
|
|
|
all_outcomes.append(&mut outcomes);
|
|
|
|
(land_on_grounds, all_outcomes)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.reduce(
|
|
|
|
|| (Vec::new(), Vec::new()),
|
|
|
|
|(mut land_on_grounds_a, mut outcomes_a),
|
|
|
|
(mut land_on_grounds_b, mut outcomes_b)| {
|
|
|
|
land_on_grounds_a.append(&mut land_on_grounds_b);
|
|
|
|
outcomes_a.append(&mut outcomes_b);
|
|
|
|
(land_on_grounds_a, outcomes_a)
|
|
|
|
},
|
|
|
|
);
|
2021-03-11 16:48:59 +00:00
|
|
|
drop(guard);
|
|
|
|
job.cpu_stats.measure(ParMode::Single);
|
2020-04-26 14:37:13 +00:00
|
|
|
|
2021-03-28 23:29:48 +00:00
|
|
|
write.outcomes.append(&mut outcomes);
|
|
|
|
|
2021-03-16 07:40:31 +00:00
|
|
|
prof_span!(guard, "write deferred pos and vel");
|
2021-07-18 00:13:36 +00:00
|
|
|
for (_, pos, vel, ori, pos_vel_ori_defer, _) in (
|
2021-03-16 07:40:31 +00:00
|
|
|
&read.entities,
|
|
|
|
&mut write.positions,
|
|
|
|
&mut write.velocities,
|
2021-07-18 00:13:36 +00:00
|
|
|
&mut write.orientations,
|
|
|
|
&mut write.pos_vel_ori_defers,
|
2021-05-29 00:25:57 +00:00
|
|
|
&read.colliders,
|
2021-03-16 07:40:31 +00:00
|
|
|
)
|
|
|
|
.join()
|
2021-07-18 00:13:36 +00:00
|
|
|
.filter(|tuple| matches!(tuple.5, Collider::Voxel { .. }) == terrain_like_entities)
|
2021-03-12 22:14:08 +00:00
|
|
|
{
|
2021-07-18 00:13:36 +00:00
|
|
|
if let Some(new_pos) = pos_vel_ori_defer.pos.take() {
|
2021-03-16 18:04:28 +00:00
|
|
|
*pos = new_pos;
|
|
|
|
}
|
2021-07-18 00:13:36 +00:00
|
|
|
if let Some(new_vel) = pos_vel_ori_defer.vel.take() {
|
2021-03-16 18:04:28 +00:00
|
|
|
*vel = new_vel;
|
|
|
|
}
|
2021-07-18 00:13:36 +00:00
|
|
|
if let Some(new_ori) = pos_vel_ori_defer.ori.take() {
|
|
|
|
*ori = new_ori;
|
|
|
|
}
|
2021-03-13 22:17:53 +00:00
|
|
|
}
|
2021-03-16 07:40:31 +00:00
|
|
|
drop(guard);
|
2021-03-13 22:17:53 +00:00
|
|
|
|
2021-03-12 22:14:08 +00:00
|
|
|
let mut event_emitter = read.event_bus.emitter();
|
2021-03-11 16:48:59 +00:00
|
|
|
land_on_grounds.into_iter().for_each(|(entity, vel)| {
|
|
|
|
event_emitter.emit(ServerEvent::LandOnGround { entity, vel: vel.0 });
|
|
|
|
});
|
|
|
|
}
|
2021-04-21 17:10:13 +00:00
|
|
|
|
|
|
|
fn update_cached_spatial_grid(&mut self) {
|
|
|
|
span!(_guard, "Update cached spatial grid");
|
|
|
|
let PhysicsData {
|
|
|
|
ref read,
|
|
|
|
ref mut write,
|
|
|
|
} = self;
|
|
|
|
|
|
|
|
let spatial_grid = &mut write.cached_spatial_grid.0;
|
|
|
|
spatial_grid.clear();
|
|
|
|
(
|
|
|
|
&read.entities,
|
|
|
|
&write.positions,
|
|
|
|
read.scales.maybe(),
|
|
|
|
read.colliders.maybe(),
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
.for_each(|(entity, pos, scale, collider)| {
|
|
|
|
let scale = scale.map(|s| s.0).unwrap_or(1.0);
|
|
|
|
let radius_2d =
|
2021-09-16 16:45:17 +00:00
|
|
|
(collider.map(|c| c.bounding_radius()).unwrap_or(0.5) * scale).ceil() as u32;
|
2021-04-21 17:10:13 +00:00
|
|
|
let pos_2d = pos.0.xy().map(|e| e as i32);
|
|
|
|
const POS_TRUNCATION_ERROR: u32 = 1;
|
|
|
|
spatial_grid.insert(pos_2d, radius_2d + POS_TRUNCATION_ERROR, entity);
|
|
|
|
});
|
|
|
|
}
|
2021-03-11 16:48:59 +00:00
|
|
|
}
|
2020-04-26 14:37:13 +00:00
|
|
|
|
2021-03-11 16:48:59 +00:00
|
|
|
impl<'a> System<'a> for Sys {
|
2021-03-12 22:14:08 +00:00
|
|
|
type SystemData = PhysicsData<'a>;
|
2020-04-26 14:37:13 +00:00
|
|
|
|
2021-03-11 16:48:59 +00:00
|
|
|
const NAME: &'static str = "phys";
|
|
|
|
const ORIGIN: Origin = Origin::Common;
|
|
|
|
const PHASE: Phase = Phase::Create;
|
2020-04-26 14:37:13 +00:00
|
|
|
|
2021-04-21 17:10:13 +00:00
|
|
|
fn run(job: &mut Job<Self>, mut physics_data: Self::SystemData) {
|
|
|
|
physics_data.reset();
|
2020-04-26 14:37:13 +00:00
|
|
|
|
2021-03-11 16:48:59 +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.
|
2021-04-21 17:10:13 +00:00
|
|
|
physics_data.maintain_pushback_cache();
|
|
|
|
|
|
|
|
let spatial_grid = physics_data.construct_spatial_grid();
|
|
|
|
physics_data.apply_pushback(job, &spatial_grid);
|
2021-03-16 05:13:52 +00:00
|
|
|
|
2021-04-21 17:10:13 +00:00
|
|
|
let voxel_collider_spatial_grid = physics_data.construct_voxel_collider_spatial_grid();
|
|
|
|
physics_data.handle_movement_and_terrain(job, &voxel_collider_spatial_grid);
|
2020-04-26 14:37:13 +00:00
|
|
|
|
2021-04-21 17:10:13 +00:00
|
|
|
// Spatial grid used by other systems
|
|
|
|
physics_data.update_cached_spatial_grid();
|
2021-03-11 16:48:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-25 16:13:30 +00:00
|
|
|
|
2021-09-14 20:04:55 +00:00
|
|
|
#[allow(clippy::too_many_arguments, clippy::too_many_lines)]
|
2021-03-15 02:36:59 +00:00
|
|
|
fn box_voxel_collision<'a, T: BaseVol<Vox = Block> + ReadVol>(
|
2021-03-23 09:51:53 +00:00
|
|
|
cylinder: (f32, f32, f32), // effective collision cylinder
|
2021-03-11 16:48:59 +00:00
|
|
|
terrain: &'a T,
|
|
|
|
entity: Entity,
|
|
|
|
pos: &mut Pos,
|
2021-03-13 15:36:56 +00:00
|
|
|
tgt_pos: Vec3<f32>,
|
2021-03-11 16:48:59 +00:00
|
|
|
vel: &mut Vel,
|
|
|
|
physics_state: &mut PhysicsState,
|
2021-03-12 13:26:02 +00:00
|
|
|
ground_vel: Vec3<f32>,
|
|
|
|
dt: &DeltaTime,
|
2021-03-12 18:53:01 +00:00
|
|
|
was_on_ground: bool,
|
2021-03-13 20:51:32 +00:00
|
|
|
block_snap: bool,
|
2021-03-14 23:33:54 +00:00
|
|
|
climbing: bool,
|
2021-03-12 11:32:19 +00:00
|
|
|
mut land_on_ground: impl FnMut(Entity, Vel),
|
2021-05-24 00:45:22 +00:00
|
|
|
read: &PhysicsRead,
|
2021-03-11 16:48:59 +00:00
|
|
|
) {
|
2021-09-14 20:04:55 +00:00
|
|
|
// FIXME: Review these
|
|
|
|
#![allow(
|
|
|
|
clippy::cast_precision_loss,
|
|
|
|
clippy::cast_possible_truncation,
|
|
|
|
clippy::cast_sign_loss
|
|
|
|
)]
|
2021-03-25 04:38:02 +00:00
|
|
|
prof_span!("box_voxel_collision");
|
2021-09-14 20:04:55 +00:00
|
|
|
|
2021-03-25 06:27:52 +00:00
|
|
|
// Convience function to compute the player aabb
|
|
|
|
fn player_aabb(pos: Vec3<f32>, radius: f32, z_range: Range<f32>) -> Aabb<f32> {
|
|
|
|
Aabb {
|
|
|
|
min: pos + Vec3::new(-radius, -radius, z_range.start),
|
|
|
|
max: pos + Vec3::new(radius, radius, z_range.end),
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 09:40:07 +00:00
|
|
|
|
2021-03-25 06:27:52 +00:00
|
|
|
// Convience function to translate the near_aabb into the world space
|
|
|
|
fn move_aabb(aabb: Aabb<i32>, pos: Vec3<f32>) -> Aabb<i32> {
|
|
|
|
Aabb {
|
|
|
|
min: aabb.min + pos.map(|e| e.floor() as i32),
|
|
|
|
max: aabb.max + pos.map(|e| e.floor() as i32),
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 09:43:03 +00:00
|
|
|
|
2021-03-11 16:48:59 +00:00
|
|
|
// Function for determining whether the player at a specific position collides
|
|
|
|
// with blocks with the given criteria
|
|
|
|
fn collision_with<'a, T: BaseVol<Vox = Block> + ReadVol>(
|
|
|
|
pos: Vec3<f32>,
|
|
|
|
terrain: &'a T,
|
|
|
|
hit: impl Fn(&Block) -> bool,
|
2021-03-25 04:38:02 +00:00
|
|
|
near_aabb: Aabb<i32>,
|
2021-03-11 16:48:59 +00:00
|
|
|
radius: f32,
|
|
|
|
z_range: Range<f32>,
|
|
|
|
) -> bool {
|
2021-03-25 06:27:52 +00:00
|
|
|
let player_aabb = player_aabb(pos, radius, z_range);
|
2021-03-25 04:38:02 +00:00
|
|
|
|
|
|
|
// Calculate the world space near aabb
|
2021-03-25 06:27:52 +00:00
|
|
|
let near_aabb = move_aabb(near_aabb, pos);
|
2021-03-25 04:38:02 +00:00
|
|
|
|
|
|
|
let mut collision = false;
|
|
|
|
// TODO: could short-circuit here
|
|
|
|
terrain.for_each_in(near_aabb, |block_pos, block| {
|
|
|
|
if block.is_solid() && hit(&block) {
|
|
|
|
let block_aabb = Aabb {
|
|
|
|
min: block_pos.map(|e| e as f32),
|
|
|
|
max: block_pos.map(|e| e as f32) + Vec3::new(1.0, 1.0, block.solid_height()),
|
|
|
|
};
|
|
|
|
if player_aabb.collides_with_aabb(block_aabb) {
|
|
|
|
collision = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
collision
|
2021-03-11 16:48:59 +00:00
|
|
|
}
|
2020-04-26 14:37:13 +00:00
|
|
|
|
2021-09-14 20:04:55 +00:00
|
|
|
// Should be easy to just make clippy happy if we want?
|
|
|
|
#[allow(clippy::trivially_copy_pass_by_ref)]
|
2021-09-11 12:06:13 +00:00
|
|
|
fn always_hits(_: &Block) -> bool { true }
|
2021-09-14 20:04:55 +00:00
|
|
|
|
2021-03-25 06:27:52 +00:00
|
|
|
// Stuff for liquid immersion and wall contact detections
|
|
|
|
let dirs = [
|
|
|
|
Vec3::unit_x(),
|
|
|
|
Vec3::unit_y(),
|
|
|
|
-Vec3::unit_x(),
|
|
|
|
-Vec3::unit_y(),
|
|
|
|
];
|
|
|
|
|
|
|
|
struct LiquidAndWalls {
|
|
|
|
liquid: Option<(LiquidKind, f32)>,
|
|
|
|
wall_dir_collisions: [bool; 4],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LiquidAndWalls {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
liquid: None,
|
|
|
|
wall_dir_collisions: [false; 4],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_max_liquid(
|
|
|
|
&mut self,
|
|
|
|
block_pos: Vec3<i32>,
|
|
|
|
block: Block,
|
|
|
|
player_aabb: Aabb<f32>,
|
|
|
|
) {
|
|
|
|
// Check for liquid blocks
|
|
|
|
if let Some(block_liquid) = block.liquid_kind() {
|
|
|
|
let liquid_aabb = Aabb {
|
|
|
|
min: block_pos.map(|e| e as f32),
|
|
|
|
// The liquid part of a liquid block always extends 1 block high.
|
|
|
|
max: block_pos.map(|e| e as f32) + Vec3::one(),
|
|
|
|
};
|
|
|
|
if player_aabb.collides_with_aabb(liquid_aabb) {
|
|
|
|
self.liquid = match self.liquid {
|
|
|
|
Some((kind, max_liquid_z)) => Some((
|
|
|
|
// TODO: merging of liquid kinds and max_liquid_z are done
|
|
|
|
// independently which allows mix and
|
|
|
|
// matching them
|
|
|
|
kind.merge(block_liquid),
|
|
|
|
max_liquid_z.max(liquid_aabb.max.z),
|
|
|
|
)),
|
|
|
|
None => Some((block_liquid, liquid_aabb.max.z)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_walls(
|
|
|
|
&mut self,
|
|
|
|
block_pos: Vec3<i32>,
|
|
|
|
block: Block,
|
|
|
|
player_aabbs: [Aabb<f32>; 4],
|
|
|
|
) {
|
|
|
|
// Check for walls
|
|
|
|
if block.is_solid() {
|
|
|
|
let block_aabb = Aabb {
|
|
|
|
min: block_pos.map(|e| e as f32),
|
|
|
|
max: block_pos.map(|e| e as f32) + Vec3::new(1.0, 1.0, block.solid_height()),
|
|
|
|
};
|
|
|
|
|
|
|
|
for dir in 0..4 {
|
|
|
|
if player_aabbs[dir].collides_with_aabb(block_aabb) {
|
|
|
|
self.wall_dir_collisions[dir] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup values for the loop below
|
|
|
|
|
2021-09-11 12:06:13 +00:00
|
|
|
let (radius, z_min, z_max) = cylinder;
|
|
|
|
|
|
|
|
// Probe distances
|
|
|
|
let hdist = radius.ceil() as i32;
|
2021-09-14 20:04:55 +00:00
|
|
|
|
2021-03-25 04:51:32 +00:00
|
|
|
// Neighbouring blocks Aabb
|
2021-03-25 04:38:02 +00:00
|
|
|
let near_aabb = Aabb {
|
|
|
|
min: Vec3::new(
|
|
|
|
-hdist,
|
|
|
|
-hdist,
|
|
|
|
1 - Block::MAX_HEIGHT.ceil() as i32 + z_min.floor() as i32,
|
|
|
|
),
|
|
|
|
max: Vec3::new(hdist, hdist, z_max.ceil() as i32),
|
|
|
|
};
|
|
|
|
|
2021-09-11 12:06:13 +00:00
|
|
|
let z_range = z_min..z_max;
|
|
|
|
|
2021-03-25 06:27:52 +00:00
|
|
|
// Compute a list of Aabbs to check for collision with nearby walls
|
|
|
|
let player_wall_aabbs = |pos| {
|
|
|
|
dirs.map(|dir| {
|
|
|
|
let pos = pos + dir * 0.01;
|
|
|
|
Aabb {
|
|
|
|
min: pos + Vec3::new(-radius, -radius, z_range.start),
|
|
|
|
max: pos + Vec3::new(radius, radius, z_range.end),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2021-06-20 03:51:04 +00:00
|
|
|
physics_state.on_ground = None;
|
2021-05-14 19:44:14 +00:00
|
|
|
physics_state.on_ceiling = false;
|
2021-03-11 16:48:59 +00:00
|
|
|
|
2021-03-25 04:38:02 +00:00
|
|
|
let mut on_ground = None::<Block>;
|
2021-03-11 16:48:59 +00:00
|
|
|
let mut on_ceiling = false;
|
2021-09-14 20:04:55 +00:00
|
|
|
// Don't loop infinitely here
|
|
|
|
let mut attempts = 0;
|
2021-03-11 16:48:59 +00:00
|
|
|
|
2021-03-13 15:36:56 +00:00
|
|
|
let mut pos_delta = tgt_pos - pos.0;
|
|
|
|
|
2021-03-11 16:48:59 +00:00
|
|
|
// Don't jump too far at once
|
2021-09-17 21:34:28 +00:00
|
|
|
const MAX_INCREMENTS: usize = 100; // The maximum number of collision tests per tick
|
|
|
|
let increments = ((pos_delta.map(|e| e.abs()).reduce_partial_max() / 0.3).ceil() as usize)
|
|
|
|
.clamped(1, MAX_INCREMENTS);
|
2021-03-11 16:48:59 +00:00
|
|
|
let old_pos = pos.0;
|
2021-09-17 21:34:28 +00:00
|
|
|
for _ in 0..increments {
|
2021-03-25 04:38:02 +00:00
|
|
|
prof_span!("increment");
|
2021-03-11 16:48:59 +00:00
|
|
|
const MAX_ATTEMPTS: usize = 16;
|
2021-09-17 21:34:28 +00:00
|
|
|
pos.0 += pos_delta / increments as f32;
|
2021-03-11 16:48:59 +00:00
|
|
|
|
2021-09-14 20:04:55 +00:00
|
|
|
let try_colliding_block = |pos: &Pos| {
|
2021-03-25 04:38:02 +00:00
|
|
|
prof_span!("most colliding check");
|
2021-09-14 20:04:55 +00:00
|
|
|
// Calculate the player's AABB
|
2021-03-25 06:27:52 +00:00
|
|
|
let player_aabb = player_aabb(pos.0, radius, z_range.clone());
|
2019-06-26 21:43:47 +00:00
|
|
|
|
2021-09-14 20:04:55 +00:00
|
|
|
// Determine the block that we are colliding with most
|
|
|
|
// (based on minimum collision axis)
|
|
|
|
// (if we are colliding with one)
|
2021-03-25 04:38:02 +00:00
|
|
|
let mut most_colliding = None;
|
|
|
|
// Calculate the world space near aabb
|
2021-03-25 06:27:52 +00:00
|
|
|
let near_aabb = move_aabb(near_aabb, pos.0);
|
2021-03-25 04:38:02 +00:00
|
|
|
let player_overlap = |block_aabb: Aabb<f32>| {
|
|
|
|
ordered_float::OrderedFloat(
|
|
|
|
(block_aabb.center() - player_aabb.center() - Vec3::unit_z() * 0.5)
|
|
|
|
.map(f32::abs)
|
|
|
|
.sum(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
terrain.for_each_in(near_aabb, |block_pos, block| {
|
|
|
|
// Make sure the block is actually solid
|
|
|
|
if block.is_solid() {
|
|
|
|
// Calculate block AABB
|
|
|
|
let block_aabb = Aabb {
|
|
|
|
min: block_pos.map(|e| e as f32),
|
|
|
|
max: block_pos.map(|e| e as f32)
|
|
|
|
+ Vec3::new(1.0, 1.0, block.solid_height()),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Determine whether the block's AABB collides with the player's AABB
|
|
|
|
if block_aabb.collides_with_aabb(player_aabb) {
|
|
|
|
most_colliding = match most_colliding {
|
|
|
|
// Select the minimum of the value from `player_overlap`
|
|
|
|
other @ Some((_, other_block_aabb, _))
|
|
|
|
if {
|
|
|
|
// TODO: comment below is outdated (as of ~1 year ago)
|
|
|
|
// Find the maximum of the minimum collision axes (this bit
|
|
|
|
// is weird, trust me that it works)
|
|
|
|
player_overlap(block_aabb) >= player_overlap(other_block_aabb)
|
|
|
|
} =>
|
|
|
|
{
|
|
|
|
other
|
|
|
|
},
|
|
|
|
_ => Some((block_pos, block_aabb, block)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
most_colliding
|
2021-09-14 20:04:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// While the player is colliding with the terrain...
|
|
|
|
while let Some((_block_pos, block_aabb, block)) = (attempts < MAX_ATTEMPTS)
|
|
|
|
.then(|| try_colliding_block(pos))
|
|
|
|
.flatten()
|
2021-03-19 01:01:32 +00:00
|
|
|
{
|
|
|
|
// Calculate the player's AABB
|
2021-03-25 06:27:52 +00:00
|
|
|
let player_aabb = player_aabb(pos.0, radius, z_range.clone());
|
2019-08-23 10:11:37 +00:00
|
|
|
|
2021-03-11 16:48:59 +00:00
|
|
|
// Find the intrusion vector of the collision
|
|
|
|
let dir = player_aabb.collision_vector_with_aabb(block_aabb);
|
2020-09-19 09:16:06 +00:00
|
|
|
|
2021-03-11 16:48:59 +00:00
|
|
|
// Determine an appropriate resolution vector (i.e: the minimum distance
|
|
|
|
// needed to push out of the block)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// When the resolution direction is pointing upwards, we must be on the
|
|
|
|
// ground
|
2021-09-14 20:04:55 +00:00
|
|
|
/* if resolve_dir.z > 0.0 && vel.0.z <= 0.0 { */
|
|
|
|
if resolve_dir.z > 0.0 {
|
2021-03-25 04:38:02 +00:00
|
|
|
on_ground = Some(block);
|
2021-03-11 16:48:59 +00:00
|
|
|
|
|
|
|
if !was_on_ground {
|
2021-03-12 11:32:19 +00:00
|
|
|
land_on_ground(entity, *vel);
|
2021-03-11 16:48:59 +00:00
|
|
|
}
|
|
|
|
} else if resolve_dir.z < 0.0 && vel.0.z >= 0.0 {
|
|
|
|
on_ceiling = true;
|
2019-09-09 19:11:40 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 16:48:59 +00:00
|
|
|
// When the resolution direction is non-vertical, we must be colliding
|
2021-09-14 20:04:55 +00:00
|
|
|
// with a wall
|
|
|
|
//
|
|
|
|
// If we're being pushed out horizontally...
|
2021-03-25 04:38:02 +00:00
|
|
|
if resolve_dir.z == 0.0
|
2021-09-14 20:04:55 +00:00
|
|
|
// ...and the vertical resolution direction is sufficiently great...
|
2021-03-25 04:38:02 +00:00
|
|
|
&& dir.z < -0.1
|
2021-09-14 20:04:55 +00:00
|
|
|
// ...and the space above is free...
|
2021-03-25 04:38:02 +00:00
|
|
|
&& {
|
|
|
|
prof_span!("space above free");
|
|
|
|
!collision_with(
|
|
|
|
Vec3::new(pos.0.x, pos.0.y, (pos.0.z + 0.1).ceil()),
|
|
|
|
&terrain,
|
|
|
|
always_hits,
|
|
|
|
near_aabb,
|
|
|
|
radius,
|
|
|
|
z_range.clone(),
|
|
|
|
)
|
|
|
|
}
|
2021-09-14 20:04:55 +00:00
|
|
|
// ...and there is a collision with a block beneath our current hitbox...
|
2021-03-25 04:38:02 +00:00
|
|
|
&& {
|
|
|
|
prof_span!("collision beneath");
|
|
|
|
collision_with(
|
|
|
|
pos.0 + resolve_dir - Vec3::unit_z() * 1.25,
|
|
|
|
&terrain,
|
|
|
|
always_hits,
|
|
|
|
near_aabb,
|
|
|
|
radius,
|
|
|
|
z_range.clone(),
|
|
|
|
)
|
|
|
|
} {
|
2021-03-11 16:48:59 +00:00
|
|
|
// ...block-hop!
|
2021-07-25 19:03:17 +00:00
|
|
|
pos.0.z = pos.0.z.max(block_aabb.max.z);
|
2021-03-14 23:07:32 +00:00
|
|
|
vel.0.z = vel.0.z.max(0.0);
|
2021-09-14 20:04:55 +00:00
|
|
|
// Push the character on to the block very slightly
|
|
|
|
// to avoid jitter due to imprecision
|
|
|
|
if (vel.0 * resolve_dir).xy().magnitude_squared() < 1.0_f32.powi(2) {
|
2021-04-20 13:54:21 +00:00
|
|
|
pos.0 -= resolve_dir.normalized() * 0.05;
|
|
|
|
}
|
2021-03-25 04:38:02 +00:00
|
|
|
on_ground = Some(block);
|
2021-03-11 16:48:59 +00:00
|
|
|
break;
|
2021-03-13 06:48:30 +00:00
|
|
|
}
|
2020-08-13 08:12:14 +00:00
|
|
|
|
2021-09-14 20:04:55 +00:00
|
|
|
// If not, correct the velocity
|
|
|
|
vel.0 = vel.0.map2(
|
|
|
|
resolve_dir,
|
|
|
|
|e, d| {
|
|
|
|
if d * e.signum() < 0.0 { 0.0 } else { e }
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
pos_delta *= resolve_dir.map(|e| if e == 0.0 { 1.0 } else { 0.0 });
|
|
|
|
|
2021-03-11 16:48:59 +00:00
|
|
|
// Resolve the collision normally
|
|
|
|
pos.0 += resolve_dir;
|
|
|
|
|
|
|
|
attempts += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if attempts == MAX_ATTEMPTS {
|
|
|
|
vel.0 = Vec3::zero();
|
|
|
|
pos.0 = old_pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 06:27:52 +00:00
|
|
|
// Report on_ceiling state
|
2021-03-11 16:48:59 +00:00
|
|
|
if on_ceiling {
|
|
|
|
physics_state.on_ceiling = true;
|
|
|
|
}
|
|
|
|
|
2021-06-20 03:51:04 +00:00
|
|
|
if on_ground.is_some() {
|
|
|
|
physics_state.on_ground = on_ground;
|
2021-03-11 16:48:59 +00:00
|
|
|
// If the space below us is free, then "snap" to the ground
|
2021-03-25 04:52:42 +00:00
|
|
|
} else if vel.0.z <= 0.0 && was_on_ground && block_snap && {
|
2021-03-25 04:38:02 +00:00
|
|
|
prof_span!("snap check");
|
|
|
|
collision_with(
|
|
|
|
pos.0 - Vec3::unit_z() * 1.1,
|
|
|
|
&terrain,
|
|
|
|
always_hits,
|
|
|
|
near_aabb,
|
|
|
|
radius,
|
|
|
|
z_range.clone(),
|
|
|
|
)
|
2021-03-25 04:52:42 +00:00
|
|
|
} {
|
2021-03-25 04:38:02 +00:00
|
|
|
prof_span!("snap!!");
|
2021-03-11 16:48:59 +00:00
|
|
|
let snap_height = terrain
|
2021-03-12 19:49:20 +00:00
|
|
|
.get(Vec3::new(pos.0.x, pos.0.y, pos.0.z - 0.1).map(|e| e.floor() as i32))
|
2021-03-11 16:48:59 +00:00
|
|
|
.ok()
|
|
|
|
.filter(|block| block.is_solid())
|
2021-09-14 20:04:55 +00:00
|
|
|
.map_or(0.0, Block::solid_height);
|
2021-03-13 14:25:56 +00:00
|
|
|
vel.0.z = 0.0;
|
2021-03-12 19:49:20 +00:00
|
|
|
pos.0.z = (pos.0.z - 0.1).floor() + snap_height;
|
2021-08-04 13:12:10 +00:00
|
|
|
physics_state.on_ground = terrain
|
|
|
|
.get(Vec3::new(pos.0.x, pos.0.y, pos.0.z - 0.01).map(|e| e.floor() as i32))
|
|
|
|
.ok()
|
|
|
|
.copied();
|
2019-03-02 03:48:30 +00:00
|
|
|
}
|
2021-03-11 16:48:59 +00:00
|
|
|
|
2021-03-25 06:27:52 +00:00
|
|
|
// Find liquid immersion and wall collision
|
|
|
|
prof_span!(guard, "liquid/walls");
|
|
|
|
let mut liquid_and_walls = LiquidAndWalls::new();
|
2021-03-19 01:01:32 +00:00
|
|
|
|
2021-03-25 06:27:52 +00:00
|
|
|
let player_aabb = player_aabb(pos.0, radius, z_range.clone());
|
|
|
|
let player_wall_aabbs = player_wall_aabbs(pos.0);
|
2021-03-18 09:01:26 +00:00
|
|
|
|
2021-03-25 04:51:32 +00:00
|
|
|
// Calculate the world space near aabb
|
2021-03-25 06:27:52 +00:00
|
|
|
let near_aabb = move_aabb(near_aabb, pos.0);
|
2021-03-18 09:01:26 +00:00
|
|
|
|
2021-03-25 06:27:52 +00:00
|
|
|
terrain.for_each_in(near_aabb, |block_pos, block| {
|
|
|
|
liquid_and_walls.update_max_liquid(block_pos, block, player_aabb);
|
|
|
|
liquid_and_walls.update_walls(block_pos, block, player_wall_aabbs);
|
2021-03-18 09:01:26 +00:00
|
|
|
});
|
2021-03-25 04:38:02 +00:00
|
|
|
drop(guard);
|
2021-03-18 09:01:26 +00:00
|
|
|
|
|
|
|
// Use wall collision results to determine if we are against a wall
|
|
|
|
let mut on_wall = None;
|
|
|
|
for dir in 0..4 {
|
2021-03-25 06:27:52 +00:00
|
|
|
if liquid_and_walls.wall_dir_collisions[dir] {
|
2021-03-18 09:01:26 +00:00
|
|
|
on_wall = Some(match on_wall {
|
|
|
|
Some(acc) => acc + dirs[dir],
|
|
|
|
None => dirs[dir],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-09-14 20:04:55 +00:00
|
|
|
|
2021-03-18 09:01:26 +00:00
|
|
|
physics_state.on_wall = on_wall;
|
2021-06-20 05:37:22 +00:00
|
|
|
let fric_mod = read.stats.get(entity).map_or(1.0, |s| s.friction_modifier);
|
2021-09-14 20:04:55 +00:00
|
|
|
|
2021-06-20 03:51:04 +00:00
|
|
|
if physics_state.on_ground.is_some() || (physics_state.on_wall.is_some() && climbing) {
|
2021-05-24 00:45:22 +00:00
|
|
|
vel.0 *= (1.0 - FRIC_GROUND.min(1.0) * fric_mod).powf(dt.0 * 60.0);
|
2021-03-18 09:01:26 +00:00
|
|
|
physics_state.ground_vel = ground_vel;
|
|
|
|
}
|
|
|
|
|
2021-03-25 06:27:52 +00:00
|
|
|
physics_state.in_fluid = liquid_and_walls
|
|
|
|
.liquid
|
2021-09-14 20:04:55 +00:00
|
|
|
.map(|(kind, max_z)| {
|
|
|
|
// NOTE: assumes min_z == 0.0
|
|
|
|
let depth = max_z - pos.0.z;
|
|
|
|
|
|
|
|
// This is suboptimal because it doesn't check for true depth,
|
|
|
|
// so it can cause problems for situations like swimming down
|
|
|
|
// a river and spawning or teleporting in(/to) water
|
|
|
|
let new_depth = physics_state.in_liquid().map_or(depth, |old_depth| {
|
|
|
|
(old_depth + old_pos.z - pos.0.z).max(depth)
|
|
|
|
});
|
|
|
|
|
|
|
|
Fluid::Liquid {
|
|
|
|
kind,
|
|
|
|
depth: new_depth,
|
|
|
|
vel: Vel::zero(),
|
|
|
|
}
|
2021-03-23 09:51:53 +00:00
|
|
|
})
|
2021-04-27 14:41:48 +00:00
|
|
|
.or_else(|| match physics_state.in_fluid {
|
2021-06-16 21:40:18 +00:00
|
|
|
Some(Fluid::Liquid { .. }) | None => Some(Fluid::Air {
|
2021-03-23 09:51:53 +00:00
|
|
|
elevation: pos.0.z,
|
2021-04-27 14:41:48 +00:00
|
|
|
vel: Vel::default(),
|
|
|
|
}),
|
|
|
|
fluid => fluid,
|
2021-03-23 09:51:53 +00:00
|
|
|
});
|
2019-03-02 03:48:30 +00:00
|
|
|
}
|
2021-03-17 03:32:03 +00:00
|
|
|
|
|
|
|
fn voxel_collider_bounding_sphere(
|
|
|
|
voxel_collider: &VoxelCollider,
|
|
|
|
pos: &Pos,
|
|
|
|
ori: &Ori,
|
|
|
|
) -> Sphere<f32, f32> {
|
|
|
|
let origin_offset = voxel_collider.translation;
|
|
|
|
use common::vol::SizedVol;
|
|
|
|
let lower_bound = voxel_collider.dyna.lower_bound().map(|e| e as f32);
|
|
|
|
let upper_bound = voxel_collider.dyna.upper_bound().map(|e| e as f32);
|
|
|
|
let center = (lower_bound + upper_bound) / 2.0;
|
|
|
|
// Compute vector from the origin (where pos value corresponds to) and the model
|
|
|
|
// center
|
|
|
|
let center_offset = center + origin_offset;
|
|
|
|
// Rotate
|
|
|
|
let oriented_center_offset = ori.local_to_global(center_offset);
|
|
|
|
// Add to pos to get world coordinates of the center
|
|
|
|
let wpos_center = oriented_center_offset + pos.0;
|
|
|
|
|
|
|
|
// Note: to not get too fine grained we use a 2D grid for now
|
|
|
|
const SPRITE_AND_MAYBE_OTHER_THINGS: f32 = 4.0;
|
|
|
|
let radius = ((upper_bound - lower_bound) / 2.0
|
|
|
|
+ Vec3::broadcast(SPRITE_AND_MAYBE_OTHER_THINGS))
|
|
|
|
.magnitude();
|
|
|
|
|
|
|
|
Sphere {
|
|
|
|
center: wpos_center,
|
|
|
|
radius,
|
|
|
|
}
|
|
|
|
}
|
2021-09-15 15:11:41 +00:00
|
|
|
|
2021-09-16 16:45:17 +00:00
|
|
|
/// Returns whether interesction between entities occured
|
2021-09-15 21:27:48 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2021-09-16 16:45:17 +00:00
|
|
|
fn resolve_e2e_collision(
|
2021-09-15 21:27:48 +00:00
|
|
|
// utility variables for our entity
|
|
|
|
collision_registered: &mut bool,
|
|
|
|
entity_entity_collisions: &mut u64,
|
|
|
|
factor: f32,
|
|
|
|
physics: &mut PhysicsState,
|
|
|
|
char_state_maybe: Option<&CharacterState>,
|
|
|
|
vel_delta: &mut Vec3<f32>,
|
|
|
|
step_delta: f32,
|
|
|
|
// physics flags
|
|
|
|
is_mid_air: bool,
|
|
|
|
is_sticky: bool,
|
|
|
|
is_projectile: bool,
|
|
|
|
// entity we colliding with
|
|
|
|
other: Uid,
|
|
|
|
// symetrical collider context
|
|
|
|
pos: &Pos,
|
|
|
|
pos_other: &Pos,
|
|
|
|
previous_cache: &PreviousPhysCache,
|
|
|
|
previous_cache_other: &PreviousPhysCache,
|
|
|
|
z_limits: (f32, f32),
|
|
|
|
z_limits_other: (f32, f32),
|
|
|
|
collider: Option<&Collider>,
|
|
|
|
collider_other: Option<&Collider>,
|
|
|
|
mass: Mass,
|
|
|
|
mass_other: Mass,
|
2021-09-16 16:45:17 +00:00
|
|
|
) -> bool {
|
2021-09-15 21:27:48 +00:00
|
|
|
// Find the distance betwen our collider and
|
|
|
|
// collider we collide with and get vector of pushback.
|
2021-09-15 15:11:41 +00:00
|
|
|
//
|
2021-09-15 21:27:48 +00:00
|
|
|
// If we aren't colliding, just skip step.
|
|
|
|
|
|
|
|
// Get positions
|
|
|
|
let pos = pos.0 + previous_cache.velocity_dt * factor;
|
|
|
|
let pos_other = pos_other.0 + previous_cache_other.velocity_dt * factor;
|
|
|
|
|
|
|
|
// Compare Z ranges
|
2021-09-16 00:08:54 +00:00
|
|
|
let (z_min, z_max) = z_limits;
|
|
|
|
let ceiling = pos.z + z_max * previous_cache.scale;
|
|
|
|
let floor = pos.z + z_min * previous_cache.scale;
|
2021-09-15 21:27:48 +00:00
|
|
|
|
2021-09-16 00:08:54 +00:00
|
|
|
let (z_min_other, z_max_other) = z_limits_other;
|
|
|
|
let ceiling_other = pos_other.z + z_max_other * previous_cache_other.scale;
|
|
|
|
let floor_other = pos_other.z + z_min_other * previous_cache_other.scale;
|
2021-09-15 21:27:48 +00:00
|
|
|
|
|
|
|
let in_z_range = ceiling >= floor_other && floor <= ceiling_other;
|
2021-09-15 15:11:41 +00:00
|
|
|
|
2021-09-16 00:08:54 +00:00
|
|
|
if !in_z_range {
|
2021-09-16 16:45:17 +00:00
|
|
|
return false;
|
2021-09-16 00:08:54 +00:00
|
|
|
}
|
2021-09-15 21:27:48 +00:00
|
|
|
|
2021-09-16 00:08:54 +00:00
|
|
|
let ours = ColliderContext {
|
|
|
|
pos,
|
|
|
|
previous_cache,
|
|
|
|
};
|
|
|
|
let theirs = ColliderContext {
|
|
|
|
pos: pos_other,
|
|
|
|
previous_cache: previous_cache_other,
|
|
|
|
};
|
|
|
|
let (diff, collision_dist) = projection_between(ours, theirs);
|
2021-09-15 21:27:48 +00:00
|
|
|
let in_collision_range = diff.magnitude_squared() <= collision_dist.powi(2);
|
2021-09-16 00:08:54 +00:00
|
|
|
|
2021-09-15 21:27:48 +00:00
|
|
|
if !in_collision_range {
|
2021-09-16 16:45:17 +00:00
|
|
|
return false;
|
2021-09-15 21:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If entities have not yet collided this tick (but just did) and if entity
|
|
|
|
// is either in mid air or is not sticky, then mark them as colliding with
|
|
|
|
// the other entity.
|
|
|
|
if !*collision_registered && (is_mid_air || !is_sticky) {
|
|
|
|
physics.touch_entities.insert(other);
|
|
|
|
*entity_entity_collisions += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't apply e2e pushback to entities that are in a forced movement state
|
|
|
|
// (e.g. roll, leapmelee).
|
|
|
|
//
|
|
|
|
// This allows leaps to work properly (since you won't get pushed away
|
|
|
|
// before delivering the hit), and allows rolling through an enemy when
|
|
|
|
// trapped (e.g. with minotaur).
|
|
|
|
//
|
|
|
|
// This allows using e2e pushback to gain speed by jumping out of a roll
|
|
|
|
// while in the middle of a collider, this is an intentional combat mechanic.
|
|
|
|
let forced_movement = matches!(char_state_maybe, Some(cs) if cs.is_forced_movement());
|
|
|
|
|
|
|
|
// Don't apply repulsive force to projectiles,
|
|
|
|
// or if we're colliding with a terrain-like entity,
|
|
|
|
// or if we are a terrain-like entity.
|
|
|
|
//
|
|
|
|
// Don't apply force when entity is a sticky which is on the ground
|
|
|
|
// (or on the wall).
|
|
|
|
if !forced_movement
|
|
|
|
&& (!is_sticky || is_mid_air)
|
|
|
|
&& diff.magnitude_squared() > 0.0
|
|
|
|
&& !is_projectile
|
|
|
|
&& !matches!(collider_other, Some(Collider::Voxel { .. }))
|
|
|
|
&& !matches!(collider, Some(Collider::Voxel { .. }))
|
|
|
|
{
|
2021-09-16 16:45:17 +00:00
|
|
|
const ELASTIC_FORCE_COEFFICIENT: f32 = 400.0;
|
2021-09-16 00:08:54 +00:00
|
|
|
let mass_coefficient = mass_other.0 / (mass.0 + mass_other.0);
|
|
|
|
let distance_coefficient = collision_dist - diff.magnitude();
|
2021-09-16 16:45:17 +00:00
|
|
|
let force = ELASTIC_FORCE_COEFFICIENT * distance_coefficient * mass_coefficient;
|
2021-09-15 21:27:48 +00:00
|
|
|
|
|
|
|
*vel_delta += Vec3::from(diff.normalized()) * force * step_delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
*collision_registered = true;
|
|
|
|
|
2021-09-16 16:45:17 +00:00
|
|
|
true
|
2021-09-15 21:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ColliderContext<'a> {
|
|
|
|
pos: Vec3<f32>,
|
|
|
|
previous_cache: &'a PreviousPhysCache,
|
|
|
|
}
|
|
|
|
|
2021-09-16 10:42:07 +00:00
|
|
|
/// Find pushback vector and collision_distance we assume between this
|
|
|
|
/// colliders.
|
2021-09-16 00:08:54 +00:00
|
|
|
fn projection_between(c0: ColliderContext, c1: ColliderContext) -> (Vec2<f32>, f32) {
|
2021-09-16 15:25:05 +00:00
|
|
|
const DIFF_THRESHOLD: f32 = std::f32::EPSILON;
|
|
|
|
let our_radius = c0.previous_cache.neighborhood_radius;
|
|
|
|
let their_radius = c1.previous_cache.neighborhood_radius;
|
|
|
|
let collision_dist = our_radius + their_radius;
|
|
|
|
|
|
|
|
let we = c0.pos.xy();
|
|
|
|
let other = c1.pos.xy();
|
|
|
|
|
|
|
|
let (p0_offset, p1_offset) = match c0.previous_cache.origins {
|
|
|
|
Some(origins) => origins,
|
|
|
|
// fallback to simpler model
|
|
|
|
None => return capsule2cylinder(c0, c1),
|
|
|
|
};
|
|
|
|
let segment = LineSegment2 {
|
|
|
|
start: we + p0_offset,
|
|
|
|
end: we + p1_offset,
|
|
|
|
};
|
|
|
|
|
|
|
|
let (p0_offset_other, p1_offset_other) = match c1.previous_cache.origins {
|
|
|
|
Some(origins) => origins,
|
|
|
|
// fallback to simpler model
|
|
|
|
None => return capsule2cylinder(c0, c1),
|
|
|
|
};
|
|
|
|
let segment_other = LineSegment2 {
|
|
|
|
start: other + p0_offset_other,
|
|
|
|
end: other + p1_offset_other,
|
|
|
|
};
|
|
|
|
|
|
|
|
let (our, their) = closest_points(segment, segment_other);
|
|
|
|
let diff = our - their;
|
|
|
|
|
|
|
|
if diff.magnitude_squared() < DIFF_THRESHOLD {
|
|
|
|
capsule2cylinder(c0, c1)
|
|
|
|
} else {
|
|
|
|
(diff, collision_dist)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 16:45:17 +00:00
|
|
|
/// Returns the points on line segments n and m respectively that are the
|
|
|
|
/// closest to one-another. If the lines are parallel, an arbitrary,
|
|
|
|
/// unspecified pair of points that sit on the line segments will be chosen.
|
2021-09-16 15:25:05 +00:00
|
|
|
fn closest_points(n: LineSegment2<f32>, m: LineSegment2<f32>) -> (Vec2<f32>, Vec2<f32>) {
|
2021-09-16 16:45:17 +00:00
|
|
|
// TODO: Rewrite this to something reasonable, if you have faith
|
|
|
|
#![allow(clippy::many_single_char_names)]
|
|
|
|
|
|
|
|
let a = n.start;
|
|
|
|
let b = n.end - n.start;
|
|
|
|
let c = m.start;
|
|
|
|
let d = m.end - m.start;
|
|
|
|
|
|
|
|
// Check to prevent div by 0.0 (produces NaNs) and minimize precision
|
|
|
|
// loss from dividing by small values.
|
|
|
|
// If both d.x and d.y are 0.0 then the segment is a point and we are fine
|
|
|
|
// to fallback to the end point projection.
|
|
|
|
let t = if d.x > d.y {
|
|
|
|
(d.y / d.x * (c.x - a.x) + a.y - c.y) / (b.x * d.y / d.x - b.y)
|
|
|
|
} else {
|
|
|
|
(d.x / d.y * (c.y - a.y) + a.x - c.x) / (b.y * d.x / d.y - b.x)
|
|
|
|
};
|
|
|
|
let u = if d.y > d.x {
|
|
|
|
(a.y + t * b.y - c.y) / d.y
|
|
|
|
} else {
|
|
|
|
(a.x + t * b.x - c.x) / d.x
|
|
|
|
};
|
2021-09-16 15:25:05 +00:00
|
|
|
|
|
|
|
// Check to see whether the lines are parallel
|
|
|
|
if !t.is_finite() || !u.is_finite() {
|
|
|
|
core::array::IntoIter::new([
|
|
|
|
(n.projected_point(m.start), m.start),
|
|
|
|
(n.projected_point(m.end), m.end),
|
|
|
|
(n.start, m.projected_point(n.start)),
|
|
|
|
(n.end, m.projected_point(n.end)),
|
|
|
|
])
|
2021-09-16 16:45:17 +00:00
|
|
|
.min_by_key(|(a, b)| ordered_float::OrderedFloat(a.distance_squared(*b)))
|
|
|
|
.expect("Lines had non-finite elements")
|
2021-09-16 15:25:05 +00:00
|
|
|
} else {
|
|
|
|
let t = t.clamped(0.0, 1.0);
|
|
|
|
let u = u.clamped(0.0, 1.0);
|
|
|
|
|
2021-09-16 16:45:17 +00:00
|
|
|
let close_n = a + b * t;
|
|
|
|
let close_m = c + d * u;
|
2021-09-16 15:25:05 +00:00
|
|
|
|
|
|
|
let proj_n = n.projected_point(close_m);
|
|
|
|
let proj_m = m.projected_point(close_n);
|
|
|
|
|
|
|
|
if proj_n.distance_squared(close_m) < proj_m.distance_squared(close_n) {
|
|
|
|
(proj_n, close_m)
|
|
|
|
} else {
|
|
|
|
(close_n, proj_m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find pushback vector and collision_distance we assume between this
|
|
|
|
/// colliders assuming that only one of them is capsule prism.
|
|
|
|
fn capsule2cylinder(c0: ColliderContext, c1: ColliderContext) -> (Vec2<f32>, f32) {
|
2021-09-15 21:27:48 +00:00
|
|
|
// "Proper" way to do this would be handle the case when both our colliders
|
|
|
|
// are capsule prisms by building origins from p0, p1 offsets and our
|
|
|
|
// positions and find some sort of projection between line segments of
|
|
|
|
// both colliders.
|
|
|
|
// While it's possible, it's not a trivial operation especially
|
|
|
|
// in the case when they are intersect. Because in such case,
|
|
|
|
// even when you found intersection and you should push entities back
|
|
|
|
// from each other, you get then difference between them is 0 vector.
|
|
|
|
//
|
|
|
|
// Considering that we won't fully simulate collision of capsule prism.
|
|
|
|
// As intermediate solution, we would assume that bigger collider
|
|
|
|
// (with bigger scaled_radius) is capsule prism (cylinder is special
|
|
|
|
// case of capsule prism too) and smaller collider is cylinder (point is
|
|
|
|
// special case of cylinder).
|
2021-09-16 00:08:54 +00:00
|
|
|
// So in the end our model of collision and pushback vector is simplified
|
|
|
|
// to checking distance of the point between segment of capsule.
|
|
|
|
//
|
|
|
|
// NOTE: no matter if we consider our collider capsule prism or cylinder
|
|
|
|
// we should always build pushback vector to have direction
|
|
|
|
// of motion from our target collider to our collider.
|
|
|
|
//
|
|
|
|
// TODO: can code beloew be deduplicated? :think:
|
2021-09-16 13:53:29 +00:00
|
|
|
let we = c0.pos.xy();
|
|
|
|
let other = c1.pos.xy();
|
2021-09-15 21:27:48 +00:00
|
|
|
if c0.previous_cache.scaled_radius > c1.previous_cache.scaled_radius {
|
2021-09-16 00:08:54 +00:00
|
|
|
let our_radius = c0.previous_cache.neighborhood_radius;
|
|
|
|
let their_radius = c1.previous_cache.scaled_radius;
|
|
|
|
let collision_dist = our_radius + their_radius;
|
|
|
|
|
|
|
|
let (p0_offset, p1_offset) = match c0.previous_cache.origins {
|
|
|
|
Some(origins) => origins,
|
|
|
|
None => return (we - other, collision_dist),
|
|
|
|
};
|
2021-09-15 21:27:48 +00:00
|
|
|
let segment = LineSegment2 {
|
2021-09-16 00:08:54 +00:00
|
|
|
start: we + p0_offset,
|
|
|
|
end: we + p1_offset,
|
2021-09-15 21:27:48 +00:00
|
|
|
};
|
|
|
|
|
2021-09-16 13:53:29 +00:00
|
|
|
let projection = segment.projected_point(other) - other;
|
2021-09-16 00:08:54 +00:00
|
|
|
|
|
|
|
(projection, collision_dist)
|
2021-09-15 21:27:48 +00:00
|
|
|
} else {
|
2021-09-16 00:08:54 +00:00
|
|
|
let our_radius = c0.previous_cache.scaled_radius;
|
|
|
|
let their_radius = c1.previous_cache.neighborhood_radius;
|
|
|
|
let collision_dist = our_radius + their_radius;
|
|
|
|
|
|
|
|
let (p0_offset_other, p1_offset_other) = match c1.previous_cache.origins {
|
|
|
|
Some(origins) => origins,
|
|
|
|
None => return (we - other, collision_dist),
|
|
|
|
};
|
2021-09-15 21:27:48 +00:00
|
|
|
let segment_other = LineSegment2 {
|
2021-09-16 00:08:54 +00:00
|
|
|
start: other + p0_offset_other,
|
|
|
|
end: other + p1_offset_other,
|
2021-09-15 21:27:48 +00:00
|
|
|
};
|
|
|
|
|
2021-09-16 00:08:54 +00:00
|
|
|
let projection = we - segment_other.projected_point(we);
|
|
|
|
|
|
|
|
(projection, collision_dist)
|
2021-09-15 21:27:48 +00:00
|
|
|
}
|
2021-09-15 15:11:41 +00:00
|
|
|
}
|