Compare commits
5 Commits
29ecf7edf6
...
ccd7b52176
Author | SHA1 | Date | |
---|---|---|---|
ccd7b52176 | |||
55435182d7 | |||
aa391965b7 | |||
c061916511 | |||
ef1f675d6b |
132
src/body.rs
132
src/body.rs
@ -5,6 +5,14 @@ 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 {
|
||||||
@ -13,6 +21,13 @@ 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>,
|
||||||
@ -170,41 +185,19 @@ 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, control_dir: glam::Vec3, controls: u32){
|
pub fn run(&mut self, time: TIME){
|
||||||
let target_tick = (time*self.strafe_tick_num/self.strafe_tick_den) as u32;
|
//prepare is ommitted - everything is done via instructions.
|
||||||
//the game code can run for 1 month before running out of ticks
|
while let Some(instruction) = self.next_instruction() {//collect
|
||||||
while self.tick<target_tick {
|
if time<instruction.time {
|
||||||
self.tick += 1;
|
break;
|
||||||
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
|
||||||
@ -216,15 +209,48 @@ 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>> {
|
||||||
@ -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.
|
//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 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)
|
//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
|
||||||
best.collect(Some(TimedInstruction{
|
collector.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() {
|
||||||
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!!)
|
//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 {
|
||||||
best.collect(self.predict_collision_start(model));
|
collector.collect(self.predict_collision_start(model));
|
||||||
}
|
}
|
||||||
if self.grounded {
|
if self.grounded {
|
||||||
//walk maintenance
|
//walk maintenance
|
||||||
best.collect(self.next_walk_instruction());
|
collector.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
|
||||||
best.collect(self.next_strafe_instruction());
|
collector.collect(self.next_strafe_instruction());
|
||||||
}
|
}
|
||||||
best.instruction()
|
collector.instruction()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsState {
|
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
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,3 +1,3 @@
|
|||||||
pub mod framework;
|
pub mod framework;
|
||||||
pub mod body;
|
pub mod body;
|
||||||
pub mod event;
|
pub mod instruction;
|
||||||
|
Loading…
Reference in New Issue
Block a user