Compare commits

..

5 Commits

Author SHA1 Message Date
ccd7b52176 wip: tickless physics 2023-09-08 20:14:18 -07:00
55435182d7 comments on implementing game mechanics 2023-09-08 20:13:30 -07:00
aa391965b7 rename best to collector 2023-09-08 20:12:58 -07:00
c061916511 rename 2023-09-08 17:45:29 -07:00
ef1f675d6b rename event.rs to instruction.rs 2023-09-08 17:34:27 -07:00
3 changed files with 92 additions and 42 deletions

View File

@ -5,6 +5,14 @@ pub enum PhysicsInstruction {
CollisionEnd(RelativeCollision),
StrafeTick,
Jump,
SetWalkTargetVelocity(glam::Vec3),
ReachWalkTargetVelocity,
// Water,
// Spawn(
// Option<SpawnId>,
// bool,//true = Trigger; false = teleport
// bool,//true = Force
// )
}
pub struct Body {
@ -13,6 +21,13 @@ pub struct Body {
pub time: TIME,//nanoseconds x xxxxD!
}
pub enum MoveRestriction {
Air,
Water,
Ground,
Ladder,//multiple ladders how
}
pub struct PhysicsState {
pub body: Body,
pub contacts: Vec<RelativeCollision>,
@ -170,41 +185,19 @@ pub type TIME = i64;
const CONTROL_JUMP:u32 = 0b01000000;//temp DATA NORMALIZATION!@#$
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*self.strafe_tick_num/self.strafe_tick_den) as u32;
//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 {
//PhysicsInstruction::WalkTargetReached
self.body.velocity=targetv;
}
pub fn run(&mut self, time: TIME){
//prepare is ommitted - everything is done via instructions.
while let Some(instruction) = self.next_instruction() {//collect
if time<instruction.time {
break;
}
//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
@ -216,15 +209,48 @@ impl PhysicsState {
fn next_strafe_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::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>> {
//check if you are accelerating towards a walk target velocity and create an instruction
return None;
}
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
}
fn predict_collision_start(&self,model:&Model) -> Option<TimedInstruction<PhysicsInstruction>> {
@ -236,36 +262,60 @@ 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.
fn next_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
//JUST POLLING!!! NO MUTATION
let mut best = crate::instruction::InstructionCollector::new();
let mut collector = crate::instruction::InstructionCollector::new();
//autohop (already pressing spacebar; the signal to begin trying to jump is different)
if self.grounded&&self.jump_trying {
//scroll will be implemented with InputInstruction::Jump(true) but it blocks setting self.jump_trying=true
best.collect(Some(TimedInstruction{
collector.collect(Some(TimedInstruction{
time:self.time,
instruction:PhysicsInstruction::Jump
}));
}
//check for collision stop instructions with curent contacts
for collision_data in self.contacts.iter() {
best.collect(self.predict_collision_end(self.models_cringe_clone.get(collision_data.model as usize).unwrap()));
collector.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!!)
for model in &self.models_cringe_clone {
best.collect(self.predict_collision_start(model));
collector.collect(self.predict_collision_start(model));
}
if self.grounded {
//walk maintenance
best.collect(self.next_walk_instruction());
collector.collect(self.next_walk_instruction());
}else{
//check to see when the next strafe tick is
best.collect(self.next_strafe_instruction());
collector.collect(self.next_strafe_instruction());
}
best.instruction()
collector.instruction()
}
}
impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsState {
fn process_instruction(&mut self, instruction:TimedInstruction<PhysicsInstruction>) {
//
fn process_instruction(&mut self, ins: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 body;
pub mod event;
pub mod instruction;