Compare commits

..

No commits in common. "ccd7b5217615abac14f4ce0a83557d65071a8c4f" and "29ecf7edf6227f3f70d1d8acbb7d156e28cc0477" have entirely different histories.

3 changed files with 42 additions and 92 deletions

View File

@ -5,14 +5,6 @@ pub enum PhysicsInstruction {
CollisionEnd(RelativeCollision), CollisionEnd(RelativeCollision),
StrafeTick, StrafeTick,
Jump, Jump,
SetWalkTargetVelocity(glam::Vec3),
ReachWalkTargetVelocity,
// Water,
// Spawn(
// Option<SpawnId>,
// bool,//true = Trigger; false = teleport
// bool,//true = Force
// )
} }
pub struct Body { pub struct Body {
@ -21,13 +13,6 @@ pub struct Body {
pub time: TIME,//nanoseconds x xxxxD! pub time: TIME,//nanoseconds x xxxxD!
} }
pub enum MoveRestriction {
Air,
Water,
Ground,
Ladder,//multiple ladders how
}
pub struct PhysicsState { pub struct PhysicsState {
pub body: Body, pub body: Body,
pub contacts: Vec<RelativeCollision>, pub contacts: Vec<RelativeCollision>,
@ -185,19 +170,41 @@ pub type TIME = i64;
const CONTROL_JUMP:u32 = 0b01000000;//temp DATA NORMALIZATION!@#$ const CONTROL_JUMP:u32 = 0b01000000;//temp DATA NORMALIZATION!@#$
impl PhysicsState { impl PhysicsState {
//delete this, we are tickless gamers //delete this, we are tickless gamers
pub fn run(&mut self, time: TIME){ pub fn run(&mut self, time: TIME, control_dir: glam::Vec3, controls: u32){
//prepare is ommitted - everything is done via instructions. let target_tick = (time*self.strafe_tick_num/self.strafe_tick_den) as u32;
while let Some(instruction) = self.next_instruction() {//collect //the game code can run for 1 month before running out of ticks
if time<instruction.time { while self.tick<target_tick {
break; 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 {
//PhysicsInstruction::WalkTargetReached
self.body.velocity=targetv;
}
} }
//advance
self.advance_time(instruction.time);
//process
self.process_instruction(instruction);
//write hash lol
} }
self.body.time=target_tick as TIME*self.strafe_tick_den/self.strafe_tick_num;
} }
//delete this //delete this
@ -209,48 +216,15 @@ impl PhysicsState {
fn next_strafe_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> { fn next_strafe_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
return Some(TimedInstruction{ return Some(TimedInstruction{
time:(self.time*self.strafe_tick_num/self.strafe_tick_den+1)*self.strafe_tick_den/self.strafe_tick_num, time:(self.time*self.strafe_tick_num/self.strafe_tick_den+1)*self.strafe_tick_den/self.strafe_tick_num,
//only poll the physics if there is a before and after mouse event
instruction:PhysicsInstruction::StrafeTick instruction:PhysicsInstruction::StrafeTick
}); });
} }
//state mutated on collision:
//Accelerator
//stair step-up
//state mutated on instruction
//change fly acceleration (fly_sustain)
//change fly velocity
//generic event emmiters
//PlatformStandTime
//walk/swim/air/ladder sounds
//VState?
//falling under the map
// fn next_respawn_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
// if self.body.position<self.world.min_y {
// return Some(TimedInstruction{
// time:self.time,
// instruction:PhysicsInstruction::Trigger(None)
// });
// }
// }
// fn next_water_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
// return Some(TimedInstruction{
// time:(self.time*self.strafe_tick_num/self.strafe_tick_den+1)*self.strafe_tick_den/self.strafe_tick_num,
// //only poll the physics if there is a before and after mouse event
// instruction:PhysicsInstruction::Water
// });
// }
fn next_walk_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> { fn next_walk_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
//check if you are accelerating towards a walk target velocity and create an instruction //check if you are accelerating towards a walk target velocity and create an instruction
return None; return None;
} }
fn predict_collision_end(&self,model:&Model) -> Option<TimedInstruction<PhysicsInstruction>> { fn predict_collision_end(&self,model:&Model) -> Option<TimedInstruction<PhysicsInstruction>> {
//must treat cancollide false objects differently: you may not exit through the same face you entered.
None None
} }
fn predict_collision_start(&self,model:&Model) -> Option<TimedInstruction<PhysicsInstruction>> { fn predict_collision_start(&self,model:&Model) -> Option<TimedInstruction<PhysicsInstruction>> {
@ -262,60 +236,36 @@ impl crate::instruction::InstructionEmitter<PhysicsInstruction> for PhysicsState
//this little next instruction function can cache its return value and invalidate the cached value by watching the State. //this little next instruction function can cache its return value and invalidate the cached value by watching the State.
fn next_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> { fn next_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
//JUST POLLING!!! NO MUTATION //JUST POLLING!!! NO MUTATION
let mut collector = crate::instruction::InstructionCollector::new(); let mut best = crate::instruction::InstructionCollector::new();
//autohop (already pressing spacebar; the signal to begin trying to jump is different) //autohop (already pressing spacebar; the signal to begin trying to jump is different)
if self.grounded&&self.jump_trying { if self.grounded&&self.jump_trying {
//scroll will be implemented with InputInstruction::Jump(true) but it blocks setting self.jump_trying=true //scroll will be implemented with InputInstruction::Jump(true) but it blocks setting self.jump_trying=true
collector.collect(Some(TimedInstruction{ best.collect(Some(TimedInstruction{
time:self.time, time:self.time,
instruction:PhysicsInstruction::Jump instruction:PhysicsInstruction::Jump
})); }));
} }
//check for collision stop instructions with curent contacts //check for collision stop instructions with curent contacts
for collision_data in self.contacts.iter() { for collision_data in self.contacts.iter() {
collector.collect(self.predict_collision_end(self.models_cringe_clone.get(collision_data.model as usize).unwrap())); best.collect(self.predict_collision_end(self.models_cringe_clone.get(collision_data.model as usize).unwrap()));
} }
//check for collision start instructions (against every part in the game with no optimization!!) //check for collision start instructions (against every part in the game with no optimization!!)
for model in &self.models_cringe_clone { for model in &self.models_cringe_clone {
collector.collect(self.predict_collision_start(model)); best.collect(self.predict_collision_start(model));
} }
if self.grounded { if self.grounded {
//walk maintenance //walk maintenance
collector.collect(self.next_walk_instruction()); best.collect(self.next_walk_instruction());
}else{ }else{
//check to see when the next strafe tick is //check to see when the next strafe tick is
collector.collect(self.next_strafe_instruction()); best.collect(self.next_strafe_instruction());
} }
collector.instruction() best.instruction()
} }
} }
impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsState { impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsState {
fn process_instruction(&mut self, ins:TimedInstruction<PhysicsInstruction>) { fn process_instruction(&mut self, instruction:TimedInstruction<PhysicsInstruction>) {
//mutate position and velocity and time //
self.body.advance_time(ins.time);//should this be in a separate function: self.advance_time?
match ins.instruction {
PhysicsInstruction::CollisionStart(_) => todo!(),
PhysicsInstruction::CollisionEnd(_) => todo!(),
PhysicsInstruction::StrafeTick => {
let control_dir=self.get_control_dir();//this respects your mouse interpolation settings
let d=self.body.velocity.dot(control_dir);
if d<self.mv {
self.body.velocity+=(self.mv-d)*control_dir;
}
}
PhysicsInstruction::Jump => {
self.grounded=false;//do I need this?
self.body.velocity+=glam::Vec3::new(0.0,0.715588/2.0*100.0,0.0);
}
PhysicsInstruction::ReachWalkTargetVelocity => {
//precisely set velocity
self.body.velocity=self.walk_target_velocity;
}
PhysicsInstruction::SetWalkTargetVelocity(v) => {
self.walk_target_velocity=v;
//calculate acceleration yada yada
},
}
} }
} }

View File

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