Compare commits

..

No commits in common. "free-body" and "master" have entirely different histories.

4 changed files with 48 additions and 163 deletions

View File

@ -1,120 +0,0 @@
pub struct Body {
pub position: glam::Vec3,//I64 where 2^32 = 1 u
pub velocity: glam::Vec3,//I64 where 2^32 = 1 u/s
pub time: TIME,//nanoseconds x xxxxD!
}
pub struct PhysicsState {
pub body: Body,
//pub contacts: Vec<RelativeCollision>,
pub time: TIME,
pub strafe_tick_rate: TIME,
pub tick: u32,
pub mv: f32,
pub walkspeed: f32,
pub friction: f32,
pub gravity: glam::Vec3,
pub grounded: bool,
pub jump_trying: bool,
}
pub type TIME = i64;
const CONTROL_JUMP:u32 = 0b01000000;//temp
impl PhysicsState {
//delete this, we are tickless gamers
pub fn run(&mut self, time: TIME, 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
while self.tick<target_tick {
self.tick += 1;
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.body.time=target_tick as TIME*10_000_000;
}
//delete this
pub fn extrapolate_position(&self, time: TIME) -> 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)
}
fn next_strafe_event(&self) -> Option<crate::event::EventStruct> {
return Some(crate::event::EventStruct{
time:self.time/self.strafe_tick_rate*self.strafe_tick_rate,//this is floor(n) I need ceil(n)+1
event:crate::event::EventEnum::StrafeTick
});
}
}
impl crate::event::EventTrait for PhysicsState {
//this little next event function can cache its return value and invalidate the cached value by watching the State.
fn next_event(&self) -> Option<crate::event::EventStruct> {
//JUST POLLING!!! NO MUTATION
let mut best_event: Option<crate::event::EventStruct> = None;
let collect_event = |test_event:Option<crate::event::EventStruct>|{
match test_event {
Some(unwrap_test_event) => match best_event {
Some(unwrap_best_event) => if unwrap_test_event.time<unwrap_best_event.time {
best_event=test_event;
},
None => best_event=test_event,
},
None => (),
}
};
//check to see if yee need to jump (this is not the way lol)
if self.grounded&&self.jump_trying {
//scroll will be implemented with InputEvent::InstantJump rather than InputEvent::Jump(true)
collect_event(Some(crate::event::EventStruct{
time:self.time,
event:crate::event::EventEnum::Jump
}));
}
//check for collision stop events with curent contacts
for collision_data in self.contacts.iter() {
collect_event(self.model.predict_collision(collision_data.model));
}
//check for collision start events (against every part in the game with no optimization!!)
for &model in self.world.models {
collect_event(self.model.predict_collision(&model));
}
//check to see when the next strafe tick is
collect_event(self.next_strafe_event());
best_event
}
}
//something that implements body + hitbox can predict collision
impl crate::sweep::PredictCollision for Model {
fn predict_collision(&self,other:&Model) -> Option<crate::event::EventStruct> {
//math!
None
}
}

View File

@ -1,15 +0,0 @@
pub struct EventStruct {
pub time: crate::body::TIME,
pub event: EventEnum,
}
pub enum EventEnum {
CollisionStart,//(Collideable),//Body::CollisionStart
CollisionEnd,//(Collideable),//Body::CollisionEnd
StrafeTick,
Jump,
}
pub trait EventTrait {
fn next_event(&self) -> Option<EventStruct>;
}

View File

@ -1,3 +1 @@
pub mod framework; pub mod framework;
pub mod body;
pub mod event;

View File

@ -34,12 +34,20 @@ 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;
@ -92,7 +100,7 @@ fn get_control_dir(controls: u32) -> glam::Vec3{
} }
impl Camera { impl Camera {
fn to_uniform_data(&self, pos: glam::Vec3) -> [f32; 16 * 3 + 4] { fn to_uniform_data(&self) -> [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
@ -101,7 +109,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(pos+self.offset) * glam::Mat4::from_euler(glam::EulerRot::YXZ, self.yaw, self.pitch, 0f32); let view = glam::Mat4::from_translation(self.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];
@ -114,9 +122,7 @@ 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,
@ -284,30 +290,22 @@ 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,
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,
tick_rate: 100,
gravity: glam::Vec3::new(0.0,-100.0,0.0),
friction: 90.0,
mv: 2.7, mv: 2.7,
controls:0,
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),
@ -541,9 +539,7 @@ 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,
@ -629,18 +625,44 @@ 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);
let time=self.start_time.elapsed().as_nanos() as i64; if d<self.camera.mv {
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(self.physics.extrapolate_position(time)); let camera_uniforms = self.camera.to_uniform_data();
self.staging_belt self.staging_belt
.write_buffer( .write_buffer(
&mut encoder, &mut encoder,