Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions server/src/physics/block_bboxes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! Bounding boxes for every non-cubic block.

use crate::physics::component::bbox;
use feather_blocks::Block;
use ncollide3d::bounding_volume::AABB;

/// Returns the bounding box for the given block.
///
/// Non-solid blocks have no bounding box,
/// and the bounding box for a non-solid block
/// is undefined.
pub fn bbox_for_block(block: &Block) -> AABB<f64> {
match block {
Block::WhiteBed(_)
| Block::OrangeBed(_)
| Block::MagentaBed(_)
| Block::LightBlueBed(_)
| Block::YellowBed(_)
| Block::LimeBed(_)
| Block::PinkBed(_)
| Block::GrayBed(_)
| Block::LightGrayBed(_)
| Block::CyanBed(_)
| Block::PurpleBed(_)
| Block::BlueBed(_)
| Block::BrownBed(_)
| Block::GreenBed(_)
| Block::RedBed(_)
| Block::BlackBed(_)
| Block::PrismarineSlab(_)
| Block::PrismarineBrickSlab(_)
| Block::DarkPrismarineSlab(_)
| Block::OakSlab(_)
| Block::SpruceSlab(_)
| Block::BirchSlab(_)
| Block::JungleSlab(_)
| Block::AcaciaSlab(_)
| Block::DarkOakSlab(_)
| Block::StoneSlab(_)
| Block::SandstoneSlab(_)
| Block::PetrifiedOakSlab(_)
| Block::CobblestoneSlab(_)
| Block::BrickSlab(_)
| Block::StoneBrickSlab(_)
| Block::NetherBrickSlab(_)
| Block::QuartzSlab(_)
| Block::RedSandstoneSlab(_)
| Block::PurpurSlab(_) => bbox(1.0, 0.5),
_ => bbox(1.0, 1.0),
}
}
31 changes: 18 additions & 13 deletions server/src/physics/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
//! the physics system.

use crate::entity::{ChunkEntities, PositionComponent};
use crate::physics::block_bboxes::bbox_for_block;
use crate::physics::BoundingBoxComponent;
use feather_blocks::Block;
use feather_core::world::{BlockPosition, ChunkMap, Position};
use feather_core::{BlockExt, ChunkPosition};
use glm::{vec3, DVec3, Vec3};
Expand Down Expand Up @@ -157,12 +159,14 @@ pub fn block_impacted_by_ray(
// Calculate world-space position of
// impact using `ncollide`.
let ray = Ray::new(Point3::from(origin), direction);
let shape = block_shape();
let shape = block_shape(&block);
let isometry = block_isometry(current_pos);

let impact = shape
.toi_and_normal_with_ray(&isometry, &ray, true)
.unwrap(); // Unwrap is safe because we know the ray intersects the block
let impact = match shape.toi_and_normal_with_ray(&isometry, &ray, true) {
Some(toi) => toi,
None => continue,
};

let pos = Position::from(origin + impact.toi * direction);

return Some(RayImpact {
Expand Down Expand Up @@ -418,7 +422,7 @@ pub fn adjacent_to_bbox(
let sign = f64::from(sign);

let size = bbox.size() / 2.0;
let mut blocks: SmallVec<[BlockPosition; 4]> = smallvec![];
let mut blocks: SmallVec<[(BlockPosition, Block); 4]> = smallvec![];

let other_axis1 = match axis {
0 => 1,
Expand Down Expand Up @@ -473,27 +477,28 @@ pub fn adjacent_to_bbox(
Some(block) => {
if block.is_solid() {
checked.insert(block_pos).unwrap();
blocks.push(block_pos);
blocks.push((block_pos, block));
}
}
None => continue,
}
}

let mut shapes = Vec::with_capacity(4);
let block_shape = block_shape();

for block in &blocks {
let isometry = block_isometry(*block);
shapes.push((isometry, ShapeHandle::new(block_shape.clone())));
for (block_pos, block) in &blocks {
let isometry = block_isometry(*block_pos);
let shape = block_shape(&block);
shapes.push((isometry, ShapeHandle::new(shape)));
}

Compound::new(shapes)
}

/// Returns an `ncollide` `Cuboid` corresponding to a block.
pub fn block_shape() -> Cuboid<f64> {
Cuboid::new(vec3(0.5, 0.5, 0.5))
/// Returns an `ncollide` `Cuboid` corresponding to the given block.
pub fn block_shape(block: &Block) -> Cuboid<f64> {
let bbox = bbox_for_block(block);
Cuboid::new(bbox.half_extents())
}

/// Returns an `Isometry` representing a block's translation.
Expand Down
1 change: 1 addition & 0 deletions server/src/physics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Module for calculating physics interactions.

mod block_bboxes;
mod component;
mod entity;
mod math;
Expand Down