2023-09-18 20:20:51 +00:00
|
|
|
use crate::{instruction::{InstructionEmitter, InstructionConsumer, TimedInstruction}, zeroes::zeroes2};
|
2023-09-08 19:03:32 +00:00
|
|
|
|
2023-09-27 09:12:20 +00:00
|
|
|
use crate::integer::{Time,Planar64,Planar64Vec3,Planar64Mat3,Angle32,Ratio64,Ratio64Vec2};
|
|
|
|
|
2023-09-19 01:34:48 +00:00
|
|
|
#[derive(Debug)]
|
2023-09-09 00:34:26 +00:00
|
|
|
pub enum PhysicsInstruction {
|
2023-09-09 00:00:50 +00:00
|
|
|
CollisionStart(RelativeCollision),
|
|
|
|
CollisionEnd(RelativeCollision),
|
|
|
|
StrafeTick,
|
2023-09-09 03:14:18 +00:00
|
|
|
ReachWalkTargetVelocity,
|
2023-09-09 03:13:30 +00:00
|
|
|
// Water,
|
|
|
|
// Spawn(
|
|
|
|
// Option<SpawnId>,
|
|
|
|
// bool,//true = Trigger; false = teleport
|
|
|
|
// bool,//true = Force
|
|
|
|
// )
|
2023-09-20 00:53:29 +00:00
|
|
|
//InputInstructions conditionally activate RefreshWalkTarget (by doing what SetWalkTargetVelocity used to do and then flagging it)
|
2023-10-05 03:04:04 +00:00
|
|
|
Input(PhysicsInputInstruction),
|
|
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum PhysicsInputInstruction {
|
|
|
|
ReplaceMouse(MouseState,MouseState),
|
|
|
|
SetNextMouse(MouseState),
|
|
|
|
SetMoveRight(bool),
|
|
|
|
SetMoveUp(bool),
|
2023-10-10 22:33:32 +00:00
|
|
|
SetMoveBack(bool),
|
|
|
|
SetMoveLeft(bool),
|
2023-10-05 03:04:04 +00:00
|
|
|
SetMoveDown(bool),
|
2023-10-10 22:33:32 +00:00
|
|
|
SetMoveForward(bool),
|
2023-10-05 03:04:04 +00:00
|
|
|
SetJump(bool),
|
|
|
|
SetZoom(bool),
|
|
|
|
Reset,
|
|
|
|
Idle,
|
2023-10-25 03:22:30 +00:00
|
|
|
//Idle: there were no input events, but the simulation is safe to advance to this timestep
|
|
|
|
//for interpolation / networking / playback reasons, most playback heads will always want
|
|
|
|
//to be 1 instruction ahead to generate the next state for interpolation.
|
2023-09-20 00:53:29 +00:00
|
|
|
}
|
2023-10-20 01:42:00 +00:00
|
|
|
|
2023-10-25 03:22:30 +00:00
|
|
|
#[derive(Clone,Hash,Default)]
|
2023-09-08 18:33:16 +00:00
|
|
|
pub struct Body {
|
2023-09-27 09:12:20 +00:00
|
|
|
position: Planar64Vec3,//I64 where 2^32 = 1 u
|
|
|
|
velocity: Planar64Vec3,//I64 where 2^32 = 1 u/s
|
|
|
|
acceleration: Planar64Vec3,//I64 where 2^32 = 1 u/s/s
|
|
|
|
time:Time,//nanoseconds x xxxxD!
|
2023-09-19 04:10:07 +00:00
|
|
|
}
|
2023-09-09 03:14:18 +00:00
|
|
|
|
2023-10-03 05:45:48 +00:00
|
|
|
//hey dumbass just use a delta
|
2023-10-05 03:04:04 +00:00
|
|
|
#[derive(Clone,Debug)]
|
|
|
|
pub struct MouseState {
|
|
|
|
pub pos: glam::IVec2,
|
2023-09-27 09:12:20 +00:00
|
|
|
pub time:Time,
|
2023-09-10 20:24:47 +00:00
|
|
|
}
|
2023-10-05 03:04:04 +00:00
|
|
|
impl Default for MouseState{
|
|
|
|
fn default() -> Self {
|
2023-09-20 00:53:29 +00:00
|
|
|
Self {
|
2023-09-27 09:12:20 +00:00
|
|
|
time:Time::ZERO,
|
2023-10-05 03:04:04 +00:00
|
|
|
pos:glam::IVec2::ZERO,
|
2023-09-20 00:53:29 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-05 03:04:04 +00:00
|
|
|
}
|
|
|
|
impl MouseState {
|
2023-09-27 09:12:20 +00:00
|
|
|
pub fn lerp(&self,target:&MouseState,time:Time)->glam::IVec2 {
|
2023-10-05 03:04:04 +00:00
|
|
|
let m0=self.pos.as_i64vec2();
|
|
|
|
let m1=target.pos.as_i64vec2();
|
|
|
|
//these are deltas
|
2023-09-27 09:12:20 +00:00
|
|
|
let t1t=(target.time-time).nanos();
|
|
|
|
let tt0=(time-self.time).nanos();
|
|
|
|
let dt=(target.time-self.time).nanos();
|
2023-10-05 03:04:04 +00:00
|
|
|
((m0*t1t+m1*tt0)/dt).as_ivec2()
|
2023-09-10 20:24:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-17 07:17:54 +00:00
|
|
|
enum WalkEnum{
|
2023-09-19 06:36:14 +00:00
|
|
|
Reached,
|
2023-10-17 07:17:54 +00:00
|
|
|
Transient(WalkTarget),
|
2023-09-19 06:36:14 +00:00
|
|
|
}
|
2023-10-17 07:17:54 +00:00
|
|
|
struct WalkTarget{
|
|
|
|
velocity:Planar64Vec3,
|
|
|
|
time:Time,
|
2023-09-19 04:10:07 +00:00
|
|
|
}
|
2023-10-17 07:17:54 +00:00
|
|
|
struct WalkState{
|
|
|
|
normal:Planar64Vec3,
|
|
|
|
state:WalkEnum,
|
|
|
|
}
|
|
|
|
impl WalkEnum{
|
|
|
|
//args going crazy
|
|
|
|
//(walk_enum,body.acceleration)=with_target_velocity();
|
2023-10-18 23:21:06 +00:00
|
|
|
fn with_target_velocity(touching:&TouchingState,body:&Body,style:&StyleModifiers,models:&PhysicsModels,mut velocity:Planar64Vec3,normal:&Planar64Vec3)->(WalkEnum,Planar64Vec3){
|
2023-10-17 07:17:54 +00:00
|
|
|
touching.constrain_velocity(models,&mut velocity);
|
|
|
|
let mut target_diff=velocity-body.velocity;
|
|
|
|
//remove normal component
|
|
|
|
target_diff-=normal.clone()*(normal.dot(target_diff)/normal.dot(normal.clone()));
|
|
|
|
if target_diff==Planar64Vec3::ZERO{
|
|
|
|
let mut a=Planar64Vec3::ZERO;
|
|
|
|
touching.constrain_acceleration(models,&mut a);
|
|
|
|
(WalkEnum::Reached,a)
|
|
|
|
}else{
|
|
|
|
//normal friction acceleration is clippedAcceleration.dot(normal)*friction
|
2023-10-17 23:18:55 +00:00
|
|
|
let diff_len=target_diff.length();
|
|
|
|
let friction=if diff_len<style.walk_speed{
|
|
|
|
style.static_friction
|
|
|
|
}else{
|
|
|
|
style.kinetic_friction
|
|
|
|
};
|
|
|
|
let accel=style.walk_accel.min(style.gravity.dot(Planar64Vec3::NEG_Y)*friction);
|
|
|
|
let time_delta=diff_len/accel;
|
2023-10-17 07:17:54 +00:00
|
|
|
let mut a=target_diff.with_length(accel);
|
|
|
|
touching.constrain_acceleration(models,&mut a);
|
|
|
|
(WalkEnum::Transient(WalkTarget{velocity,time:body.time+Time::from(time_delta)}),a)
|
2023-09-19 04:10:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-17 07:17:54 +00:00
|
|
|
impl WalkState{
|
2023-10-18 23:21:06 +00:00
|
|
|
fn ground(touching:&TouchingState,body:&Body,style:&StyleModifiers,models:&PhysicsModels,velocity:Planar64Vec3)->(Self,Planar64Vec3){
|
2023-10-17 07:17:54 +00:00
|
|
|
let (walk_enum,a)=WalkEnum::with_target_velocity(touching,body,style,models,velocity,&Planar64Vec3::Y);
|
|
|
|
(Self{
|
|
|
|
state:walk_enum,
|
|
|
|
normal:Planar64Vec3::Y,
|
|
|
|
},a)
|
|
|
|
}
|
2023-10-18 23:21:06 +00:00
|
|
|
fn ladder(touching:&TouchingState,body:&Body,style:&StyleModifiers,models:&PhysicsModels,velocity:Planar64Vec3,normal:&Planar64Vec3)->(Self,Planar64Vec3){
|
2023-10-17 07:17:54 +00:00
|
|
|
let (walk_enum,a)=WalkEnum::with_target_velocity(touching,body,style,models,velocity,normal);
|
|
|
|
(Self{
|
|
|
|
state:walk_enum,
|
|
|
|
normal:normal.clone(),
|
|
|
|
},a)
|
|
|
|
}
|
|
|
|
}
|
2023-09-19 04:10:07 +00:00
|
|
|
|
2023-10-18 23:18:17 +00:00
|
|
|
struct Modes{
|
|
|
|
modes:Vec<crate::model::ModeDescription>,
|
|
|
|
mode_from_mode_id:std::collections::HashMap::<u32,usize>,
|
|
|
|
}
|
|
|
|
impl Modes{
|
|
|
|
fn clear(&mut self){
|
|
|
|
self.modes.clear();
|
|
|
|
self.mode_from_mode_id.clear();
|
|
|
|
}
|
|
|
|
fn get_mode(&self,mode_id:u32)->Option<&crate::model::ModeDescription>{
|
|
|
|
self.modes.get(*self.mode_from_mode_id.get(&mode_id)?)
|
|
|
|
}
|
|
|
|
fn insert(&mut self,temp_map_mode_id:u32,mode:crate::model::ModeDescription){
|
|
|
|
let mode_id=self.modes.len();
|
|
|
|
self.mode_from_mode_id.insert(temp_map_mode_id,mode_id);
|
|
|
|
self.modes.push(mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Default for Modes{
|
|
|
|
fn default() -> Self {
|
|
|
|
Self{
|
|
|
|
modes:Vec::new(),
|
|
|
|
mode_from_mode_id:std::collections::HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
|
2023-10-18 23:21:06 +00:00
|
|
|
struct PhysicsModels{
|
|
|
|
models:Vec<ModelPhysics>,
|
|
|
|
model_id_from_wormhole_id:std::collections::HashMap::<u32,usize>,
|
|
|
|
}
|
|
|
|
impl PhysicsModels{
|
|
|
|
fn clear(&mut self){
|
|
|
|
self.models.clear();
|
|
|
|
self.model_id_from_wormhole_id.clear();
|
|
|
|
}
|
|
|
|
fn get(&self,i:usize)->Option<&ModelPhysics>{
|
|
|
|
self.models.get(i)
|
|
|
|
}
|
|
|
|
fn get_wormhole_model(&self,wormhole_id:u32)->Option<&ModelPhysics>{
|
|
|
|
self.models.get(*self.model_id_from_wormhole_id.get(&wormhole_id)?)
|
|
|
|
}
|
|
|
|
fn push(&mut self,model:ModelPhysics)->usize{
|
|
|
|
let model_id=self.models.len();
|
|
|
|
self.models.push(model);
|
|
|
|
model_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Default for PhysicsModels{
|
|
|
|
fn default() -> Self {
|
|
|
|
Self{
|
|
|
|
models:Vec::new(),
|
|
|
|
model_id_from_wormhole_id:std::collections::HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-05 03:04:04 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct PhysicsCamera {
|
2023-09-27 09:12:20 +00:00
|
|
|
offset: Planar64Vec3,
|
|
|
|
//punch: Planar64Vec3,
|
|
|
|
//punch_velocity: Planar64Vec3,
|
|
|
|
sensitivity:Ratio64Vec2,//dots to Angle32 ratios
|
|
|
|
mouse:MouseState,//last seen absolute mouse pos
|
|
|
|
clamped_mouse_pos:glam::IVec2,//angles are calculated from this cumulative value
|
|
|
|
angle_pitch_lower_limit:Angle32,
|
|
|
|
angle_pitch_upper_limit:Angle32,
|
|
|
|
//angle limits could be an enum + struct that defines whether it's limited and selects clamp or wrap depending
|
|
|
|
// enum AngleLimit{
|
|
|
|
// Unlimited,
|
|
|
|
// Limited{lower:Angle32,upper:Angle32},
|
|
|
|
// }
|
|
|
|
//pitch_limit:AngleLimit,
|
|
|
|
//yaw_limit:AngleLimit,
|
2023-09-20 00:53:29 +00:00
|
|
|
}
|
|
|
|
|
2023-10-05 03:04:04 +00:00
|
|
|
impl PhysicsCamera {
|
2023-09-27 09:12:20 +00:00
|
|
|
pub fn from_offset(offset:Planar64Vec3) -> Self {
|
2023-09-20 00:53:29 +00:00
|
|
|
Self{
|
2023-10-04 21:01:06 +00:00
|
|
|
offset,
|
2023-09-27 09:12:20 +00:00
|
|
|
sensitivity:Ratio64Vec2::ONE*200_000,
|
|
|
|
mouse:MouseState::default(),//t=0 does not cause divide by zero because it's immediately replaced
|
|
|
|
clamped_mouse_pos:glam::IVec2::ZERO,
|
|
|
|
angle_pitch_lower_limit:-Angle32::FRAC_PI_2,
|
|
|
|
angle_pitch_upper_limit:Angle32::FRAC_PI_2,
|
2023-09-20 00:53:29 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
pub fn move_mouse(&mut self,mouse_pos:glam::IVec2){
|
|
|
|
let mut unclamped_mouse_pos=self.clamped_mouse_pos+mouse_pos-self.mouse.pos;
|
|
|
|
unclamped_mouse_pos.y=unclamped_mouse_pos.y.clamp(
|
|
|
|
self.sensitivity.y.rhs_div_int(self.angle_pitch_lower_limit.get() as i64) as i32,
|
|
|
|
self.sensitivity.y.rhs_div_int(self.angle_pitch_upper_limit.get() as i64) as i32,
|
|
|
|
);
|
|
|
|
self.clamped_mouse_pos=unclamped_mouse_pos;
|
2023-09-20 00:53:29 +00:00
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
pub fn simulate_move_angles(&self,mouse_pos:glam::IVec2)->glam::Vec2 {
|
|
|
|
let a=-self.sensitivity.mul_int((mouse_pos-self.mouse.pos+self.clamped_mouse_pos).as_i64vec2());
|
|
|
|
let ax=Angle32::wrap_from_i64(a.x);
|
|
|
|
let ay=Angle32::clamp_from_i64(a.y)
|
|
|
|
//clamp to actual vertical cam limit
|
|
|
|
.clamp(self.angle_pitch_lower_limit,self.angle_pitch_upper_limit);
|
|
|
|
return glam::vec2(ax.into(),ay.into());
|
|
|
|
}
|
2023-10-17 06:20:38 +00:00
|
|
|
fn simulate_move_rotation(&self,mouse_pos:glam::IVec2)->Planar64Mat3{
|
|
|
|
let a=-self.sensitivity.mul_int((mouse_pos-self.mouse.pos+self.clamped_mouse_pos).as_i64vec2());
|
|
|
|
let ax=Angle32::wrap_from_i64(a.x);
|
|
|
|
let ay=Angle32::clamp_from_i64(a.y)
|
|
|
|
//clamp to actual vertical cam limit
|
|
|
|
.clamp(self.angle_pitch_lower_limit,self.angle_pitch_upper_limit);
|
|
|
|
Planar64Mat3::from_rotation_yx(ax,ay)
|
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
fn simulate_move_rotation_y(&self,mouse_pos_x:i32)->Planar64Mat3{
|
|
|
|
let ax=-self.sensitivity.x.mul_int((mouse_pos_x-self.mouse.pos.x+self.clamped_mouse_pos.x) as i64);
|
|
|
|
Planar64Mat3::from_rotation_y(Angle32::wrap_from_i64(ax))
|
2023-09-20 00:53:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 03:22:30 +00:00
|
|
|
impl std::default::Default for PhysicsCamera{
|
|
|
|
fn default()->Self{
|
|
|
|
Self{
|
|
|
|
offset:Planar64Vec3::ZERO,//TODO: delete this from PhysicsCamera, it should be GraphicsCamera only
|
|
|
|
sensitivity:Ratio64Vec2::ONE*200_000,
|
|
|
|
mouse:MouseState::default(),//t=0 does not cause divide by zero because it's immediately replaced
|
|
|
|
clamped_mouse_pos:glam::IVec2::ZERO,
|
|
|
|
angle_pitch_lower_limit:-Angle32::FRAC_PI_2,
|
|
|
|
angle_pitch_upper_limit:Angle32::FRAC_PI_2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 05:45:20 +00:00
|
|
|
pub struct GameMechanicsState{
|
2023-10-04 21:15:04 +00:00
|
|
|
pub stage_id:u32,
|
2023-10-03 05:45:20 +00:00
|
|
|
//jump_counts:HashMap<u32,u32>,
|
|
|
|
}
|
|
|
|
impl std::default::Default for GameMechanicsState{
|
|
|
|
fn default() -> Self {
|
|
|
|
Self{
|
2023-10-04 21:15:04 +00:00
|
|
|
stage_id:0,
|
2023-10-03 05:45:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-17 23:18:55 +00:00
|
|
|
struct WorldState{}
|
|
|
|
|
|
|
|
enum JumpCalculation{
|
|
|
|
Capped,//roblox
|
|
|
|
Energy,//new
|
|
|
|
Linear,//source
|
|
|
|
}
|
|
|
|
|
|
|
|
enum JumpImpulse{
|
|
|
|
FromTime(Time),//jump time is invariant across mass and gravity changes
|
|
|
|
FromHeight(Planar64),//jump height is invariant across mass and gravity changes
|
|
|
|
FromDeltaV(Planar64),//jump velocity is invariant across mass and gravity changes
|
|
|
|
FromEnergy(Planar64),// :)
|
|
|
|
}
|
|
|
|
//Jumping acts on dot(walks_state.normal,body.velocity)
|
|
|
|
//Capped means it increases the dot to the cap
|
|
|
|
//Energy means it adds energy
|
|
|
|
//Linear means it linearly adds on
|
2023-10-03 05:45:20 +00:00
|
|
|
|
2023-10-17 23:18:55 +00:00
|
|
|
struct StyleModifiers{
|
|
|
|
controls_mask:u32,//controls which are unable to be activated
|
|
|
|
controls_held:u32,//controls which must be active to be able to strafe
|
|
|
|
strafe_tick_rate:Ratio64,
|
|
|
|
jump_impulse:JumpImpulse,
|
|
|
|
jump_calculation:JumpCalculation,
|
|
|
|
static_friction:Planar64,
|
|
|
|
kinetic_friction:Planar64,
|
|
|
|
walk_speed:Planar64,
|
|
|
|
walk_accel:Planar64,
|
|
|
|
ladder_speed:Planar64,
|
|
|
|
ladder_accel:Planar64,
|
|
|
|
ladder_dot:Planar64,
|
|
|
|
swim_speed:Planar64,
|
|
|
|
mass:Planar64,
|
|
|
|
mv:Planar64,
|
|
|
|
air_accel_limit:Option<Planar64>,
|
|
|
|
gravity:Planar64Vec3,
|
|
|
|
hitbox_halfsize:Planar64Vec3,
|
2023-10-03 05:45:20 +00:00
|
|
|
}
|
|
|
|
impl std::default::Default for StyleModifiers{
|
|
|
|
fn default() -> Self {
|
2023-10-17 23:18:55 +00:00
|
|
|
Self::roblox_bhop()
|
2023-10-03 05:45:20 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-03 23:52:45 +00:00
|
|
|
impl StyleModifiers{
|
2023-09-27 09:12:20 +00:00
|
|
|
const CONTROL_MOVEFORWARD:u32=0b00000001;
|
|
|
|
const CONTROL_MOVEBACK:u32=0b00000010;
|
|
|
|
const CONTROL_MOVERIGHT:u32=0b00000100;
|
|
|
|
const CONTROL_MOVELEFT:u32=0b00001000;
|
|
|
|
const CONTROL_MOVEUP:u32=0b00010000;
|
|
|
|
const CONTROL_MOVEDOWN:u32=0b00100000;
|
|
|
|
const CONTROL_JUMP:u32=0b01000000;
|
|
|
|
const CONTROL_ZOOM:u32=0b10000000;
|
2023-10-03 23:52:45 +00:00
|
|
|
|
2023-09-27 09:12:20 +00:00
|
|
|
const RIGHT_DIR:Planar64Vec3=Planar64Vec3::X;
|
|
|
|
const UP_DIR:Planar64Vec3=Planar64Vec3::Y;
|
|
|
|
const FORWARD_DIR:Planar64Vec3=Planar64Vec3::NEG_Z;
|
2023-10-03 23:52:45 +00:00
|
|
|
|
2023-10-17 23:18:55 +00:00
|
|
|
fn new()->Self{
|
|
|
|
Self{
|
|
|
|
controls_mask:!0,//&!(Self::CONTROL_MOVEUP|Self::CONTROL_MOVEDOWN),
|
|
|
|
controls_held:0,
|
|
|
|
strafe_tick_rate:Ratio64::new(128,Time::ONE_SECOND.nanos() as u64).unwrap(),
|
|
|
|
jump_impulse:JumpImpulse::FromEnergy(Planar64::int(512)),
|
|
|
|
jump_calculation:JumpCalculation::Energy,
|
|
|
|
gravity:Planar64Vec3::int(0,-80,0),
|
|
|
|
static_friction:Planar64::int(2),
|
|
|
|
kinetic_friction:Planar64::int(3),//unrealistic: kinetic friction is typically lower than static
|
|
|
|
mass:Planar64::int(1),
|
|
|
|
mv:Planar64::int(2),
|
|
|
|
air_accel_limit:None,
|
|
|
|
walk_speed:Planar64::int(16),
|
|
|
|
walk_accel:Planar64::int(80),
|
|
|
|
ladder_speed:Planar64::int(16),
|
|
|
|
ladder_accel:Planar64::int(160),
|
|
|
|
ladder_dot:(Planar64::int(1)/2).sqrt(),
|
|
|
|
swim_speed:Planar64::int(12),
|
|
|
|
hitbox_halfsize:Planar64Vec3::int(2,5,2)/2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn roblox_bhop()->Self{
|
|
|
|
Self{
|
|
|
|
controls_mask:!0,//&!(Self::CONTROL_MOVEUP|Self::CONTROL_MOVEDOWN),
|
|
|
|
controls_held:0,
|
|
|
|
strafe_tick_rate:Ratio64::new(100,Time::ONE_SECOND.nanos() as u64).unwrap(),
|
|
|
|
jump_impulse:JumpImpulse::FromTime(Time::from_micros(715_588)),
|
|
|
|
jump_calculation:JumpCalculation::Capped,
|
|
|
|
gravity:Planar64Vec3::int(0,-100,0),
|
|
|
|
static_friction:Planar64::int(2),
|
|
|
|
kinetic_friction:Planar64::int(3),//unrealistic: kinetic friction is typically lower than static
|
|
|
|
mass:Planar64::int(1),
|
|
|
|
mv:Planar64::int(27)/10,
|
|
|
|
air_accel_limit:None,
|
|
|
|
walk_speed:Planar64::int(18),
|
|
|
|
walk_accel:Planar64::int(90),
|
|
|
|
ladder_speed:Planar64::int(18),
|
|
|
|
ladder_accel:Planar64::int(180),
|
|
|
|
ladder_dot:(Planar64::int(1)/2).sqrt(),
|
|
|
|
swim_speed:Planar64::int(12),
|
|
|
|
hitbox_halfsize:Planar64Vec3::int(2,5,2)/2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn roblox_surf()->Self{
|
|
|
|
Self{
|
|
|
|
controls_mask:!0,//&!(Self::CONTROL_MOVEUP|Self::CONTROL_MOVEDOWN),
|
|
|
|
controls_held:0,
|
|
|
|
strafe_tick_rate:Ratio64::new(100,Time::ONE_SECOND.nanos() as u64).unwrap(),
|
|
|
|
jump_impulse:JumpImpulse::FromTime(Time::from_micros(715_588)),
|
|
|
|
jump_calculation:JumpCalculation::Capped,
|
|
|
|
gravity:Planar64Vec3::int(0,-50,0),
|
|
|
|
static_friction:Planar64::int(2),
|
|
|
|
kinetic_friction:Planar64::int(3),//unrealistic: kinetic friction is typically lower than static
|
|
|
|
mass:Planar64::int(1),
|
|
|
|
mv:Planar64::int(27)/10,
|
|
|
|
air_accel_limit:None,
|
|
|
|
walk_speed:Planar64::int(18),
|
|
|
|
walk_accel:Planar64::int(90),
|
|
|
|
ladder_speed:Planar64::int(18),
|
|
|
|
ladder_accel:Planar64::int(180),
|
|
|
|
ladder_dot:(Planar64::int(1)/2).sqrt(),
|
|
|
|
swim_speed:Planar64::int(12),
|
|
|
|
hitbox_halfsize:Planar64Vec3::int(2,5,2)/2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn source_bhop()->Self{
|
|
|
|
//camera_offset=vec3(0,64/16-73/16/2,0),
|
|
|
|
Self{
|
|
|
|
controls_mask:!0,//&!(Self::CONTROL_MOVEUP|Self::CONTROL_MOVEDOWN),
|
|
|
|
controls_held:0,
|
|
|
|
strafe_tick_rate:Ratio64::new(100,Time::ONE_SECOND.nanos() as u64).unwrap(),
|
|
|
|
jump_impulse:JumpImpulse::FromHeight(Planar64::raw(52<<28)),
|
|
|
|
jump_calculation:JumpCalculation::Linear,
|
|
|
|
gravity:Planar64Vec3::raw(0,-800<<28,0),
|
|
|
|
static_friction:Planar64::int(2),//?
|
|
|
|
kinetic_friction:Planar64::int(3),//?
|
|
|
|
mass:Planar64::int(1),
|
|
|
|
mv:Planar64::raw(30<<28),
|
|
|
|
air_accel_limit:Some(Planar64::raw(150<<28)*66),
|
|
|
|
walk_speed:Planar64::int(18),//?
|
|
|
|
walk_accel:Planar64::int(90),//?
|
|
|
|
ladder_speed:Planar64::int(18),//?
|
|
|
|
ladder_accel:Planar64::int(180),//?
|
|
|
|
ladder_dot:(Planar64::int(1)/2).sqrt(),//?
|
|
|
|
swim_speed:Planar64::int(12),//?
|
|
|
|
hitbox_halfsize:Planar64Vec3::raw(33<<28,73<<28,33<<28)/2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn source_surf()->Self{
|
|
|
|
//camera_offset=vec3(0,64/16-73/16/2,0),
|
|
|
|
Self{
|
|
|
|
controls_mask:!0,//&!(Self::CONTROL_MOVEUP|Self::CONTROL_MOVEDOWN),
|
|
|
|
controls_held:0,
|
|
|
|
strafe_tick_rate:Ratio64::new(66,Time::ONE_SECOND.nanos() as u64).unwrap(),
|
|
|
|
jump_impulse:JumpImpulse::FromHeight(Planar64::raw(52<<28)),
|
|
|
|
jump_calculation:JumpCalculation::Linear,
|
|
|
|
gravity:Planar64Vec3::raw(0,-800<<28,0),
|
|
|
|
static_friction:Planar64::int(2),//?
|
|
|
|
kinetic_friction:Planar64::int(3),//?
|
|
|
|
mass:Planar64::int(1),
|
|
|
|
mv:Planar64::raw(30<<28),
|
|
|
|
air_accel_limit:Some(Planar64::raw(150<<28)*66),
|
|
|
|
walk_speed:Planar64::int(18),//?
|
|
|
|
walk_accel:Planar64::int(90),//?
|
|
|
|
ladder_speed:Planar64::int(18),//?
|
|
|
|
ladder_accel:Planar64::int(180),//?
|
|
|
|
ladder_dot:(Planar64::int(1)/2).sqrt(),//?
|
|
|
|
swim_speed:Planar64::int(12),//?
|
|
|
|
hitbox_halfsize:Planar64Vec3::raw(33<<28,73<<28,33<<28)/2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 23:52:45 +00:00
|
|
|
fn get_control(&self,control:u32,controls:u32)->bool{
|
2023-10-05 03:04:04 +00:00
|
|
|
controls&self.controls_mask&control==control
|
2023-10-03 23:52:45 +00:00
|
|
|
}
|
|
|
|
|
2023-09-27 09:12:20 +00:00
|
|
|
fn get_control_dir(&self,controls:u32)->Planar64Vec3{
|
2023-10-03 23:52:45 +00:00
|
|
|
//don't get fancy just do it
|
2023-09-27 09:12:20 +00:00
|
|
|
let mut control_dir:Planar64Vec3 = Planar64Vec3::ZERO;
|
2023-10-03 23:52:45 +00:00
|
|
|
//Disallow strafing if held controls are not held
|
|
|
|
if controls&self.controls_held!=self.controls_held{
|
|
|
|
return control_dir;
|
|
|
|
}
|
|
|
|
//Apply mask after held check so you can require non-allowed keys to be held for some reason
|
|
|
|
let controls=controls&self.controls_mask;
|
|
|
|
if controls & Self::CONTROL_MOVEFORWARD == Self::CONTROL_MOVEFORWARD {
|
|
|
|
control_dir+=Self::FORWARD_DIR;
|
|
|
|
}
|
|
|
|
if controls & Self::CONTROL_MOVEBACK == Self::CONTROL_MOVEBACK {
|
2023-09-27 09:12:20 +00:00
|
|
|
control_dir-=Self::FORWARD_DIR;
|
2023-10-03 23:52:45 +00:00
|
|
|
}
|
|
|
|
if controls & Self::CONTROL_MOVELEFT == Self::CONTROL_MOVELEFT {
|
2023-09-27 09:12:20 +00:00
|
|
|
control_dir-=Self::RIGHT_DIR;
|
2023-10-03 23:52:45 +00:00
|
|
|
}
|
|
|
|
if controls & Self::CONTROL_MOVERIGHT == Self::CONTROL_MOVERIGHT {
|
|
|
|
control_dir+=Self::RIGHT_DIR;
|
|
|
|
}
|
|
|
|
if controls & Self::CONTROL_MOVEUP == Self::CONTROL_MOVEUP {
|
|
|
|
control_dir+=Self::UP_DIR;
|
|
|
|
}
|
|
|
|
if controls & Self::CONTROL_MOVEDOWN == Self::CONTROL_MOVEDOWN {
|
2023-09-27 09:12:20 +00:00
|
|
|
control_dir-=Self::UP_DIR;
|
2023-10-03 23:52:45 +00:00
|
|
|
}
|
|
|
|
return control_dir
|
|
|
|
}
|
2023-10-14 21:51:13 +00:00
|
|
|
|
2023-10-17 23:18:55 +00:00
|
|
|
//fn get_jump_time(&self)->Planar64
|
|
|
|
//fn get_jump_height(&self)->Planar64
|
|
|
|
//fn get_jump_energy(&self)->Planar64
|
|
|
|
fn get_jump_deltav(&self)->Planar64{
|
|
|
|
match &self.jump_impulse{
|
|
|
|
&JumpImpulse::FromTime(time)=>self.gravity.length()*(time/2),
|
|
|
|
&JumpImpulse::FromHeight(height)=>(self.gravity.length()*height*2).sqrt(),
|
|
|
|
&JumpImpulse::FromDeltaV(deltav)=>deltav,
|
|
|
|
&JumpImpulse::FromEnergy(energy)=>(energy*2/self.mass).sqrt(),
|
|
|
|
}
|
2023-10-17 07:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2023-10-17 23:18:55 +00:00
|
|
|
control_dir*self.walk_speed
|
|
|
|
}
|
|
|
|
fn get_ladder_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);
|
|
|
|
// local m=sqrt(ControlDir.length_squared())
|
|
|
|
// local d=dot(Normal,ControlDir)/m
|
|
|
|
// if d<-LadderDot then
|
|
|
|
// ControlDir=Up*m
|
|
|
|
// d=dot(Normal,Up)
|
|
|
|
// elseif LadderDot<d then
|
|
|
|
// ControlDir=Up*-m
|
|
|
|
// d=-dot(Normal,Up)
|
|
|
|
// end
|
|
|
|
// return cross(cross(Normal,ControlDir),Normal)/sqrt(1-d*d)
|
|
|
|
control_dir*self.walk_speed
|
2023-10-17 07:17:54 +00:00
|
|
|
}
|
|
|
|
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);
|
2023-10-17 23:18:55 +00:00
|
|
|
control_dir*self.walk_speed
|
2023-10-14 21:51:13 +00:00
|
|
|
}
|
2023-10-03 23:52:45 +00:00
|
|
|
}
|
2023-10-03 05:45:20 +00:00
|
|
|
|
2023-10-13 02:44:46 +00:00
|
|
|
enum MoveState{
|
|
|
|
Air,
|
|
|
|
Walk(WalkState),
|
|
|
|
Water,
|
|
|
|
Ladder(WalkState),
|
|
|
|
}
|
|
|
|
|
2023-10-03 05:45:20 +00:00
|
|
|
pub struct PhysicsState{
|
2023-10-20 01:42:05 +00:00
|
|
|
time:Time,
|
2023-10-17 01:57:37 +00:00
|
|
|
body:Body,
|
|
|
|
world:WorldState,//currently there is only one state the world can be in
|
2023-10-20 01:42:05 +00:00
|
|
|
game:GameMechanicsState,
|
2023-10-17 01:57:37 +00:00
|
|
|
style:StyleModifiers,
|
2023-10-13 02:44:46 +00:00
|
|
|
touching:TouchingState,
|
2023-09-10 20:24:47 +00:00
|
|
|
//camera must exist in state because wormholes modify the camera, also camera punch
|
2023-10-17 01:57:37 +00:00
|
|
|
camera:PhysicsCamera,
|
2023-10-20 01:42:00 +00:00
|
|
|
pub next_mouse:MouseState,//Where is the mouse headed next
|
2023-10-17 01:57:37 +00:00
|
|
|
controls:u32,
|
|
|
|
move_state:MoveState,
|
2023-10-03 05:45:20 +00:00
|
|
|
//all models
|
2023-10-18 23:21:06 +00:00
|
|
|
models:PhysicsModels,
|
2023-10-17 01:57:37 +00:00
|
|
|
bvh:crate::bvh::BvhNode,
|
2023-10-03 05:45:20 +00:00
|
|
|
|
2023-10-18 23:18:17 +00:00
|
|
|
modes:Modes,
|
2023-10-03 05:45:20 +00:00
|
|
|
//the spawn point is where you spawn when you load into the map.
|
|
|
|
//This is not the same as Reset which teleports you to Spawn0
|
2023-10-20 01:42:05 +00:00
|
|
|
spawn_point:Planar64Vec3,
|
2023-09-08 18:33:16 +00:00
|
|
|
}
|
2023-10-25 03:22:30 +00:00
|
|
|
#[derive(Clone,Default)]
|
2023-10-05 03:04:04 +00:00
|
|
|
pub struct PhysicsOutputState{
|
|
|
|
camera:PhysicsCamera,
|
|
|
|
body:Body,
|
|
|
|
}
|
|
|
|
impl PhysicsOutputState{
|
2023-10-25 01:51:01 +00:00
|
|
|
pub fn extrapolate(&self,mouse_pos:glam::IVec2,time:Time)->(glam::Vec3,glam::Vec2){
|
|
|
|
((self.body.extrapolated_position(time)+self.camera.offset).into(),self.camera.simulate_move_angles(mouse_pos))
|
2023-10-05 03:04:04 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-08 18:33:16 +00:00
|
|
|
|
2023-09-18 20:20:51 +00:00
|
|
|
//pretend to be using what we want to eventually do
|
2023-10-06 03:32:02 +00:00
|
|
|
type TreyMeshFace = crate::aabb::AabbFace;
|
|
|
|
type TreyMesh = crate::aabb::Aabb;
|
2023-09-08 22:54:43 +00:00
|
|
|
|
2023-10-03 05:45:20 +00:00
|
|
|
enum PhysicsCollisionAttributes{
|
|
|
|
Contact{//track whether you are contacting the object
|
|
|
|
contacting:crate::model::ContactingAttributes,
|
|
|
|
general:crate::model::GameMechanicAttributes,
|
|
|
|
},
|
|
|
|
Intersect{//track whether you are intersecting the object
|
|
|
|
intersecting:crate::model::IntersectingAttributes,
|
|
|
|
general:crate::model::GameMechanicAttributes,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-09-21 07:01:31 +00:00
|
|
|
pub struct ModelPhysics {
|
2023-09-08 22:54:43 +00:00
|
|
|
//A model is a thing that has a hitbox. can be represented by a list of TreyMesh-es
|
|
|
|
//in this iteration, all it needs is extents.
|
2023-09-28 23:15:12 +00:00
|
|
|
mesh: TreyMesh,
|
2023-09-27 09:12:20 +00:00
|
|
|
transform:crate::integer::Planar64Affine3,
|
2023-10-03 05:45:20 +00:00
|
|
|
attributes:PhysicsCollisionAttributes,
|
2023-09-08 22:54:43 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 07:01:31 +00:00
|
|
|
impl ModelPhysics {
|
2023-09-27 09:12:20 +00:00
|
|
|
fn from_model_transform_attributes(model:&crate::model::IndexedModel,transform:&crate::integer::Planar64Affine3,attributes:PhysicsCollisionAttributes)->Self{
|
|
|
|
let mut aabb=TreyMesh::default();
|
2023-09-28 23:15:12 +00:00
|
|
|
for indexed_vertex in &model.unique_vertices {
|
2023-09-27 09:12:20 +00:00
|
|
|
aabb.grow(transform.transform_point3(model.unique_pos[indexed_vertex.pos as usize]));
|
2023-09-28 23:15:12 +00:00
|
|
|
}
|
|
|
|
Self{
|
|
|
|
mesh:aabb,
|
2023-10-03 23:34:54 +00:00
|
|
|
attributes,
|
2023-10-04 21:15:04 +00:00
|
|
|
transform:transform.clone(),
|
2023-10-03 23:34:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn from_model(model:&crate::model::IndexedModel,instance:&crate::model::ModelInstance) -> Option<Self> {
|
|
|
|
match &instance.attributes{
|
|
|
|
crate::model::CollisionAttributes::Contact{contacting,general}=>Some(ModelPhysics::from_model_transform_attributes(model,&instance.transform,PhysicsCollisionAttributes::Contact{contacting:contacting.clone(),general:general.clone()})),
|
2023-10-04 21:05:53 +00:00
|
|
|
crate::model::CollisionAttributes::Intersect{intersecting,general}=>Some(ModelPhysics::from_model_transform_attributes(model,&instance.transform,PhysicsCollisionAttributes::Intersect{intersecting:intersecting.clone(),general:general.clone()})),
|
|
|
|
crate::model::CollisionAttributes::Decoration=>None,
|
2023-09-28 23:15:12 +00:00
|
|
|
}
|
2023-09-08 23:14:01 +00:00
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
pub fn unit_vertices(&self) -> [Planar64Vec3;8] {
|
2023-10-06 03:32:02 +00:00
|
|
|
TreyMesh::unit_vertices()
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
2023-09-28 23:15:12 +00:00
|
|
|
pub fn mesh(&self) -> &TreyMesh {
|
|
|
|
return &self.mesh;
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
// pub fn face_mesh(&self,face:TreyMeshFace)->TreyMesh{
|
|
|
|
// self.mesh.face(face)
|
|
|
|
// }
|
|
|
|
pub fn face_normal(&self,face:TreyMeshFace) -> Planar64Vec3 {
|
2023-10-06 03:32:02 +00:00
|
|
|
TreyMesh::normal(face)//this is wrong for scale
|
2023-09-08 22:54:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-18 20:20:51 +00:00
|
|
|
//need non-face (full model) variant for CanCollide false objects
|
2023-09-18 20:44:43 +00:00
|
|
|
//OR have a separate list from contacts for model intersection
|
2023-09-19 01:34:48 +00:00
|
|
|
#[derive(Debug,Clone,Eq,Hash,PartialEq)]
|
2023-09-08 22:54:22 +00:00
|
|
|
pub struct RelativeCollision {
|
2023-10-18 23:21:06 +00:00
|
|
|
face:TreyMeshFace,//just an id
|
|
|
|
model:usize,//using id to avoid lifetimes
|
2023-09-08 22:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RelativeCollision {
|
2023-10-18 23:21:06 +00:00
|
|
|
fn model<'a>(&self,models:&'a PhysicsModels)->Option<&'a ModelPhysics>{
|
|
|
|
models.get(self.model)
|
2023-10-04 00:20:35 +00:00
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
// pub fn mesh(&self,models:&Vec<ModelPhysics>) -> TreyMesh {
|
|
|
|
// return self.model(models).unwrap().face_mesh(self.face).clone()
|
|
|
|
// }
|
2023-10-18 23:21:06 +00:00
|
|
|
fn normal(&self,models:&PhysicsModels) -> Planar64Vec3 {
|
2023-10-04 00:20:35 +00:00
|
|
|
return self.model(models).unwrap().face_normal(self.face)
|
2023-09-08 22:54:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 02:44:46 +00:00
|
|
|
struct TouchingState{
|
2023-10-18 23:21:06 +00:00
|
|
|
contacts:std::collections::HashMap::<usize,RelativeCollision>,
|
|
|
|
intersects:std::collections::HashMap::<usize,RelativeCollision>,
|
2023-10-13 02:44:46 +00:00
|
|
|
}
|
|
|
|
impl TouchingState{
|
|
|
|
fn clear(&mut self){
|
|
|
|
self.contacts.clear();
|
|
|
|
self.intersects.clear();
|
|
|
|
}
|
2023-10-18 23:21:06 +00:00
|
|
|
fn insert_contact(&mut self,model_id:usize,collision:RelativeCollision)->Option<RelativeCollision>{
|
2023-10-13 02:44:46 +00:00
|
|
|
self.contacts.insert(model_id,collision)
|
|
|
|
}
|
2023-10-18 23:21:06 +00:00
|
|
|
fn remove_contact(&mut self,model_id:usize)->Option<RelativeCollision>{
|
2023-10-13 02:44:46 +00:00
|
|
|
self.contacts.remove(&model_id)
|
|
|
|
}
|
2023-10-18 23:21:06 +00:00
|
|
|
fn insert_intersect(&mut self,model_id:usize,collision:RelativeCollision)->Option<RelativeCollision>{
|
2023-10-13 02:44:46 +00:00
|
|
|
self.intersects.insert(model_id,collision)
|
|
|
|
}
|
2023-10-18 23:21:06 +00:00
|
|
|
fn remove_intersect(&mut self,model_id:usize)->Option<RelativeCollision>{
|
2023-10-13 02:44:46 +00:00
|
|
|
self.intersects.remove(&model_id)
|
|
|
|
}
|
2023-10-18 23:21:06 +00:00
|
|
|
fn constrain_velocity(&self,models:&PhysicsModels,velocity:&mut Planar64Vec3){
|
2023-10-13 02:44:46 +00:00
|
|
|
for (_,contact) in &self.contacts {
|
|
|
|
let n=contact.normal(models);
|
|
|
|
let d=velocity.dot(n);
|
|
|
|
if d<Planar64::ZERO{
|
|
|
|
(*velocity)-=n*(d/n.dot(n));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-18 23:21:06 +00:00
|
|
|
fn constrain_acceleration(&self,models:&PhysicsModels,acceleration:&mut Planar64Vec3){
|
2023-10-13 02:44:46 +00:00
|
|
|
for (_,contact) in &self.contacts {
|
|
|
|
let n=contact.normal(models);
|
|
|
|
let d=acceleration.dot(n);
|
|
|
|
if d<Planar64::ZERO{
|
|
|
|
(*acceleration)-=n*(d/n.dot(n));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Default for TouchingState{
|
|
|
|
fn default() -> Self {
|
|
|
|
Self{
|
|
|
|
contacts: std::collections::HashMap::new(),
|
|
|
|
intersects: std::collections::HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-09 03:14:18 +00:00
|
|
|
impl Body {
|
2023-09-27 09:12:20 +00:00
|
|
|
pub fn with_pva(position:Planar64Vec3,velocity:Planar64Vec3,acceleration:Planar64Vec3) -> Self {
|
2023-09-09 03:14:18 +00:00
|
|
|
Self{
|
2023-09-18 23:03:27 +00:00
|
|
|
position,
|
|
|
|
velocity,
|
|
|
|
acceleration,
|
2023-09-27 09:12:20 +00:00
|
|
|
time:Time::ZERO,
|
2023-09-09 03:14:18 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
pub fn extrapolated_position(&self,time:Time)->Planar64Vec3{
|
|
|
|
let dt=time-self.time;
|
|
|
|
self.position+self.velocity*dt+self.acceleration*(dt*dt/2)
|
2023-09-09 03:14:18 +00:00
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
pub fn extrapolated_velocity(&self,time:Time)->Planar64Vec3{
|
|
|
|
let dt=time-self.time;
|
|
|
|
self.velocity+self.acceleration*dt
|
2023-09-18 23:04:18 +00:00
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
pub fn advance_time(&mut self,time:Time){
|
2023-09-09 03:14:18 +00:00
|
|
|
self.position=self.extrapolated_position(time);
|
2023-09-18 23:04:18 +00:00
|
|
|
self.velocity=self.extrapolated_velocity(time);
|
2023-09-09 03:14:18 +00:00
|
|
|
self.time=time;
|
|
|
|
}
|
|
|
|
}
|
2023-10-14 22:41:59 +00:00
|
|
|
impl std::fmt::Display for Body{
|
|
|
|
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
|
|
|
|
write!(f,"p({}) v({}) a({}) t({})",self.position,self.velocity,self.acceleration,self.time)
|
|
|
|
}
|
|
|
|
}
|
2023-09-09 03:14:18 +00:00
|
|
|
|
2023-10-05 03:04:04 +00:00
|
|
|
impl Default for PhysicsState{
|
|
|
|
fn default() -> Self {
|
|
|
|
Self{
|
2023-09-27 09:12:20 +00:00
|
|
|
spawn_point:Planar64Vec3::int(0,50,0),
|
|
|
|
body: Body::with_pva(Planar64Vec3::int(0,50,0),Planar64Vec3::int(0,0,0),Planar64Vec3::int(0,-100,0)),
|
|
|
|
time: Time::ZERO,
|
2023-10-05 03:04:04 +00:00
|
|
|
style:StyleModifiers::default(),
|
2023-10-13 02:44:46 +00:00
|
|
|
touching:TouchingState::default(),
|
2023-10-18 23:21:06 +00:00
|
|
|
models:PhysicsModels::default(),
|
2023-10-06 06:52:04 +00:00
|
|
|
bvh:crate::bvh::BvhNode::default(),
|
2023-10-13 02:44:46 +00:00
|
|
|
move_state: MoveState::Air,
|
2023-09-27 09:12:20 +00:00
|
|
|
camera: PhysicsCamera::from_offset(Planar64Vec3::int(0,2,0)),//4.5-2.5=2
|
2023-10-05 03:04:04 +00:00
|
|
|
next_mouse: MouseState::default(),
|
|
|
|
controls: 0,
|
|
|
|
world:WorldState{},
|
|
|
|
game:GameMechanicsState::default(),
|
2023-10-18 23:18:17 +00:00
|
|
|
modes:Modes::default(),
|
2023-10-05 03:04:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 18:33:16 +00:00
|
|
|
impl PhysicsState {
|
2023-10-04 02:43:41 +00:00
|
|
|
pub fn clear(&mut self){
|
|
|
|
self.models.clear();
|
|
|
|
self.modes.clear();
|
2023-10-13 02:44:46 +00:00
|
|
|
self.touching.clear();
|
2023-10-04 02:43:41 +00:00
|
|
|
}
|
2023-10-05 03:04:04 +00:00
|
|
|
|
|
|
|
pub fn output(&self)->PhysicsOutputState{
|
|
|
|
PhysicsOutputState{
|
|
|
|
body:self.body.clone(),
|
|
|
|
camera:self.camera.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-20 02:19:05 +00:00
|
|
|
pub fn spawn(&mut self,spawn_point:Planar64Vec3){
|
|
|
|
self.game.stage_id=0;
|
|
|
|
self.spawn_point=spawn_point;
|
|
|
|
self.process_instruction(crate::instruction::TimedInstruction{
|
|
|
|
time:self.time,
|
|
|
|
instruction: PhysicsInstruction::Input(PhysicsInputInstruction::Reset),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-05 03:04:04 +00:00
|
|
|
pub fn generate_models(&mut self,indexed_models:&crate::model::IndexedModelInstances){
|
|
|
|
let mut starts=Vec::new();
|
|
|
|
let mut spawns=Vec::new();
|
|
|
|
let mut ordered_checkpoints=Vec::new();
|
|
|
|
let mut unordered_checkpoints=Vec::new();
|
|
|
|
for model in &indexed_models.models{
|
|
|
|
//make aabb and run vertices to get realistic bounds
|
|
|
|
for model_instance in &model.instances{
|
|
|
|
if let Some(model_physics)=ModelPhysics::from_model(model,model_instance){
|
2023-10-18 22:59:38 +00:00
|
|
|
let model_id=self.models.push(model_physics);
|
2023-10-05 03:04:04 +00:00
|
|
|
for attr in &model_instance.temp_indexing{
|
|
|
|
match attr{
|
2023-10-18 22:59:38 +00:00
|
|
|
crate::model::TempIndexedAttributes::Start(s)=>starts.push((model_id,s.clone())),
|
|
|
|
crate::model::TempIndexedAttributes::Spawn(s)=>spawns.push((model_id,s.clone())),
|
|
|
|
crate::model::TempIndexedAttributes::OrderedCheckpoint(s)=>ordered_checkpoints.push((model_id,s.clone())),
|
|
|
|
crate::model::TempIndexedAttributes::UnorderedCheckpoint(s)=>unordered_checkpoints.push((model_id,s.clone())),
|
|
|
|
crate::model::TempIndexedAttributes::Wormhole(s)=>{self.models.model_id_from_wormhole_id.insert(s.wormhole_id,model_id);},
|
2023-10-05 03:04:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-18 23:21:06 +00:00
|
|
|
self.bvh=crate::bvh::generate_bvh(self.models.models.iter().map(|m|m.mesh().clone()).collect());
|
2023-10-05 03:04:04 +00:00
|
|
|
//I don't wanna write structs for temporary structures
|
|
|
|
//this code builds ModeDescriptions from the unsorted lists at the top of the function
|
2023-10-18 22:59:38 +00:00
|
|
|
starts.sort_by_key(|tup|tup.1.mode_id);
|
|
|
|
let mut mode_id_from_map_mode_id=std::collections::HashMap::new();
|
|
|
|
let mut modedatas:Vec<(usize,Vec<(u32,usize)>,Vec<(u32,usize)>,Vec<usize>,u32)>=starts.into_iter().enumerate().map(|(i,(model_id,s))|{
|
|
|
|
mode_id_from_map_mode_id.insert(s.mode_id,i);
|
|
|
|
(model_id,Vec::new(),Vec::new(),Vec::new(),s.mode_id)
|
2023-10-05 03:04:04 +00:00
|
|
|
}).collect();
|
2023-10-18 22:59:38 +00:00
|
|
|
for (model_id,s) in spawns{
|
|
|
|
if let Some(mode_id)=mode_id_from_map_mode_id.get(&s.mode_id){
|
2023-10-05 03:04:04 +00:00
|
|
|
if let Some(modedata)=modedatas.get_mut(*mode_id){
|
2023-10-18 22:59:38 +00:00
|
|
|
modedata.1.push((s.stage_id,model_id));
|
2023-10-05 03:04:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-18 22:59:38 +00:00
|
|
|
for (model_id,s) in ordered_checkpoints{
|
|
|
|
if let Some(mode_id)=mode_id_from_map_mode_id.get(&s.mode_id){
|
2023-10-05 03:04:04 +00:00
|
|
|
if let Some(modedata)=modedatas.get_mut(*mode_id){
|
2023-10-18 22:59:38 +00:00
|
|
|
modedata.2.push((s.checkpoint_id,model_id));
|
2023-10-05 03:04:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-18 22:59:38 +00:00
|
|
|
for (model_id,s) in unordered_checkpoints{
|
|
|
|
if let Some(mode_id)=mode_id_from_map_mode_id.get(&s.mode_id){
|
2023-10-05 03:04:04 +00:00
|
|
|
if let Some(modedata)=modedatas.get_mut(*mode_id){
|
2023-10-18 22:59:38 +00:00
|
|
|
modedata.3.push(model_id);
|
2023-10-05 03:04:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-18 22:59:38 +00:00
|
|
|
for mut tup in modedatas.into_iter(){
|
2023-10-05 03:04:04 +00:00
|
|
|
tup.1.sort_by_key(|tup|tup.0);
|
|
|
|
tup.2.sort_by_key(|tup|tup.0);
|
|
|
|
let mut eshmep1=std::collections::HashMap::new();
|
|
|
|
let mut eshmep2=std::collections::HashMap::new();
|
2023-10-18 22:59:38 +00:00
|
|
|
self.modes.insert(tup.4,crate::model::ModeDescription{
|
|
|
|
start:tup.0,
|
|
|
|
spawns:tup.1.into_iter().enumerate().map(|(i,tup)|{eshmep1.insert(tup.0,i);tup.1}).collect(),
|
|
|
|
ordered_checkpoints:tup.2.into_iter().enumerate().map(|(i,tup)|{eshmep2.insert(tup.0,i);tup.1}).collect(),
|
|
|
|
unordered_checkpoints:tup.3,
|
2023-10-05 03:04:04 +00:00
|
|
|
spawn_from_stage_id:eshmep1,
|
|
|
|
ordered_checkpoint_from_checkpoint_id:eshmep2,
|
2023-10-18 22:59:38 +00:00
|
|
|
});
|
|
|
|
}
|
2023-10-18 23:21:06 +00:00
|
|
|
println!("Physics Objects: {}",self.models.models.len());
|
2023-10-05 03:04:04 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 02:44:49 +00:00
|
|
|
pub fn load_user_settings(&mut self,user_settings:&crate::settings::UserSettings){
|
|
|
|
self.camera.sensitivity=user_settings.calculate_sensitivity();
|
|
|
|
}
|
|
|
|
|
2023-09-09 03:14:18 +00:00
|
|
|
//tickless gaming
|
2023-09-27 09:12:20 +00:00
|
|
|
pub fn run(&mut self, time_limit:Time){
|
2023-09-09 03:14:18 +00:00
|
|
|
//prepare is ommitted - everything is done via instructions.
|
2023-09-19 01:06:03 +00:00
|
|
|
while let Some(instruction) = self.next_instruction(time_limit) {//collect
|
2023-09-09 03:14:18 +00:00
|
|
|
//process
|
|
|
|
self.process_instruction(instruction);
|
|
|
|
//write hash lol
|
2023-09-08 18:33:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-27 09:12:20 +00:00
|
|
|
pub fn advance_time(&mut self, time: Time){
|
2023-09-09 03:14:18 +00:00
|
|
|
self.body.advance_time(time);
|
|
|
|
self.time=time;
|
2023-09-08 18:33:16 +00:00
|
|
|
}
|
2023-09-08 18:33:20 +00:00
|
|
|
|
2023-09-20 00:53:29 +00:00
|
|
|
fn set_control(&mut self,control:u32,state:bool){
|
|
|
|
self.controls=if state{self.controls|control}else{self.controls&!control};
|
|
|
|
}
|
|
|
|
fn jump(&mut self){
|
2023-10-13 02:44:46 +00:00
|
|
|
match &self.move_state{
|
2023-10-17 07:17:54 +00:00
|
|
|
MoveState::Walk(walk_state)|MoveState::Ladder(walk_state)=>{
|
2023-10-17 23:18:55 +00:00
|
|
|
let mut v=self.body.velocity+walk_state.normal*self.style.get_jump_deltav();
|
2023-10-13 02:44:46 +00:00
|
|
|
self.touching.constrain_velocity(&self.models,&mut v);
|
|
|
|
self.body.velocity=v;
|
|
|
|
},
|
2023-10-17 07:17:54 +00:00
|
|
|
MoveState::Air|MoveState::Water=>(),
|
2023-09-19 06:36:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-13 02:44:46 +00:00
|
|
|
|
2023-09-09 00:34:26 +00:00
|
|
|
fn next_strafe_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
|
|
|
|
return Some(TimedInstruction{
|
2023-09-27 09:12:20 +00:00
|
|
|
time:Time::from_nanos(self.style.strafe_tick_rate.rhs_div_int(self.style.strafe_tick_rate.mul_int(self.time.nanos())+1)),
|
2023-09-09 03:13:30 +00:00
|
|
|
//only poll the physics if there is a before and after mouse event
|
2023-09-09 00:34:26 +00:00
|
|
|
instruction:PhysicsInstruction::StrafeTick
|
2023-09-08 18:33:20 +00:00
|
|
|
});
|
|
|
|
}
|
2023-09-08 21:11:24 +00:00
|
|
|
|
2023-09-09 03:13:30 +00:00
|
|
|
//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
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
2023-09-20 00:53:29 +00:00
|
|
|
fn refresh_walk_target(&mut self){
|
2023-10-13 02:44:46 +00:00
|
|
|
match &mut self.move_state{
|
2023-10-17 07:17:54 +00:00
|
|
|
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);
|
2023-10-13 02:44:46 +00:00
|
|
|
},
|
2023-10-17 07:17:54 +00:00
|
|
|
MoveState::Ladder(WalkState{normal,state})=>{
|
|
|
|
let n=normal;
|
2023-10-17 23:18:55 +00:00
|
|
|
(*state,self.body.acceleration)=WalkEnum::with_target_velocity(&self.touching,&self.body,&self.style,&self.models,self.style.get_ladder_target_velocity(&self.camera,self.controls,&self.next_mouse,self.time),&n);
|
2023-10-13 02:44:46 +00:00
|
|
|
},
|
2023-09-20 00:53:29 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-13 02:44:46 +00:00
|
|
|
fn next_move_instruction(&self)->Option<TimedInstruction<PhysicsInstruction>>{
|
2023-09-19 04:10:07 +00:00
|
|
|
//check if you have a valid walk state and create an instruction
|
2023-10-13 02:44:46 +00:00
|
|
|
match &self.move_state{
|
|
|
|
MoveState::Walk(walk_state)|MoveState::Ladder(walk_state)=>match &walk_state.state{
|
2023-10-17 07:17:54 +00:00
|
|
|
WalkEnum::Transient(walk_target)=>Some(TimedInstruction{
|
|
|
|
time:walk_target.time,
|
2023-09-19 06:36:14 +00:00
|
|
|
instruction:PhysicsInstruction::ReachWalkTargetVelocity
|
|
|
|
}),
|
|
|
|
WalkEnum::Reached=>None,
|
|
|
|
}
|
2023-10-13 02:44:46 +00:00
|
|
|
MoveState::Air=>self.next_strafe_instruction(),
|
|
|
|
MoveState::Water=>None,//TODO
|
2023-09-19 04:10:07 +00:00
|
|
|
}
|
2023-09-08 21:11:24 +00:00
|
|
|
}
|
2023-09-18 20:20:51 +00:00
|
|
|
fn mesh(&self) -> TreyMesh {
|
2023-09-27 09:12:20 +00:00
|
|
|
let mut aabb=TreyMesh::default();
|
2023-10-06 03:32:02 +00:00
|
|
|
for vertex in TreyMesh::unit_vertices(){
|
2023-10-03 05:45:20 +00:00
|
|
|
aabb.grow(self.body.position+self.style.hitbox_halfsize*vertex);
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
|
|
|
aabb
|
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
fn predict_collision_end(&self,time:Time,time_limit:Time,collision_data:&RelativeCollision) -> Option<TimedInstruction<PhysicsInstruction>> {
|
2023-09-09 03:13:30 +00:00
|
|
|
//must treat cancollide false objects differently: you may not exit through the same face you entered.
|
2023-09-18 20:20:51 +00:00
|
|
|
//RelativeCollsion must reference the full model instead of a particular face
|
|
|
|
//this is Ctrl+C Ctrl+V of predict_collision_start but with v=-v before the calc and t=-t after the calc
|
|
|
|
//find best t
|
2023-09-19 01:06:03 +00:00
|
|
|
let mut best_time=time_limit;
|
|
|
|
let mut exit_face:Option<TreyMeshFace>=None;
|
2023-09-18 20:20:51 +00:00
|
|
|
let mesh0=self.mesh();
|
2023-09-21 07:01:31 +00:00
|
|
|
let mesh1=self.models.get(collision_data.model as usize).unwrap().mesh();
|
2023-09-19 01:06:03 +00:00
|
|
|
let (v,a)=(-self.body.velocity,self.body.acceleration);
|
2023-09-18 20:20:51 +00:00
|
|
|
//collect x
|
2023-09-19 01:06:03 +00:00
|
|
|
match collision_data.face {
|
2023-10-06 03:32:02 +00:00
|
|
|
TreyMeshFace::Top|TreyMeshFace::Back|TreyMeshFace::Bottom|TreyMeshFace::Front=>{
|
2023-09-27 09:12:20 +00:00
|
|
|
for t in zeroes2(mesh0.max.x()-mesh1.min.x(),v.x(),a.x()/2) {
|
2023-09-19 01:06:03 +00:00
|
|
|
//negative t = back in time
|
|
|
|
//must be moving towards surface to collide
|
|
|
|
//must beat the current soonest collision time
|
|
|
|
//must be moving towards surface
|
2023-09-27 09:12:20 +00:00
|
|
|
let t_time=self.body.time-Time::from(t);
|
|
|
|
if time<=t_time&&t_time<best_time&&Planar64::ZERO<v.x()+a.x()*-t{
|
2023-09-19 01:06:03 +00:00
|
|
|
//collect valid t
|
|
|
|
best_time=t_time;
|
|
|
|
exit_face=Some(TreyMeshFace::Left);
|
2023-09-20 01:14:24 +00:00
|
|
|
break;
|
2023-09-19 01:06:03 +00:00
|
|
|
}
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
for t in zeroes2(mesh0.min.x()-mesh1.max.x(),v.x(),a.x()/2) {
|
2023-09-19 01:06:03 +00:00
|
|
|
//negative t = back in time
|
|
|
|
//must be moving towards surface to collide
|
|
|
|
//must beat the current soonest collision time
|
|
|
|
//must be moving towards surface
|
2023-09-27 09:12:20 +00:00
|
|
|
let t_time=self.body.time-Time::from(t);
|
|
|
|
if time<=t_time&&t_time<best_time&&v.x()+a.x()*-t<Planar64::ZERO{
|
2023-09-19 01:06:03 +00:00
|
|
|
//collect valid t
|
|
|
|
best_time=t_time;
|
|
|
|
exit_face=Some(TreyMeshFace::Right);
|
2023-09-20 01:14:24 +00:00
|
|
|
break;
|
2023-09-19 01:06:03 +00:00
|
|
|
}
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
2023-09-19 01:06:03 +00:00
|
|
|
},
|
2023-10-06 03:32:02 +00:00
|
|
|
TreyMeshFace::Left=>{
|
2023-09-19 01:06:03 +00:00
|
|
|
//generate event if v.x<0||a.x<0
|
2023-09-27 09:12:20 +00:00
|
|
|
if -v.x()<Planar64::ZERO{
|
2023-09-19 01:06:03 +00:00
|
|
|
best_time=time;
|
|
|
|
exit_face=Some(TreyMeshFace::Left);
|
|
|
|
}
|
|
|
|
},
|
2023-10-06 03:32:02 +00:00
|
|
|
TreyMeshFace::Right=>{
|
2023-09-19 01:06:03 +00:00
|
|
|
//generate event if 0<v.x||0<a.x
|
2023-09-27 09:12:20 +00:00
|
|
|
if Planar64::ZERO<(-v.x()){
|
2023-09-19 01:06:03 +00:00
|
|
|
best_time=time;
|
|
|
|
exit_face=Some(TreyMeshFace::Right);
|
|
|
|
}
|
|
|
|
},
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
|
|
|
//collect y
|
2023-09-19 01:06:03 +00:00
|
|
|
match collision_data.face {
|
2023-10-06 03:32:02 +00:00
|
|
|
TreyMeshFace::Left|TreyMeshFace::Back|TreyMeshFace::Right|TreyMeshFace::Front=>{
|
2023-09-27 09:12:20 +00:00
|
|
|
for t in zeroes2(mesh0.max.y()-mesh1.min.y(),v.y(),a.y()/2) {
|
2023-09-19 01:06:03 +00:00
|
|
|
//negative t = back in time
|
|
|
|
//must be moving towards surface to collide
|
|
|
|
//must beat the current soonest collision time
|
|
|
|
//must be moving towards surface
|
2023-09-27 09:12:20 +00:00
|
|
|
let t_time=self.body.time-Time::from(t);
|
|
|
|
if time<=t_time&&t_time<best_time&&Planar64::ZERO<v.y()+a.y()*-t{
|
2023-09-19 01:06:03 +00:00
|
|
|
//collect valid t
|
|
|
|
best_time=t_time;
|
|
|
|
exit_face=Some(TreyMeshFace::Bottom);
|
2023-09-20 01:14:24 +00:00
|
|
|
break;
|
2023-09-19 01:06:03 +00:00
|
|
|
}
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
for t in zeroes2(mesh0.min.y()-mesh1.max.y(),v.y(),a.y()/2) {
|
2023-09-19 01:06:03 +00:00
|
|
|
//negative t = back in time
|
|
|
|
//must be moving towards surface to collide
|
|
|
|
//must beat the current soonest collision time
|
|
|
|
//must be moving towards surface
|
2023-09-27 09:12:20 +00:00
|
|
|
let t_time=self.body.time-Time::from(t);
|
|
|
|
if time<=t_time&&t_time<best_time&&v.y()+a.y()*-t<Planar64::ZERO{
|
2023-09-19 01:06:03 +00:00
|
|
|
//collect valid t
|
|
|
|
best_time=t_time;
|
|
|
|
exit_face=Some(TreyMeshFace::Top);
|
2023-09-20 01:14:24 +00:00
|
|
|
break;
|
2023-09-19 01:06:03 +00:00
|
|
|
}
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
2023-09-19 01:06:03 +00:00
|
|
|
},
|
2023-10-06 03:32:02 +00:00
|
|
|
TreyMeshFace::Bottom=>{
|
2023-09-19 01:06:03 +00:00
|
|
|
//generate event if v.y<0||a.y<0
|
2023-09-27 09:12:20 +00:00
|
|
|
if -v.y()<Planar64::ZERO{
|
2023-09-19 01:06:03 +00:00
|
|
|
best_time=time;
|
|
|
|
exit_face=Some(TreyMeshFace::Bottom);
|
|
|
|
}
|
|
|
|
},
|
2023-10-06 03:32:02 +00:00
|
|
|
TreyMeshFace::Top=>{
|
2023-09-19 01:06:03 +00:00
|
|
|
//generate event if 0<v.y||0<a.y
|
2023-09-27 09:12:20 +00:00
|
|
|
if Planar64::ZERO<(-v.y()){
|
2023-09-19 01:06:03 +00:00
|
|
|
best_time=time;
|
|
|
|
exit_face=Some(TreyMeshFace::Top);
|
|
|
|
}
|
|
|
|
},
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
|
|
|
//collect z
|
2023-09-19 01:06:03 +00:00
|
|
|
match collision_data.face {
|
2023-10-06 03:32:02 +00:00
|
|
|
TreyMeshFace::Left|TreyMeshFace::Bottom|TreyMeshFace::Right|TreyMeshFace::Top=>{
|
2023-09-27 09:12:20 +00:00
|
|
|
for t in zeroes2(mesh0.max.z()-mesh1.min.z(),v.z(),a.z()/2) {
|
2023-09-19 01:06:03 +00:00
|
|
|
//negative t = back in time
|
|
|
|
//must be moving towards surface to collide
|
|
|
|
//must beat the current soonest collision time
|
|
|
|
//must be moving towards surface
|
2023-09-27 09:12:20 +00:00
|
|
|
let t_time=self.body.time-Time::from(t);
|
|
|
|
if time<=t_time&&t_time<best_time&&Planar64::ZERO<v.z()+a.z()*-t{
|
2023-09-19 01:06:03 +00:00
|
|
|
//collect valid t
|
|
|
|
best_time=t_time;
|
|
|
|
exit_face=Some(TreyMeshFace::Front);
|
2023-09-20 01:14:24 +00:00
|
|
|
break;
|
2023-09-19 01:06:03 +00:00
|
|
|
}
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
for t in zeroes2(mesh0.min.z()-mesh1.max.z(),v.z(),a.z()/2) {
|
2023-09-19 01:06:03 +00:00
|
|
|
//negative t = back in time
|
|
|
|
//must be moving towards surface to collide
|
|
|
|
//must beat the current soonest collision time
|
|
|
|
//must be moving towards surface
|
2023-09-27 09:12:20 +00:00
|
|
|
let t_time=self.body.time-Time::from(t);
|
|
|
|
if time<=t_time&&t_time<best_time&&v.z()+a.z()*-t<Planar64::ZERO{
|
2023-09-19 01:06:03 +00:00
|
|
|
//collect valid t
|
|
|
|
best_time=t_time;
|
|
|
|
exit_face=Some(TreyMeshFace::Back);
|
2023-09-20 01:14:24 +00:00
|
|
|
break;
|
2023-09-19 01:06:03 +00:00
|
|
|
}
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
2023-09-19 01:06:03 +00:00
|
|
|
},
|
2023-10-06 03:32:02 +00:00
|
|
|
TreyMeshFace::Front=>{
|
2023-09-19 01:06:03 +00:00
|
|
|
//generate event if v.z<0||a.z<0
|
2023-09-27 09:12:20 +00:00
|
|
|
if -v.z()<Planar64::ZERO{
|
2023-09-19 01:06:03 +00:00
|
|
|
best_time=time;
|
|
|
|
exit_face=Some(TreyMeshFace::Front);
|
|
|
|
}
|
|
|
|
},
|
2023-10-06 03:32:02 +00:00
|
|
|
TreyMeshFace::Back=>{
|
2023-09-19 01:06:03 +00:00
|
|
|
//generate event if 0<v.z||0<a.z
|
2023-09-27 09:12:20 +00:00
|
|
|
if Planar64::ZERO<(-v.z()){
|
2023-09-19 01:06:03 +00:00
|
|
|
best_time=time;
|
|
|
|
exit_face=Some(TreyMeshFace::Back);
|
|
|
|
}
|
|
|
|
},
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
|
|
|
//generate instruction
|
2023-09-19 01:06:03 +00:00
|
|
|
if let Some(face) = exit_face{
|
2023-09-18 20:20:51 +00:00
|
|
|
return Some(TimedInstruction {
|
2023-09-19 01:06:03 +00:00
|
|
|
time: best_time,
|
|
|
|
instruction: PhysicsInstruction::CollisionEnd(collision_data.clone())
|
2023-09-18 20:20:51 +00:00
|
|
|
})
|
|
|
|
}
|
2023-09-08 22:55:33 +00:00
|
|
|
None
|
|
|
|
}
|
2023-10-18 23:21:06 +00:00
|
|
|
fn predict_collision_start(&self,time:Time,time_limit:Time,model_id:usize) -> Option<TimedInstruction<PhysicsInstruction>> {
|
2023-10-06 03:32:19 +00:00
|
|
|
let mesh0=self.mesh();
|
2023-10-18 23:21:06 +00:00
|
|
|
let mesh1=self.models.get(model_id).unwrap().mesh();
|
2023-10-10 23:05:47 +00:00
|
|
|
let (p,v,a,body_time)=(self.body.position,self.body.velocity,self.body.acceleration,self.body.time);
|
2023-09-18 20:20:51 +00:00
|
|
|
//find best t
|
2023-09-19 01:06:03 +00:00
|
|
|
let mut best_time=time_limit;
|
2023-09-18 20:20:51 +00:00
|
|
|
let mut best_face:Option<TreyMeshFace>=None;
|
|
|
|
//collect x
|
2023-09-27 09:12:20 +00:00
|
|
|
for t in zeroes2(mesh0.max.x()-mesh1.min.x(),v.x(),a.x()/2) {
|
2023-09-19 01:50:12 +00:00
|
|
|
//must collide now or in the future
|
2023-09-18 20:20:51 +00:00
|
|
|
//must beat the current soonest collision time
|
|
|
|
//must be moving towards surface
|
2023-09-27 09:12:20 +00:00
|
|
|
let t_time=body_time+Time::from(t);
|
|
|
|
if time<=t_time&&t_time<best_time&&Planar64::ZERO<v.x()+a.x()*t{
|
2023-09-19 01:06:03 +00:00
|
|
|
let dp=self.body.extrapolated_position(t_time)-p;
|
2023-09-18 20:20:51 +00:00
|
|
|
//faces must be overlapping
|
2023-09-27 09:12:20 +00:00
|
|
|
if mesh1.min.y()<mesh0.max.y()+dp.y()&&mesh0.min.y()+dp.y()<mesh1.max.y()&&mesh1.min.z()<mesh0.max.z()+dp.z()&&mesh0.min.z()+dp.z()<mesh1.max.z() {
|
2023-09-18 20:20:51 +00:00
|
|
|
//collect valid t
|
2023-09-19 01:06:03 +00:00
|
|
|
best_time=t_time;
|
2023-09-18 20:20:51 +00:00
|
|
|
best_face=Some(TreyMeshFace::Left);
|
2023-09-20 01:14:24 +00:00
|
|
|
break;
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
for t in zeroes2(mesh0.min.x()-mesh1.max.x(),v.x(),a.x()/2) {
|
2023-09-19 01:50:12 +00:00
|
|
|
//must collide now or in the future
|
2023-09-18 20:20:51 +00:00
|
|
|
//must beat the current soonest collision time
|
|
|
|
//must be moving towards surface
|
2023-09-27 09:12:20 +00:00
|
|
|
let t_time=body_time+Time::from(t);
|
|
|
|
if time<=t_time&&t_time<best_time&&v.x()+a.x()*t<Planar64::ZERO{
|
2023-09-19 01:06:03 +00:00
|
|
|
let dp=self.body.extrapolated_position(t_time)-p;
|
2023-09-18 20:20:51 +00:00
|
|
|
//faces must be overlapping
|
2023-09-27 09:12:20 +00:00
|
|
|
if mesh1.min.y()<mesh0.max.y()+dp.y()&&mesh0.min.y()+dp.y()<mesh1.max.y()&&mesh1.min.z()<mesh0.max.z()+dp.z()&&mesh0.min.z()+dp.z()<mesh1.max.z() {
|
2023-09-18 20:20:51 +00:00
|
|
|
//collect valid t
|
2023-09-19 01:06:03 +00:00
|
|
|
best_time=t_time;
|
2023-09-18 20:20:51 +00:00
|
|
|
best_face=Some(TreyMeshFace::Right);
|
2023-09-20 01:14:24 +00:00
|
|
|
break;
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//collect y
|
2023-09-27 09:12:20 +00:00
|
|
|
for t in zeroes2(mesh0.max.y()-mesh1.min.y(),v.y(),a.y()/2) {
|
2023-09-19 01:50:12 +00:00
|
|
|
//must collide now or in the future
|
2023-09-18 20:20:51 +00:00
|
|
|
//must beat the current soonest collision time
|
|
|
|
//must be moving towards surface
|
2023-09-27 09:12:20 +00:00
|
|
|
let t_time=body_time+Time::from(t);
|
|
|
|
if time<=t_time&&t_time<best_time&&Planar64::ZERO<v.y()+a.y()*t{
|
2023-09-19 01:06:03 +00:00
|
|
|
let dp=self.body.extrapolated_position(t_time)-p;
|
2023-09-18 20:20:51 +00:00
|
|
|
//faces must be overlapping
|
2023-09-27 09:12:20 +00:00
|
|
|
if mesh1.min.x()<mesh0.max.x()+dp.x()&&mesh0.min.x()+dp.x()<mesh1.max.x()&&mesh1.min.z()<mesh0.max.z()+dp.z()&&mesh0.min.z()+dp.z()<mesh1.max.z() {
|
2023-09-18 20:20:51 +00:00
|
|
|
//collect valid t
|
2023-09-19 01:06:03 +00:00
|
|
|
best_time=t_time;
|
2023-09-18 20:20:51 +00:00
|
|
|
best_face=Some(TreyMeshFace::Bottom);
|
2023-09-20 01:14:24 +00:00
|
|
|
break;
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
for t in zeroes2(mesh0.min.y()-mesh1.max.y(),v.y(),a.y()/2) {
|
2023-09-19 01:50:12 +00:00
|
|
|
//must collide now or in the future
|
2023-09-18 20:20:51 +00:00
|
|
|
//must beat the current soonest collision time
|
|
|
|
//must be moving towards surface
|
2023-09-27 09:12:20 +00:00
|
|
|
let t_time=body_time+Time::from(t);
|
|
|
|
if time<=t_time&&t_time<best_time&&v.y()+a.y()*t<Planar64::ZERO{
|
2023-09-19 01:06:03 +00:00
|
|
|
let dp=self.body.extrapolated_position(t_time)-p;
|
2023-09-18 20:20:51 +00:00
|
|
|
//faces must be overlapping
|
2023-09-27 09:12:20 +00:00
|
|
|
if mesh1.min.x()<mesh0.max.x()+dp.x()&&mesh0.min.x()+dp.x()<mesh1.max.x()&&mesh1.min.z()<mesh0.max.z()+dp.z()&&mesh0.min.z()+dp.z()<mesh1.max.z() {
|
2023-09-18 20:20:51 +00:00
|
|
|
//collect valid t
|
2023-09-19 01:06:03 +00:00
|
|
|
best_time=t_time;
|
2023-09-18 20:20:51 +00:00
|
|
|
best_face=Some(TreyMeshFace::Top);
|
2023-09-20 01:14:24 +00:00
|
|
|
break;
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//collect z
|
2023-09-27 09:12:20 +00:00
|
|
|
for t in zeroes2(mesh0.max.z()-mesh1.min.z(),v.z(),a.z()/2) {
|
2023-09-19 01:50:12 +00:00
|
|
|
//must collide now or in the future
|
2023-09-18 20:20:51 +00:00
|
|
|
//must beat the current soonest collision time
|
|
|
|
//must be moving towards surface
|
2023-09-27 09:12:20 +00:00
|
|
|
let t_time=body_time+Time::from(t);
|
|
|
|
if time<=t_time&&t_time<best_time&&Planar64::ZERO<v.z()+a.z()*t{
|
2023-09-19 01:06:03 +00:00
|
|
|
let dp=self.body.extrapolated_position(t_time)-p;
|
2023-09-18 20:20:51 +00:00
|
|
|
//faces must be overlapping
|
2023-09-27 09:12:20 +00:00
|
|
|
if mesh1.min.y()<mesh0.max.y()+dp.y()&&mesh0.min.y()+dp.y()<mesh1.max.y()&&mesh1.min.x()<mesh0.max.x()+dp.x()&&mesh0.min.x()+dp.x()<mesh1.max.x() {
|
2023-09-18 20:20:51 +00:00
|
|
|
//collect valid t
|
2023-09-19 01:06:03 +00:00
|
|
|
best_time=t_time;
|
2023-09-18 20:20:51 +00:00
|
|
|
best_face=Some(TreyMeshFace::Front);
|
2023-09-20 01:14:24 +00:00
|
|
|
break;
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-27 09:12:20 +00:00
|
|
|
for t in zeroes2(mesh0.min.z()-mesh1.max.z(),v.z(),a.z()/2) {
|
2023-09-19 01:50:12 +00:00
|
|
|
//must collide now or in the future
|
2023-09-18 20:20:51 +00:00
|
|
|
//must beat the current soonest collision time
|
|
|
|
//must be moving towards surface
|
2023-09-27 09:12:20 +00:00
|
|
|
let t_time=body_time+Time::from(t);
|
|
|
|
if time<=t_time&&t_time<best_time&&v.z()+a.z()*t<Planar64::ZERO{
|
2023-09-19 01:06:03 +00:00
|
|
|
let dp=self.body.extrapolated_position(t_time)-p;
|
2023-09-18 20:20:51 +00:00
|
|
|
//faces must be overlapping
|
2023-09-27 09:12:20 +00:00
|
|
|
if mesh1.min.y()<mesh0.max.y()+dp.y()&&mesh0.min.y()+dp.y()<mesh1.max.y()&&mesh1.min.x()<mesh0.max.x()+dp.x()&&mesh0.min.x()+dp.x()<mesh1.max.x() {
|
2023-09-18 20:20:51 +00:00
|
|
|
//collect valid t
|
2023-09-19 01:06:03 +00:00
|
|
|
best_time=t_time;
|
2023-09-18 20:20:51 +00:00
|
|
|
best_face=Some(TreyMeshFace::Back);
|
2023-09-20 01:14:24 +00:00
|
|
|
break;
|
2023-09-18 20:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//generate instruction
|
2023-10-18 23:21:06 +00:00
|
|
|
if let Some(face)=best_face{
|
|
|
|
return Some(TimedInstruction{
|
2023-09-19 01:06:03 +00:00
|
|
|
time: best_time,
|
2023-10-18 23:21:06 +00:00
|
|
|
instruction:PhysicsInstruction::CollisionStart(RelativeCollision{
|
2023-09-18 20:20:51 +00:00
|
|
|
face,
|
2023-10-18 23:21:06 +00:00
|
|
|
model:model_id
|
2023-09-18 20:20:51 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2023-09-08 22:55:33 +00:00
|
|
|
None
|
|
|
|
}
|
2023-09-08 18:33:20 +00:00
|
|
|
}
|
|
|
|
|
2023-09-09 00:34:26 +00:00
|
|
|
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.
|
2023-09-27 09:12:20 +00:00
|
|
|
fn next_instruction(&self,time_limit:Time) -> Option<TimedInstruction<PhysicsInstruction>> {
|
2023-09-08 18:33:20 +00:00
|
|
|
//JUST POLLING!!! NO MUTATION
|
2023-09-09 19:42:51 +00:00
|
|
|
let mut collector = crate::instruction::InstructionCollector::new(time_limit);
|
2023-09-09 00:34:26 +00:00
|
|
|
//check for collision stop instructions with curent contacts
|
2023-10-17 23:34:53 +00:00
|
|
|
//TODO: make this into a touching.next_instruction(&mut collector) member function
|
2023-10-13 02:44:46 +00:00
|
|
|
for (_,collision_data) in &self.touching.contacts {
|
2023-09-19 01:06:03 +00:00
|
|
|
collector.collect(self.predict_collision_end(self.time,time_limit,collision_data));
|
2023-09-08 18:33:20 +00:00
|
|
|
}
|
2023-10-04 21:15:04 +00:00
|
|
|
// for collision_data in &self.intersects{
|
|
|
|
// collector.collect(self.predict_collision_end2(self.time,time_limit,collision_data));
|
|
|
|
// }
|
2023-09-09 00:34:26 +00:00
|
|
|
//check for collision start instructions (against every part in the game with no optimization!!)
|
2023-09-27 09:12:20 +00:00
|
|
|
let mut aabb=crate::aabb::Aabb::default();
|
2023-10-06 06:52:04 +00:00
|
|
|
aabb.grow(self.body.extrapolated_position(self.time));
|
|
|
|
aabb.grow(self.body.extrapolated_position(time_limit));
|
|
|
|
aabb.inflate(self.style.hitbox_halfsize);
|
|
|
|
self.bvh.the_tester(&aabb,&mut |id|{
|
2023-10-13 02:44:46 +00:00
|
|
|
if !(self.touching.contacts.contains_key(&id)||self.touching.intersects.contains_key(&id)){
|
2023-10-06 06:52:04 +00:00
|
|
|
collector.collect(self.predict_collision_start(self.time,time_limit,id));
|
2023-10-04 21:15:04 +00:00
|
|
|
}
|
2023-10-06 06:52:04 +00:00
|
|
|
});
|
2023-10-13 02:44:46 +00:00
|
|
|
collector.collect(self.next_move_instruction());
|
2023-09-09 03:12:58 +00:00
|
|
|
collector.instruction()
|
2023-09-08 18:33:20 +00:00
|
|
|
}
|
2023-09-08 18:33:16 +00:00
|
|
|
}
|
2023-09-09 00:15:49 +00:00
|
|
|
|
2023-10-18 23:18:17 +00:00
|
|
|
fn teleport(body:&mut Body,touching:&mut TouchingState,style:&StyleModifiers,point:Planar64Vec3)->MoveState{
|
|
|
|
body.position=point;
|
|
|
|
//manual clear //for c in contacts{process_instruction(CollisionEnd(c))}
|
|
|
|
touching.clear();
|
|
|
|
body.acceleration=style.gravity;
|
|
|
|
MoveState::Air
|
|
|
|
//TODO: calculate contacts and determine the actual state
|
|
|
|
//touching.recalculate(body);
|
|
|
|
}
|
|
|
|
|
2023-10-18 23:15:42 +00:00
|
|
|
fn run_teleport_behaviour(teleport_behaviour:&Option<crate::model::TeleportBehaviour>,game:&mut GameMechanicsState,models:&PhysicsModels,modes:&Modes,style:&StyleModifiers,touching:&mut TouchingState,body:&mut Body,model:&ModelPhysics)->Option<MoveState>{
|
2023-10-18 23:18:17 +00:00
|
|
|
match teleport_behaviour{
|
|
|
|
Some(crate::model::TeleportBehaviour::StageElement(stage_element))=>{
|
|
|
|
if stage_element.force||game.stage_id<stage_element.stage_id{
|
|
|
|
game.stage_id=stage_element.stage_id;
|
|
|
|
}
|
|
|
|
match &stage_element.behaviour{
|
|
|
|
crate::model::StageElementBehaviour::SpawnAt=>None,
|
|
|
|
crate::model::StageElementBehaviour::Trigger
|
|
|
|
|crate::model::StageElementBehaviour::Teleport=>{
|
|
|
|
//I guess this is correct behaviour when trying to teleport to a non-existent spawn but it's still weird
|
|
|
|
let model=models.get(*modes.get_mode(stage_element.mode_id)?.get_spawn_model_id(game.stage_id)? as usize)?;
|
|
|
|
let point=model.transform.transform_point3(Planar64Vec3::Y)+Planar64Vec3::Y*(style.hitbox_halfsize.y()+Planar64::ONE/16);
|
|
|
|
Some(teleport(body,touching,style,point))
|
|
|
|
},
|
|
|
|
crate::model::StageElementBehaviour::Platform=>None,
|
|
|
|
crate::model::StageElementBehaviour::JumpLimit(_)=>None,//TODO
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Some(crate::model::TeleportBehaviour::Wormhole(wormhole))=>{
|
2023-10-18 23:15:42 +00:00
|
|
|
let origin_model=model;
|
|
|
|
let destination_model=models.get_wormhole_model(wormhole.destination_model_id)?;
|
|
|
|
//ignore the transform for now
|
|
|
|
Some(teleport(body,touching,style,body.position-origin_model.transform.translation+destination_model.transform.translation))
|
2023-10-18 23:18:17 +00:00
|
|
|
}
|
|
|
|
None=>None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-09 00:34:26 +00:00
|
|
|
impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsState {
|
2023-09-09 03:14:18 +00:00
|
|
|
fn process_instruction(&mut self, ins:TimedInstruction<PhysicsInstruction>) {
|
2023-09-19 06:36:14 +00:00
|
|
|
match &ins.instruction {
|
2023-10-05 03:04:04 +00:00
|
|
|
PhysicsInstruction::Input(PhysicsInputInstruction::Idle)
|
|
|
|
|PhysicsInstruction::Input(PhysicsInputInstruction::SetNextMouse(_))
|
|
|
|
|PhysicsInstruction::Input(PhysicsInputInstruction::ReplaceMouse(_,_))
|
|
|
|
|PhysicsInstruction::StrafeTick => (),
|
2023-10-05 06:51:39 +00:00
|
|
|
_=>println!("{}|{:?}",ins.time,ins.instruction),
|
2023-09-19 06:36:14 +00:00
|
|
|
}
|
|
|
|
//selectively update body
|
|
|
|
match &ins.instruction {
|
2023-10-19 22:04:29 +00:00
|
|
|
PhysicsInstruction::Input(PhysicsInputInstruction::Idle)=>self.time=ins.time,//idle simply updates time
|
2023-10-04 21:01:06 +00:00
|
|
|
PhysicsInstruction::Input(_)
|
|
|
|
|PhysicsInstruction::ReachWalkTargetVelocity
|
|
|
|
|PhysicsInstruction::CollisionStart(_)
|
|
|
|
|PhysicsInstruction::CollisionEnd(_)
|
2023-10-19 22:04:29 +00:00
|
|
|
|PhysicsInstruction::StrafeTick=>self.advance_time(ins.time),
|
2023-09-19 06:36:14 +00:00
|
|
|
}
|
2023-09-09 03:14:18 +00:00
|
|
|
match ins.instruction {
|
2023-09-18 20:20:51 +00:00
|
|
|
PhysicsInstruction::CollisionStart(c) => {
|
2023-10-04 21:15:04 +00:00
|
|
|
let model=c.model(&self.models).unwrap();
|
|
|
|
match &model.attributes{
|
|
|
|
PhysicsCollisionAttributes::Contact{contacting,general}=>{
|
2023-10-18 02:12:25 +00:00
|
|
|
let mut v=self.body.velocity;
|
2023-10-17 07:17:54 +00:00
|
|
|
match &contacting.contact_behaviour{
|
|
|
|
Some(crate::model::ContactingBehaviour::Surf)=>println!("I'm surfing!"),
|
2023-10-18 02:12:25 +00:00
|
|
|
&Some(crate::model::ContactingBehaviour::Elastic(elasticity))=>{
|
|
|
|
let n=c.normal(&self.models);
|
|
|
|
let d=n.dot(v)*Planar64::raw(-1-elasticity as i64);
|
|
|
|
v-=n*(d/n.dot(n));
|
|
|
|
},
|
2023-10-17 07:17:54 +00:00
|
|
|
Some(crate::model::ContactingBehaviour::Ladder(contacting_ladder))=>{
|
|
|
|
if contacting_ladder.sticky{
|
|
|
|
//kill v
|
2023-10-18 02:12:25 +00:00
|
|
|
v=Planar64Vec3::ZERO;//model.velocity
|
2023-10-17 07:17:54 +00:00
|
|
|
}
|
|
|
|
//ladder walkstate
|
2023-10-17 23:18:55 +00:00
|
|
|
let (walk_state,a)=WalkState::ladder(&self.touching,&self.body,&self.style,&self.models,self.style.get_ladder_target_velocity(&self.camera,self.controls,&self.next_mouse,self.time),&c.normal(&self.models));
|
2023-10-17 07:17:54 +00:00
|
|
|
self.move_state=MoveState::Ladder(walk_state);
|
|
|
|
self.body.acceleration=a;
|
|
|
|
}
|
2023-10-04 21:15:04 +00:00
|
|
|
None=>match &c.face {
|
2023-10-06 03:32:02 +00:00
|
|
|
TreyMeshFace::Top => {
|
2023-10-04 21:15:04 +00:00
|
|
|
//ground
|
2023-10-17 07:17:54 +00:00
|
|
|
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.body.acceleration=a;
|
2023-10-04 21:15:04 +00:00
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
}
|
2023-10-05 03:04:04 +00:00
|
|
|
//check ground
|
2023-10-13 02:44:46 +00:00
|
|
|
self.touching.insert_contact(c.model,c);
|
2023-10-18 23:18:17 +00:00
|
|
|
//I love making functions with 10 arguments to dodge the borrow checker
|
2023-10-18 23:15:42 +00:00
|
|
|
run_teleport_behaviour(&general.teleport_behaviour,&mut self.game,&self.models,&self.modes,&self.style,&mut self.touching,&mut self.body,model);
|
2023-10-04 21:15:04 +00:00
|
|
|
//flatten v
|
2023-10-13 02:44:46 +00:00
|
|
|
self.touching.constrain_velocity(&self.models,&mut v);
|
2023-10-05 03:04:04 +00:00
|
|
|
match &general.booster{
|
|
|
|
Some(booster)=>{
|
2023-10-18 02:12:25 +00:00
|
|
|
match booster{
|
|
|
|
&crate::model::GameMechanicBooster::Affine(transform)=>v=transform.transform_point3(v),
|
|
|
|
&crate::model::GameMechanicBooster::Velocity(velocity)=>v+=velocity,
|
|
|
|
&crate::model::GameMechanicBooster::Energy{direction,energy}=>todo!(),
|
|
|
|
}
|
|
|
|
self.touching.constrain_velocity(&self.models,&mut v);
|
|
|
|
},
|
|
|
|
None=>(),
|
|
|
|
}
|
|
|
|
match &general.trajectory{
|
|
|
|
Some(trajectory)=>{
|
|
|
|
match trajectory{
|
|
|
|
crate::model::GameMechanicSetTrajectory::AirTime(_) => todo!(),
|
|
|
|
crate::model::GameMechanicSetTrajectory::Height(_) => todo!(),
|
|
|
|
crate::model::GameMechanicSetTrajectory::TargetPointTime { target_point, time } => todo!(),
|
|
|
|
crate::model::GameMechanicSetTrajectory::TrajectoryTargetPoint { target_point, speed, trajectory_choice } => todo!(),
|
|
|
|
&crate::model::GameMechanicSetTrajectory::Velocity(velocity)=>v=velocity,
|
|
|
|
crate::model::GameMechanicSetTrajectory::DotVelocity { direction, dot } => todo!(),
|
|
|
|
}
|
2023-10-13 02:44:46 +00:00
|
|
|
self.touching.constrain_velocity(&self.models,&mut v);
|
2023-10-05 03:04:04 +00:00
|
|
|
},
|
|
|
|
None=>(),
|
|
|
|
}
|
2023-10-04 21:15:04 +00:00
|
|
|
self.body.velocity=v;
|
2023-10-13 02:44:46 +00:00
|
|
|
if self.style.get_control(StyleModifiers::CONTROL_JUMP,self.controls){
|
2023-10-04 21:15:04 +00:00
|
|
|
self.jump();
|
|
|
|
}
|
|
|
|
self.refresh_walk_target();
|
|
|
|
},
|
|
|
|
PhysicsCollisionAttributes::Intersect{intersecting,general}=>{
|
|
|
|
//I think that setting the velocity to 0 was preventing surface contacts from entering an infinite loop
|
2023-10-13 02:44:46 +00:00
|
|
|
self.touching.insert_intersect(c.model,c);
|
2023-10-18 23:15:42 +00:00
|
|
|
run_teleport_behaviour(&general.teleport_behaviour,&mut self.game,&self.models,&self.modes,&self.style,&mut self.touching,&mut self.body,model);
|
2023-10-04 21:15:04 +00:00
|
|
|
},
|
2023-09-20 00:53:29 +00:00
|
|
|
}
|
2023-09-18 20:20:51 +00:00
|
|
|
},
|
|
|
|
PhysicsInstruction::CollisionEnd(c) => {
|
2023-10-04 21:15:04 +00:00
|
|
|
let model=c.model(&self.models).unwrap();
|
|
|
|
match &model.attributes{
|
|
|
|
PhysicsCollisionAttributes::Contact{contacting,general}=>{
|
2023-10-13 02:44:46 +00:00
|
|
|
self.touching.remove_contact(c.model);//remove contact before calling contact_constrain_acceleration
|
2023-10-04 21:15:04 +00:00
|
|
|
let mut a=self.style.gravity;
|
2023-10-13 02:44:46 +00:00
|
|
|
self.touching.constrain_acceleration(&self.models,&mut a);
|
2023-10-04 21:15:04 +00:00
|
|
|
self.body.acceleration=a;
|
|
|
|
//check ground
|
2023-10-17 23:34:53 +00:00
|
|
|
//self.touching.get_move_state();
|
2023-10-04 21:15:04 +00:00
|
|
|
match &c.face {
|
2023-10-06 03:32:02 +00:00
|
|
|
TreyMeshFace::Top => {
|
2023-10-17 07:17:54 +00:00
|
|
|
//TODO: make this more advanced checking contacts
|
2023-10-13 02:44:46 +00:00
|
|
|
self.move_state=MoveState::Air;
|
2023-10-04 21:15:04 +00:00
|
|
|
},
|
2023-10-17 07:17:54 +00:00
|
|
|
_=>self.refresh_walk_target(),
|
2023-10-04 21:15:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
PhysicsCollisionAttributes::Intersect{intersecting,general}=>{
|
2023-10-13 02:44:46 +00:00
|
|
|
self.touching.remove_intersect(c.model);
|
2023-10-04 21:15:04 +00:00
|
|
|
},
|
|
|
|
}
|
2023-09-18 20:20:51 +00:00
|
|
|
},
|
2023-09-09 03:14:18 +00:00
|
|
|
PhysicsInstruction::StrafeTick => {
|
2023-10-05 03:04:04 +00:00
|
|
|
let camera_mat=self.camera.simulate_move_rotation_y(self.camera.mouse.lerp(&self.next_mouse,self.time).x);
|
2023-10-03 23:52:45 +00:00
|
|
|
let control_dir=camera_mat*self.style.get_control_dir(self.controls);
|
2023-09-20 00:53:29 +00:00
|
|
|
let d=self.body.velocity.dot(control_dir);
|
2023-10-03 05:45:20 +00:00
|
|
|
if d<self.style.mv {
|
2023-09-27 09:12:20 +00:00
|
|
|
let mut v=self.body.velocity+control_dir*(self.style.mv-d);
|
2023-10-13 02:44:46 +00:00
|
|
|
self.touching.constrain_velocity(&self.models,&mut v);
|
2023-09-19 06:36:14 +00:00
|
|
|
self.body.velocity=v;
|
2023-09-09 03:14:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PhysicsInstruction::ReachWalkTargetVelocity => {
|
2023-10-13 02:44:46 +00:00
|
|
|
match &mut self.move_state{
|
|
|
|
MoveState::Air|MoveState::Water=>(),
|
|
|
|
MoveState::Walk(walk_state)|MoveState::Ladder(walk_state)=>{
|
2023-10-17 07:17:54 +00:00
|
|
|
match &mut walk_state.state{
|
|
|
|
WalkEnum::Reached=>(),
|
|
|
|
WalkEnum::Transient(walk_target)=>{
|
|
|
|
//precisely set velocity
|
|
|
|
let mut a=self.style.gravity;
|
|
|
|
self.touching.constrain_acceleration(&self.models,&mut a);
|
|
|
|
self.body.acceleration=a;
|
|
|
|
let mut v=walk_target.velocity;
|
|
|
|
self.touching.constrain_velocity(&self.models,&mut v);
|
|
|
|
self.body.velocity=v;
|
|
|
|
walk_state.state=WalkEnum::Reached;
|
|
|
|
},
|
|
|
|
}
|
2023-10-13 02:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-19 06:36:14 +00:00
|
|
|
},
|
2023-09-20 00:53:29 +00:00
|
|
|
PhysicsInstruction::Input(input_instruction) => {
|
2023-10-02 02:25:16 +00:00
|
|
|
let mut refresh_walk_target=true;
|
2023-09-20 00:53:29 +00:00
|
|
|
match input_instruction{
|
2023-10-05 03:04:04 +00:00
|
|
|
PhysicsInputInstruction::SetNextMouse(m) => {
|
2023-09-27 09:12:20 +00:00
|
|
|
self.camera.move_mouse(self.next_mouse.pos);
|
2023-10-05 03:04:04 +00:00
|
|
|
(self.camera.mouse,self.next_mouse)=(self.next_mouse.clone(),m);
|
|
|
|
},
|
|
|
|
PhysicsInputInstruction::ReplaceMouse(m0,m1) => {
|
2023-09-27 09:12:20 +00:00
|
|
|
self.camera.move_mouse(m0.pos);
|
2023-10-05 03:04:04 +00:00
|
|
|
(self.camera.mouse,self.next_mouse)=(m0,m1);
|
2023-09-20 00:53:29 +00:00
|
|
|
},
|
2023-10-05 03:04:04 +00:00
|
|
|
PhysicsInputInstruction::SetMoveForward(s) => self.set_control(StyleModifiers::CONTROL_MOVEFORWARD,s),
|
|
|
|
PhysicsInputInstruction::SetMoveLeft(s) => self.set_control(StyleModifiers::CONTROL_MOVELEFT,s),
|
|
|
|
PhysicsInputInstruction::SetMoveBack(s) => self.set_control(StyleModifiers::CONTROL_MOVEBACK,s),
|
|
|
|
PhysicsInputInstruction::SetMoveRight(s) => self.set_control(StyleModifiers::CONTROL_MOVERIGHT,s),
|
|
|
|
PhysicsInputInstruction::SetMoveUp(s) => self.set_control(StyleModifiers::CONTROL_MOVEUP,s),
|
|
|
|
PhysicsInputInstruction::SetMoveDown(s) => self.set_control(StyleModifiers::CONTROL_MOVEDOWN,s),
|
|
|
|
PhysicsInputInstruction::SetJump(s) => {
|
2023-10-03 23:52:45 +00:00
|
|
|
self.set_control(StyleModifiers::CONTROL_JUMP,s);
|
2023-10-13 02:44:46 +00:00
|
|
|
self.jump();
|
2023-10-17 07:17:54 +00:00
|
|
|
refresh_walk_target=false;
|
2023-09-20 00:53:29 +00:00
|
|
|
},
|
2023-10-05 03:04:04 +00:00
|
|
|
PhysicsInputInstruction::SetZoom(s) => {
|
2023-10-03 23:52:45 +00:00
|
|
|
self.set_control(StyleModifiers::CONTROL_ZOOM,s);
|
2023-10-02 02:25:16 +00:00
|
|
|
refresh_walk_target=false;
|
2023-09-20 00:53:29 +00:00
|
|
|
},
|
2023-10-05 03:04:04 +00:00
|
|
|
PhysicsInputInstruction::Reset => {
|
2023-09-20 00:53:29 +00:00
|
|
|
//temp
|
|
|
|
self.body.position=self.spawn_point;
|
2023-09-27 09:12:20 +00:00
|
|
|
self.body.velocity=Planar64Vec3::ZERO;
|
2023-09-20 00:53:29 +00:00
|
|
|
//manual clear //for c in self.contacts{process_instruction(CollisionEnd(c))}
|
2023-10-13 02:44:46 +00:00
|
|
|
self.touching.clear();
|
2023-10-03 05:45:20 +00:00
|
|
|
self.body.acceleration=self.style.gravity;
|
2023-10-13 02:44:46 +00:00
|
|
|
self.move_state=MoveState::Air;
|
2023-10-02 02:25:16 +00:00
|
|
|
refresh_walk_target=false;
|
2023-09-20 00:53:29 +00:00
|
|
|
},
|
2023-10-05 03:04:04 +00:00
|
|
|
PhysicsInputInstruction::Idle => {refresh_walk_target=false;},//literally idle!
|
2023-09-20 00:53:29 +00:00
|
|
|
}
|
|
|
|
if refresh_walk_target{
|
|
|
|
self.refresh_walk_target();
|
2023-09-19 04:10:07 +00:00
|
|
|
}
|
2023-09-19 06:36:14 +00:00
|
|
|
},
|
2023-09-09 03:14:18 +00:00
|
|
|
}
|
2023-09-09 00:15:49 +00:00
|
|
|
}
|
2023-09-23 02:41:27 +00:00
|
|
|
}
|