mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-12-26 21:58:20 -06:00
refactor stuff
This commit is contained in:
parent
71d089c784
commit
36cec2b1e5
|
@ -8,8 +8,21 @@ use crate::{delta_time::DeltaTime, world::ChunkStorage};
|
||||||
#[derive(Unique)]
|
#[derive(Unique)]
|
||||||
pub struct GlobalClPhysicsConfig {
|
pub struct GlobalClPhysicsConfig {
|
||||||
pub gravity: Vec3,
|
pub gravity: Vec3,
|
||||||
|
///XXX: currenly unused:
|
||||||
|
pub iterations: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for GlobalClPhysicsConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
gravity: Vec3::new(0., -9.8, 0.),
|
||||||
|
iterations: 10,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: actors should be represented by a vertical line, not a point.
|
||||||
|
//XXX: maybe a capsule? (or configurable hull?)
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct ClPhysicsActor {
|
pub struct ClPhysicsActor {
|
||||||
pub offset: Vec3,
|
pub offset: Vec3,
|
||||||
|
@ -18,7 +31,8 @@ pub struct ClPhysicsActor {
|
||||||
pub terminal_velocity: f32,
|
pub terminal_velocity: f32,
|
||||||
//TODO: this should be configurable per block
|
//TODO: this should be configurable per block
|
||||||
pub friction_agains_ground: f32,
|
pub friction_agains_ground: f32,
|
||||||
on_ground_flag: bool,
|
flag_ground: bool,
|
||||||
|
flag_collision: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClPhysicsActor {
|
impl ClPhysicsActor {
|
||||||
|
@ -27,7 +41,7 @@ impl ClPhysicsActor {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn on_ground(&self) -> bool {
|
pub fn on_ground(&self) -> bool {
|
||||||
self.on_ground_flag
|
self.flag_ground
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,73 +54,91 @@ impl Default for ClPhysicsActor {
|
||||||
velocity: Vec3::ZERO,
|
velocity: Vec3::ZERO,
|
||||||
terminal_velocity: 40.,
|
terminal_velocity: 40.,
|
||||||
friction_agains_ground: 0.5,
|
friction_agains_ground: 0.5,
|
||||||
on_ground_flag: false,
|
flag_ground: false,
|
||||||
|
flag_collision: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trait BlockCollisionExt {
|
||||||
|
fn collision_type(&self) -> CollisionType;
|
||||||
|
fn is_solid(&self) -> bool {
|
||||||
|
self.collision_type() == CollisionType::Solid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BlockCollisionExt for Option<Block> {
|
||||||
|
fn collision_type(&self) -> CollisionType {
|
||||||
|
self.unwrap_or(Block::Air).descriptor().collision
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BlockCollisionExt for Block {
|
||||||
|
fn collision_type(&self) -> CollisionType {
|
||||||
|
self.descriptor().collision
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn init_client_physics(
|
pub fn init_client_physics(
|
||||||
storages: AllStoragesView,
|
storages: AllStoragesView,
|
||||||
) {
|
) {
|
||||||
storages.add_unique(GlobalClPhysicsConfig {
|
storages.add_unique(GlobalClPhysicsConfig::default());
|
||||||
gravity: Vec3::new(0., -1.0, 0.),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_client_physics_late(
|
pub fn update_client_physics_late(
|
||||||
mut actors: ViewMut<ClPhysicsActor>,
|
mut actors: ViewMut<ClPhysicsActor>,
|
||||||
mut transforms: ViewMut<Transform, track::All>,
|
mut transforms: ViewMut<Transform, track::All>,
|
||||||
phy_conf: UniqueView<GlobalClPhysicsConfig>,
|
conf: UniqueView<GlobalClPhysicsConfig>,
|
||||||
world: UniqueView<ChunkStorage>,
|
world: UniqueView<ChunkStorage>,
|
||||||
dt: UniqueView<DeltaTime>,
|
dt: UniqueView<DeltaTime>,
|
||||||
) {
|
) {
|
||||||
for (mut actor, mut transform) in (&mut actors, &mut transforms).iter() {
|
for (mut actor, mut transform) in (&mut actors, &mut transforms).iter() {
|
||||||
//apply forces
|
//apply forces
|
||||||
let actor_forces = actor.forces;
|
let actor_forces = actor.forces;
|
||||||
actor.velocity += actor_forces + phy_conf.gravity;
|
actor.velocity += (actor_forces + conf.gravity) * dt.0.as_secs_f32();
|
||||||
actor.forces = Vec3::ZERO;
|
actor.forces = Vec3::ZERO;
|
||||||
|
|
||||||
|
//get position
|
||||||
let (scale, rotation, mut actor_position) = transform.0.to_scale_rotation_translation();
|
let (scale, rotation, mut actor_position) = transform.0.to_scale_rotation_translation();
|
||||||
actor_position -= actor.offset;
|
actor_position -= actor.offset;
|
||||||
|
|
||||||
|
//get grid-aligned pos and blocks
|
||||||
let actor_block_pos = actor_position.floor().as_ivec3();
|
let actor_block_pos = actor_position.floor().as_ivec3();
|
||||||
let actor_block = world.get_block(actor_block_pos);
|
let actor_block = world.get_block(actor_block_pos);
|
||||||
let actor_block_below = world.get_block(actor_block_pos + IVec3::NEG_Y);
|
let actor_block_below = world.get_block(actor_block_pos + IVec3::NEG_Y);
|
||||||
actor.on_ground_flag =
|
|
||||||
actor_block_below.map_or_else(|| false, |x| x.descriptor().collision == CollisionType::Solid) ||
|
//update flags
|
||||||
actor_block.map_or_else(|| false, |x| x.descriptor().collision == CollisionType::Solid);
|
actor.flag_collision = actor_block.is_solid();
|
||||||
|
actor.flag_ground = actor.flag_collision || actor_block_below.is_solid();
|
||||||
|
|
||||||
//push actor back out of the block
|
//push actor back out of the block
|
||||||
if actor_block.is_some() {
|
if actor.flag_collision {
|
||||||
//first, compute the normal (assuming actor is a point)
|
//first, compute restitution, based on position inside the block
|
||||||
//must be accurate!
|
// let block_center = actor_block_pos.as_f32() + Vec3::ONE * 0.5;
|
||||||
let mut normal = Vec3::ZERO;
|
// let to_block_center = actor_position - block_center;
|
||||||
for i in 0..3 {
|
|
||||||
let mut offset = Vec3::ZERO;
|
|
||||||
offset[i] = 0.5;
|
|
||||||
let block_pos = actor_block_pos + offset.as_ivec3();
|
|
||||||
let block = world.get_block(block_pos).unwrap_or(Block::Air);
|
|
||||||
if block.descriptor().collision == CollisionType::Solid {
|
|
||||||
normal[i] = 1.;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//then, based on normal:
|
//then, based on normal:
|
||||||
//push the actor back
|
//push the actor back
|
||||||
actor_position += normal * 0.5;
|
//actor_position += normal * 0.5;
|
||||||
//cancel out velocity in the direction of the normal
|
//cancel out velocity in the direction of the normal
|
||||||
// let dot = actor.velocity.dot(normal);
|
// let dot = actor.velocity.dot(normal);
|
||||||
// if dot > 0. {
|
// if dot > 0. {
|
||||||
// //actor.velocity -= normal * dot;
|
// //actor.velocity -= normal * dot;
|
||||||
// actor.velocity = Vec3::ZERO;
|
// actor.velocity = Vec3::ZERO;
|
||||||
// }
|
// }
|
||||||
if actor.on_ground_flag {
|
|
||||||
|
//HACK: for now, just stop the vertical velocity if on ground altogether,
|
||||||
|
//as we don't have proper collision velocity resolution yet (we need to compute dot product or sth)
|
||||||
|
if actor.flag_ground {
|
||||||
actor.velocity.y = 0.;
|
actor.velocity.y = 0.;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Apply velocity
|
//Apply velocity
|
||||||
actor_position += actor.velocity * dt.0.as_secs_f32();
|
actor_position += actor.velocity * dt.0.as_secs_f32();
|
||||||
actor_position += actor.offset;
|
actor_position += actor.offset;
|
||||||
transform.0 = Mat4::from_scale_rotation_translation(scale, rotation, actor_position);
|
transform.0 = Mat4::from_scale_rotation_translation(scale, rotation, actor_position);
|
||||||
}
|
}
|
||||||
|
|
||||||
// for (_, mut transform) in (&controllers, &mut transforms).iter() {
|
// for (_, mut transform) in (&controllers, &mut transforms).iter() {
|
||||||
// let (scale, rotation, mut translation) = transform.0.to_scale_rotation_translation();
|
// let (scale, rotation, mut translation) = transform.0.to_scale_rotation_translation();
|
||||||
// translation.y -= dt.0.as_secs_f32() * 100.;
|
// translation.y -= dt.0.as_secs_f32() * 100.;
|
||||||
|
|
Loading…
Reference in a new issue