Compare commits

...

66 Commits

Author SHA1 Message Date
5fb63375d3 do not step physics on mouse input, only update pos (overwriting previous pos) 2023-09-19 22:04:35 -07:00
765da37cfa fix angles 2023-09-19 21:57:20 -07:00
b3f3c26cfa accumulate deltas 2023-09-19 21:57:16 -07:00
cfdfb29432 implement jump() + remove jump_trying + prevent air jumping 2023-09-19 21:55:11 -07:00
319d864045 wip 2 2023-09-19 20:36:50 -07:00
b98618ef10 wip 2023-09-19 18:14:37 -07:00
5fc4044284 small physics opti 2023-09-19 18:14:24 -07:00
6f4fda8cc0 clip walk target velocity in RefreshWalkTarget 2023-09-19 17:54:43 -07:00
d58f915082 v0.3.0 physics + walking 2023-09-19 01:21:00 -07:00
8e95fe484a unnecessary cast 2023-09-19 00:58:35 -07:00
5854128164 edit comments 2023-09-19 00:00:14 -07:00
db5b3328bd update time for extern instructions 2023-09-19 00:00:06 -07:00
4951d1513d stop skipping over time 2023-09-18 23:57:42 -07:00
17e71d884f SetControlDir + contact constrain + walk.state enum instead of hash 2023-09-18 23:36:14 -07:00
1dc98d9c2d stupid mistake 2023-09-18 23:33:52 -07:00
fd38502e07 only walk if grounded 2023-09-18 21:28:09 -07:00
0632e322cf implement walk with hashing lole!! 2023-09-18 21:10:07 -07:00
7544c6e6ef fix roblox camera offset 2023-09-18 19:47:15 -07:00
2f0a073fd5 fix aabb normals + face_mesh 2023-09-18 19:04:32 -07:00
21dc425fc2 edit comments 2023-09-18 19:04:32 -07:00
ca141c800c debug print instructions 2023-09-18 19:04:32 -07:00
63ce06f069 syntactic sugar 2023-09-18 18:31:15 -07:00
fe14d5e0fa worthless opti 2023-09-18 18:31:04 -07:00
bffc254a0d put -t back 2023-09-18 18:27:02 -07:00
a5f203484b comment on CollisionEnd handling 2023-09-18 18:08:03 -07:00
e67479a9bd TEMP: move jump out 2023-09-18 18:08:03 -07:00
ad7abbdf1c rework predict_collision_end 2023-09-18 18:08:03 -07:00
3a0b3900ec fix falling 2023-09-18 16:04:55 -07:00
765ed42b9d start in air 2023-09-18 16:04:55 -07:00
8137a26f81 standardize vec init 2023-09-18 16:04:55 -07:00
21ae7a0e4f use pva for init 2023-09-18 16:04:55 -07:00
5a886b76d1 comment about processed_time 2023-09-18 16:04:55 -07:00
f1e26cb07a don't print frame delta 2023-09-18 16:04:55 -07:00
14a74c6e1e consume vec 2023-09-18 16:04:55 -07:00
5b55873bd5 halfsize 2023-09-18 16:04:55 -07:00
fd5d71e1af use hashset for contacts 2023-09-18 16:04:55 -07:00
28c3f21736 implement aabb collision 2023-09-18 16:04:55 -07:00
a58464efb0 how will I do this 2023-09-18 15:49:09 -07:00
b070b9706f InputState 2023-09-18 15:49:09 -07:00
dcfbee8de1 MouseInterpolationState 2023-09-18 15:49:09 -07:00
c5636f7fcd delete unused 2023-09-18 15:26:21 -07:00
a5aa89064b next_instruction non-optional time_limit 2023-09-18 15:26:21 -07:00
0fe14749d3 wip: tickless physics 2023-09-18 15:26:21 -07:00
8daf432991 zeroes 2023-09-18 15:26:21 -07:00
4f5c9afed3 comments on implementing game mechanics 2023-09-08 20:18:31 -07:00
53605746d4 rename best to collector 2023-09-08 20:18:31 -07:00
cead05b08b rename event.rs to instruction.rs 2023-09-08 20:18:31 -07:00
949897a558 rename EventStruct to TimedInstruction 2023-09-08 20:18:31 -07:00
a4ed50fc38 EventConsumer 2023-09-08 20:15:36 -07:00
846f681648 generic events 2023-09-08 20:15:36 -07:00
ff54a03487 happy compiler happy programmer 2023-09-08 20:15:36 -07:00
8c2dda5205 bad data normalization 2023-09-08 16:06:17 -07:00
addde65caa fix up next_event lsp errors 2023-09-08 16:06:17 -07:00
2c4e6f642b boilerplate 2023-09-08 16:06:17 -07:00
f11742ef3b RelativeCollision concept 2023-09-08 16:06:17 -07:00
e6862b5bad rename Model to ModelGRaphics + move model graphics gen code 2023-09-08 16:06:17 -07:00
e18e8a9a7d walk event 2023-09-08 16:06:17 -07:00
f600092f13 LETS GET RUSTY 2023-09-08 16:06:17 -07:00
1f1ef5d3ad there is no model 2023-09-08 12:28:16 -07:00
54bf0358d6 can't stand the idea of 10 nanoseconds less than 66 ticks per second 2023-09-08 12:27:45 -07:00
6caa273623 jump implementation details 2023-09-08 12:04:30 -07:00
101f0f8d12 localize EventStruct 2023-09-08 12:03:32 -07:00
fc751a9fe8 stage sweep 2023-09-08 11:38:47 -07:00
5391b635fb reorder elements 2023-09-08 11:38:47 -07:00
230469496e brainstorm tickless events 2023-09-08 11:38:47 -07:00
ea3134de51 move physics into its own module 2023-09-08 11:35:40 -07:00
9 changed files with 1250 additions and 251 deletions

