Addressed review issues

This commit is contained in:
Joshua Barretto 2020-08-25 11:01:17 +01:00
parent 12ea028a3d
commit 37b45ba5f4
3 changed files with 23 additions and 32 deletions

View File

@ -100,7 +100,7 @@ pub struct PhysicsState {
} }
impl PhysicsState { impl PhysicsState {
pub fn clear(&mut self) { pub fn reset(&mut self) {
// Avoid allocation overhead! // Avoid allocation overhead!
let mut touch_entities = std::mem::take(&mut self.touch_entities); let mut touch_entities = std::mem::take(&mut self.touch_entities);
touch_entities.clear(); touch_entities.clear();

View File

@ -418,7 +418,7 @@ impl Chaser {
}); });
if !walking_towards_edge { if !walking_towards_edge {
Some(((tgt - pos) * Vec3::new(1.0, 1.0, 0.0), 0.75)) Some(((tgt - pos) * Vec3::new(1.0, 1.0, 0.0), 1.0))
} else { } else {
None None
} }
@ -458,7 +458,7 @@ where
let is_walkable = |pos: &Vec3<i32>| walkable(vol, *pos); let is_walkable = |pos: &Vec3<i32>| walkable(vol, *pos);
let get_walkable_z = |pos| { let get_walkable_z = |pos| {
let mut z_incr = 0; let mut z_incr = 0;
for _ in 0..128 { for _ in 0..32 {
let test_pos = pos + Vec3::unit_z() * z_incr; let test_pos = pos + Vec3::unit_z() * z_incr;
if is_walkable(&test_pos) { if is_walkable(&test_pos) {
return Some(test_pos); return Some(test_pos);

View File

@ -11,8 +11,8 @@ use crate::{
}; };
use rayon::iter::ParallelIterator; use rayon::iter::ParallelIterator;
use specs::{ use specs::{
saveload::MarkerAllocator, storage, Entities, Join, ParJoin, Read, ReadExpect, ReadStorage, saveload::MarkerAllocator, Entities, Join, ParJoin, Read, ReadExpect, ReadStorage, System,
System, WriteStorage, WriteStorage,
}; };
use std::ops::Range; use std::ops::Range;
use vek::*; use vek::*;
@ -96,25 +96,18 @@ impl<'a> System<'a> for Sys {
let mut event_emitter = event_bus.emitter(); let mut event_emitter = event_bus.emitter();
// Add/reset physics state components // Add/reset physics state components
for entity in ( for (entity, _, _, _, _) in (
&entities, &entities,
!&physics_states,
&colliders, &colliders,
&positions, &positions,
&velocities, &velocities,
&orientations, &orientations,
) )
.join() .join()
.map(|(e, _, _, _, _, _)| e)
.collect::<Vec<_>>()
{ {
match physics_states.entry(entity) { let _ = physics_states
Ok(storage::StorageEntry::Occupied(mut o)) => o.get_mut().clear(), .entry(entity)
Ok(storage::StorageEntry::Vacant(v)) => { .map(|e| e.or_insert_with(Default::default));
v.insert(Default::default());
},
Err(_) => {},
}
} }
// Apply pushback // Apply pushback
@ -159,6 +152,8 @@ impl<'a> System<'a> for Sys {
.and_then(|uid| uid_allocator.retrieve_entity_internal(uid.into())) .and_then(|uid| uid_allocator.retrieve_entity_internal(uid.into()))
.and_then(|e| groups.get(e)); .and_then(|e| groups.get(e));
let mut vel_delta = Vec3::zero();
for ( for (
entity_other, entity_other,
other, other,
@ -200,23 +195,17 @@ impl<'a> System<'a> for Sys {
let vel_other = velocities.get(entity_other).copied().unwrap_or_default().0; let vel_other = velocities.get(entity_other).copied().unwrap_or_default().0;
// Sanity check: don't try colliding entities that are too far from each other // Sanity check: don't try colliding entities that are too far from each other
// Note: I think this catches all cases. If you get entitiy collision problems, // Note: I think this catches all cases. If you get entity collision problems,
// try removing this! // try removing this!
if (pos.0 - pos_other.0).magnitude() if (pos.0 - pos_other.0).magnitude()
> ((vel + vel_other) * dt.0).magnitude() + collision_dist > ((vel - vel_other) * dt.0).magnitude() + collision_dist
{ {
continue; continue;
} }
let min_collision_dist = 0.3; let min_collision_dist = 0.3;
// Ideally we'd deal with collision speed to minimise work here, but for not let increments = ((vel - vel_other).magnitude() * dt.0 / min_collision_dist)
// taking the maximum velocity of the two is fine. .max(1.0)
let increments = (vel
.magnitude_squared()
.max(vel_other.magnitude_squared())
.sqrt()
* dt.0
/ min_collision_dist)
.ceil() as usize; .ceil() as usize;
let step_delta = 1.0 / increments as f32; let step_delta = 1.0 / increments as f32;
let mut collided = false; let mut collided = false;
@ -241,16 +230,18 @@ impl<'a> System<'a> for Sys {
let force = 40.0 * (collision_dist - diff.magnitude()) * mass_other let force = 40.0 * (collision_dist - diff.magnitude()) * mass_other
/ (mass + mass_other); / (mass + mass_other);
// Change velocity vel_delta += Vec3::from(diff.normalized()) * force * step_delta;
velocities.get_mut(entity).map(|vel| {
vel.0 += Vec3::from(diff.normalized()) * force * dt.0 * step_delta
});
} }
collided = true; collided = true;
} }
} }
} }
// Change velocity
velocities
.get_mut(entity)
.map(|vel| vel.0 += vel_delta * dt.0);
} }
// Apply movement inputs // Apply movement inputs
@ -258,7 +249,7 @@ impl<'a> System<'a> for Sys {
&entities, &entities,
scales.maybe(), scales.maybe(),
stickies.maybe(), stickies.maybe(),
colliders.maybe(), &colliders,
&mut positions, &mut positions,
&mut velocities, &mut velocities,
&mut orientations, &mut orientations,
@ -318,7 +309,7 @@ impl<'a> System<'a> for Sys {
Vec3::zero() Vec3::zero()
}; };
match collider.copied().unwrap_or(Collider::Point) { match *collider {
Collider::Box { Collider::Box {
radius, radius,
z_min, z_min,