2019-08-23 10:11:37 +00:00
|
|
|
use {
|
|
|
|
crate::{
|
2019-09-09 19:11:40 +00:00
|
|
|
comp::{Body, Mounting, Ori, PhysicsState, Pos, Scale, Vel},
|
2019-08-25 18:31:56 +00:00
|
|
|
event::{EventBus, LocalEvent},
|
2019-08-23 10:11:37 +00:00
|
|
|
state::DeltaTime,
|
2019-09-09 19:11:40 +00:00
|
|
|
terrain::{Block, TerrainGrid},
|
2019-09-04 23:03:49 +00:00
|
|
|
vol::ReadVol,
|
2019-08-03 11:26:05 +00:00
|
|
|
},
|
2019-08-23 10:11:37 +00:00
|
|
|
specs::{Entities, Join, Read, ReadExpect, ReadStorage, System, WriteStorage},
|
|
|
|
vek::*,
|
2019-03-02 03:48:30 +00:00
|
|
|
};
|
|
|
|
|
2019-09-09 19:11:40 +00:00
|
|
|
pub const GRAVITY: f32 = 9.81 * 4.0;
|
|
|
|
const BOUYANCY: f32 = 0.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
|
|
|
|
// amount an object will slow down within 1/60th of a second. Eg. if the frction
|
|
|
|
// 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.
|
|
|
|
const FRIC_GROUND: f32 = 0.125;
|
|
|
|
const FRIC_AIR: f32 = 0.0125;
|
2019-09-09 19:11:40 +00:00
|
|
|
const FRIC_FLUID: f32 = 0.2;
|
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
|
|
|
|
2019-09-05 10:24:38 +00:00
|
|
|
lv.z = (lv.z - grav * dt).max(-50.0);
|
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 {
|
2019-03-02 03:48:30 +00:00
|
|
|
type SystemData = (
|
2019-06-09 19:33:20 +00:00
|
|
|
Entities<'a>,
|
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>,
|
2019-08-25 18:31:56 +00:00
|
|
|
Read<'a, EventBus<LocalEvent>>,
|
2019-08-03 11:26:05 +00:00
|
|
|
ReadStorage<'a, Scale>,
|
|
|
|
ReadStorage<'a, Body>,
|
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>,
|
2019-09-09 19:11:40 +00:00
|
|
|
ReadStorage<'a, Mounting>,
|
2019-03-02 03:48:30 +00:00
|
|
|
);
|
|
|
|
|
2019-06-09 19:33:20 +00:00
|
|
|
fn run(
|
|
|
|
&mut self,
|
2019-06-11 19:28:25 +00:00
|
|
|
(
|
|
|
|
entities,
|
|
|
|
terrain,
|
|
|
|
dt,
|
2019-08-07 15:39:16 +00:00
|
|
|
event_bus,
|
2019-08-03 11:26:05 +00:00
|
|
|
scales,
|
|
|
|
bodies,
|
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,
|
2019-09-09 19:11:40 +00:00
|
|
|
mountings,
|
2019-06-11 19:28:25 +00:00
|
|
|
): Self::SystemData,
|
2019-06-09 19:33:20 +00:00
|
|
|
) {
|
2019-08-07 15:39:16 +00:00
|
|
|
let mut event_emitter = event_bus.emitter();
|
|
|
|
|
2019-06-11 19:28:25 +00:00
|
|
|
// Apply movement inputs
|
2019-09-09 19:11:40 +00:00
|
|
|
for (entity, scale, _b, mut pos, mut vel, _ori, _) in (
|
2019-06-13 18:09:50 +00:00
|
|
|
&entities,
|
2019-08-03 11:26:05 +00:00
|
|
|
scales.maybe(),
|
|
|
|
&bodies,
|
2019-06-13 18:09:50 +00:00
|
|
|
&mut positions,
|
|
|
|
&mut velocities,
|
|
|
|
&mut orientations,
|
2019-09-09 19:11:40 +00:00
|
|
|
!&mountings,
|
2019-06-13 18:09:50 +00:00
|
|
|
)
|
|
|
|
.join()
|
2019-06-09 19:33:20 +00:00
|
|
|
{
|
2019-08-23 10:11:37 +00:00
|
|
|
let mut physics_state = physics_states.get(entity).cloned().unwrap_or_default();
|
2019-08-03 11:26:05 +00:00
|
|
|
let scale = scale.map(|s| s.0).unwrap_or(1.0);
|
|
|
|
|
2019-04-23 22:48:31 +00:00
|
|
|
// Basic collision with terrain
|
2019-08-03 11:26:05 +00:00
|
|
|
let player_rad = 0.3 * scale; // half-width of the player's AABB
|
|
|
|
let player_height = 1.5 * scale;
|
2019-06-27 14:37:33 +00:00
|
|
|
|
|
|
|
// Probe distances
|
|
|
|
let hdist = player_rad.ceil() as i32;
|
|
|
|
let vdist = player_height.ceil() as i32;
|
|
|
|
// Neighbouring blocks iterator
|
|
|
|
let near_iter = (-hdist..=hdist)
|
|
|
|
.map(move |i| (-hdist..=hdist).map(move |j| (0..=vdist).map(move |k| (i, j, k))))
|
2019-06-25 16:13:30 +00:00
|
|
|
.flatten()
|
|
|
|
.flatten();
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
.max(if physics_state.in_fluid {
|
|
|
|
FRIC_FLUID
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
});
|
|
|
|
let downward_force = if physics_state.in_fluid {
|
|
|
|
(1.0 - BOUYANCY) * GRAVITY
|
2019-09-05 10:24:38 +00:00
|
|
|
} else {
|
2019-09-09 19:11:40 +00:00
|
|
|
GRAVITY
|
2019-09-05 10:24:38 +00:00
|
|
|
};
|
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
|
|
|
|
let pos_delta = if terrain
|
|
|
|
.get_key(terrain.pos_key(pos.0.map(|e| e.floor() as i32)))
|
|
|
|
.is_some()
|
|
|
|
{
|
2019-09-06 04:04:20 +00:00
|
|
|
// this is an approximation that allows most framerates to
|
|
|
|
// behave in a similar manner.
|
2019-09-05 10:24:38 +00:00
|
|
|
(vel.0 + old_vel.0 * 4.0) * dt.0 * 0.2
|
|
|
|
} else {
|
|
|
|
Vec3::zero()
|
|
|
|
};
|
|
|
|
|
2019-06-26 00:28:30 +00:00
|
|
|
// Function for determining whether the player at a specific position collides with the ground
|
2019-09-09 19:11:40 +00:00
|
|
|
let collision_with = |pos: Vec3<f32>, hit: fn(&Block) -> bool, near_iter| {
|
2019-06-25 18:57:44 +00:00
|
|
|
for (i, j, k) in near_iter {
|
|
|
|
let block_pos = pos.map(|e| e.floor() as i32) + Vec3::new(i, j, k);
|
|
|
|
|
2019-09-09 19:11:40 +00:00
|
|
|
if terrain.get(block_pos).map(hit).unwrap_or(false) {
|
2019-06-26 09:40:07 +00:00
|
|
|
let player_aabb = Aabb {
|
2019-06-26 00:28:30 +00:00
|
|
|
min: pos + Vec3::new(-player_rad, -player_rad, 0.0),
|
|
|
|
max: pos + Vec3::new(player_rad, player_rad, player_height),
|
|
|
|
};
|
|
|
|
let block_aabb = Aabb {
|
|
|
|
min: block_pos.map(|e| e as f32),
|
|
|
|
max: block_pos.map(|e| e as f32) + 1.0,
|
|
|
|
};
|
2019-06-25 18:57:44 +00:00
|
|
|
|
2019-06-26 09:40:07 +00:00
|
|
|
if player_aabb.collides_with_aabb(block_aabb) {
|
2019-06-25 20:26:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-06-25 18:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
2019-08-23 10:11:37 +00:00
|
|
|
let was_on_ground = physics_state.on_ground;
|
|
|
|
physics_state.on_ground = false;
|
2019-06-25 18:57:44 +00:00
|
|
|
|
|
|
|
let mut on_ground = false;
|
2019-06-26 10:50:55 +00:00
|
|
|
let mut attempts = 0; // Don't loop infinitely here
|
|
|
|
|
2019-06-26 21:43:47 +00:00
|
|
|
// Don't jump too far at once
|
2019-06-30 17:58:40 +00:00
|
|
|
let increments = (pos_delta.map(|e| e.abs()).reduce_partial_max() / 0.3)
|
2019-06-26 21:43:47 +00:00
|
|
|
.ceil()
|
|
|
|
.max(1.0);
|
2019-07-25 17:41:06 +00:00
|
|
|
let old_pos = pos.0;
|
2019-06-26 21:43:47 +00:00
|
|
|
for _ in 0..increments as usize {
|
2019-06-30 17:58:40 +00:00
|
|
|
pos.0 += pos_delta / increments;
|
2019-06-25 16:13:30 +00:00
|
|
|
|
2019-08-06 15:00:14 +00:00
|
|
|
const MAX_ATTEMPTS: usize = 16;
|
|
|
|
|
2019-06-26 21:43:47 +00:00
|
|
|
// While the player is colliding with the terrain...
|
2019-09-09 19:11:40 +00:00
|
|
|
while collision_with(pos.0, |vox| vox.is_solid(), near_iter.clone())
|
|
|
|
&& attempts < MAX_ATTEMPTS
|
|
|
|
{
|
2019-06-26 21:43:47 +00:00
|
|
|
// Calculate the player's AABB
|
|
|
|
let player_aabb = Aabb {
|
|
|
|
min: pos.0 + Vec3::new(-player_rad, -player_rad, 0.0),
|
|
|
|
max: pos.0 + Vec3::new(player_rad, player_rad, player_height),
|
|
|
|
};
|
2019-06-25 16:13:30 +00:00
|
|
|
|
2019-06-26 21:43:47 +00:00
|
|
|
// Determine the block that we are colliding with most (based on minimum collision axis)
|
2019-07-01 20:42:43 +00:00
|
|
|
let (_block_pos, block_aabb) = near_iter
|
2019-06-26 21:43:47 +00:00
|
|
|
.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))
|
|
|
|
// Calculate the AABB of the block
|
|
|
|
.map(|block_pos| {
|
|
|
|
(
|
|
|
|
block_pos,
|
|
|
|
Aabb {
|
|
|
|
min: block_pos.map(|e| e as f32),
|
|
|
|
max: block_pos.map(|e| e as f32) + 1.0,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
// Make sure the block is actually solid
|
|
|
|
.filter(|(block_pos, _)| {
|
|
|
|
terrain
|
|
|
|
.get(*block_pos)
|
2019-08-24 09:04:32 +00:00
|
|
|
.map(|vox| vox.is_solid())
|
2019-06-26 21:43:47 +00:00
|
|
|
.unwrap_or(false)
|
|
|
|
})
|
2019-09-09 19:11:40 +00:00
|
|
|
// Determine whether the block's AABB collides with the player's AABB
|
|
|
|
.filter(|(_, block_aabb)| block_aabb.collides_with_aabb(player_aabb))
|
2019-06-26 21:43:47 +00:00
|
|
|
// Find the maximum of the minimum collision axes (this bit is weird, trust me that it works)
|
2019-09-09 19:11:40 +00:00
|
|
|
.min_by_key(|(_, block_aabb)| {
|
|
|
|
((block_aabb.center() - player_aabb.center() - Vec3::unit_z() * 0.5)
|
2019-06-26 21:43:47 +00:00
|
|
|
.map(|e| e.abs())
|
2019-09-09 19:11:40 +00:00
|
|
|
.sum()
|
2019-06-30 22:46:46 +00:00
|
|
|
* 1_000_000.0) as i32
|
2019-06-26 21:43:47 +00:00
|
|
|
})
|
|
|
|
.expect("Collision detected, but no colliding blocks found!");
|
2019-06-25 16:13:30 +00:00
|
|
|
|
2019-06-26 21:43:47 +00:00
|
|
|
// Find the intrusion vector of the collision
|
|
|
|
let dir = player_aabb.collision_vector_with_aabb(block_aabb);
|
2019-06-25 16:13:30 +00:00
|
|
|
|
2019-06-26 21:43:47 +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();
|
2019-07-01 20:42:43 +00:00
|
|
|
let resolve_dir = -dir.map(|e| {
|
|
|
|
if e.abs().to_bits() == max_axis.to_bits() {
|
|
|
|
e
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
}
|
|
|
|
});
|
2019-06-26 09:40:07 +00:00
|
|
|
|
2019-06-26 21:43:47 +00:00
|
|
|
// When the resolution direction is pointing upwards, we must be on the ground
|
2019-06-27 14:21:41 +00:00
|
|
|
if resolve_dir.z > 0.0 && vel.0.z <= 0.0 {
|
2019-06-26 21:43:47 +00:00
|
|
|
on_ground = true;
|
2019-08-07 09:50:49 +00:00
|
|
|
|
2019-08-07 15:39:16 +00:00
|
|
|
if !was_on_ground {
|
2019-08-25 18:31:56 +00:00
|
|
|
event_emitter.emit(LocalEvent::LandOnGround { entity, vel: vel.0 });
|
2019-08-07 09:50:49 +00:00
|
|
|
}
|
2019-06-26 21:43:47 +00:00
|
|
|
}
|
2019-06-26 09:43:03 +00:00
|
|
|
|
2019-06-26 21:43:47 +00:00
|
|
|
// When the resolution direction is non-vertical, we must be colliding with a wall
|
|
|
|
// If the space above is free...
|
2019-09-09 19:11:40 +00:00
|
|
|
if !collision_with(Vec3::new(pos.0.x, pos.0.y, (pos.0.z + 0.1).ceil()), |vox| vox.is_solid(), near_iter.clone())
|
2019-06-30 22:46:46 +00:00
|
|
|
// ...and we're being pushed out horizontally...
|
2019-06-29 13:28:09 +00:00
|
|
|
&& resolve_dir.z == 0.0
|
2019-07-09 10:51:00 +00:00
|
|
|
// ...and the vertical resolution direction is sufficiently great...
|
2019-07-09 12:37:51 +00:00
|
|
|
&& -dir.z > 0.1
|
2019-06-30 22:46:46 +00:00
|
|
|
// ...and we're falling/standing OR there is a block *directly* beneath our current origin (note: not hitbox)...
|
2019-07-26 13:42:36 +00:00
|
|
|
&& (vel.0.z <= 0.0 || terrain
|
|
|
|
.get((pos.0 - Vec3::unit_z() * 0.1).map(|e| e.floor() as i32))
|
2019-08-24 09:04:32 +00:00
|
|
|
.map(|vox| vox.is_solid())
|
2019-06-30 22:46:46 +00:00
|
|
|
.unwrap_or(false))
|
|
|
|
// ...and there is a collision with a block beneath our current hitbox...
|
2019-06-29 21:42:20 +00:00
|
|
|
&& collision_with(
|
2019-07-25 17:41:06 +00:00
|
|
|
old_pos + resolve_dir - Vec3::unit_z() * 1.05,
|
2019-09-09 19:11:40 +00:00
|
|
|
|vox| vox.is_solid(),
|
2019-06-29 21:42:20 +00:00
|
|
|
near_iter.clone(),
|
|
|
|
)
|
2019-06-26 21:43:47 +00:00
|
|
|
{
|
|
|
|
// ...block-hop!
|
2019-07-09 10:51:00 +00:00
|
|
|
pos.0.z = (pos.0.z + 0.1).ceil();
|
2019-06-26 21:43:47 +00:00
|
|
|
on_ground = true;
|
|
|
|
break;
|
|
|
|
} else {
|
2019-07-09 14:07:53 +00:00
|
|
|
// Correct the velocity
|
2019-07-09 12:37:51 +00:00
|
|
|
vel.0 = vel.0.map2(
|
|
|
|
resolve_dir,
|
|
|
|
|e, d| if d * e.signum() < 0.0 { 0.0 } else { e },
|
|
|
|
);
|
2019-06-26 21:43:47 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 14:07:53 +00:00
|
|
|
// Resolve the collision normally
|
|
|
|
pos.0 += resolve_dir;
|
|
|
|
|
2019-06-26 21:43:47 +00:00
|
|
|
attempts += 1;
|
|
|
|
}
|
2019-08-06 15:00:14 +00:00
|
|
|
|
|
|
|
if attempts == MAX_ATTEMPTS {
|
|
|
|
pos.0 = old_pos;
|
|
|
|
break;
|
|
|
|
}
|
2019-04-23 22:48:31 +00:00
|
|
|
}
|
2019-06-25 18:57:44 +00:00
|
|
|
|
|
|
|
if on_ground {
|
2019-08-23 10:11:37 +00:00
|
|
|
physics_state.on_ground = true;
|
2019-07-09 10:51:00 +00:00
|
|
|
// If the space below us is free, then "snap" to the ground
|
2019-09-09 19:11:40 +00:00
|
|
|
} else if collision_with(
|
|
|
|
pos.0 - Vec3::unit_z() * 1.05,
|
|
|
|
|vox| vox.is_solid(),
|
|
|
|
near_iter.clone(),
|
|
|
|
) && vel.0.z < 0.0
|
2019-07-09 14:07:53 +00:00
|
|
|
&& vel.0.z > -1.5
|
2019-06-27 14:21:41 +00:00
|
|
|
&& was_on_ground
|
2019-06-26 00:28:30 +00:00
|
|
|
{
|
2019-06-25 20:26:04 +00:00
|
|
|
pos.0.z = (pos.0.z - 0.05).floor();
|
2019-08-23 10:11:37 +00:00
|
|
|
physics_state.on_ground = true;
|
2019-06-25 20:26:04 +00:00
|
|
|
}
|
2019-08-23 10:11:37 +00:00
|
|
|
|
2019-09-09 19:11:40 +00:00
|
|
|
let dirs = [
|
|
|
|
Vec3::unit_x(),
|
|
|
|
Vec3::unit_y(),
|
|
|
|
-Vec3::unit_x(),
|
|
|
|
-Vec3::unit_y(),
|
|
|
|
];
|
|
|
|
|
|
|
|
if let (wall_dir, true) = dirs.iter().fold((Vec3::zero(), false), |(a, hit), dir| {
|
|
|
|
if collision_with(pos.0 + *dir * 0.01, |vox| vox.is_solid(), near_iter.clone()) {
|
|
|
|
(a + dir, true)
|
|
|
|
} else {
|
|
|
|
(a, hit)
|
|
|
|
}
|
|
|
|
}) {
|
|
|
|
physics_state.on_wall = Some(wall_dir);
|
|
|
|
} else {
|
|
|
|
physics_state.on_wall = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out if we're in water
|
|
|
|
physics_state.in_fluid = collision_with(pos.0, |vox| vox.is_fluid(), near_iter.clone());
|
|
|
|
|
2019-08-23 10:11:37 +00:00
|
|
|
let _ = physics_states.insert(entity, physics_state);
|
2019-04-23 22:48:31 +00:00
|
|
|
}
|
2019-08-03 11:26:05 +00:00
|
|
|
|
|
|
|
// Apply pushback
|
2019-09-09 19:11:40 +00:00
|
|
|
for (pos, scale, vel, _, _) in (
|
|
|
|
&positions,
|
|
|
|
scales.maybe(),
|
|
|
|
&mut velocities,
|
|
|
|
&bodies,
|
|
|
|
!&mountings,
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2019-08-03 11:26:05 +00:00
|
|
|
let scale = scale.map(|s| s.0).unwrap_or(1.0);
|
2019-09-09 19:11:40 +00:00
|
|
|
for (pos_other, scale_other, _, _) in
|
|
|
|
(&positions, scales.maybe(), &bodies, !&mountings).join()
|
|
|
|
{
|
2019-08-03 11:26:05 +00:00
|
|
|
let scale_other = scale_other.map(|s| s.0).unwrap_or(1.0);
|
|
|
|
let diff = Vec2::<f32>::from(pos.0 - pos_other.0);
|
|
|
|
|
2019-08-03 21:11:57 +00:00
|
|
|
let collision_dist = 0.95 * (scale + scale_other);
|
2019-08-03 11:26:05 +00:00
|
|
|
|
|
|
|
if diff.magnitude_squared() > 0.0
|
|
|
|
&& diff.magnitude_squared() < collision_dist.powf(2.0)
|
2019-08-03 18:09:01 +00:00
|
|
|
&& pos.0.z + 1.6 * scale > pos_other.0.z
|
|
|
|
&& pos.0.z < pos_other.0.z + 1.6 * scale_other
|
2019-08-03 11:26:05 +00:00
|
|
|
{
|
|
|
|
vel.0 +=
|
2019-08-03 21:11:57 +00:00
|
|
|
Vec3::from(diff.normalized()) * (collision_dist - diff.magnitude()) * 1.0;
|
2019-08-03 11:26:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-02 03:48:30 +00:00
|
|
|
}
|
|
|
|
}
|