2
Cargo.lock generated

@ -1405,7 +1405,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "strafe-client"
version = "0.2.0"
version = "0.3.0"
dependencies = [
"async-executor",
"bytemuck",

@ -1,6 +1,6 @@
[package]
name = "strafe-client"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

1021
src/body.rs Normal file

File diff suppressed because it is too large Load Diff

@ -1,12 +1,10 @@
use std::future::Future;
#[cfg(target_arch = "wasm32")]
use std::str::FromStr;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
use web_sys::{ImageBitmapRenderingContext, OffscreenCanvas};
use winit::{
event::{self, WindowEvent},
event::{self, WindowEvent, DeviceEvent},
event_loop::{ControlFlow, EventLoop},
};
@ -54,7 +52,7 @@ pub trait Example: 'static + Sized {
queue: &wgpu::Queue,
);
fn update(&mut self, event: WindowEvent);
fn move_mouse(&mut self, delta: (f64,f64));
fn device_event(&mut self, event: DeviceEvent);
fn render(
&mut self,
view: &wgpu::TextureView,
@ -279,11 +277,6 @@ fn start<E: Example>(
log::info!("Initializing the example...");
let mut example = E::init(&config, &adapter, &device, &queue);
#[cfg(not(target_arch = "wasm32"))]
let mut last_frame_inst = Instant::now();
#[cfg(not(target_arch = "wasm32"))]
let (mut frame_count, mut accum_time) = (0, 0.0);
log::info!("Entering render loop...");
event_loop.run(move |event, _, control_flow| {
let _ = (&instance, &adapter); // force ownership by the closure
@ -355,29 +348,12 @@ fn start<E: Example>(
}
},
event::Event::DeviceEvent {
event:
winit::event::DeviceEvent::MouseMotion {
delta,
},
event,
..
} => {
example.move_mouse(delta);
example.device_event(event);
},
event::Event::RedrawRequested(_) => {
#[cfg(not(target_arch = "wasm32"))]
{
accum_time += last_frame_inst.elapsed().as_secs_f32();
last_frame_inst = Instant::now();
frame_count += 1;
if frame_count == 100 {
println!(
"Avg frame time {}ms",
accum_time * 1000.0 / frame_count as f32
);
accum_time = 0.0;
frame_count = 0;
}
}
let frame = match surface.get_current_texture() {
Ok(frame) => frame,

48
src/instruction.rs Normal file

@ -0,0 +1,48 @@
#[derive(Debug)]
pub struct TimedInstruction<I> {
pub time: crate::body::TIME,
pub instruction: I,
}
pub trait InstructionEmitter<I> {
fn next_instruction(&self, time_limit:crate::body::TIME) -> Option<TimedInstruction<I>>;
}
pub trait InstructionConsumer<I> {
fn process_instruction(&mut self, instruction:TimedInstruction<I>);
}
//PROPER PRIVATE FIELDS!!!
pub struct InstructionCollector<I> {
time: crate::body::TIME,
instruction: Option<I>,
}
impl<I> InstructionCollector<I> {
pub fn new(time:crate::body::TIME) -> Self {
Self{
time,
instruction:None
}
}
pub fn collect(&mut self,instruction:Option<TimedInstruction<I>>){
match instruction {
Some(unwrap_instruction) => {
if unwrap_instruction.time<self.time {
self.time=unwrap_instruction.time;
self.instruction=Some(unwrap_instruction.instruction);
}
},
None => (),
}
}
pub fn instruction(self) -> Option<TimedInstruction<I>> {
//STEAL INSTRUCTION AND DESTROY INSTRUCTIONCOLLECTOR
match self.instruction {
Some(instruction)=>Some(TimedInstruction{
time:self.time,
instruction
}),
None => None,
}
}
}

@ -1 +1,4 @@
pub mod framework;
pub mod body;
pub mod zeroes;
pub mod instruction;

@ -1,4 +1,5 @@
use bytemuck::{Pod, Zeroable};
use strafe_client::{instruction::{TimedInstruction, InstructionConsumer},body::{InputInstruction, PhysicsInstruction}};
use std::{borrow::Cow, time::Instant};
use wgpu::{util::DeviceExt, AstcBlock, AstcChannel};
@ -24,7 +25,7 @@ struct ModelData {
entities: Vec<Entity>,
}
struct Model {
struct ModelGraphics {
transform: glam::Mat4,
vertex_buf: wgpu::Buffer,
entities: Vec<Entity>,
@ -32,103 +33,16 @@ struct Model {
model_buf: wgpu::Buffer,
}
// Note: we use the Y=up coordinate space in this example.
struct Camera {
time: Instant,
pos: glam::Vec3,
vel: glam::Vec3,
gravity: glam::Vec3,
friction: f32,
screen_size: (u32, u32),
offset: glam::Vec3,
fov: f32,
yaw: f32,
pitch: f32,
controls: u32,
mv: f32,
grounded: bool,
walkspeed: f32,
}
const CONTROL_MOVEFORWARD:u32 = 0b00000001;
const CONTROL_MOVEBACK:u32 = 0b00000010;
const CONTROL_MOVERIGHT:u32 = 0b00000100;
const CONTROL_MOVELEFT:u32 = 0b00001000;
const CONTROL_MOVEUP:u32 = 0b00010000;
const CONTROL_MOVEDOWN:u32 = 0b00100000;
const CONTROL_JUMP:u32 = 0b01000000;
const CONTROL_ZOOM:u32 = 0b10000000;
const FORWARD_DIR:glam::Vec3 = glam::Vec3::new(0.0,0.0,-1.0);
const RIGHT_DIR:glam::Vec3 = glam::Vec3::new(1.0,0.0,0.0);
const UP_DIR:glam::Vec3 = glam::Vec3::new(0.0,1.0,0.0);
fn get_control_dir(controls: u32) -> glam::Vec3{
//don't get fancy just do it
let mut control_dir:glam::Vec3 = glam::Vec3::new(0.0,0.0,0.0);
if controls & CONTROL_MOVEFORWARD == CONTROL_MOVEFORWARD {
control_dir+=FORWARD_DIR;
}
if controls & CONTROL_MOVEBACK == CONTROL_MOVEBACK {
control_dir+=-FORWARD_DIR;
}
if controls & CONTROL_MOVELEFT == CONTROL_MOVELEFT {
control_dir+=-RIGHT_DIR;
}
if controls & CONTROL_MOVERIGHT == CONTROL_MOVERIGHT {
control_dir+=RIGHT_DIR;
}
if controls & CONTROL_MOVEUP == CONTROL_MOVEUP {
control_dir+=UP_DIR;
}
if controls & CONTROL_MOVEDOWN == CONTROL_MOVEDOWN {
control_dir+=-UP_DIR;
}
return control_dir
}
#[inline]
fn perspective_rh(fov_y_slope: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> glam::Mat4 {
//glam_assert!(z_near > 0.0 && z_far > 0.0);
let r = z_far / (z_near - z_far);
glam::Mat4::from_cols(
glam::Vec4::new(1.0/(fov_y_slope * aspect_ratio), 0.0, 0.0, 0.0),
glam::Vec4::new(0.0, 1.0/fov_y_slope, 0.0, 0.0),
glam::Vec4::new(0.0, 0.0, r, -1.0),
glam::Vec4::new(0.0, 0.0, r * z_near, 0.0),
)
}
impl Camera {
fn to_uniform_data(&self) -> [f32; 16 * 3 + 4] {
let aspect = self.screen_size.0 as f32 / self.screen_size.1 as f32;
let fov = if self.controls&CONTROL_ZOOM==0 {
self.fov
}else{
self.fov/5.0
};
let proj = perspective_rh(fov, aspect, 0.5, 1000.0);
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_inv = view.inverse();
let mut raw = [0f32; 16 * 3 + 4];
raw[..16].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj)[..]);
raw[16..32].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj_inv)[..]);
raw[32..48].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view_inv)[..]);
raw[48..52].copy_from_slice(AsRef::<[f32; 4]>::as_ref(&view.col(3)));
raw
}
}
pub struct Skybox {
camera: Camera,
start_time: std::time::Instant,
screen_size: (u32, u32),
physics: strafe_client::body::PhysicsState,
sky_pipeline: wgpu::RenderPipeline,
entity_pipeline: wgpu::RenderPipeline,
ground_pipeline: wgpu::RenderPipeline,
main_bind_group: wgpu::BindGroup,
camera_buf: wgpu::Buffer,
models: Vec<Model>,
models: Vec<ModelGraphics>,
depth_view: wgpu::TextureView,
staging_belt: wgpu::util::StagingBelt,
}
@ -178,7 +92,7 @@ fn add_obj(device:&wgpu::Device,modeldatas:& mut Vec<ModelData>,source:&[u8]){
for &index in &[0, end_index - 1, end_index] {
let vert = poly.0[index];
if let Some(&i)=vertex_index.get(&vert){
indices.push(i as u16);
indices.push(i);
}else{
let i=vertices.len() as u16;
vertices.push(Vertex {
@ -215,6 +129,21 @@ fn add_obj(device:&wgpu::Device,modeldatas:& mut Vec<ModelData>,source:&[u8]){
}
}
fn to_uniform_data(camera: &strafe_client::body::Camera, pos: glam::Vec3) -> [f32; 16 * 3 + 4] {
let proj=camera.proj();
let proj_inv = proj.inverse();
let view=camera.view(pos);
let view_inv = view.inverse();
let mut raw = [0f32; 16 * 3 + 4];
raw[..16].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj)[..]);
raw[16..32].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj_inv)[..]);
raw[32..48].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view_inv)[..]);
raw[48..52].copy_from_slice(AsRef::<[f32; 4]>::as_ref(&view.col(3)));
raw
}
impl strafe_client::framework::Example for Skybox {
fn optional_features() -> wgpu::Features {
wgpu::Features::TEXTURE_COMPRESSION_ASTC
@ -289,29 +218,63 @@ impl strafe_client::framework::Example for Skybox {
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
});
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),
offset: glam::Vec3::new(0.0,4.5,0.0),
fov: 1.0, //fov_slope = tan(fov_y/2)
pitch: 0.0,
yaw: 0.0,
let physics = strafe_client::body::PhysicsState {
body: strafe_client::body::Body::with_pva(glam::vec3(0.0,50.0,0.0),glam::vec3(0.0,0.0,0.0),glam::vec3(0.0,-100.0,0.0)),
time: 0,
tick: 0,
strafe_tick_num: 100,//100t
strafe_tick_den: 1_000_000_000,
gravity: glam::vec3(0.0,-100.0,0.0),
friction: 1.2,
walk_accel: 90.0,
mv: 2.7,
controls:0,
grounded: true,
grounded: false,
walkspeed: 18.0,
contacts: std::collections::HashSet::new(),
models_cringe_clone: modeldatas.iter().map(|m|strafe_client::body::Model::new(m.transform)).collect(),
walk: strafe_client::body::WalkState::new(),
hitbox_halfsize: glam::vec3(1.0,2.5,1.0),
camera: strafe_client::body::Camera::from_offset(glam::vec3(0.0,4.5-2.5,0.0),(config.width as f32)/(config.height as f32)),
mouse_interpolation: strafe_client::body::MouseInterpolationState::new(),
controls: 0,
};
let camera_uniforms = camera.to_uniform_data();
let camera_uniforms = to_uniform_data(&physics.camera,physics.body.extrapolated_position(0));
let camera_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Camera"),
contents: bytemuck::cast_slice(&camera_uniforms),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
//drain the modeldata vec so entities can be /moved/ to models.entities
let mut models = Vec::<ModelGraphics>::with_capacity(modeldatas.len());
for (i,modeldata) in modeldatas.drain(..).enumerate() {
let model_uniforms = get_transform_uniform_data(&modeldata.transform);
let model_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some(format!("ModelGraphics{}",i).as_str()),
contents: bytemuck::cast_slice(&model_uniforms),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let model_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &model_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: model_buf.as_entire_binding(),
},
],
label: Some(format!("ModelGraphics{}",i).as_str()),
});
//all of these are being moved here
models.push(ModelGraphics{
transform: modeldata.transform,
vertex_buf:modeldata.vertex_buf,
entities: modeldata.entities,
bind_group: model_bind_group,
model_buf,
})
}
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[&main_bind_group_layout, &model_bind_group_layout],
@ -507,39 +470,12 @@ impl strafe_client::framework::Example for Skybox {
label: Some("Camera"),
});
//drain the modeldata vec so entities can be /moved/ to models.entities
let mut models = Vec::<Model>::with_capacity(modeldatas.len());
for (i,modeldata) in modeldatas.drain(..).enumerate() {
let model_uniforms = get_transform_uniform_data(&modeldata.transform);
let model_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some(format!("Model{}",i).as_str()),
contents: bytemuck::cast_slice(&model_uniforms),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let model_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &model_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: model_buf.as_entire_binding(),
},
],
label: Some(format!("Model{}",i).as_str()),
});
//all of these are being moved here
models.push(Model{
transform: modeldata.transform,
vertex_buf:modeldata.vertex_buf,
entities: modeldata.entities,
bind_group: model_bind_group,
model_buf,
})
}
let depth_view = Self::create_depth_texture(config, device);
Skybox {
camera,
start_time: Instant::now(),
screen_size: (config.width,config.height),
physics,
sky_pipeline,
entity_pipeline,
ground_pipeline,
@ -553,59 +489,67 @@ impl strafe_client::framework::Example for Skybox {
#[allow(clippy::single_match)]
fn update(&mut self, event: winit::event::WindowEvent) {
match event {
winit::event::WindowEvent::KeyboardInput {
input:
winit::event::KeyboardInput {
state,
virtual_keycode: Some(keycode),
..
},
..
} => {
match (state,keycode) {
(k,winit::event::VirtualKeyCode::W) => match k {
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_MOVEFORWARD,
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_MOVEFORWARD,
}
(k,winit::event::VirtualKeyCode::A) => match k {
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_MOVELEFT,
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_MOVELEFT,
}
(k,winit::event::VirtualKeyCode::S) => match k {
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_MOVEBACK,
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_MOVEBACK,
}
(k,winit::event::VirtualKeyCode::D) => match k {
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_MOVERIGHT,
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_MOVERIGHT,
}
(k,winit::event::VirtualKeyCode::E) => match k {
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_MOVEUP,
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_MOVEUP,
}
(k,winit::event::VirtualKeyCode::Q) => match k {
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_MOVEDOWN,
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_MOVEDOWN,
}
(k,winit::event::VirtualKeyCode::Space) => match k {
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_JUMP,
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_JUMP,
}
(k,winit::event::VirtualKeyCode::Z) => match k {
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_ZOOM,
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_ZOOM,
}
_ => (),
}
}
_ => {}
}
//nothing atm
}
fn move_mouse(&mut self, delta: (f64,f64)) {
self.camera.pitch=(self.camera.pitch as f64+delta.1/-2048.) as f32;
self.camera.yaw=(self.camera.yaw as f64+delta.0/-2048.) as f32;
fn device_event(&mut self, event: winit::event::DeviceEvent) {
//there's no way this is the best way get a timestamp.
let time=self.start_time.elapsed().as_nanos() as i64;
match event {
winit::event::DeviceEvent::Key(winit::event::KeyboardInput {
state,
scancode: keycode,
..
}) => {
let s=match state {
winit::event::ElementState::Pressed => true,
winit::event::ElementState::Released => false,
};
if let Some(input_instruction)=match keycode {
17 => Some(InputInstruction::MoveForward(s)),//W
30 => Some(InputInstruction::MoveLeft(s)),//A
31 => Some(InputInstruction::MoveBack(s)),//S
32 => Some(InputInstruction::MoveRight(s)),//D
18 => Some(InputInstruction::MoveUp(s)),//E
16 => Some(InputInstruction::MoveDown(s)),//Q
57 => Some(InputInstruction::Jump(s)),//Space
44 => Some(InputInstruction::Zoom(s)),//Z
19 => if s{Some(InputInstruction::Reset)}else{None},//R
_ => None,
}
{
self.physics.run(time);
self.physics.process_instruction(TimedInstruction{
time,
instruction:PhysicsInstruction::Input(input_instruction),
})
}
},
winit::event::DeviceEvent::MouseMotion {
delta,//these (f64,f64) are integers on my machine
} => {
//do not step the physics because the mouse polling rate is higher than the physics can run.
//essentially the previous input will be overwritten until a true step runs
//which is fine because they run all the time.
self.physics.process_instruction(TimedInstruction{
time,
instruction:PhysicsInstruction::Input(InputInstruction::MoveMouse(glam::ivec2(delta.0 as i32,delta.1 as i32))),
})
},
winit::event::DeviceEvent::MouseWheel {
delta,
} => {
println!("mousewheel{:?}",delta);
if true{//self.physics.use_scroll
self.physics.run(time);
self.physics.process_instruction(TimedInstruction{
time,
instruction:PhysicsInstruction::Input(InputInstruction::Jump(true)),//activates the immediate jump path, but the style modifier prevents controls&CONTROL_JUMP bit from being set to auto jump
})
}
}
_=>(),
}
}
fn resize(
@ -615,7 +559,8 @@ impl strafe_client::framework::Example for Skybox {
_queue: &wgpu::Queue,
) {
self.depth_view = Self::create_depth_texture(config, device);
self.camera.screen_size = (config.width, config.height);
self.screen_size = (config.width, config.height);
self.physics.camera.set_fov_aspect(1.0,(config.width as f32)/(config.height as f32));
}
fn render(
@ -625,44 +570,15 @@ impl strafe_client::framework::Example for Skybox {
queue: &wgpu::Queue,
_spawner: &strafe_client::framework::Spawner,
) {
let time = Instant::now();
let time=self.start_time.elapsed().as_nanos() as i64;
//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 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 {
self.camera.vel+=(self.camera.mv-d)*control_dir;
}
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;
}
}
self.physics.run(time);
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
// update rotation
let camera_uniforms = self.camera.to_uniform_data();
let camera_uniforms = to_uniform_data(&self.physics.camera,self.physics.body.extrapolated_position(time));
self.staging_belt
.write_buffer(
&mut encoder,

8
src/sweep.rs Normal file

@ -0,0 +1,8 @@
//something that implements body + hitbox + transform can predict collision
impl crate::sweep::PredictCollision for Model {
fn predict_collision(&self,other:&Model) -> Option<crate::event::EventStruct> {
//math!
None
}
}

27
src/zeroes.rs Normal file

@ -0,0 +1,27 @@
//find roots of polynomials
pub fn zeroes2(a0:f32,a1:f32,a2:f32) -> Vec<f32>{
if a2==0f32{
return zeroes1(a0, a1);
}
let mut radicand=a1*a1-4f32*a2*a0;
if 0f32<radicand {
radicand=radicand.sqrt();
if 0f32<a2 {
return vec![(-a1-radicand)/(2f32*a2),(-a1+radicand)/(2f32*a2)];
} else {
return vec![(-a1+radicand)/(2f32*a2),(-a1-radicand)/(2f32*a2)];
}
} else if radicand==0f32 {
return vec![-a1/(2f32*a2)];
} else {
return vec![];
}
}
#[inline]
pub fn zeroes1(a0:f32,a1:f32) -> Vec<f32> {
if a1==0f32{
return vec![];
} else {
return vec![-a0/a1];
}
}