wip physics

This commit is contained in:
griffi-gh 2023-01-18 18:19:11 +01:00
parent 4bd7371f84
commit cd20b4ae32
4 changed files with 63 additions and 2 deletions

View file

@ -15,6 +15,8 @@ mod world;
mod blocks;
mod items;
mod options;
mod physics;
mod player;
use assets::Assets;
use display::init_display;

38
src/game/physics.rs Normal file
View file

@ -0,0 +1,38 @@
use glam::{Vec3A, vec3a};
use crate::game::World;
const GRAVITY: Vec3A = vec3a(0., -1., 0.);
pub struct BasicPhysicsActor {
pub height: f32,
pub gravity: Vec3A,
pub position: Vec3A,
pub velocity: Vec3A,
}
impl BasicPhysicsActor {
pub fn new(height: f32) -> Self {
Self {
height,
gravity: GRAVITY,
position: vec3a(0., 0., 0.),
velocity: vec3a(0., 0., 0.),
}
}
pub fn update(&mut self, world: &World, dt: f32) {
self.velocity += GRAVITY;
self.position += self.velocity;
loop {
let block_pos = self.position.floor().as_ivec3();
if let Some(block) = world.try_get(block_pos) {
match block.descriptor().collision {
Some(super::blocks::CollisionType::Solid) => {
//todo compute restitution here
}
_ => break
}
} else {
break
}
}
}
}

7
src/game/player.rs Normal file
View file

@ -0,0 +1,7 @@
use crate::game::camera::Camera;
use crate::game::physics::BasicPhysicsActor;
pub struct MainPlayer {
pub camera: Camera,
pub actor: BasicPhysicsActor,
}

View file

@ -1,4 +1,4 @@
use glam::{Vec2, IVec2};
use glam::{Vec2, IVec2, IVec3, Vec3Swizzles};
use glium::{
Display, Frame, Surface,
DrawParameters, Depth,
@ -13,7 +13,8 @@ use hashbrown::HashMap;
use crate::game::{
options::GameOptions,
shaders::Programs,
assets::Assets
assets::Assets,
blocks::Block,
};
mod chunk;
@ -43,6 +44,19 @@ impl World {
]
}
pub fn try_get(&self, position: IVec3) -> Option<Block> {
let chunk_coord = IVec2::new(position.x, position.z) / CHUNK_SIZE as i32;
let chunk = self.chunks.get(&chunk_coord)?;
let block_data = chunk.block_data.as_ref()?;
let block_position = position - (chunk_coord * CHUNK_SIZE as i32).extend(0).xzy();
Some(
*block_data
.get(block_position.x as usize)?
.get(block_position.y as usize)?
.get(block_position.z as usize)?
)
}
pub fn new() -> Self {
Self {
chunks: HashMap::new(),