wip: ladders
This commit is contained in:
parent
2d2f0099ab
commit
c5ecf6b34a
132
src/physics.rs
132
src/physics.rs
@ -128,37 +128,37 @@ struct WalkState{
|
|||||||
state:WalkEnum,
|
state:WalkEnum,
|
||||||
}
|
}
|
||||||
impl WalkEnum{
|
impl WalkEnum{
|
||||||
//so many args bruh
|
//args going crazy
|
||||||
fn with_target_velocity(physics:&PhysicsState,mut velocity:Planar64Vec3,normal:&Planar64Vec3)->(WalkEnum,Planar64Vec3){
|
//(walk_enum,body.acceleration)=with_target_velocity();
|
||||||
physics.touching.constrain_velocity(&physics.models,&mut velocity);
|
fn with_target_velocity(touching:&TouchingState,body:&Body,style:&StyleModifiers,models:&Vec<ModelPhysics>,mut velocity:Planar64Vec3,normal:&Planar64Vec3)->(WalkEnum,Planar64Vec3){
|
||||||
let mut target_diff=velocity-physics.body.velocity;
|
touching.constrain_velocity(models,&mut velocity);
|
||||||
|
let mut target_diff=velocity-body.velocity;
|
||||||
//remove normal component
|
//remove normal component
|
||||||
target_diff-=normal.clone()*(normal.dot(target_diff)/normal.dot(normal.clone()));
|
target_diff-=normal.clone()*(normal.dot(target_diff)/normal.dot(normal.clone()));
|
||||||
if target_diff==Planar64Vec3::ZERO{
|
if target_diff==Planar64Vec3::ZERO{
|
||||||
let mut a=Planar64Vec3::ZERO;
|
let mut a=Planar64Vec3::ZERO;
|
||||||
physics.touching.constrain_acceleration(&physics.models,&mut a);
|
touching.constrain_acceleration(models,&mut a);
|
||||||
(WalkEnum::Reached,a)
|
(WalkEnum::Reached,a)
|
||||||
}else{
|
}else{
|
||||||
//normal friction acceleration is clippedAcceleration.dot(normal)*friction
|
//normal friction acceleration is clippedAcceleration.dot(normal)*friction
|
||||||
let accel=physics.style.walk_accel.min(physics.style.gravity.dot(Planar64Vec3::NEG_Y)*physics.style.friction);
|
let accel=style.walk_accel.min(style.gravity.dot(Planar64Vec3::NEG_Y)*style.friction);
|
||||||
let time_delta=target_diff.length()/accel;
|
let time_delta=target_diff.length()/accel;
|
||||||
let mut a=target_diff.with_length(accel);
|
let mut a=target_diff.with_length(accel);
|
||||||
physics.touching.constrain_acceleration(&physics.models,&mut a);
|
touching.constrain_acceleration(models,&mut a);
|
||||||
(WalkEnum::Transient(WalkTarget{velocity,time:physics.body.time+Time::from(time_delta)}),a)
|
(WalkEnum::Transient(WalkTarget{velocity,time:body.time+Time::from(time_delta)}),a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//(walk_enum,body.acceleration)=with_target_velocity();
|
|
||||||
}
|
}
|
||||||
impl WalkState{
|
impl WalkState{
|
||||||
fn ground(physics:&PhysicsState,mut velocity:Planar64Vec3)->(Self,Planar64Vec3){
|
fn ground(touching:&TouchingState,body:&Body,style:&StyleModifiers,models:&Vec<ModelPhysics>,mut velocity:Planar64Vec3)->(Self,Planar64Vec3){
|
||||||
let (walk_enum,a)=WalkEnum::with_target_velocity(physics,velocity,&Planar64Vec3::Y);
|
let (walk_enum,a)=WalkEnum::with_target_velocity(touching,body,style,models,velocity,&Planar64Vec3::Y);
|
||||||
(Self{
|
(Self{
|
||||||
state:walk_enum,
|
state:walk_enum,
|
||||||
normal:Planar64Vec3::Y,
|
normal:Planar64Vec3::Y,
|
||||||
},a)
|
},a)
|
||||||
}
|
}
|
||||||
fn ladder(physics:&PhysicsState,mut velocity:Planar64Vec3,normal:&Planar64Vec3)->(Self,Planar64Vec3){
|
fn ladder(touching:&TouchingState,body:&Body,style:&StyleModifiers,models:&Vec<ModelPhysics>,mut velocity:Planar64Vec3,normal:&Planar64Vec3)->(Self,Planar64Vec3){
|
||||||
let (walk_enum,a)=WalkEnum::with_target_velocity(physics,velocity,normal);
|
let (walk_enum,a)=WalkEnum::with_target_velocity(touching,body,style,models,velocity,normal);
|
||||||
(Self{
|
(Self{
|
||||||
state:walk_enum,
|
state:walk_enum,
|
||||||
normal:normal.clone(),
|
normal:normal.clone(),
|
||||||
@ -321,6 +321,17 @@ impl StyleModifiers{
|
|||||||
fn get_jump_power(&self)->Planar64{
|
fn get_jump_power(&self)->Planar64{
|
||||||
Planar64::int(715588)/(2*1000000/100)
|
Planar64::int(715588)/(2*1000000/100)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_walk_target_velocity(&self,camera:&PhysicsCamera,controls:u32,next_mouse:&MouseState,time:Time)->Planar64Vec3{
|
||||||
|
let camera_mat=camera.simulate_move_rotation_y(camera.mouse.lerp(&next_mouse,time).x);
|
||||||
|
let control_dir=camera_mat*self.get_control_dir(controls);
|
||||||
|
control_dir*self.walkspeed
|
||||||
|
}
|
||||||
|
fn get_propulsion_target_velocity(&self,camera:&PhysicsCamera,controls:u32,next_mouse:&MouseState,time:Time)->Planar64Vec3{
|
||||||
|
let camera_mat=camera.simulate_move_rotation(camera.mouse.lerp(&next_mouse,time));
|
||||||
|
let control_dir=camera_mat*self.get_control_dir(controls);
|
||||||
|
control_dir*self.walkspeed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum MoveState{
|
enum MoveState{
|
||||||
@ -793,39 +804,24 @@ impl PhysicsState {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
fn refresh_walk_target(&mut self){
|
fn refresh_walk_target(&mut self){
|
||||||
//calculate acceleration yada yada
|
|
||||||
match &mut self.move_state{
|
match &mut self.move_state{
|
||||||
MoveState::Walk(walk_state)|MoveState::Ladder(walk_state)=>{
|
|
||||||
let mut v=walk_state.target_velocity;
|
|
||||||
self.touching.constrain_velocity(&self.models,&mut v);
|
|
||||||
let mut target_diff=v-self.body.velocity;
|
|
||||||
//remove normal component
|
|
||||||
target_diff-=walk_state.normal*(walk_state.normal.dot(target_diff)/walk_state.normal.dot(walk_state.normal));
|
|
||||||
if target_diff==Planar64Vec3::ZERO{
|
|
||||||
let mut a=Planar64Vec3::ZERO;
|
|
||||||
self.touching.constrain_acceleration(&self.models,&mut a);
|
|
||||||
self.body.acceleration=a;
|
|
||||||
walk_state.state=WalkEnum::Reached;
|
|
||||||
}else{
|
|
||||||
//normal friction acceleration is clippedAcceleration.dot(normal)*friction
|
|
||||||
let accel=self.style.walk_accel.min(self.style.gravity.dot(Planar64Vec3::NEG_Y)*self.style.friction);
|
|
||||||
let time_delta=target_diff.length()/accel;
|
|
||||||
let mut a=target_diff.with_length(accel);
|
|
||||||
self.touching.constrain_acceleration(&self.models,&mut a);
|
|
||||||
self.body.acceleration=a;
|
|
||||||
walk_state.target_time=self.body.time+Time::from(time_delta);
|
|
||||||
walk_state.state=WalkEnum::Transient;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
MoveState::Air|MoveState::Water=>(),
|
MoveState::Air|MoveState::Water=>(),
|
||||||
|
MoveState::Walk(WalkState{normal,state})=>{
|
||||||
|
let n=normal;
|
||||||
|
(*state,self.body.acceleration)=WalkEnum::with_target_velocity(&self.touching,&self.body,&self.style,&self.models,self.style.get_walk_target_velocity(&self.camera,self.controls,&self.next_mouse,self.time),&n);
|
||||||
|
},
|
||||||
|
MoveState::Ladder(WalkState{normal,state})=>{
|
||||||
|
let n=normal;
|
||||||
|
(*state,self.body.acceleration)=WalkEnum::with_target_velocity(&self.touching,&self.body,&self.style,&self.models,self.style.get_propulsion_target_velocity(&self.camera,self.controls,&self.next_mouse,self.time),&n);
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn next_move_instruction(&self)->Option<TimedInstruction<PhysicsInstruction>>{
|
fn next_move_instruction(&self)->Option<TimedInstruction<PhysicsInstruction>>{
|
||||||
//check if you have a valid walk state and create an instruction
|
//check if you have a valid walk state and create an instruction
|
||||||
match &self.move_state{
|
match &self.move_state{
|
||||||
MoveState::Walk(walk_state)|MoveState::Ladder(walk_state)=>match &walk_state.state{
|
MoveState::Walk(walk_state)|MoveState::Ladder(walk_state)=>match &walk_state.state{
|
||||||
WalkEnum::Transient=>Some(TimedInstruction{
|
WalkEnum::Transient(walk_target)=>Some(TimedInstruction{
|
||||||
time:walk_state.target_time,
|
time:walk_target.time,
|
||||||
instruction:PhysicsInstruction::ReachWalkTargetVelocity
|
instruction:PhysicsInstruction::ReachWalkTargetVelocity
|
||||||
}),
|
}),
|
||||||
WalkEnum::Reached=>None,
|
WalkEnum::Reached=>None,
|
||||||
@ -1167,10 +1163,20 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
|
|||||||
PhysicsCollisionAttributes::Contact{contacting,general}=>{
|
PhysicsCollisionAttributes::Contact{contacting,general}=>{
|
||||||
match &contacting.contact_behaviour{
|
match &contacting.contact_behaviour{
|
||||||
Some(crate::model::ContactingBehaviour::Surf)=>println!("I'm surfing!"),
|
Some(crate::model::ContactingBehaviour::Surf)=>println!("I'm surfing!"),
|
||||||
|
Some(crate::model::ContactingBehaviour::Ladder(contacting_ladder))=>{
|
||||||
|
if contacting_ladder.sticky{
|
||||||
|
//kill v
|
||||||
|
self.body.velocity=Planar64Vec3::ZERO;//model.velocity
|
||||||
|
}
|
||||||
|
//ladder walkstate
|
||||||
|
let (walk_state,a)=WalkState::ladder(&self.touching,&self.body,&self.style,&self.models,self.style.get_propulsion_target_velocity(&self.camera,self.controls,&self.next_mouse,self.time),&c.normal(&self.models));
|
||||||
|
self.move_state=MoveState::Ladder(walk_state);
|
||||||
|
self.body.acceleration=a;
|
||||||
|
}
|
||||||
None=>match &c.face {
|
None=>match &c.face {
|
||||||
TreyMeshFace::Top => {
|
TreyMeshFace::Top => {
|
||||||
//ground
|
//ground
|
||||||
let (walk_state,a)=WalkState::ground(&self,self.get_walk_target());
|
let (walk_state,a)=WalkState::ground(&self.touching,&self.body,&self.style,&self.models,self.style.get_walk_target_velocity(&self.camera,self.controls,&self.next_mouse,self.time));
|
||||||
self.move_state=MoveState::Walk(walk_state);
|
self.move_state=MoveState::Walk(walk_state);
|
||||||
self.body.acceleration=a;
|
self.body.acceleration=a;
|
||||||
},
|
},
|
||||||
@ -1272,11 +1278,11 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
|
|||||||
//check ground
|
//check ground
|
||||||
match &c.face {
|
match &c.face {
|
||||||
TreyMeshFace::Top => {
|
TreyMeshFace::Top => {
|
||||||
|
//TODO: make this more advanced checking contacts
|
||||||
self.move_state=MoveState::Air;
|
self.move_state=MoveState::Air;
|
||||||
},
|
},
|
||||||
_ => (),
|
_=>self.refresh_walk_target(),
|
||||||
}
|
}
|
||||||
self.refresh_walk_target();
|
|
||||||
},
|
},
|
||||||
PhysicsCollisionAttributes::Intersect{intersecting,general}=>{
|
PhysicsCollisionAttributes::Intersect{intersecting,general}=>{
|
||||||
self.touching.remove_intersect(c.model);
|
self.touching.remove_intersect(c.model);
|
||||||
@ -1297,20 +1303,24 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
|
|||||||
match &mut self.move_state{
|
match &mut self.move_state{
|
||||||
MoveState::Air|MoveState::Water=>(),
|
MoveState::Air|MoveState::Water=>(),
|
||||||
MoveState::Walk(walk_state)|MoveState::Ladder(walk_state)=>{
|
MoveState::Walk(walk_state)|MoveState::Ladder(walk_state)=>{
|
||||||
//precisely set velocity
|
match &mut walk_state.state{
|
||||||
let mut a=self.style.gravity;
|
WalkEnum::Reached=>(),
|
||||||
self.touching.constrain_acceleration(&self.models,&mut a);
|
WalkEnum::Transient(walk_target)=>{
|
||||||
self.body.acceleration=a;
|
//precisely set velocity
|
||||||
let mut v=walk_state.target_velocity;
|
let mut a=self.style.gravity;
|
||||||
self.touching.constrain_velocity(&self.models,&mut v);
|
self.touching.constrain_acceleration(&self.models,&mut a);
|
||||||
self.body.velocity=v;
|
self.body.acceleration=a;
|
||||||
walk_state.state=WalkEnum::Reached;
|
let mut v=walk_target.velocity;
|
||||||
|
self.touching.constrain_velocity(&self.models,&mut v);
|
||||||
|
self.body.velocity=v;
|
||||||
|
walk_state.state=WalkEnum::Reached;
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
PhysicsInstruction::Input(input_instruction) => {
|
PhysicsInstruction::Input(input_instruction) => {
|
||||||
let mut refresh_walk_target=true;
|
let mut refresh_walk_target=true;
|
||||||
let mut refresh_walk_target_velocity=true;
|
|
||||||
match input_instruction{
|
match input_instruction{
|
||||||
PhysicsInputInstruction::SetNextMouse(m) => {
|
PhysicsInputInstruction::SetNextMouse(m) => {
|
||||||
self.camera.move_mouse(self.next_mouse.pos);
|
self.camera.move_mouse(self.next_mouse.pos);
|
||||||
@ -1329,7 +1339,7 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
|
|||||||
PhysicsInputInstruction::SetJump(s) => {
|
PhysicsInputInstruction::SetJump(s) => {
|
||||||
self.set_control(StyleModifiers::CONTROL_JUMP,s);
|
self.set_control(StyleModifiers::CONTROL_JUMP,s);
|
||||||
self.jump();
|
self.jump();
|
||||||
refresh_walk_target_velocity=false;
|
refresh_walk_target=false;
|
||||||
},
|
},
|
||||||
PhysicsInputInstruction::SetZoom(s) => {
|
PhysicsInputInstruction::SetZoom(s) => {
|
||||||
self.set_control(StyleModifiers::CONTROL_ZOOM,s);
|
self.set_control(StyleModifiers::CONTROL_ZOOM,s);
|
||||||
@ -1348,25 +1358,7 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
|
|||||||
PhysicsInputInstruction::Idle => {refresh_walk_target=false;},//literally idle!
|
PhysicsInputInstruction::Idle => {refresh_walk_target=false;},//literally idle!
|
||||||
}
|
}
|
||||||
if refresh_walk_target{
|
if refresh_walk_target{
|
||||||
//calculate walk target velocity
|
self.refresh_walk_target();
|
||||||
if refresh_walk_target_velocity{
|
|
||||||
match &mut self.move_state{
|
|
||||||
MoveState::Walk(walk_state)=>{
|
|
||||||
let camera_mat=self.camera.simulate_move_rotation_y(self.camera.mouse.lerp(&self.next_mouse,self.time).x);
|
|
||||||
let control_dir=camera_mat*self.style.get_control_dir(self.controls);
|
|
||||||
walk_state.target_velocity=control_dir*self.style.walkspeed;
|
|
||||||
self.refresh_walk_target();
|
|
||||||
},
|
|
||||||
MoveState::Ladder(walk_state)=>{
|
|
||||||
let camera_mat=self.camera.simulate_move_rotation(self.camera.mouse.lerp(&self.next_mouse,self.time));
|
|
||||||
let control_dir=camera_mat*self.style.get_control_dir(self.controls);
|
|
||||||
walk_state.target_velocity=control_dir*self.style.walkspeed;
|
|
||||||
self.refresh_walk_target();
|
|
||||||
},
|
|
||||||
MoveState::Water=>(),
|
|
||||||
MoveState::Air=>(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user