Skip to content

Commit

Permalink
still buggy?
Browse files Browse the repository at this point in the history
  • Loading branch information
meyerzinn committed May 2, 2023
1 parent 7cf8854 commit 513cb3d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
37 changes: 27 additions & 10 deletions src/voxel/world/physics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy::prelude::{
SystemSet, Transform, Vec3,
};
use bevy::render::primitives::Aabb;
use itertools::iproduct;
use itertools::{iproduct, Itertools};
use std::cmp::Ordering;
use std::f32::{INFINITY, NEG_INFINITY};

Expand All @@ -33,7 +33,7 @@ const SIM_TIME: f32 = 1.0 / 20.0;
#[derive(Component)]
/// Marker component for entities that can collide with voxels.
pub struct Collider {
pub half_extents: Vec3A,
pub aabb: Aabb,
}

#[derive(Debug)]
Expand Down Expand Up @@ -128,24 +128,41 @@ fn step(
}

let mut displacement = **velocity * SIM_TIME;
if let Some(&Collider { half_extents }) = collider {
if let Some(&Collider { aabb }) = collider {
let aabb = Aabb {
center: transform.translation.into(),
half_extents,
center: Vec3A::from(transform.translation) + aabb.center,
..aabb
};

let start = (Vec3A::min(aabb.min(), aabb.min() + displacement).floor()).as_ivec3()
+ IVec3::NEG_Y;
let end = Vec3A::max(aabb.max(), aabb.max() + displacement)
.floor()
.as_ivec3();

println!(
"pos: {}, start: {}, end: {}",
aabb.center, start, end
);

assert!(start.cmple(end).all());

if let Some(collision) = iproduct!(start.x..end.x, start.y..end.y, start.z..end.z)
let all_voxels = iproduct!(start.x..end.x, start.y..end.y, start.z..end.z)
.map(|(x, y, z)| IVec3::new(x, y, z))
.filter(|voxel| voxels.voxel_at(*voxel).is_some_and(Voxel::collidable))
.map(|voxel| swept_voxel_collision(aabb, displacement, voxel_aabb(voxel)))
.collect_vec();

let interesting_voxels = all_voxels
.iter()
.filter(|voxel| voxels.voxel_at(**voxel).is_some_and(Voxel::collidable))
.collect_vec();

// println!(
// "pos: {}, voxels: {:?}, interesting voxels: {:?}",
// transform.translation, all_voxels, interesting_voxels
// );

if let Some(collision) = interesting_voxels
.into_iter()
.map(|voxel| swept_voxel_collision(aabb, displacement, voxel_aabb(*voxel)))
.flatten()
.min_by(|a, b| {
if a.time < b.time {
Expand All @@ -158,7 +175,7 @@ fn step(
println!("resolving collision!");

// clip the displacement to avoid overlap
displacement = **velocity * collision.time;
displacement = **velocity * collision.time * SIM_TIME;

let previous_velocity = **velocity;
// cancel velocity in the normal direction
Expand Down
17 changes: 13 additions & 4 deletions src/voxel/world/player.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use bevy::{input::mouse::MouseMotion, math::Vec3A, prelude::*, window::CursorGrabMode};
use bevy::{
input::mouse::MouseMotion, math::Vec3A, prelude::*, render::primitives::Aabb,
window::CursorGrabMode,
};
use bevy_egui::EguiContexts;
use std::f32::consts::FRAC_PI_2;

Expand Down Expand Up @@ -26,9 +29,12 @@ impl Default for PlayerBundle {
velocity: Default::default(),
acceleration: Default::default(),
collider: Collider {
half_extents: Vec3A::new(0.25, 0.9, 0.25),
aabb: Aabb {
center: Vec3A::new(0.0, -0.7, 0.0), // the collision center is ~0.6m below the eyes, which are ~0.2m below the top of the collision box
half_extents: Vec3A::new(0.4, 0.9, 0.4),
},
},
drag: Drag(0.99),
drag: Drag(0.98),
}
}
}
Expand Down Expand Up @@ -135,7 +141,10 @@ pub fn handle_player_input(
// transform.translation += direction.x * right * acceleration
// + direction.z * forward * acceleration
// + direction.y * Vec3::Y * acceleration;
**acceleration = (speed * direction.x * right + direction.z * forward * speed + direction.y * Vec3::Y * speed).into();
**acceleration = (speed * direction.x * right
+ direction.z * forward * speed
+ direction.y * Vec3::Y * speed)
.into();
}

#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, SystemSet)]
Expand Down

0 comments on commit 513cb3d

Please sign in to comment.