This commit is contained in:
Quaternions 2023-09-07 17:55:47 -07:00
parent 7a3e1e39dc
commit 6262553c02
3 changed files with 83 additions and 51 deletions

View File

@ -1,5 +1,59 @@
pub struct Body { pub struct Body {
position: glam::I64Vec3,//2^32 = 1 u pub position: glam::Vec3,//I64 where 2^32 = 1 u
velocity: glam::I64Vec3,//2^32 = 1 u/s pub velocity: glam::Vec3,//I64 where 2^32 = 1 u/s
time: i64,//nanoseconds x xxxxD! pub time: i64,//nanoseconds x xxxxD!
}
pub struct PhysicsState {
pub body: Body,
pub time: i64,
pub tick: u32,
pub gravity: glam::Vec3,
pub friction: f32,
pub mv: f32,
pub grounded: bool,
pub walkspeed: f32,
}
const CONTROL_JUMP:u32 = 0b01000000;//temp
impl PhysicsState {
pub fn run(&mut self, time: i64, control_dir: glam::Vec3, controls: u32){
let target_tick = (time/10_000_000) as u32;//100t
//the game code can run for 1 month before running out of ticks
for tick in self.tick+1..=target_tick {
let dt=0.01;
let d=self.body.velocity.dot(control_dir);
if d<self.mv {
self.body.velocity+=(self.mv-d)*control_dir;
}
self.body.velocity+=self.gravity*dt;
self.body.position+=self.body.velocity*dt;
if self.body.position.y<0.0{
self.body.position.y=0.0;
self.body.velocity.y=0.0;
self.grounded=true;
}
if self.grounded&&(controls&CONTROL_JUMP)!=0 {
self.grounded=false;
self.body.velocity+=glam::Vec3::new(0.0,0.715588/2.0*100.0,0.0);
}
if self.grounded {
let applied_friction=self.friction*dt;
let targetv=control_dir*self.walkspeed;
let diffv=targetv-self.body.velocity;
if applied_friction*applied_friction<diffv.length_squared() {
self.body.velocity+=applied_friction*diffv.normalize();
} else {
self.body.velocity=targetv;
}
}
}
self.tick=target_tick;
self.body.time=target_tick as i64*10_000_000;
}
pub fn extrapolate_position(&self, time: i64) -> glam::Vec3 {
let dt=(time-self.body.time) as f64/1_000_000_000f64;
self.body.position+self.body.velocity*(dt as f32)+self.gravity*((0.5*dt*dt) as f32)
}
} }

View File

@ -1 +1,2 @@
pub mod framework; pub mod framework;
pub mod body;

View File

