Compare commits

...

13 Commits

Author SHA1 Message Date
ed69cf2297 plumb Run into graphics 2024-08-21 14:21:02 -07:00
b2a84e3be1 update common 2024-08-21 14:20:09 -07:00
0468484788 make physics-graphics communication a bit less insane 2024-08-21 14:20:09 -07:00
b9dc97053f graphics: spaces 2024-08-21 14:20:09 -07:00
40ed173b60 physics: unused field 2024-08-21 14:20:09 -07:00
fd5a813357 graphics: bundle FrameState into struct 2024-08-21 14:20:09 -07:00
50726199b9 todo 2024-08-21 14:20:09 -07:00
0676007430 graphics: drop model_buf after upload 2024-08-21 14:20:09 -07:00
97c49c9351 graphics: unused struct 2024-08-21 14:20:09 -07:00
10689784be graphics_worker: untab 2024-08-21 14:20:09 -07:00
2eff5dda9e graphics_worker: tweaks (rust master) 2024-08-21 14:20:09 -07:00
93b04f4f1f physics: recalculate touching parts in set_position 2024-08-21 14:20:09 -07:00
c616e82c47 use const 2024-08-19 17:04:53 -07:00
6 changed files with 453 additions and 252 deletions

4
Cargo.lock generated

@ -1912,9 +1912,9 @@ dependencies = [
[[package]] [[package]]
name = "strafesnet_common" name = "strafesnet_common"
version = "0.4.0" version = "0.4.1"
source = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/" source = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/"
checksum = "ea4126f6fbf9aecf89c9e319290f0221d177dcaa8659b4b9e3d82acc37829f12" checksum = "9a2621612e675a8f804abbbbe3b60caeafe58a2422cccbe453268d6f457df4f3"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"bitflags 2.6.0", "bitflags 2.6.0",

@ -6,12 +6,6 @@ use strafesnet_common::model::{self, ColorId, NormalId, PolygonIter, PositionId,
use wgpu::{util::DeviceExt,AstcBlock,AstcChannel}; use wgpu::{util::DeviceExt,AstcBlock,AstcChannel};
use crate::model_graphics::{self,IndexedGraphicsMeshOwnedRenderConfig,IndexedGraphicsMeshOwnedRenderConfigId,GraphicsMeshOwnedRenderConfig,GraphicsModelColor4,GraphicsModelOwned,GraphicsVertex}; use crate::model_graphics::{self,IndexedGraphicsMeshOwnedRenderConfig,IndexedGraphicsMeshOwnedRenderConfigId,GraphicsMeshOwnedRenderConfig,GraphicsModelColor4,GraphicsModelOwned,GraphicsVertex};
#[derive(Clone)]
pub struct GraphicsModelUpdate{
transform:Option<glam::Mat4>,
color:Option<glam::Vec4>,
}
struct Indices{ struct Indices{
count:u32, count:u32,
buf:wgpu::Buffer, buf:wgpu::Buffer,
@ -32,7 +26,6 @@ impl Indices{
} }
struct GraphicsModel{ struct GraphicsModel{
indices:Indices, indices:Indices,
model_buf:wgpu::Buffer,
vertex_buf:wgpu::Buffer, vertex_buf:wgpu::Buffer,
bind_group:wgpu::BindGroup, bind_group:wgpu::BindGroup,
instance_count:u32, instance_count:u32,
@ -65,12 +58,12 @@ struct GraphicsCamera{
#[inline] #[inline]
fn perspective_rh(fov_x_slope:f32,fov_y_slope:f32,z_near:f32,z_far:f32)->glam::Mat4{ fn perspective_rh(fov_x_slope:f32,fov_y_slope:f32,z_near:f32,z_far:f32)->glam::Mat4{
//glam_assert!(z_near > 0.0 && z_far > 0.0); //glam_assert!(z_near > 0.0 && z_far > 0.0);
let r=z_far / (z_near-z_far); let r=z_far/(z_near-z_far);
glam::Mat4::from_cols( glam::Mat4::from_cols(
glam::Vec4::new(1.0/fov_x_slope,0.0,0.0,0.0), glam::Vec4::new(1.0/fov_x_slope,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,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,-1.0),
glam::Vec4::new(0.0,0.0,r * z_near,0.0), glam::Vec4::new(0.0,0.0,r*z_near,0.0),
) )
} }
impl GraphicsCamera{ impl GraphicsCamera{
@ -79,10 +72,10 @@ impl GraphicsCamera{
} }
pub fn world(&self,pos:glam::Vec3,angles:glam::Vec2)->glam::Mat4{ pub fn world(&self,pos:glam::Vec3,angles:glam::Vec2)->glam::Mat4{
//f32 good enough for view matrix //f32 good enough for view matrix
glam::Mat4::from_translation(pos) * glam::Mat4::from_euler(glam::EulerRot::YXZ,angles.x,angles.y,0f32) glam::Mat4::from_translation(pos)*glam::Mat4::from_euler(glam::EulerRot::YXZ,angles.x,angles.y,0f32)
} }
pub fn to_uniform_data(&self,(pos,angles):(glam::Vec3,glam::Vec2))->[f32; 16 * 4]{ pub fn to_uniform_data(&self,pos:glam::Vec3,angles:glam::Vec2)->[f32;16*4]{
let proj=self.proj(); let proj=self.proj();
let proj_inv=proj.inverse(); let proj_inv=proj.inverse();
let view_inv=self.world(pos,angles); let view_inv=self.world(pos,angles);
@ -105,6 +98,13 @@ impl std::default::Default for GraphicsCamera{
} }
} }
pub struct FrameState{
pub body:crate::physics::Body,
pub camera:crate::physics::PhysicsCamera,
pub time:integer::Time,
pub run:strafesnet_common::run::Run,
}
pub struct GraphicsState{ pub struct GraphicsState{
pipelines:GraphicsPipelines, pipelines:GraphicsPipelines,
bind_groups:GraphicsBindGroups, bind_groups:GraphicsBindGroups,
@ -507,7 +507,6 @@ impl GraphicsState{
model_graphics::Indices::U16(indices)=>Indices::new(device,indices,wgpu::IndexFormat::Uint16), model_graphics::Indices::U16(indices)=>Indices::new(device,indices,wgpu::IndexFormat::Uint16),
}, },
bind_group, bind_group,
model_buf,
}); });
} }
} }
@ -816,7 +815,7 @@ impl GraphicsState{
}); });
let camera=GraphicsCamera::default(); let camera=GraphicsCamera::default();
let camera_uniforms=camera.to_uniform_data(crate::physics::PhysicsOutputState::default().extrapolate(strafesnet_common::mouse::MouseState::default())); let camera_uniforms=camera.to_uniform_data(glam::Vec3::ZERO,glam::Vec2::ZERO);
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),
@ -884,16 +883,17 @@ impl GraphicsState{
view:&wgpu::TextureView, view:&wgpu::TextureView,
device:&wgpu::Device, device:&wgpu::Device,
queue:&wgpu::Queue, queue:&wgpu::Queue,
physics_output:crate::physics::PhysicsOutputState, frame_state:FrameState,
predicted_time:integer::Time,
mouse_pos:glam::IVec2,
){ ){
//TODO:use scheduled frame times to create beautiful smoothing simulation physics extrapolation assuming no input //TODO:use scheduled frame times to create beautiful smoothing simulation physics extrapolation assuming no input
let mut encoder=device.create_command_encoder(&wgpu::CommandEncoderDescriptor{label:None}); let mut encoder=device.create_command_encoder(&wgpu::CommandEncoderDescriptor{label:None});
// update rotation // update rotation
let camera_uniforms=self.camera.to_uniform_data(physics_output.extrapolate(strafesnet_common::mouse::MouseState{pos:mouse_pos,time:predicted_time})); let camera_uniforms=self.camera.to_uniform_data(
frame_state.body.extrapolated_position(frame_state.time).into(),
frame_state.camera.simulate_move_angles(glam::IVec2::ZERO)
);
self.staging_belt self.staging_belt
.write_buffer( .write_buffer(
&mut encoder, &mut encoder,

@ -1,7 +1,5 @@
use strafesnet_common::integer;
pub enum Instruction{ pub enum Instruction{
Render(crate::physics::PhysicsOutputState,integer::Time,glam::IVec2), Render(crate::graphics::FrameState),
//UpdateModel(crate::graphics::GraphicsModelUpdate), //UpdateModel(crate::graphics::GraphicsModelUpdate),
Resize(winit::dpi::PhysicalSize<u32>,crate::settings::UserSettings), Resize(winit::dpi::PhysicalSize<u32>,crate::settings::UserSettings),
ChangeMap(strafesnet_common::map::CompleteMap), ChangeMap(strafesnet_common::map::CompleteMap),
@ -17,12 +15,12 @@ WorkerDescription{
//up to three frames in flight, dropping new frame requests when all three are busy, and dropping output frames when one renders out of order //up to three frames in flight, dropping new frame requests when all three are busy, and dropping output frames when one renders out of order
pub fn new<'a>( pub fn new<'a>(
mut graphics:crate::graphics::GraphicsState, mut graphics:crate::graphics::GraphicsState,
mut config:wgpu::SurfaceConfiguration, mut config:wgpu::SurfaceConfiguration,
surface:wgpu::Surface<'a>, surface:wgpu::Surface<'a>,
device:wgpu::Device, device:wgpu::Device,
queue:wgpu::Queue, queue:wgpu::Queue,
)->crate::compat_worker::INWorker<'a,Instruction>{ )->crate::compat_worker::INWorker<'a,Instruction>{
let mut resize=None; let mut resize=None;
crate::compat_worker::INWorker::new(move |ins:Instruction|{ crate::compat_worker::INWorker::new(move |ins:Instruction|{
match ins{ match ins{
@ -33,18 +31,16 @@ pub fn new<'a>(
Instruction::Resize(size,user_settings)=>{ Instruction::Resize(size,user_settings)=>{
resize=Some((size,user_settings)); resize=Some((size,user_settings));
} }
Instruction::Render(physics_output,predicted_time,mouse_pos)=>{ Instruction::Render(frame_state)=>{
if let Some((size,user_settings))=&resize{ if let Some((size,user_settings))=resize.take(){
println!("Resizing to {:?}",size); println!("Resizing to {:?}",size);
let t0=std::time::Instant::now(); let t0=std::time::Instant::now();
config.width=size.width.max(1); config.width=size.width.max(1);
config.height=size.height.max(1); config.height=size.height.max(1);
surface.configure(&device,&config); surface.configure(&device,&config);
graphics.resize(&device,&config,user_settings); graphics.resize(&device,&config,&user_settings);
println!("Resize took {:?}",t0.elapsed()); println!("Resize took {:?}",t0.elapsed());
} }
//clear every time w/e
resize=None;
//this has to go deeper somehow //this has to go deeper somehow
let frame=match surface.get_current_texture(){ let frame=match surface.get_current_texture(){
Ok(frame)=>frame, Ok(frame)=>frame,
@ -60,7 +56,7 @@ pub fn new<'a>(
..wgpu::TextureViewDescriptor::default() ..wgpu::TextureViewDescriptor::default()
}); });
graphics.render(&view,&device,&queue,physics_output,predicted_time,mouse_pos); graphics.render(&view,&device,&queue,frame_state);
frame.present(); frame.present();
} }

@ -462,7 +462,7 @@ pub struct TransformedMesh<'a>{
transform:&'a PhysicsMeshTransform, transform:&'a PhysicsMeshTransform,
} }
impl TransformedMesh<'_>{ impl TransformedMesh<'_>{
pub fn new<'a>( pub const fn new<'a>(
view:PhysicsMeshView<'a>, view:PhysicsMeshView<'a>,
transform:&'a PhysicsMeshTransform, transform:&'a PhysicsMeshTransform,
)->TransformedMesh<'a>{ )->TransformedMesh<'a>{
@ -751,6 +751,23 @@ impl MinkowskiMesh<'_>{
} }
best_edge.map(|e|(e.as_undirected(),best_time)) best_edge.map(|e|(e.as_undirected(),best_time))
} }
fn infinity_in(&self,infinity_body:crate::physics::Body)->Option<(MinkowskiFace,integer::Time)>{
let infinity_fev=self.infinity_fev(-infinity_body.velocity,infinity_body.position);
match crate::face_crawler::crawl_fev(infinity_fev,self,&infinity_body,integer::Time::MIN,infinity_body.time){
crate::face_crawler::CrawlResult::Miss(_)=>None,
crate::face_crawler::CrawlResult::Hit(face,time)=>Some((face,time)),
}
}
pub fn is_point_in_mesh(&self,point:Planar64Vec3)->bool{
let infinity_body=crate::physics::Body::new(point,Planar64Vec3::Y,Planar64Vec3::ZERO,integer::Time::ZERO);
//movement must escape the mesh forwards and backwards in time,
//otherwise the point is not inside the mesh
self.infinity_in(infinity_body)
.is_some_and(|_|
self.infinity_in(-infinity_body)
.is_some()
)
}
} }
impl MeshQuery<MinkowskiFace,MinkowskiDirectedEdge,MinkowskiVert> for MinkowskiMesh<'_>{ impl MeshQuery<MinkowskiFace,MinkowskiDirectedEdge,MinkowskiVert> for MinkowskiMesh<'_>{
fn face_nd(&self,face_id:MinkowskiFace)->(Planar64Vec3,Planar64){ fn face_nd(&self,face_id:MinkowskiFace)->(Planar64Vec3,Planar64){

@ -419,7 +419,7 @@ impl HitboxMesh{
} }
} }
#[inline] #[inline]
fn transformed_mesh(&self)->TransformedMesh{ const fn transformed_mesh(&self)->TransformedMesh{
TransformedMesh::new(self.mesh.complete_mesh_view(),&self.transform) TransformedMesh::new(self.mesh.complete_mesh_view(),&self.transform)
} }
} }
@ -579,18 +579,37 @@ impl MoveState{
MoveState::Fly=>None, MoveState::Fly=>None,
} }
} }
} //lmao idk this is convenient
fn apply_enum_and_input_and_body(&mut self,body:&mut Body,touching:&TouchingState,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,style:&StyleModifiers,camera:&PhysicsCamera,input_state:&InputState){
#[derive(Clone,Default)] self.apply_enum(body,touching,models,hitbox_mesh,style,camera,input_state);
pub struct PhysicsOutputState{ self.apply_input(body,touching,models,hitbox_mesh,style,camera,input_state);
body:Body, self.apply_to_body(body,touching,models,hitbox_mesh,style,camera,input_state);
camera:PhysicsCamera, }
camera_offset:Planar64Vec3, fn apply_enum_and_body(&mut self,body:&mut Body,touching:&TouchingState,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,style:&StyleModifiers,camera:&PhysicsCamera,input_state:&InputState){
mouse_pos:glam::IVec2, self.apply_enum(body,touching,models,hitbox_mesh,style,camera,input_state);
} self.apply_to_body(body,touching,models,hitbox_mesh,style,camera,input_state);
impl PhysicsOutputState{ }
pub fn extrapolate(&self,mouse:MouseState)->(glam::Vec3,glam::Vec2){ fn apply_input_and_body(&mut self,body:&mut Body,touching:&TouchingState,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,style:&StyleModifiers,camera:&PhysicsCamera,input_state:&InputState){
((self.body.extrapolated_position(mouse.time)+self.camera_offset).into(),self.camera.simulate_move_angles(mouse.pos-self.mouse_pos)) self.apply_input(body,touching,models,hitbox_mesh,style,camera,input_state);
self.apply_to_body(body,touching,models,hitbox_mesh,style,camera,input_state);
}
fn set_move_state(&mut self,move_state:MoveState,body:&mut Body,touching:&TouchingState,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,style:&StyleModifiers,camera:&PhysicsCamera,input_state:&InputState){
*self=move_state;
//this function call reads the above state that was just set
self.apply_enum_and_body(body,touching,models,hitbox_mesh,style,camera,input_state);
}
fn cull_velocity(&mut self,velocity:Planar64Vec3,body:&mut Body,touching:&mut TouchingState,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,style:&StyleModifiers,camera:&PhysicsCamera,input_state:&InputState){
//TODO: be more precise about contacts
if set_velocity_cull(body,touching,models,hitbox_mesh,velocity){
//TODO do better
match self.get_walk_state(){
//did you stop touching the thing you were walking on?
Some(walk_state)=>if !touching.contacts.contains(&walk_state.contact){
self.set_move_state(MoveState::Air,body,touching,models,hitbox_mesh,style,camera,input_state);
},
None=>self.apply_enum_and_body(body,touching,models,hitbox_mesh,style,camera,input_state),
}
}
} }
} }
@ -890,7 +909,7 @@ impl VirtualBody<'_>{
pub struct PhysicsState{ pub struct PhysicsState{
time:Time, time:Time,
body:Body, body:Body,
world:WorldState,//currently there is only one state the world can be in _world:WorldState,//currently there is only one state the world can be in
touching:TouchingState, touching:TouchingState,
//camera must exist in state because wormholes modify the camera, also camera punch //camera must exist in state because wormholes modify the camera, also camera punch
camera:PhysicsCamera, camera:PhysicsCamera,
@ -927,7 +946,7 @@ impl Default for PhysicsState{
move_state:MoveState::Air, move_state:MoveState::Air,
camera:PhysicsCamera::default(), camera:PhysicsCamera::default(),
input_state:InputState::default(), input_state:InputState::default(),
world:WorldState{}, _world:WorldState{},
mode_state:ModeState::default(), mode_state:ModeState::default(),
run:run::Run::new(), run:run::Run::new(),
} }
@ -956,39 +975,15 @@ impl PhysicsState{
fn next_move_instruction(&self)->Option<TimedInstruction<PhysicsInternalInstruction>>{ fn next_move_instruction(&self)->Option<TimedInstruction<PhysicsInternalInstruction>>{
self.move_state.next_move_instruction(&self.style.strafe,self.time) self.move_state.next_move_instruction(&self.style.strafe,self.time)
} }
//lmao idk this is convenient fn cull_velocity(&mut self,data:&PhysicsData,velocity:Planar64Vec3){
fn apply_enum_and_input_and_body(&mut self,data:&PhysicsData){ self.move_state.cull_velocity(velocity,&mut self.body,&mut self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
self.move_state.apply_enum(&mut self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
self.move_state.apply_input(&self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
self.move_state.apply_to_body(&mut self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
}
fn apply_enum_and_body(&mut self,data:&PhysicsData){
self.move_state.apply_enum(&mut self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
self.move_state.apply_to_body(&mut self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
}
fn apply_input_and_body(&mut self,data:&PhysicsData){
self.move_state.apply_input(&self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
self.move_state.apply_to_body(&mut self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
} }
fn set_move_state(&mut self,data:&PhysicsData,move_state:MoveState){ fn set_move_state(&mut self,data:&PhysicsData,move_state:MoveState){
self.move_state=move_state; self.move_state.set_move_state(move_state,&mut self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
//this function call reads the above state that was just set
self.apply_enum_and_body(data);
} }
fn cull_velocity(&mut self,data:&PhysicsData,velocity:Planar64Vec3){ fn apply_input_and_body(&mut self,data:&PhysicsData){
//TODO: be more precise about contacts self.move_state.apply_input_and_body(&mut self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
if set_velocity_cull(&mut self.body,&mut self.touching,&data.models,&data.hitbox_mesh,velocity){
//TODO do better
match self.move_state.get_walk_state(){
//did you stop touching the thing you were walking on?
Some(walk_state)=>if !self.touching.contacts.contains(&walk_state.contact){
self.set_move_state(data,MoveState::Air);
},
None=>self.apply_enum_and_body(data),
}
}
} }
//state mutated on collision: //state mutated on collision:
//Accelerator //Accelerator
//stair step-up //stair step-up
@ -1039,17 +1034,21 @@ impl instruction::InstructionEmitter<PhysicsInternalInstruction> for PhysicsCont
} }
} }
impl PhysicsContext{ impl PhysicsContext{
pub const fn output(&self)->PhysicsOutputState{ pub fn camera_body(&self)->Body{
PhysicsOutputState{ Body{
body:self.state.body, position:self.state.body.position+self.state.style.camera_offset,
camera:self.state.camera, ..self.state.body
camera_offset:self.state.style.camera_offset,
mouse_pos:self.state.input_state.mouse.pos,
} }
} }
pub const fn camera(&self)->PhysicsCamera{
self.state.camera
}
pub const fn get_next_mouse(&self)->&MouseState{ pub const fn get_next_mouse(&self)->&MouseState{
self.state.input_state.get_next_mouse() self.state.input_state.get_next_mouse()
} }
pub const fn run(&self)->run::Run{
self.state.run
}
/// use with caution, this is the only non-instruction way to mess with physics /// use with caution, this is the only non-instruction way to mess with physics
pub fn generate_models(&mut self,map:&map::CompleteMap){ pub fn generate_models(&mut self,map:&map::CompleteMap){
self.state.clear(); self.state.clear();
@ -1254,14 +1253,80 @@ fn contact_normal(models:&PhysicsModels,hitbox_mesh:&HitboxMesh,contact:&Contact
minkowski.face_nd(contact.face_id).0 minkowski.face_nd(contact.face_id).0
} }
fn set_position(body:&mut Body,touching:&mut TouchingState,point:Planar64Vec3)->Planar64Vec3{ fn recalculate_touching(
move_state:&mut MoveState,
body:&mut Body,
touching:&mut TouchingState,
run:&mut run::Run,
mode_state:&mut ModeState,
mode:Option<&gameplay_modes::Mode>,
models:&PhysicsModels,
hitbox_mesh:&HitboxMesh,
bvh:&bvh::BvhNode<ConvexMeshId>,
style:&StyleModifiers,
camera:&PhysicsCamera,
input_state:&InputState,
time:Time,
){
//collision_end all existing contacts
//I would have preferred while let Some(contact)=contacts.pop()
//but there is no such method
while let Some(&contact)=touching.contacts.iter().next(){
collision_end_contact(move_state,body,touching,models,hitbox_mesh,style,camera,input_state,models.contact_attr(contact.model_id),contact)
}
while let Some(&intersect)=touching.intersects.iter().next(){
collision_end_intersect(touching,mode,run,models.intersect_attr(intersect.model_id),intersect,time);
}
//find all models in the teleport region
let mut aabb=aabb::Aabb::default();
aabb.grow(body.position);
aabb.inflate(hitbox_mesh.halfsize);
//relative to moving platforms
//let relative_body=&VirtualBody::relative(&Body::default(),&state.body).body(state.time);
bvh.the_tester(&aabb,&mut |&convex_mesh_id|{
//no checks are needed because of the time limits.
let model_mesh=models.mesh(convex_mesh_id);
let minkowski=model_physics::MinkowskiMesh::minkowski_sum(model_mesh,hitbox_mesh.transformed_mesh());
if minkowski.is_point_in_mesh(body.position){
match convex_mesh_id.model_id{
//being inside of contact objects is an invalid physics state
//but the physics isn't advanced enough to do anything about it yet
//TODO: PushSolve and search for the closest valid position
PhysicsModelId::Contact(_)=>(),
PhysicsModelId::Intersect(model_id)=>
collision_start_intersect(move_state,body,mode_state,touching,mode,run,models,hitbox_mesh,bvh,style,camera,input_state,
models.intersect_attr(model_id),
IntersectCollision{
model_id,
submesh_id:convex_mesh_id.submesh_id,
},
time,
),
}
}
});
}
fn set_position(
point:Planar64Vec3,
move_state:&mut MoveState,
body:&mut Body,
touching:&mut TouchingState,
run:&mut run::Run,
mode_state:&mut ModeState,
mode:Option<&gameplay_modes::Mode>,
models:&PhysicsModels,
hitbox_mesh:&HitboxMesh,
bvh:&bvh::BvhNode<ConvexMeshId>,
style:&StyleModifiers,
camera:&PhysicsCamera,
input_state:&InputState,
time:Time,
)->Planar64Vec3{
//test intersections at new position //test intersections at new position
//hovering above the surface 0 units is not intersecting. you will fall into it just fine //hovering above the surface 0 units is not intersecting. you will fall into it just fine
body.position=point; body.position=point;
//manual clear //for c in contacts{process_instruction(CollisionEnd(c))} //calculate contacts and determine the actual state
touching.clear(); recalculate_touching(move_state,body,touching,run,mode_state,mode,models,hitbox_mesh,bvh,style,camera,input_state,time);
//TODO: calculate contacts and determine the actual state
//touching.recalculate(body);
point point
} }
fn set_velocity_cull(body:&mut Body,touching:&mut TouchingState,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,v:Planar64Vec3)->bool{ fn set_velocity_cull(body:&mut Body,touching:&mut TouchingState,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,v:Planar64Vec3)->bool{
@ -1303,31 +1368,71 @@ fn set_acceleration(body:&mut Body,touching:&TouchingState,models:&PhysicsModels
body.acceleration=a; body.acceleration=a;
} }
fn teleport(body:&mut Body,touching:&mut TouchingState,models:&PhysicsModels,style:&StyleModifiers,hitbox_mesh:&HitboxMesh,point:Planar64Vec3)->MoveState{ fn teleport(
set_position(body,touching,point); point:Planar64Vec3,
move_state:&mut MoveState,
body:&mut Body,
touching:&mut TouchingState,
run:&mut run::Run,
mode_state:&mut ModeState,
mode:Option<&gameplay_modes::Mode>,
models:&PhysicsModels,
hitbox_mesh:&HitboxMesh,
bvh:&bvh::BvhNode<ConvexMeshId>,
style:&StyleModifiers,
camera:&PhysicsCamera,
input_state:&InputState,
time:Time,
){
set_position(point,move_state,body,touching,run,mode_state,mode,models,hitbox_mesh,bvh,style,camera,input_state,time);
set_acceleration(body,touching,models,hitbox_mesh,style.gravity); set_acceleration(body,touching,models,hitbox_mesh,style.gravity);
MoveState::Air
} }
fn teleport_to_spawn(body:&mut Body,touching:&mut TouchingState,style:&StyleModifiers,hitbox_mesh:&HitboxMesh,mode:&gameplay_modes::Mode,models:&PhysicsModels,stage_id:gameplay_modes::StageId)->Option<MoveState>{ enum TeleportToSpawnError{
let transform=models.get_model_transform(mode.get_spawn_model_id(stage_id)?)?; NoModel,
}
fn teleport_to_spawn(
stage:&gameplay_modes::Stage,
move_state:&mut MoveState,
body:&mut Body,
touching:&mut TouchingState,
run:&mut run::Run,
mode_state:&mut ModeState,
mode:&gameplay_modes::Mode,
models:&PhysicsModels,
hitbox_mesh:&HitboxMesh,
bvh:&bvh::BvhNode<ConvexMeshId>,
style:&StyleModifiers,
camera:&PhysicsCamera,
input_state:&InputState,
time:Time,
)->Result<(),TeleportToSpawnError>{
let transform=models.get_model_transform(stage.spawn()).ok_or(TeleportToSpawnError::NoModel)?;
let point=transform.vertex.transform_point3(Planar64Vec3::Y)+Planar64Vec3::Y*(style.hitbox.halfsize.y()+Planar64::ONE/16); let point=transform.vertex.transform_point3(Planar64Vec3::Y)+Planar64Vec3::Y*(style.hitbox.halfsize.y()+Planar64::ONE/16);
Some(teleport(body,touching,models,style,hitbox_mesh,point)) teleport(point,move_state,body,touching,run,mode_state,Some(mode),models,hitbox_mesh,bvh,style,camera,input_state,time);
Ok(())
} }
fn run_teleport_behaviour( fn run_teleport_behaviour(
wormhole:&Option<gameplay_attributes::Wormhole>,
models:&PhysicsModels,
mode:&gameplay_modes::Mode,
style:&StyleModifiers,
hitbox_mesh:&HitboxMesh,
mode_state:&mut ModeState,
touching:&mut TouchingState,
body:&mut Body,
model_id:ModelId, model_id:ModelId,
)->Option<MoveState>{ wormhole:Option<&gameplay_attributes::Wormhole>,
mode:Option<&gameplay_modes::Mode>,
move_state:&mut MoveState,
body:&mut Body,
touching:&mut TouchingState,
run:&mut run::Run,
mode_state:&mut ModeState,
models:&PhysicsModels,
hitbox_mesh:&HitboxMesh,
bvh:&bvh::BvhNode<ConvexMeshId>,
style:&StyleModifiers,
camera:&PhysicsCamera,
input_state:&InputState,
time:Time,
){
//TODO: jump count and checkpoints are always reset on teleport. //TODO: jump count and checkpoints are always reset on teleport.
//Map makers are expected to use tools to prevent //Map makers are expected to use tools to prevent
//multi-boosting on JumpLimit boosters such as spawning into a SetVelocity //multi-boosting on JumpLimit boosters such as spawning into a SetVelocity
if let Some(mode)=mode{
if let Some(stage_element)=mode.get_element(model_id){ if let Some(stage_element)=mode.get_element(model_id){
if let Some(stage)=mode.get_stage(stage_element.stage_id()){ if let Some(stage)=mode.get_stage(stage_element.stage_id()){
if mode_state.get_stage_id()<stage_element.stage_id(){ if mode_state.get_stage_id()<stage_element.stage_id(){
@ -1336,20 +1441,33 @@ fn run_teleport_behaviour(
if let Some(current_stage)=mode.get_stage(mode_state.get_stage_id()){ if let Some(current_stage)=mode.get_stage(mode_state.get_stage_id()){
if !current_stage.is_complete(mode_state.ordered_checkpoint_count(),mode_state.unordered_checkpoint_count()){ if !current_stage.is_complete(mode_state.ordered_checkpoint_count(),mode_state.unordered_checkpoint_count()){
//do the stage checkpoints have to be reset? //do the stage checkpoints have to be reset?
return teleport_to_spawn(body,touching,style,hitbox_mesh,mode,models,mode_state.get_stage_id()); let _=teleport_to_spawn(current_stage,move_state,body,touching,run,mode_state,mode,models,hitbox_mesh,bvh,style,camera,input_state,time);
return;
} }
} }
//check if all between stages have no checkpoints required to pass them //check if all between stages have no checkpoints required to pass them
let mut loop_unbroken=true;
for stage_id in mode_state.get_stage_id().get()+1..stage_element.stage_id().get(){ for stage_id in mode_state.get_stage_id().get()+1..stage_element.stage_id().get(){
let stage_id=StageId::new(stage_id); let stage_id=StageId::new(stage_id);
//check if none of the between stages has checkpoints, if they do teleport back to that stage //check if none of the between stages has checkpoints, if they do teleport back to that stage
if !mode.get_stage(stage_id)?.is_empty(){ match mode.get_stage(stage_id){
mode_state.set_stage_id(stage_id); Some(stage)=>if !stage.is_empty(){
return teleport_to_spawn(body,touching,style,hitbox_mesh,mode,models,stage_id); mode_state.set_stage_id(stage_id);
let _=teleport_to_spawn(stage,move_state,body,touching,run,mode_state,mode,models,hitbox_mesh,bvh,style,camera,input_state,time);
return;
},
None=>{
//no such stage! set to last existing stage and break loop
mode_state.set_stage_id(StageId::new(stage_id.get()-1));
loop_unbroken=false;
break;
},
} }
} };
//notably you do not get teleported for touching ordered checkpoints in the wrong order within the same stage. //notably you do not get teleported for touching ordered checkpoints in the wrong order within the same stage.
mode_state.set_stage_id(stage_element.stage_id()); if loop_unbroken{
mode_state.set_stage_id(stage_element.stage_id());
}
}else if stage_element.force(){ }else if stage_element.force(){
//forced stage_element will set the stage_id even if the stage has already been passed //forced stage_element will set the stage_id even if the stage has already been passed
mode_state.set_stage_id(stage_element.stage_id()); mode_state.set_stage_id(stage_element.stage_id());
@ -1357,9 +1475,10 @@ fn run_teleport_behaviour(
match stage_element.behaviour(){ match stage_element.behaviour(){
gameplay_modes::StageElementBehaviour::SpawnAt=>(), gameplay_modes::StageElementBehaviour::SpawnAt=>(),
gameplay_modes::StageElementBehaviour::Trigger gameplay_modes::StageElementBehaviour::Trigger
|gameplay_modes::StageElementBehaviour::Teleport=>{ |gameplay_modes::StageElementBehaviour::Teleport=>if let Some(mode_state_stage)=mode.get_stage(mode_state.get_stage_id()){
//I guess this is correct behaviour when trying to teleport to a non-existent spawn but it's still weird //I guess this is correct behaviour when trying to teleport to a non-existent spawn but it's still weird
return teleport_to_spawn(body,touching,style,hitbox_mesh,mode,models,mode_state.get_stage_id()); let _=teleport_to_spawn(mode_state_stage,move_state,body,touching,run,mode_state,mode,models,hitbox_mesh,bvh,style,camera,input_state,time);
return;
}, },
gameplay_modes::StageElementBehaviour::Platform=>(), gameplay_modes::StageElementBehaviour::Platform=>(),
gameplay_modes::StageElementBehaviour::Check=>(),//this is to run the checkpoint check behaviour without any other side effects gameplay_modes::StageElementBehaviour::Check=>(),//this is to run the checkpoint check behaviour without any other side effects
@ -1373,158 +1492,185 @@ fn run_teleport_behaviour(
} }
} }
} }
match wormhole{ }
&Some(gameplay_attributes::Wormhole{destination_model})=>{ if let Some(&gameplay_attributes::Wormhole{destination_model})=wormhole{
let origin=models.get_model_transform(model_id)?; if let (Some(origin),Some(destination))=(models.get_model_transform(model_id),models.get_model_transform(destination_model)){
let destination=models.get_model_transform(destination_model)?; let point=body.position-origin.vertex.translation+destination.vertex.translation;
//ignore the transform for now //TODO: camera angles
Some(teleport(body,touching,models,style,hitbox_mesh,body.position-origin.vertex.translation+destination.vertex.translation)) teleport(point,move_state,body,touching,run,mode_state,mode,models,hitbox_mesh,bvh,style,camera,input_state,time);
} }
None=>None,
} }
} }
fn collision_start_contact( fn collision_start_contact(
state:&mut PhysicsState, move_state:&mut MoveState,
data:&PhysicsData, body:&mut Body,
mode_state:&mut ModeState,
touching:&mut TouchingState,
run:&mut run::Run,
mode:Option<&gameplay_modes::Mode>,
models:&PhysicsModels,
hitbox_mesh:&HitboxMesh,
bvh:&bvh::BvhNode<ConvexMeshId>,
style:&StyleModifiers,
camera:&PhysicsCamera,
input_state:&InputState,
attr:&gameplay_attributes::ContactAttributes, attr:&gameplay_attributes::ContactAttributes,
contact:ContactCollision, contact:ContactCollision,
time:Time,
){ ){
let incident_velocity=state.body.velocity; let incident_velocity=body.velocity;
//add to touching //add to touching
state.touching.insert(Collision::Contact(contact)); touching.insert(Collision::Contact(contact));
//clip v //clip v
set_velocity(&mut state.body,&state.touching,&data.models,&data.hitbox_mesh,incident_velocity); set_velocity(body,touching,models,hitbox_mesh,incident_velocity);
match &attr.contacting.contact_behaviour{ match &attr.contacting.contact_behaviour{
Some(gameplay_attributes::ContactingBehaviour::Surf)=>println!("I'm surfing!"), Some(gameplay_attributes::ContactingBehaviour::Surf)=>println!("I'm surfing!"),
Some(gameplay_attributes::ContactingBehaviour::Cling)=>println!("Unimplemented!"), Some(gameplay_attributes::ContactingBehaviour::Cling)=>println!("Unimplemented!"),
&Some(gameplay_attributes::ContactingBehaviour::Elastic(elasticity))=>{ &Some(gameplay_attributes::ContactingBehaviour::Elastic(elasticity))=>{
let reflected_velocity=state.body.velocity+(state.body.velocity-incident_velocity)*Planar64::raw(elasticity as i64+1); let reflected_velocity=body.velocity+(body.velocity-incident_velocity)*Planar64::raw(elasticity as i64+1);
set_velocity(&mut state.body,&state.touching,&data.models,&data.hitbox_mesh,reflected_velocity); set_velocity(body,touching,models,hitbox_mesh,reflected_velocity);
}, },
Some(gameplay_attributes::ContactingBehaviour::Ladder(contacting_ladder))=> Some(gameplay_attributes::ContactingBehaviour::Ladder(contacting_ladder))=>
if let Some(ladder_settings)=&state.style.ladder{ if let Some(ladder_settings)=&style.ladder{
if contacting_ladder.sticky{ if contacting_ladder.sticky{
//kill v //kill v
//actually you could do this with a booster attribute :thinking: //actually you could do this with a booster attribute :thinking:
//it's a little bit different because maybe you want to chain ladders together //it's a little bit different because maybe you want to chain ladders together
set_velocity(&mut state.body,&state.touching,&data.models,&data.hitbox_mesh,Planar64Vec3::ZERO);//model.velocity set_velocity(body,touching,models,hitbox_mesh,Planar64Vec3::ZERO);//model.velocity
} }
//ladder walkstate //ladder walkstate
let (gravity,target_velocity)=ladder_things(ladder_settings,&contact,&state.touching,&data.models,&data.hitbox_mesh,&state.style,&state.camera,&state.input_state); let (gravity,target_velocity)=ladder_things(ladder_settings,&contact,touching,models,hitbox_mesh,style,camera,input_state);
let walk_state=ContactMoveState::ladder(ladder_settings,&state.body,gravity,target_velocity,contact); let walk_state=ContactMoveState::ladder(ladder_settings,body,gravity,target_velocity,contact);
state.set_move_state(data,MoveState::Ladder(walk_state)); move_state.set_move_state(MoveState::Ladder(walk_state),body,touching,models,hitbox_mesh,style,camera,input_state);
}, },
Some(gameplay_attributes::ContactingBehaviour::NoJump)=>todo!("nyi"), Some(gameplay_attributes::ContactingBehaviour::NoJump)=>todo!("nyi"),
None=>if let Some(walk_settings)=&state.style.walk{ None=>if let Some(walk_settings)=&style.walk{
if walk_settings.is_slope_walkable(contact_normal(&data.models,&data.hitbox_mesh,&contact),Planar64Vec3::Y){ if walk_settings.is_slope_walkable(contact_normal(models,hitbox_mesh,&contact),Planar64Vec3::Y){
//ground //ground
let (gravity,target_velocity)=ground_things(walk_settings,&contact,&state.touching,&data.models,&data.hitbox_mesh,&state.style,&state.camera,&state.input_state); let (gravity,target_velocity)=ground_things(walk_settings,&contact,touching,models,hitbox_mesh,style,camera,input_state);
let walk_state=ContactMoveState::ground(walk_settings,&state.body,gravity,target_velocity,contact); let walk_state=ContactMoveState::ground(walk_settings,body,gravity,target_velocity,contact);
state.set_move_state(data,MoveState::Walk(walk_state)); move_state.set_move_state(MoveState::Walk(walk_state),body,touching,models,hitbox_mesh,style,camera,input_state);
} }
}, },
} }
//I love making functions with 10 arguments to dodge the borrow checker //I love making functions with 10 arguments to dodge the borrow checker
if let Some(mode)=data.modes.get_mode(state.mode_state.get_mode_id()){ run_teleport_behaviour(contact.model_id.into(),attr.general.wormhole.as_ref(),mode,move_state,body,touching,run,mode_state,models,hitbox_mesh,bvh,style,camera,input_state,time);
run_teleport_behaviour(&attr.general.wormhole,&data.models,mode,&state.style,&data.hitbox_mesh,&mut state.mode_state,&mut state.touching,&mut state.body,contact.model_id.into()); if style.get_control(Controls::Jump,input_state.controls){
} if let (Some(jump_settings),Some(walk_state))=(&style.jump,move_state.get_walk_state()){
if state.style.get_control(Controls::Jump,state.input_state.controls){ let jump_dir=walk_state.jump_direction.direction(models,hitbox_mesh,&walk_state.contact);
if let (Some(jump_settings),Some(walk_state))=(&state.style.jump,state.move_state.get_walk_state()){ let jumped_velocity=jump_settings.jumped_velocity(style,jump_dir,body.velocity,attr.general.booster.as_ref());
let jump_dir=walk_state.jump_direction.direction(&data.models,&data.hitbox_mesh,&walk_state.contact); move_state.cull_velocity(jumped_velocity,body,touching,models,hitbox_mesh,style,camera,input_state);
let jumped_velocity=jump_settings.jumped_velocity(&state.style,jump_dir,state.body.velocity,attr.general.booster.as_ref()); }
state.cull_velocity(data,jumped_velocity); }
} match &attr.general.trajectory{
} Some(trajectory)=>{
match &attr.general.trajectory{ match trajectory{
Some(trajectory)=>{ gameplay_attributes::SetTrajectory::AirTime(_)=>todo!(),
match trajectory{ gameplay_attributes::SetTrajectory::Height(_)=>todo!(),
gameplay_attributes::SetTrajectory::AirTime(_)=>todo!(), gameplay_attributes::SetTrajectory::TargetPointTime { target_point: _, time: _ }=>todo!(),
gameplay_attributes::SetTrajectory::Height(_)=>todo!(), gameplay_attributes::SetTrajectory::TargetPointSpeed { target_point: _, speed: _, trajectory_choice: _ }=>todo!(),
gameplay_attributes::SetTrajectory::TargetPointTime { target_point: _, time: _ }=>todo!(), &gameplay_attributes::SetTrajectory::Velocity(velocity)=>{
gameplay_attributes::SetTrajectory::TargetPointSpeed { target_point: _, speed: _, trajectory_choice: _ }=>todo!(), move_state.cull_velocity(velocity,body,touching,models,hitbox_mesh,style,camera,input_state);
&gameplay_attributes::SetTrajectory::Velocity(velocity)=>{ },
state.cull_velocity(data,velocity); gameplay_attributes::SetTrajectory::DotVelocity { direction: _, dot: _ }=>todo!(),
}, }
gameplay_attributes::SetTrajectory::DotVelocity { direction: _, dot: _ }=>todo!(), },
} None=>(),
}, }
None=>(), //doing enum to set the acceleration when surfing
} //doing input_and_body to refresh the walk state if you hit a wall while accelerating
//doing enum to set the acceleration when surfing move_state.apply_enum_and_input_and_body(body,touching,models,hitbox_mesh,style,camera,input_state);
//doing input_and_body to refresh the walk state if you hit a wall while accelerating
state.apply_enum_and_input_and_body(data);
} }
fn collision_start_intersect( fn collision_start_intersect(
state:&mut PhysicsState, move_state:&mut MoveState,
data:&PhysicsData, body:&mut Body,
mode_state:&mut ModeState,
touching:&mut TouchingState,
mode:Option<&gameplay_modes::Mode>,
run:&mut run::Run,
models:&PhysicsModels,
hitbox_mesh:&HitboxMesh,
bvh:&bvh::BvhNode<ConvexMeshId>,
style:&StyleModifiers,
camera:&PhysicsCamera,
input_state:&InputState,
attr:&gameplay_attributes::IntersectAttributes, attr:&gameplay_attributes::IntersectAttributes,
intersect:IntersectCollision, intersect:IntersectCollision,
time:Time,
){ ){
//I think that setting the velocity to 0 was preventing surface contacts from entering an infinite loop //I think that setting the velocity to 0 was preventing surface contacts from entering an infinite loop
state.touching.insert(Collision::Intersect(intersect)); touching.insert(Collision::Intersect(intersect));
//insta booster! //insta booster!
if let Some(booster)=&attr.general.booster{ if let Some(booster)=&attr.general.booster{
state.cull_velocity(data,booster.boost(state.body.velocity)); move_state.cull_velocity(booster.boost(body.velocity),body,touching,models,hitbox_mesh,style,camera,input_state);
} }
if let Some(mode)=data.modes.get_mode(state.mode_state.get_mode_id()){ if let Some(mode)=mode{
let zone=mode.get_zone(intersect.model_id.into()); let zone=mode.get_zone(intersect.model_id.into());
match zone{ match zone{
Some(gameplay_modes::Zone::Start)=>{ Some(gameplay_modes::Zone::Start)=>{
println!("@@@@ Starting new run!"); println!("@@@@ Starting new run!");
state.run=run::Run::new(); *run=run::Run::new();
}, },
Some(gameplay_modes::Zone::Finish)=>{ Some(gameplay_modes::Zone::Finish)=>{
match state.run.finish(state.time){ match run.finish(time){
Ok(())=>println!("@@@@ Finished run time={}",state.run.time(state.time)), Ok(())=>println!("@@@@ Finished run time={}",run.time(time)),
Err(e)=>println!("@@@@ Run Finish error:{e:?}"), Err(e)=>println!("@@@@ Run Finish error:{e:?}"),
} }
}, },
Some(gameplay_modes::Zone::Anticheat)=>state.run.flag(run::FlagReason::Anticheat), Some(gameplay_modes::Zone::Anticheat)=>run.flag(run::FlagReason::Anticheat),
None=>(), None=>(),
} }
run_teleport_behaviour(&attr.general.wormhole,&data.models,mode,&state.style,&data.hitbox_mesh,&mut state.mode_state,&mut state.touching,&mut state.body,intersect.model_id.into()); }
} run_teleport_behaviour(intersect.model_id.into(),attr.general.wormhole.as_ref(),mode,move_state,body,touching,run,mode_state,models,hitbox_mesh,bvh,style,camera,input_state,time);
} }
fn collision_end_contact( fn collision_end_contact(
state:&mut PhysicsState, move_state:&mut MoveState,
data:&PhysicsData, body:&mut Body,
touching:&mut TouchingState,
models:&PhysicsModels,
hitbox_mesh:&HitboxMesh,
style:&StyleModifiers,
camera:&PhysicsCamera,
input_state:&InputState,
_attr:&gameplay_attributes::ContactAttributes, _attr:&gameplay_attributes::ContactAttributes,
contact:ContactCollision, contact:ContactCollision,
){ ){
state.touching.remove(&Collision::Contact(contact));//remove contact before calling contact_constrain_acceleration touching.remove(&Collision::Contact(contact));//remove contact before calling contact_constrain_acceleration
//check ground //check ground
//TODO do better //TODO do better
//this is inner code from state.cull_velocity //this is inner code from move_state.cull_velocity
match state.move_state.get_walk_state(){ match move_state.get_walk_state(){
//did you stop touching the thing you were walking on? //did you stop touching the thing you were walking on?
Some(walk_state)=>if walk_state.contact==contact{ Some(walk_state)=>if walk_state.contact==contact{
state.set_move_state(data,MoveState::Air); move_state.set_move_state(MoveState::Air,body,touching,models,hitbox_mesh,style,camera,input_state);
}, },
None=>state.apply_enum_and_body(data), None=>move_state.apply_enum_and_body(body,touching,models,hitbox_mesh,style,camera,input_state),
} }
} }
fn collision_end_intersect( fn collision_end_intersect(
state:&mut PhysicsState, touching:&mut TouchingState,
data:&PhysicsData, mode:Option<&gameplay_modes::Mode>,
run:&mut run::Run,
_attr:&gameplay_attributes::IntersectAttributes, _attr:&gameplay_attributes::IntersectAttributes,
intersect:IntersectCollision, intersect:IntersectCollision,
time:Time,
){ ){
state.touching.remove(&Collision::Intersect(intersect)); touching.remove(&Collision::Intersect(intersect));
if let Some(mode)=data.modes.get_mode(state.mode_state.get_mode_id()){ if let Some(mode)=mode{
let zone=mode.get_zone(intersect.model_id.into()); let zone=mode.get_zone(intersect.model_id.into());
match zone{ match zone{
Some(gameplay_modes::Zone::Start)=>{ Some(gameplay_modes::Zone::Start)=>{
match state.run.start(state.time){ match run.start(time){
Ok(())=>println!("@@@@ Started run"), Ok(())=>println!("@@@@ Started run"),
Err(e)=>println!("@@@@ Run Start error:{e:?}"), Err(e)=>println!("@@@@ Run Start error:{e:?}"),
} }
}, },
_=>(), _=>(),
} }
} }
} }
fn atomic_internal_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:TimedInstruction<PhysicsInternalInstruction>){ fn atomic_internal_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:TimedInstruction<PhysicsInternalInstruction>){
state.time=ins.time; state.time=ins.time;
@ -1538,13 +1684,41 @@ fn atomic_internal_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:Tim
state.body.advance_time(state.time); state.body.advance_time(state.time);
} }
match ins.instruction{ match ins.instruction{
PhysicsInternalInstruction::CollisionStart(collision)=>match collision{ PhysicsInternalInstruction::CollisionStart(collision)=>{
Collision::Contact(contact)=>collision_start_contact(state,data,data.models.contact_attr(contact.model_id),contact), let mode=data.modes.get_mode(state.mode_state.get_mode_id());
Collision::Intersect(intersect)=>collision_start_intersect(state,data,data.models.intersect_attr(intersect.model_id),intersect), match collision{
Collision::Contact(contact)=>collision_start_contact(
&mut state.move_state,&mut state.body,&mut state.mode_state,&mut state.touching,&mut state.run,
mode,
&data.models,&data.hitbox_mesh,&data.bvh,&state.style,&state.camera,&state.input_state,
data.models.contact_attr(contact.model_id),
contact,
state.time,
),
Collision::Intersect(intersect)=>collision_start_intersect(
&mut state.move_state,&mut state.body,&mut state.mode_state,&mut state.touching,
mode,
&mut state.run,&data.models,&data.hitbox_mesh,&data.bvh,&state.style,&state.camera,&state.input_state,
data.models.intersect_attr(intersect.model_id),
intersect,
state.time,
),
}
}, },
PhysicsInternalInstruction::CollisionEnd(collision)=>match collision{ PhysicsInternalInstruction::CollisionEnd(collision)=>match collision{
Collision::Contact(contact)=>collision_end_contact(state,data,data.models.contact_attr(contact.model_id),contact), Collision::Contact(contact)=>collision_end_contact(
Collision::Intersect(intersect)=>collision_end_intersect(state,data,data.models.intersect_attr(intersect.model_id),intersect), &mut state.move_state,&mut state.body,&mut state.touching,&data.models,&data.hitbox_mesh,&state.style,&state.camera,&state.input_state,
data.models.contact_attr(contact.model_id),
contact
),
Collision::Intersect(intersect)=>collision_end_intersect(
&mut state.touching,
data.modes.get_mode(state.mode_state.get_mode_id()),
&mut state.run,
data.models.intersect_attr(intersect.model_id),
intersect,
state.time
),
}, },
PhysicsInternalInstruction::StrafeTick=>{ PhysicsInternalInstruction::StrafeTick=>{
//TODO make this less huge //TODO make this less huge
@ -1667,14 +1841,15 @@ fn atomic_input_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:TimedI
}, },
PhysicsInputInstruction::Restart=>{ PhysicsInputInstruction::Restart=>{
//teleport to start zone //teleport to start zone
let spawn_point=data.modes.get_mode(state.mode_state.get_mode_id()).and_then(|mode| let mode=data.modes.get_mode(state.mode_state.get_mode_id());
let spawn_point=mode.and_then(|mode|
//TODO: spawn at the bottom of the start zone plus the hitbox size //TODO: spawn at the bottom of the start zone plus the hitbox size
//TODO: set camera andles to face the same way as the start zone //TODO: set camera andles to face the same way as the start zone
data.models.get_model_transform(mode.get_start().into()).map(|transform| data.models.get_model_transform(mode.get_start().into()).map(|transform|
transform.vertex.translation transform.vertex.translation
) )
).unwrap_or(Planar64Vec3::ZERO); ).unwrap_or(Planar64Vec3::ZERO);
set_position(&mut state.body,&mut state.touching,spawn_point); set_position(spawn_point,&mut state.move_state,&mut state.body,&mut state.touching,&mut state.run,&mut state.mode_state,mode,&data.models,&data.hitbox_mesh,&data.bvh,&state.style,&state.camera,&state.input_state,state.time);
set_velocity(&mut state.body,&state.touching,&data.models,&data.hitbox_mesh,Planar64Vec3::ZERO); set_velocity(&mut state.body,&state.touching,&data.models,&data.hitbox_mesh,Planar64Vec3::ZERO);
state.set_move_state(data,MoveState::Air); state.set_move_state(data,MoveState::Air);
b_refresh_walk_target=false; b_refresh_walk_target=false;
@ -1682,7 +1857,14 @@ fn atomic_input_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:TimedI
PhysicsInputInstruction::Spawn(mode_id,stage_id)=>{ PhysicsInputInstruction::Spawn(mode_id,stage_id)=>{
//spawn at a particular stage //spawn at a particular stage
if let Some(mode)=data.modes.get_mode(mode_id){ if let Some(mode)=data.modes.get_mode(mode_id){
teleport_to_spawn(&mut state.body,&mut state.touching,&state.style,&data.hitbox_mesh,mode,&data.models,stage_id); if let Some(stage)=mode.get_stage(stage_id){
let _=teleport_to_spawn(
stage,
&mut state.move_state,&mut state.body,&mut state.touching,&mut state.run,&mut state.mode_state,
mode,
&data.models,&data.hitbox_mesh,&data.bvh,&state.style,&state.camera,&state.input_state,state.time
);
}
} }
b_refresh_walk_target=false; b_refresh_walk_target=false;
}, },
@ -1705,6 +1887,7 @@ fn atomic_input_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:TimedI
if b_refresh_walk_target{ if b_refresh_walk_target{
state.apply_input_and_body(data); state.apply_input_and_body(data);
state.cull_velocity(data,state.body.velocity); state.cull_velocity(data,state.body.velocity);
//also check if accelerating away from surface
} }
} }

@ -180,8 +180,13 @@ impl MouseInterpolator{
self.empty_queue(); self.empty_queue();
} }
} }
pub fn get_render_stuff(&self,time:Time)->(crate::physics::PhysicsOutputState,Time,glam::IVec2){ pub fn get_frame_state(&self,time:Time)->crate::graphics::FrameState{
(self.physics.output(),self.timer.time(time),self.physics.get_next_mouse().pos) crate::graphics::FrameState{
body:self.physics.camera_body(),
camera:self.physics.camera(),
run:self.physics.run(),
time:self.timer.time(time),
}
} }
pub fn change_map(&mut self,time:Time,map:&strafesnet_common::map::CompleteMap){ pub fn change_map(&mut self,time:Time,map:&strafesnet_common::map::CompleteMap){
//dump any pending interpolation state //dump any pending interpolation state
@ -221,8 +226,8 @@ pub fn new<'a>(
interpolator.handle_instruction(&ins); interpolator.handle_instruction(&ins);
match ins.instruction{ match ins.instruction{
Instruction::Render=>{ Instruction::Render=>{
let (physics_output,time,mouse_pos)=interpolator.get_render_stuff(ins.time); let frame_state=interpolator.get_frame_state(ins.time);
graphics_worker.send(crate::graphics_worker::Instruction::Render(physics_output,time,mouse_pos)).unwrap(); graphics_worker.send(crate::graphics_worker::Instruction::Render(frame_state)).unwrap();
}, },
Instruction::Resize(size)=>{ Instruction::Resize(size)=>{
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,interpolator.user_settings().clone())).unwrap(); graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,interpolator.user_settings().clone())).unwrap();