@ -34,20 +34,12 @@ struct Model {
// Note: we use the Y=up coordinate space in this example. // Note: we use the Y=up coordinate space in this example.
struct Camera { struct Camera {
time: Instant,
pos: glam::Vec3,
vel: glam::Vec3,
gravity: glam::Vec3,
friction: f32,
screen_size: (u32, u32), screen_size: (u32, u32),
offset: glam::Vec3, offset: glam::Vec3,
fov: f32, fov: f32,
yaw: f32, yaw: f32,
pitch: f32, pitch: f32,
controls: u32, controls: u32,
mv: f32,
grounded: bool,
walkspeed: f32,
} }
const CONTROL_MOVEFORWARD:u32 = 0b00000001; const CONTROL_MOVEFORWARD:u32 = 0b00000001;
@ -100,7 +92,7 @@ fn get_control_dir(controls: u32) -> glam::Vec3{
} }
impl Camera { impl Camera {
fn to_uniform_data(&self) -> [f32; 16 * 3 + 4] { fn to_uniform_data(&self, pos: glam::Vec3) -> [f32; 16 * 3 + 4] {
let aspect = self.screen_size.0 as f32 / self.screen_size.1 as f32; let aspect = self.screen_size.0 as f32 / self.screen_size.1 as f32;
let fov = if self.controls&CONTROL_ZOOM==0 { let fov = if self.controls&CONTROL_ZOOM==0 {
self.fov self.fov
@ -109,7 +101,7 @@ impl Camera {
}; };
let proj = perspective_rh(fov, aspect, 0.5, 1000.0); let proj = perspective_rh(fov, aspect, 0.5, 1000.0);
let proj_inv = proj.inverse(); let proj_inv = proj.inverse();
let view = glam::Mat4::from_translation(self.pos+self.offset) * glam::Mat4::from_euler(glam::EulerRot::YXZ, self.yaw, self.pitch, 0f32); let view = glam::Mat4::from_translation(pos+self.offset) * glam::Mat4::from_euler(glam::EulerRot::YXZ, self.yaw, self.pitch, 0f32);
let view_inv = view.inverse(); let view_inv = view.inverse();
let mut raw = [0f32; 16 * 3 + 4]; let mut raw = [0f32; 16 * 3 + 4];
@ -122,7 +114,9 @@ impl Camera {
} }
pub struct Skybox { pub struct Skybox {
start_time: std::time::Instant,
camera: Camera, camera: Camera,
physics: strafe_client::body::PhysicsState,
sky_pipeline: wgpu::RenderPipeline, sky_pipeline: wgpu::RenderPipeline,
entity_pipeline: wgpu::RenderPipeline, entity_pipeline: wgpu::RenderPipeline,
ground_pipeline: wgpu::RenderPipeline, ground_pipeline: wgpu::RenderPipeline,
@ -290,22 +284,29 @@ impl strafe_client::framework::Example for Skybox {
}); });
let camera = Camera { let camera = Camera {
time: Instant::now(),
pos: glam::Vec3::new(5.0,0.0,5.0),
vel: glam::Vec3::new(0.0,0.0,0.0),
gravity: glam::Vec3::new(0.0,-100.0,0.0),
friction: 90.0,
screen_size: (config.width, config.height), screen_size: (config.width, config.height),
offset: glam::Vec3::new(0.0,4.5,0.0), offset: glam::Vec3::new(0.0,4.5,0.0),
fov: 1.0, //fov_slope = tan(fov_y/2) fov: 1.0, //fov_slope = tan(fov_y/2)
pitch: 0.0, pitch: 0.0,
yaw: 0.0, yaw: 0.0,
mv: 2.7,
controls:0, controls:0,
};
let physics = strafe_client::body::PhysicsState {
body: strafe_client::body::Body {
position: glam::Vec3::new(5.0,0.0,5.0),
velocity: glam::Vec3::new(0.0,0.0,0.0),
time: 0,
},
time: 0,
tick: 0,
gravity: glam::Vec3::new(0.0,-100.0,0.0),
friction: 90.0,
mv: 2.7,
grounded: true, grounded: true,
walkspeed: 18.0, walkspeed: 18.0,
}; };
let camera_uniforms = camera.to_uniform_data();
let camera_uniforms = camera.to_uniform_data(physics.extrapolate_position(0));
let camera_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { let camera_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Camera"), label: Some("Camera"),
contents: bytemuck::cast_slice(&camera_uniforms), contents: bytemuck::cast_slice(&camera_uniforms),
@ -539,7 +540,9 @@ impl strafe_client::framework::Example for Skybox {
let depth_view = Self::create_depth_texture(config, device); let depth_view = Self::create_depth_texture(config, device);
Skybox { Skybox {
start_time: Instant::now(),
camera, camera,
physics,
sky_pipeline, sky_pipeline,
entity_pipeline, entity_pipeline,
ground_pipeline, ground_pipeline,
@ -625,44 +628,18 @@ impl strafe_client::framework::Example for Skybox {
queue: &wgpu::Queue, queue: &wgpu::Queue,
_spawner: &strafe_client::framework::Spawner, _spawner: &strafe_client::framework::Spawner,
) { ) {
let time = Instant::now();
//physique
let dt=(time-self.camera.time).as_secs_f32();
self.camera.time=time;
let camera_mat=glam::Mat3::from_euler(glam::EulerRot::YXZ,self.camera.yaw,0f32,0f32); let camera_mat=glam::Mat3::from_euler(glam::EulerRot::YXZ,self.camera.yaw,0f32,0f32);
let control_dir=camera_mat*get_control_dir(self.camera.controls&(CONTROL_MOVELEFT|CONTROL_MOVERIGHT|CONTROL_MOVEFORWARD|CONTROL_MOVEBACK)).normalize_or_zero(); let control_dir=camera_mat*get_control_dir(self.camera.controls&(CONTROL_MOVELEFT|CONTROL_MOVERIGHT|CONTROL_MOVEFORWARD|CONTROL_MOVEBACK)).normalize_or_zero();
let d=self.camera.vel.dot(control_dir);
if d<self.camera.mv { let time=self.start_time.elapsed().as_nanos() as i64;
self.camera.vel+=(self.camera.mv-d)*control_dir;
} self.physics.run(time,control_dir,self.camera.controls);
self.camera.vel+=self.camera.gravity*dt;
self.camera.pos+=self.camera.vel*dt;
if self.camera.pos.y<0.0{
self.camera.pos.y=0.0;
self.camera.vel.y=0.0;
self.camera.grounded=true;
}
if self.camera.grounded&&(self.camera.controls&CONTROL_JUMP)!=0 {
self.camera.grounded=false;
self.camera.vel+=glam::Vec3::new(0.0,0.715588/2.0*100.0,0.0);
}
if self.camera.grounded {
let applied_friction=self.camera.friction*dt;
let targetv=control_dir*self.camera.walkspeed;
let diffv=targetv-self.camera.vel;
if applied_friction*applied_friction<diffv.length_squared() {
self.camera.vel+=applied_friction*diffv.normalize();
} else {
self.camera.vel=targetv;
}
}
let mut encoder = let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
// update rotation // update rotation
let camera_uniforms = self.camera.to_uniform_data(); let camera_uniforms = self.camera.to_uniform_data(self.physics.extrapolate_position(time));
self.staging_belt self.staging_belt
.write_buffer( .write_buffer(
&mut encoder, &mut encoder,