Compare commits

...

51 Commits
master ... temp

Author SHA1 Message Date
d3bc8e0e85 how does it know what to do 2024-09-27 14:50:53 -07:00
b4406522f5 plumb one power of m through to the finish line 2024-09-27 13:37:03 -07:00
4e111df791 fix walk calculations 2024-09-27 13:32:33 -07:00
def5497ec1 fix is_slope_walkable 2024-09-27 12:34:12 -07:00
ba6a2bd670 column major 2024-09-26 15:06:38 -07:00
b879b09369 vec3::{MIN,MAX} 2024-09-26 14:37:03 -07:00
e7a881ddc0 fix aabb blunder 2024-09-25 14:26:34 -07:00
f4767a1a3e dep 2024-09-25 10:12:38 -07:00
22087c53b3 mat3 from_rotation_yx + from_rotation_y 2024-09-18 15:22:21 -07:00
759648ebfb wacky angle sin cos test 2024-09-18 15:22:05 -07:00
f4903abd33 angle sin cos 2024-09-18 15:21:54 -07:00
83b948200c actually test timer scale 2024-09-18 15:15:29 -07:00
7d89eb6d0c associate const 2024-09-18 15:15:16 -07:00
9ae1c3505d add Time tests 2024-09-18 11:33:13 -07:00
43e1f8a173 add Time*i64 2024-09-18 11:33:08 -07:00
5790863111 fix Time from Planar64 2024-09-18 11:32:52 -07:00
c48b9d63bd vec3::raw_array 2024-09-16 15:47:57 -07:00
980db44c9d Fixed<1,_>::to_raw 2024-09-16 15:47:35 -07:00
af1939a576 impl Into<glam::Mat4> for Planar64Affine3 2024-09-16 15:02:03 -07:00
8bccc2fe66 Time assign operators 2024-09-16 14:06:37 -07:00
0d8ab36031 Time from ratio 2024-09-16 14:06:30 -07:00
daa86f7228 reexport stupid trait 2024-09-16 14:06:19 -07:00
406448dcca reexport Divide trait 2024-09-16 11:46:24 -07:00
734be35937 Time::to_ratio 2024-09-13 13:59:28 -07:00
4bd5815184 more vec3 consts 2024-09-13 13:59:18 -07:00
1a5a48d303 dedicated affine type means less math 2024-09-13 13:22:00 -07:00
aae9ba5e80 define more functions 2024-09-12 12:16:19 -07:00
b9f280f94b use new convenience functions 2024-09-12 10:57:54 -07:00
e8a6f4dab3 write the whole thing 2024-09-11 15:14:56 -07:00
aaad5d5c22 integer: tweak Time 2024-09-11 14:30:03 -07:00
5c059ed17d huge 2024-09-11 14:29:45 -07:00
bd72b50db6 iterate 2024-09-11 13:15:23 -07:00
5b5ed4f38e ratio ops 2024-09-11 13:01:39 -07:00
e845ce246a wip 2024-09-10 14:41:47 -07:00
18fdbb9a99 vector shift implemented 2024-09-10 14:41:18 -07:00
5bb4491e0f ops rewrite 2024-09-10 13:46:34 -07:00
ab6c5d583d array-backed rewrite 2024-09-06 13:26:07 -07:00
dc79b4011d no-traits rewrite 2024-09-06 13:26:07 -07:00
bd6b7ff217 move zeroes to fixed_wide 2024-09-06 13:26:04 -07:00
74afcf457d built in calculation 2024-08-30 22:11:42 -07:00
7881a090c3 it is a matrix 2024-08-30 22:11:24 -07:00
1f29021d4a affine deleted 2024-08-30 12:33:00 -07:00
b8714fbbc8 wip gameplay_style 2024-08-29 17:23:13 -07:00
d9a95e9284 reexport traits 2024-08-29 17:23:10 -07:00
3b54bc8326 bvec 2024-08-28 13:29:42 -07:00
0db0f2854e cordic 2024-08-28 12:32:17 -07:00
40799945ce wip 2024-08-28 12:32:17 -07:00
3bb280a787 ruin zeroes 2024-08-28 12:32:17 -07:00
76b1e6af54 aabb 2024-08-28 12:31:16 -07:00
8a13307d98 the replacement of all time 2024-08-27 14:26:51 -07:00
585d6c8a92 add deps 2024-08-27 13:51:27 -07:00
10 changed files with 377 additions and 817 deletions

45
Cargo.lock generated
View File

@ -4,9 +4,9 @@ version = 3
[[package]] [[package]]
name = "arrayvec" name = "arrayvec"
version = "0.7.4" version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
@ -14,6 +14,23 @@ version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]]
name = "bnum"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50202def95bf36cb7d1d7a7962cea1c36a3f8ad42425e5d2b71d7acb8041b5b8"
[[package]]
name = "fixed_wide"
version = "0.1.0"
source = "git+https://git.itzana.me/StrafesNET/fixed_wide_vectors?rev=438d0ec6ec4ae97d13d950ea317ce324dd205e10#438d0ec6ec4ae97d13d950ea317ce324dd205e10"
dependencies = [
"arrayvec",
"bnum",
"paste",
"ratio_ops",
]
[[package]] [[package]]
name = "glam" name = "glam"
version = "0.28.0" version = "0.28.0"
@ -31,6 +48,22 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "linear_ops"
version = "0.1.0"
source = "git+https://git.itzana.me/StrafesNET/fixed_wide_vectors?rev=438d0ec6ec4ae97d13d950ea317ce324dd205e10#438d0ec6ec4ae97d13d950ea317ce324dd205e10"
dependencies = [
"fixed_wide",
"paste",
"ratio_ops",
]
[[package]]
name = "paste"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.86" version = "1.0.86"
@ -49,14 +82,22 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "ratio_ops"
version = "0.1.0"
source = "git+https://git.itzana.me/StrafesNET/fixed_wide_vectors?rev=438d0ec6ec4ae97d13d950ea317ce324dd205e10#438d0ec6ec4ae97d13d950ea317ce324dd205e10"
[[package]] [[package]]
name = "strafesnet_common" name = "strafesnet_common"
version = "0.4.1" version = "0.4.1"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"bitflags", "bitflags",
"fixed_wide",
"glam", "glam",
"id", "id",
"linear_ops",
"ratio_ops",
] ]
[[package]] [[package]]

View File

@ -12,5 +12,8 @@ authors = ["Rhys Lloyd <krakow20@gmail.com>"]
[dependencies] [dependencies]
arrayvec = "0.7.4" arrayvec = "0.7.4"
bitflags = "2.6.0" bitflags = "2.6.0"
fixed_wide = { git = "https://git.itzana.me/StrafesNET/fixed_wide_vectors", rev = "438d0ec6ec4ae97d13d950ea317ce324dd205e10", features = ["deferred-division","zeroes","wide-mul"] }
linear_ops = { git = "https://git.itzana.me/StrafesNET/fixed_wide_vectors", rev = "438d0ec6ec4ae97d13d950ea317ce324dd205e10", features = ["deferred-division","named-fields"] }
ratio_ops = { git = "https://git.itzana.me/StrafesNET/fixed_wide_vectors", rev = "438d0ec6ec4ae97d13d950ea317ce324dd205e10" }
glam = "0.28.0" glam = "0.28.0"
id = { version = "0.1.0", registry = "strafesnet" } id = { version = "0.1.0", registry = "strafesnet" }

View File

@ -1,4 +1,4 @@
use crate::integer::Planar64Vec3; use crate::integer::{vec3,Planar64Vec3};
#[derive(Clone)] #[derive(Clone)]
pub struct Aabb{ pub struct Aabb{
@ -8,7 +8,7 @@ pub struct Aabb{
impl Default for Aabb{ impl Default for Aabb{
fn default()->Self{ fn default()->Self{
Self{min:Planar64Vec3::MAX,max:Planar64Vec3::MIN} Self{min:vec3::MAX,max:vec3::MIN}
} }
} }
@ -35,13 +35,14 @@ impl Aabb{
self.max+=hs; self.max+=hs;
} }
pub fn intersects(&self,aabb:&Aabb)->bool{ pub fn intersects(&self,aabb:&Aabb)->bool{
(self.min.cmplt(aabb.max)&aabb.min.cmplt(self.max)).all() let bvec=self.min.lt(aabb.max)&aabb.min.lt(self.max);
bvec.all()
} }
pub fn size(&self)->Planar64Vec3{ pub fn size(&self)->Planar64Vec3{
self.max-self.min self.max-self.min
} }
pub fn center(&self)->Planar64Vec3{ pub fn center(&self)->Planar64Vec3{
self.min.midpoint(self.max) self.min+(self.max-self.min)>>1
} }
//probably use floats for area & volume because we don't care about precision //probably use floats for area & volume because we don't care about precision
// pub fn area_weight(&self)->f32{ // pub fn area_weight(&self)->f32{

View File

@ -126,9 +126,9 @@ fn generate_bvh_node<T>(boxen:Vec<(T,Aabb)>,force:bool)->BvhNode<T>{
let mut sort_z=Vec::with_capacity(n); let mut sort_z=Vec::with_capacity(n);
for (i,(_,aabb)) in boxen.iter().enumerate(){ for (i,(_,aabb)) in boxen.iter().enumerate(){
let center=aabb.center(); let center=aabb.center();
sort_x.push((i,center.x())); sort_x.push((i,center.x));
sort_y.push((i,center.y())); sort_y.push((i,center.y));
sort_z.push((i,center.z())); sort_z.push((i,center.z));
} }
sort_x.sort_by(|tup0,tup1|tup0.1.cmp(&tup1.1)); sort_x.sort_by(|tup0,tup1|tup0.1.cmp(&tup1.1));
sort_y.sort_by(|tup0,tup1|tup0.1.cmp(&tup1.1)); sort_y.sort_by(|tup0,tup1|tup0.1.cmp(&tup1.1));

View File

@ -38,10 +38,11 @@ impl Booster{
pub fn boost(&self,velocity:Planar64Vec3)->Planar64Vec3{ pub fn boost(&self,velocity:Planar64Vec3)->Planar64Vec3{
match self{ match self{
&Booster::Velocity(boost_velocity)=>velocity+boost_velocity, &Booster::Velocity(boost_velocity)=>velocity+boost_velocity,
&Booster::Energy{direction,energy}=>{ &Booster::Energy{..}=>{
let d=direction.dot(velocity); todo!()
//let d=direction.dot(velocity);
//TODO: think about negative //TODO: think about negative
velocity+direction.with_length((d*d+energy).sqrt()-d) //velocity+direction.with_length((d*d+energy).sqrt()-d)
}, },
Booster::AirTime(_)=>todo!(), Booster::AirTime(_)=>todo!(),
Booster::Height(_)=>todo!(), Booster::Height(_)=>todo!(),

View File

@ -1,6 +1,6 @@
const VALVE_SCALE:Planar64=Planar64::raw(1<<28);// 1/16 const VALVE_SCALE:Planar64=Planar64::raw(1<<28);// 1/16
use crate::integer::{Time,Ratio64,Planar64,Planar64Vec3}; use crate::integer::{int,vec3::int as int3,Time,Ratio64,Planar64,Planar64Vec3};
use crate::controls_bitflag::Controls; use crate::controls_bitflag::Controls;
#[derive(Clone,Debug)] #[derive(Clone,Debug)]
@ -65,19 +65,21 @@ impl JumpImpulse{
mass:Planar64, mass:Planar64,
)->Planar64Vec3{ )->Planar64Vec3{
match self{ match self{
&JumpImpulse::Time(time)=>velocity-*gravity*time, &JumpImpulse::Time(time)=>velocity-(*gravity*time).map(|t|t.divide().fix_1()),
&JumpImpulse::Height(height)=>{ &JumpImpulse::Height(height)=>{
//height==-v.y*v.y/(2*g.y); //height==-v.y*v.y/(2*g.y);
//use energy to determine max height //use energy to determine max height
let g=gravity.length(); let gg=gravity.length_squared();
let v_g=gravity.dot(velocity)/g; let g=gg.sqrt().fix_1();
let v_g=gravity.dot(velocity);
//do it backwards //do it backwards
velocity-gravity.with_length((v_g*v_g+height*g*2).sqrt()+v_g) let radicand=v_g*v_g+(g*height*2).fix_4();
velocity-(*gravity*(radicand.sqrt().fix_2()+v_g)/gg).divide().fix_1()
}, },
&JumpImpulse::Linear(jump_speed)=>velocity+jump_dir.with_length(jump_speed), &JumpImpulse::Linear(jump_speed)=>velocity+(jump_dir*jump_speed/jump_dir.length()).divide().fix_1(),
&JumpImpulse::Energy(energy)=>{ &JumpImpulse::Energy(energy)=>{
//calculate energy //calculate energy
let e=gravity.dot(velocity); //let e=gravity.dot(velocity);
//add //add
//you get the idea //you get the idea
todo!() todo!()
@ -88,10 +90,10 @@ impl JumpImpulse{
pub fn get_jump_deltav(&self,gravity:&Planar64Vec3,mass:Planar64)->Planar64{ pub fn get_jump_deltav(&self,gravity:&Planar64Vec3,mass:Planar64)->Planar64{
//gravity.length() is actually the proper calculation because the jump is always opposite the gravity direction //gravity.length() is actually the proper calculation because the jump is always opposite the gravity direction
match self{ match self{
&JumpImpulse::Time(time)=>gravity.length()*(time/2), &JumpImpulse::Time(time)=>(gravity.length().fix_1()*time/2).divide().fix_1(),
&JumpImpulse::Height(height)=>(gravity.length()*height*2).sqrt(), &JumpImpulse::Height(height)=>(gravity.length()*height*2).sqrt().fix_1(),
&JumpImpulse::Linear(deltav)=>deltav, &JumpImpulse::Linear(deltav)=>deltav,
&JumpImpulse::Energy(energy)=>(energy*2/mass).sqrt(), &JumpImpulse::Energy(energy)=>(energy.sqrt()*2/mass.sqrt()).divide().fix_1(),
} }
} }
} }
@ -123,9 +125,10 @@ impl JumpSettings{
None=>rel_velocity, None=>rel_velocity,
}; };
let j=boost_vel.dot(jump_dir); let j=boost_vel.dot(jump_dir);
if j<jump_speed{ let js=jump_speed.fix_2();
if j<js{
//weak booster: just do a regular jump //weak booster: just do a regular jump
boost_vel+jump_dir.with_length(jump_speed-j) boost_vel+jump_dir.with_length(js-j).divide().fix_1()
}else{ }else{
//activate booster normally, jump does nothing //activate booster normally, jump does nothing
boost_vel boost_vel
@ -138,12 +141,13 @@ impl JumpSettings{
None=>rel_velocity, None=>rel_velocity,
}; };
let j=boost_vel.dot(jump_dir); let j=boost_vel.dot(jump_dir);
if j<jump_speed{ let js=jump_speed.fix_2();
if j<js{
//speed in direction of jump cannot be lower than amount //speed in direction of jump cannot be lower than amount
boost_vel+jump_dir.with_length(jump_speed-j) boost_vel+jump_dir.with_length(js-j).divide().fix_1()
}else{ }else{
//boost and jump add together //boost and jump add together
boost_vel+jump_dir.with_length(jump_speed) boost_vel+jump_dir.with_length(js).divide().fix_1()
} }
} }
(false,JumpCalculation::Max)=>{ (false,JumpCalculation::Max)=>{
@ -154,9 +158,10 @@ impl JumpSettings{
None=>rel_velocity, None=>rel_velocity,
}; };
let boost_dot=boost_vel.dot(jump_dir); let boost_dot=boost_vel.dot(jump_dir);
if boost_dot<jump_speed{ let js=jump_speed.fix_2();
if boost_dot<js{
//weak boost is extended to jump speed //weak boost is extended to jump speed
boost_vel+jump_dir.with_length(jump_speed-boost_dot) boost_vel+jump_dir.with_length(js-boost_dot).divide().fix_1()
}else{ }else{
//activate booster normally, jump does nothing //activate booster normally, jump does nothing
boost_vel boost_vel
@ -168,7 +173,7 @@ impl JumpSettings{
Some(booster)=>booster.boost(rel_velocity), Some(booster)=>booster.boost(rel_velocity),
None=>rel_velocity, None=>rel_velocity,
}; };
boost_vel+jump_dir.with_length(jump_speed) boost_vel+jump_dir.with_length(jump_speed).divide().fix_1()
}, },
} }
} }
@ -261,8 +266,9 @@ pub struct StrafeSettings{
impl StrafeSettings{ impl StrafeSettings{
pub fn tick_velocity(&self,velocity:Planar64Vec3,control_dir:Planar64Vec3)->Option<Planar64Vec3>{ pub fn tick_velocity(&self,velocity:Planar64Vec3,control_dir:Planar64Vec3)->Option<Planar64Vec3>{
let d=velocity.dot(control_dir); let d=velocity.dot(control_dir);
match d<self.mv{ let mv=self.mv.fix_2();
true=>Some(velocity+control_dir*self.air_accel_limit.map_or(self.mv-d,|limit|limit.min(self.mv-d))), match d<mv{
true=>Some(velocity+(control_dir*self.air_accel_limit.map_or(mv-d,|limit|limit.fix_2().min(mv-d))).fix_1()),
false=>None, false=>None,
} }
} }
@ -283,7 +289,7 @@ pub struct PropulsionSettings{
} }
impl PropulsionSettings{ impl PropulsionSettings{
pub fn acceleration(&self,control_dir:Planar64Vec3)->Planar64Vec3{ pub fn acceleration(&self,control_dir:Planar64Vec3)->Planar64Vec3{
control_dir*self.magnitude (control_dir*self.magnitude).fix_1()
} }
} }
@ -303,38 +309,40 @@ pub struct WalkSettings{
impl WalkSettings{ impl WalkSettings{
pub fn accel(&self,target_diff:Planar64Vec3,gravity:Planar64Vec3)->Planar64{ pub fn accel(&self,target_diff:Planar64Vec3,gravity:Planar64Vec3)->Planar64{
//TODO: fallible walk accel //TODO: fallible walk accel
let diff_len=target_diff.length(); let diff_len=target_diff.length().fix_1();
let friction=if diff_len<self.accelerate.topspeed{ let friction=if diff_len<self.accelerate.topspeed{
self.static_friction self.static_friction
}else{ }else{
self.kinetic_friction self.kinetic_friction
}; };
self.accelerate.accel.min(-Planar64Vec3::Y.dot(gravity)*friction) self.accelerate.accel.min((-gravity.y*friction).fix_1())
} }
pub fn get_walk_target_velocity(&self,control_dir:Planar64Vec3,normal:Planar64Vec3)->Planar64Vec3{ pub fn get_walk_target_velocity(&self,control_dir:Planar64Vec3,normal:Planar64Vec3)->Planar64Vec3{
if control_dir==Planar64Vec3::ZERO{ if control_dir==crate::integer::vec3::ZERO{
return control_dir; return control_dir;
} }
let n=normal.length(); let nn=normal.length_squared();
let m=control_dir.length(); let mm=control_dir.length_squared();
let d=normal.dot(control_dir)/m; let nnmm=nn*mm;
if d<n{ let d=normal.dot(control_dir);
let dd=d*d;
if dd<nnmm{
let cr=normal.cross(control_dir); let cr=normal.cross(control_dir);
if cr==Planar64Vec3::ZERO{ if cr==crate::integer::vec3::ZERO_2{
Planar64Vec3::ZERO crate::integer::vec3::ZERO
}else{ }else{
cr.cross(normal)*(self.accelerate.topspeed/(n*(n*n-d*d).sqrt()*m)) (cr.cross(normal)*self.accelerate.topspeed/((nn*(nnmm-dd)).sqrt())).divide().fix_1()
} }
}else{ }else{
Planar64Vec3::ZERO crate::integer::vec3::ZERO
} }
} }
pub fn is_slope_walkable(&self,normal:Planar64Vec3,up:Planar64Vec3)->bool{ pub fn is_slope_walkable(&self,normal:Planar64Vec3,up:Planar64Vec3)->bool{
//normal is not guaranteed to be unit length //normal is not guaranteed to be unit length
let ny=normal.dot(up); let ny=normal.dot(up);
let h=normal.length(); let h=normal.length().fix_1();
//remember this is a normal vector //remember this is a normal vector
Planar64::ZERO<ny&&h*self.surf_dot<ny ny.is_positive()&&h*self.surf_dot<ny
} }
} }
@ -351,32 +359,35 @@ impl LadderSettings{
self.accelerate.accel self.accelerate.accel
} }
pub fn get_ladder_target_velocity(&self,mut control_dir:Planar64Vec3,normal:Planar64Vec3)->Planar64Vec3{ pub fn get_ladder_target_velocity(&self,mut control_dir:Planar64Vec3,normal:Planar64Vec3)->Planar64Vec3{
if control_dir==Planar64Vec3::ZERO{ if control_dir==crate::integer::vec3::ZERO{
return control_dir; return control_dir;
} }
let n=normal.length(); let nn=normal.length_squared();
let m=control_dir.length(); let mm=control_dir.length_squared();
let mut d=normal.dot(control_dir)/m; let nnmm=nn*mm;
if d< -self.dot*n{ let d=normal.dot(control_dir);
control_dir=Planar64Vec3::Y*m; let mut dd=d*d;
d=normal.y(); if (self.dot*self.dot*nnmm).fix_4()<dd{
}else if self.dot*n<d{ if d.is_negative(){
control_dir=Planar64Vec3::NEG_Y*m; control_dir=Planar64Vec3::new([Planar64::ZERO,mm.fix_1(),Planar64::ZERO]);
d=-normal.y(); }else{
control_dir=Planar64Vec3::new([Planar64::ZERO,-mm.fix_1(),Planar64::ZERO]);
}
dd=(normal.y*normal.y).fix_4();
} }
//n=d if you are standing on top of a ladder and press E. //n=d if you are standing on top of a ladder and press E.
//two fixes: //two fixes:
//- ladder movement is not allowed on walkable surfaces //- ladder movement is not allowed on walkable surfaces
//- fix the underlying issue //- fix the underlying issue
if d.get().unsigned_abs()<n.get().unsigned_abs(){ if dd<nnmm{
let cr=normal.cross(control_dir); let cr=normal.cross(control_dir);
if cr==Planar64Vec3::ZERO{ if cr==crate::integer::vec3::ZERO_2{
Planar64Vec3::ZERO crate::integer::vec3::ZERO
}else{ }else{
cr.cross(normal)*(self.accelerate.topspeed/(n*(n*n-d*d).sqrt())) (cr.cross(normal)*self.accelerate.topspeed/((nn*(nnmm-dd)).sqrt())).divide().fix_1()
} }
}else{ }else{
Planar64Vec3::ZERO crate::integer::vec3::ZERO
} }
} }
} }
@ -399,22 +410,22 @@ pub struct Hitbox{
impl Hitbox{ impl Hitbox{
pub fn roblox()->Self{ pub fn roblox()->Self{
Self{ Self{
halfsize:Planar64Vec3::int(2,5,2)/2, halfsize:int3(2,5,2)>>1,
mesh:HitboxMesh::Cylinder, mesh:HitboxMesh::Cylinder,
} }
} }
pub fn source()->Self{ pub fn source()->Self{
Self{ Self{
halfsize:Planar64Vec3::int(33,73,33)/2*VALVE_SCALE, halfsize:((int3(33,73,33)>>1)*VALVE_SCALE).fix_1(),
mesh:HitboxMesh::Box, mesh:HitboxMesh::Box,
} }
} }
} }
impl StyleModifiers{ impl StyleModifiers{
pub const RIGHT_DIR:Planar64Vec3=Planar64Vec3::X; pub const RIGHT_DIR:Planar64Vec3=crate::integer::vec3::X;
pub const UP_DIR:Planar64Vec3=Planar64Vec3::Y; pub const UP_DIR:Planar64Vec3=crate::integer::vec3::Y;
pub const FORWARD_DIR:Planar64Vec3=Planar64Vec3::NEG_Z; pub const FORWARD_DIR:Planar64Vec3=crate::integer::vec3::NEG_Z;
pub fn neo()->Self{ pub fn neo()->Self{
Self{ Self{
@ -423,38 +434,38 @@ impl StyleModifiers{
strafe:Some(StrafeSettings{ strafe:Some(StrafeSettings{
enable:ControlsActivation::full_2d(), enable:ControlsActivation::full_2d(),
air_accel_limit:None, air_accel_limit:None,
mv:Planar64::int(3), mv:int(3),
tick_rate:Ratio64::new(64,Time::ONE_SECOND.nanos() as u64).unwrap(), tick_rate:Ratio64::new(64,Time::ONE_SECOND.nanos() as u64).unwrap(),
}), }),
jump:Some(JumpSettings{ jump:Some(JumpSettings{
impulse:JumpImpulse::Energy(Planar64::int(512)), impulse:JumpImpulse::Energy(int(512)),
calculation:JumpCalculation::JumpThenBoost, calculation:JumpCalculation::JumpThenBoost,
limit_minimum:false, limit_minimum:false,
}), }),
gravity:Planar64Vec3::int(0,-80,0), gravity:int3(0,-80,0),
mass:Planar64::int(1), mass:int(1),
rocket:None, rocket:None,
walk:Some(WalkSettings{ walk:Some(WalkSettings{
accelerate:AccelerateSettings{ accelerate:AccelerateSettings{
topspeed:Planar64::int(16), topspeed:int(16),
accel:Planar64::int(80), accel:int(80),
}, },
static_friction:Planar64::int(2), static_friction:int(2),
kinetic_friction:Planar64::int(3),//unrealistic: kinetic friction is typically lower than static kinetic_friction:int(3),//unrealistic: kinetic friction is typically lower than static
surf_dot:Planar64::int(3)/4, surf_dot:int(3)/4,
}), }),
ladder:Some(LadderSettings{ ladder:Some(LadderSettings{
accelerate:AccelerateSettings{ accelerate:AccelerateSettings{
topspeed:Planar64::int(16), topspeed:int(16),
accel:Planar64::int(160), accel:int(160),
}, },
dot:(Planar64::int(1)/2).sqrt(), dot:(int(1)/2).sqrt(),
}), }),
swim:Some(PropulsionSettings{ swim:Some(PropulsionSettings{
magnitude:Planar64::int(12), magnitude:int(12),
}), }),
hitbox:Hitbox::roblox(), hitbox:Hitbox::roblox(),
camera_offset:Planar64Vec3::int(0,2,0),//4.5-2.5=2 camera_offset:int3(0,2,0),//4.5-2.5=2
} }
} }
@ -465,7 +476,7 @@ impl StyleModifiers{
strafe:Some(StrafeSettings{ strafe:Some(StrafeSettings{
enable:ControlsActivation::full_2d(), enable:ControlsActivation::full_2d(),
air_accel_limit:None, air_accel_limit:None,
mv:Planar64::int(27)/10, mv:int(27)/10,
tick_rate:Ratio64::new(100,Time::ONE_SECOND.nanos() as u64).unwrap(), tick_rate:Ratio64::new(100,Time::ONE_SECOND.nanos() as u64).unwrap(),
}), }),
jump:Some(JumpSettings{ jump:Some(JumpSettings{
@ -473,35 +484,35 @@ impl StyleModifiers{
calculation:JumpCalculation::Max, calculation:JumpCalculation::Max,
limit_minimum:true, limit_minimum:true,
}), }),
gravity:Planar64Vec3::int(0,-100,0), gravity:int3(0,-100,0),
mass:Planar64::int(1), mass:int(1),
rocket:None, rocket:None,
walk:Some(WalkSettings{ walk:Some(WalkSettings{
accelerate:AccelerateSettings{ accelerate:AccelerateSettings{
topspeed:Planar64::int(18), topspeed:int(18),
accel:Planar64::int(90), accel:int(90),
}, },
static_friction:Planar64::int(2), static_friction:int(2),
kinetic_friction:Planar64::int(3),//unrealistic: kinetic friction is typically lower than static kinetic_friction:int(3),//unrealistic: kinetic friction is typically lower than static
surf_dot:Planar64::int(3)/4,// normal.y=0.75 surf_dot:int(3)/4,// normal.y=0.75
}), }),
ladder:Some(LadderSettings{ ladder:Some(LadderSettings{
accelerate:AccelerateSettings{ accelerate:AccelerateSettings{
topspeed:Planar64::int(18), topspeed:int(18),
accel:Planar64::int(180), accel:int(180),
}, },
dot:(Planar64::int(1)/2).sqrt(), dot:(int(1)/2).sqrt(),
}), }),
swim:Some(PropulsionSettings{ swim:Some(PropulsionSettings{
magnitude:Planar64::int(12), magnitude:int(12),
}), }),
hitbox:Hitbox::roblox(), hitbox:Hitbox::roblox(),
camera_offset:Planar64Vec3::int(0,2,0),//4.5-2.5=2 camera_offset:int3(0,2,0),//4.5-2.5=2
} }
} }
pub fn roblox_surf()->Self{ pub fn roblox_surf()->Self{
Self{ Self{
gravity:Planar64Vec3::int(0,-50,0), gravity:int3(0,-50,0),
..Self::roblox_bhop() ..Self::roblox_bhop()
} }
} }
@ -509,7 +520,7 @@ impl StyleModifiers{
Self{ Self{
strafe:None, strafe:None,
rocket:Some(PropulsionSettings{ rocket:Some(PropulsionSettings{
magnitude:Planar64::int(200), magnitude:int(200),
}), }),
..Self::roblox_bhop() ..Self::roblox_bhop()
} }
@ -522,38 +533,38 @@ impl StyleModifiers{
strafe:Some(StrafeSettings{ strafe:Some(StrafeSettings{
enable:ControlsActivation::full_2d(), enable:ControlsActivation::full_2d(),
air_accel_limit:Some(Planar64::raw(150<<28)*100), air_accel_limit:Some(Planar64::raw(150<<28)*100),
mv:Planar64::raw(30)*VALVE_SCALE, mv:(Planar64::raw(30)*VALVE_SCALE).fix_1(),
tick_rate:Ratio64::new(100,Time::ONE_SECOND.nanos() as u64).unwrap(), tick_rate:Ratio64::new(100,Time::ONE_SECOND.nanos() as u64).unwrap(),
}), }),
jump:Some(JumpSettings{ jump:Some(JumpSettings{
impulse:JumpImpulse::Height(Planar64::int(52)*VALVE_SCALE), impulse:JumpImpulse::Height((int(52)*VALVE_SCALE).fix_1()),
calculation:JumpCalculation::JumpThenBoost, calculation:JumpCalculation::JumpThenBoost,
limit_minimum:true, limit_minimum:true,
}), }),
gravity:Planar64Vec3::int(0,-800,0)*VALVE_SCALE, gravity:(int3(0,-800,0)*VALVE_SCALE).fix_1(),
mass:Planar64::int(1), mass:int(1),
rocket:None, rocket:None,
walk:Some(WalkSettings{ walk:Some(WalkSettings{
accelerate:AccelerateSettings{ accelerate:AccelerateSettings{
topspeed:Planar64::int(18),//? topspeed:int(18),//?
accel:Planar64::int(90),//? accel:int(90),//?
}, },
static_friction:Planar64::int(2),//? static_friction:int(2),//?
kinetic_friction:Planar64::int(3),//? kinetic_friction:int(3),//?
surf_dot:Planar64::int(3)/4,// normal.y=0.75 surf_dot:int(3)/4,// normal.y=0.75
}), }),
ladder:Some(LadderSettings{ ladder:Some(LadderSettings{
accelerate:AccelerateSettings{ accelerate:AccelerateSettings{
topspeed:Planar64::int(18),//? topspeed:int(18),//?
accel:Planar64::int(180),//? accel:int(180),//?
}, },
dot:(Planar64::int(1)/2).sqrt(),//? dot:(int(1)/2).sqrt(),//?
}), }),
swim:Some(PropulsionSettings{ swim:Some(PropulsionSettings{
magnitude:Planar64::int(12),//? magnitude:int(12),//?
}), }),
hitbox:Hitbox::source(), hitbox:Hitbox::source(),
camera_offset:(Planar64Vec3::int(0,64,0)-Planar64Vec3::int(0,73,0)/2)*VALVE_SCALE, camera_offset:((int3(0,64,0)-(int3(0,73,0)>>1))*VALVE_SCALE).fix_1(),
} }
} }
pub fn source_surf()->Self{ pub fn source_surf()->Self{
@ -562,39 +573,39 @@ impl StyleModifiers{
controls_mask_state:Controls::all(), controls_mask_state:Controls::all(),
strafe:Some(StrafeSettings{ strafe:Some(StrafeSettings{
enable:ControlsActivation::full_2d(), enable:ControlsActivation::full_2d(),
air_accel_limit:Some(Planar64::int(150)*66*VALVE_SCALE), air_accel_limit:Some((int(150)*66*VALVE_SCALE).fix_1()),
mv:Planar64::int(30)*VALVE_SCALE, mv:(int(30)*VALVE_SCALE).fix_1(),
tick_rate:Ratio64::new(66,Time::ONE_SECOND.nanos() as u64).unwrap(), tick_rate:Ratio64::new(66,Time::ONE_SECOND.nanos() as u64).unwrap(),
}), }),
jump:Some(JumpSettings{ jump:Some(JumpSettings{
impulse:JumpImpulse::Height(Planar64::int(52)*VALVE_SCALE), impulse:JumpImpulse::Height((int(52)*VALVE_SCALE).fix_1()),
calculation:JumpCalculation::JumpThenBoost, calculation:JumpCalculation::JumpThenBoost,
limit_minimum:true, limit_minimum:true,
}), }),
gravity:Planar64Vec3::int(0,-800,0)*VALVE_SCALE, gravity:(int3(0,-800,0)*VALVE_SCALE).fix_1(),
mass:Planar64::int(1), mass:int(1),
rocket:None, rocket:None,
walk:Some(WalkSettings{ walk:Some(WalkSettings{
accelerate:AccelerateSettings{ accelerate:AccelerateSettings{
topspeed:Planar64::int(18),//? topspeed:int(18),//?
accel:Planar64::int(90),//? accel:int(90),//?
}, },
static_friction:Planar64::int(2),//? static_friction:int(2),//?
kinetic_friction:Planar64::int(3),//? kinetic_friction:int(3),//?
surf_dot:Planar64::int(3)/4,// normal.y=0.75 surf_dot:int(3)/4,// normal.y=0.75
}), }),
ladder:Some(LadderSettings{ ladder:Some(LadderSettings{
accelerate:AccelerateSettings{ accelerate:AccelerateSettings{
topspeed:Planar64::int(18),//? topspeed:int(18),//?
accel:Planar64::int(180),//? accel:int(180),//?
}, },
dot:(Planar64::int(1)/2).sqrt(),//? dot:(int(1)/2).sqrt(),//?
}), }),
swim:Some(PropulsionSettings{ swim:Some(PropulsionSettings{
magnitude:Planar64::int(12),//? magnitude:int(12),//?
}), }),
hitbox:Hitbox::source(), hitbox:Hitbox::source(),
camera_offset:(Planar64Vec3::int(0,64,0)-Planar64Vec3::int(0,73,0)/2)*VALVE_SCALE, camera_offset:((int3(0,64,0)-(int3(0,73,0)>>1))*VALVE_SCALE).fix_1(),
} }
} }
} }

View File

@ -1,3 +1,6 @@
pub use fixed_wide::fixed::{Fixed,Fix};
pub use ratio_ops::ratio::{Ratio,Divide};
//integer units //integer units
#[derive(Clone,Copy,Hash,Eq,PartialEq,PartialOrd,Debug)] #[derive(Clone,Copy,Hash,Eq,PartialEq,PartialOrd,Debug)]
pub struct Time(i64); pub struct Time(i64);
@ -38,11 +41,26 @@ impl Time{
pub const fn nanos(self)->i64{ pub const fn nanos(self)->i64{
self.0 self.0
} }
#[inline]
pub const fn to_ratio(self)->Ratio<Planar64,Planar64>{
Ratio::new(Planar64::raw(self.0),Planar64::raw(1_000_000_000))
}
} }
impl From<Planar64> for Time{ impl From<Planar64> for Time{
#[inline] #[inline]
fn from(value:Planar64)->Self{ fn from(value:Planar64)->Self{
Time((((value.0 as i128)*1_000_000_000)>>32) as i64) Time((value*Planar64::raw(1_000_000_000)).fix_1().to_raw())
}
}
impl<Num,Den,N1,T1> From<Ratio<Num,Den>> for Time
where
Num:core::ops::Mul<Planar64,Output=N1>,
N1:Divide<Den,Output=T1>,
T1:Fix<Planar64>,
{
#[inline]
fn from(value:Ratio<Num,Den>)->Self{
Time((value*Planar64::raw(1_000_000_000)).divide().fix().to_raw())
} }
} }
impl std::fmt::Display for Time{ impl std::fmt::Display for Time{
@ -63,34 +81,81 @@ impl std::ops::Neg for Time{
Time(-self.0) Time(-self.0)
} }
} }
impl std::ops::Add<Time> for Time{ macro_rules! impl_time_additive_operator {
type Output=Time; ($trait:ty, $method:ident) => {
#[inline] impl $trait for Time{
fn add(self,rhs:Self)->Self::Output { type Output=Time;
Time(self.0+rhs.0) #[inline]
} fn $method(self,rhs:Self)->Self::Output {
Time(self.0.$method(rhs.0))
}
}
};
} }
impl std::ops::Sub<Time> for Time{ impl_time_additive_operator!(core::ops::Add,add);
type Output=Time; impl_time_additive_operator!(core::ops::Sub,sub);
#[inline] impl_time_additive_operator!(core::ops::Rem,rem);
fn sub(self,rhs:Self)->Self::Output { macro_rules! impl_time_additive_assign_operator {
Time(self.0-rhs.0) ($trait:ty, $method:ident) => {
} impl $trait for Time{
#[inline]
fn $method(&mut self,rhs:Self){
self.0.$method(rhs.0)
}
}
};
} }
impl std::ops::Mul<Time> for Time{ impl_time_additive_assign_operator!(core::ops::AddAssign,add_assign);
type Output=Time; impl_time_additive_assign_operator!(core::ops::SubAssign,sub_assign);
impl_time_additive_assign_operator!(core::ops::RemAssign,rem_assign);
impl std::ops::Mul for Time{
type Output=Ratio<fixed_wide::fixed::Fixed<2,64>,fixed_wide::fixed::Fixed<2,64>>;
#[inline] #[inline]
fn mul(self,rhs:Time)->Self::Output{ fn mul(self,rhs:Self)->Self::Output{
Self((((self.0 as i128)*(rhs.0 as i128))/1_000_000_000) as i64) Ratio::new(Fixed::raw(self.0)*Fixed::raw(rhs.0),Fixed::raw_digit(1_000_000_000i64.pow(2)))
} }
} }
impl std::ops::Div<i64> for Time{ impl std::ops::Div<i64> for Time{
type Output=Time; type Output=Time;
#[inline] #[inline]
fn div(self,rhs:i64)->Self::Output { fn div(self,rhs:i64)->Self::Output{
Time(self.0/rhs) Time(self.0/rhs)
} }
} }
impl std::ops::Mul<i64> for Time{
type Output=Time;
#[inline]
fn mul(self,rhs:i64)->Self::Output{
Time(self.0*rhs)
}
}
impl core::ops::Mul<Time> for Planar64{
type Output=Ratio<Fixed<2,64>,Planar64>;
fn mul(self,rhs:Time)->Self::Output{
Ratio::new(self*Fixed::raw(rhs.0),Planar64::raw(1_000_000_000))
}
}
#[test]
fn time_from_planar64(){
let a:Time=Planar64::from(1).into();
assert_eq!(a,Time::ONE_SECOND);
}
#[test]
fn time_from_ratio(){
let a:Time=Ratio::new(Planar64::from(1),Planar64::from(1)).into();
assert_eq!(a,Time::ONE_SECOND);
}
#[test]
fn time_squared(){
let a=Time::from_secs(2);
assert_eq!(a*a,Ratio::new(Fixed::<2,64>::raw_digit(1_000_000_000i64.pow(2))*4,Fixed::<2,64>::raw_digit(1_000_000_000i64.pow(2))));
}
#[test]
fn time_times_planar64(){
let a=Time::from_secs(2);
let b=Planar64::from(2);
assert_eq!(b*a,Ratio::new(Fixed::<2,64>::raw_digit(1_000_000_000*(1<<32))<<2,Fixed::<1,32>::raw_digit(1_000_000_000)));
}
#[inline] #[inline]
const fn gcd(mut a:u64,mut b:u64)->u64{ const fn gcd(mut a:u64,mut b:u64)->u64{
@ -309,6 +374,7 @@ impl std::ops::Mul<i64> for Ratio64Vec2{
#[derive(Clone,Copy,Hash)] #[derive(Clone,Copy,Hash)]
pub struct Angle32(i32); pub struct Angle32(i32);
impl Angle32{ impl Angle32{
const ANGLE32_TO_FLOAT64_RADIANS:f64=std::f64::consts::PI/((1i64<<31) as f64);
pub const FRAC_PI_2:Self=Self(1<<30); pub const FRAC_PI_2:Self=Self(1<<30);
pub const NEG_FRAC_PI_2:Self=Self(-1<<30); pub const NEG_FRAC_PI_2:Self=Self(-1<<30);
pub const PI:Self=Self(-1<<31); pub const PI:Self=Self(-1<<31);
@ -347,24 +413,36 @@ impl Angle32{
.wrapping_add(midpoint) .wrapping_add(midpoint)
) )
} }
/*
#[inline] #[inline]
pub fn cos(&self)->Unit32{ pub fn cos_sin(&self)->(Planar64,Planar64){
//TODO: fix this rounding towards 0 /*
Unit32(unsafe{((self.0 as f64*ANGLE32_TO_FLOAT64_RADIANS).cos()*UNIT32_ONE_FLOAT64).to_int_unchecked()}) //cordic
let a=self.0 as u32;
//initialize based on the quadrant
let (mut x,mut y)=match (a&(1<<31)!=0,a&(1<<30)!=0){
(false,false)=>( 1i64<<32, 0i64 ),//TR
(false,true )=>( 0i64 , 1i64<<32),//TL
(true ,false)=>(-1i64<<32, 0i64 ),//BL
(true ,true )=>( 0i64 ,-1i64<<32),//BR
};
println!("x={} y={}",Planar64::raw(x),Planar64::raw(y));
for i in 0..30{
if a&(1<<(29-i))!=0{
(x,y)=(x-(y>>i),y+(x>>i));
}
println!("i={i} t={} x={} y={}",(a&(1<<(29-i))!=0) as u8,Planar64::raw(x),Planar64::raw(y));
}
//don't forget the gain
(Planar64::raw(x),Planar64::raw(y))
*/
let (s,c)=(self.0 as f64*Self::ANGLE32_TO_FLOAT64_RADIANS).sin_cos();
(Planar64::raw((c*((1u64<<32) as f64)) as i64),Planar64::raw((s*((1u64<<32) as f64)) as i64))
} }
#[inline]
pub fn sin(&self)->Unit32{
//TODO: fix this rounding towards 0
Unit32(unsafe{((self.0 as f64*ANGLE32_TO_FLOAT64_RADIANS).sin()*UNIT32_ONE_FLOAT64).to_int_unchecked()})
}
*/
} }
const ANGLE32_TO_FLOAT64_RADIANS:f64=std::f64::consts::PI/((1i64<<31) as f64);
impl Into<f32> for Angle32{ impl Into<f32> for Angle32{
#[inline] #[inline]
fn into(self)->f32{ fn into(self)->f32{
(self.0 as f64*ANGLE32_TO_FLOAT64_RADIANS) as f32 (self.0 as f64*Self::ANGLE32_TO_FLOAT64_RADIANS) as f32
} }
} }
impl std::ops::Neg for Angle32{ impl std::ops::Neg for Angle32{
@ -402,6 +480,26 @@ impl std::ops::Mul<Angle32> for Angle32{
Angle32(self.0.wrapping_mul(rhs.0)) Angle32(self.0.wrapping_mul(rhs.0))
} }
} }
#[test]
fn angle_sin_cos(){
fn close_enough(lhs:Planar64,rhs:Planar64)->bool{
(lhs-rhs).abs()<Planar64::EPSILON*4
}
fn test_angle(f:f64){
let a=Angle32((f/Angle32::ANGLE32_TO_FLOAT64_RADIANS) as i32);
println!("a={:#034b}",a.0);
let (c,s)=a.cos_sin();
let h=(s*s+c*c).sqrt();
println!("cordic s={} c={}",(s/h).divide(),(c/h).divide());
let (fs,fc)=f.sin_cos();
println!("float s={} c={}",fs,fc);
assert!(close_enough((c/h).divide().fix_1(),Planar64::raw((fc*((1u64<<32) as f64)) as i64)));
assert!(close_enough((s/h).divide().fix_1(),Planar64::raw((fs*((1u64<<32) as f64)) as i64)));
}
test_angle(1.0);
test_angle(std::f64::consts::PI/4.0);
test_angle(std::f64::consts::PI/8.0);
}
/* Unit type unused for now, may revive it for map files /* Unit type unused for now, may revive it for map files
///[-1.0,1.0] = [-2^30,2^30] ///[-1.0,1.0] = [-2^30,2^30]
@ -427,655 +525,102 @@ impl TryFrom<[f32;3]> for Unit32Vec3{
} }
*/ */
///[-1.0,1.0] = [-2^32,2^32] pub type Planar64=fixed_wide::types::I32F32;
#[derive(Clone,Copy,Debug,Hash,Eq,Ord,PartialEq,PartialOrd)] pub type Planar64Vec3=linear_ops::types::Vector3<Planar64>;
pub struct Planar64(i64); pub type Planar64Mat3=linear_ops::types::Matrix3<Planar64>;
impl Planar64{ pub mod vec3{
pub const ZERO:Self=Self(0); use super::*;
pub const ONE:Self=Self(1<<32); pub use linear_ops::types::Vector3;
pub const MAX:Self=Self(i64::MAX); pub const MIN:Planar64Vec3=Planar64Vec3::new([Planar64::MIN;3]);
pub const MIN:Self=Self(i64::MIN); pub const MAX:Planar64Vec3=Planar64Vec3::new([Planar64::MAX;3]);
#[inline] pub const ZERO:Planar64Vec3=Planar64Vec3::new([Planar64::ZERO;3]);
pub const fn int(num:i32)->Self{ pub const ZERO_2:linear_ops::types::Vector3<Fixed::<2,64>>=linear_ops::types::Vector3::new([Fixed::<2,64>::ZERO;3]);
Self(Self::ONE.0*num as i64) pub const X:Planar64Vec3=Planar64Vec3::new([Planar64::ONE,Planar64::ZERO,Planar64::ZERO]);
pub const Y:Planar64Vec3=Planar64Vec3::new([Planar64::ZERO,Planar64::ONE,Planar64::ZERO]);
pub const Z:Planar64Vec3=Planar64Vec3::new([Planar64::ZERO,Planar64::ZERO,Planar64::ONE]);
pub const ONE:Planar64Vec3=Planar64Vec3::new([Planar64::ONE,Planar64::ONE,Planar64::ONE]);
pub const NEG_X:Planar64Vec3=Planar64Vec3::new([Planar64::NEG_ONE,Planar64::ZERO,Planar64::ZERO]);
pub const NEG_Y:Planar64Vec3=Planar64Vec3::new([Planar64::ZERO,Planar64::NEG_ONE,Planar64::ZERO]);
pub const NEG_Z:Planar64Vec3=Planar64Vec3::new([Planar64::ZERO,Planar64::ZERO,Planar64::NEG_ONE]);
pub const NEG_ONE:Planar64Vec3=Planar64Vec3::new([Planar64::NEG_ONE,Planar64::NEG_ONE,Planar64::NEG_ONE]);
pub fn int(x:i32,y:i32,z:i32)->Planar64Vec3{
Planar64Vec3::new([Planar64::from(x),Planar64::from(y),Planar64::from(z)])
} }
#[inline] pub fn raw_array(array:[i64;3])->Planar64Vec3{
pub const fn raw(num:i64)->Self{ Planar64Vec3::new(array.map(Planar64::raw))
Self(num)
} }
#[inline] pub fn raw_xyz(x:i64,y:i64,z:i64)->Planar64Vec3{
pub const fn get(&self)->i64{ Planar64Vec3::new([Planar64::raw(x),Planar64::raw(y),Planar64::raw(z)])
self.0
}
#[inline]
pub const fn abs(self)->Self{
Self(self.0.abs())
}
#[inline]
pub fn sqrt(&self)->Self{
Planar64(unsafe{(((self.0 as i128)<<32) as f64).sqrt().to_int_unchecked()})
}
#[inline]
pub const fn signum_i64(&self)->i64{
((self.0&(1<<63)!=0) as i64)*2-1
}
}
const PLANAR64_ONE_FLOAT32:f32=(1u64<<32) as f32;
const PLANAR64_CONVERT_TO_FLOAT32:f32=1.0/PLANAR64_ONE_FLOAT32;
const PLANAR64_ONE_FLOAT64:f64=(1u64<<32) as f64;
impl Into<f32> for Planar64{
#[inline]
fn into(self)->f32{
self.0 as f32*PLANAR64_CONVERT_TO_FLOAT32
}
}
impl From<Ratio64> for Planar64{
#[inline]
fn from(ratio:Ratio64)->Self{
Self((((ratio.num as i128)<<32)/(ratio.den as i128)) as i64)
}
}
#[derive(Debug)]
pub enum Planar64TryFromFloatError{
Nan,
Infinite,
Subnormal,
HighlyNegativeExponent,
HighlyPositiveExponent,
}
impl TryFrom<f32> for Planar64{
type Error=Planar64TryFromFloatError;
#[inline]
fn try_from(value:f32)->Result<Self,Self::Error>{
match value.classify(){
std::num::FpCategory::Nan=>Err(Self::Error::Nan),
std::num::FpCategory::Infinite=>Err(Self::Error::Infinite),
std::num::FpCategory::Zero=>Ok(Self::ZERO),
std::num::FpCategory::Subnormal
|std::num::FpCategory::Normal=>{
let planar=value*PLANAR64_ONE_FLOAT32;
if planar<(i64::MIN as f32)||(i64::MAX as f32)<planar{
Err(Self::Error::HighlyPositiveExponent)
}else{
Ok(Planar64(unsafe{planar.to_int_unchecked()}))
}
}
}
}
}
impl TryFrom<f64> for Planar64{
type Error=Planar64TryFromFloatError;
#[inline]
fn try_from(value:f64)->Result<Self,Self::Error>{
match value.classify(){
std::num::FpCategory::Nan=>Err(Self::Error::Nan),
std::num::FpCategory::Infinite=>Err(Self::Error::Infinite),
std::num::FpCategory::Zero=>Ok(Self::ZERO),
std::num::FpCategory::Subnormal
|std::num::FpCategory::Normal=>{
let planar=value*PLANAR64_ONE_FLOAT64;
if planar<(i64::MIN as f64)||(i64::MAX as f64)<planar{
Err(Self::Error::HighlyPositiveExponent)
}else{
Ok(Planar64(unsafe{planar.to_int_unchecked()}))
}
}
}
}
}
impl std::fmt::Display for Planar64{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{:.3}",
Into::<f32>::into(*self),
)
}
}
impl std::ops::Neg for Planar64{
type Output=Planar64;
#[inline]
fn neg(self)->Self::Output{
Planar64(-self.0)
}
}
impl std::ops::Add<Planar64> for Planar64{
type Output=Planar64;
#[inline]
fn add(self, rhs: Self) -> Self::Output {
Planar64(self.0+rhs.0)
}
}
impl std::ops::AddAssign<Planar64> for Planar64{
#[inline]
fn add_assign(&mut self,rhs:Self){
*self=*self+rhs;
}
}
impl std::ops::Sub<Planar64> for Planar64{
type Output=Planar64;
#[inline]
fn sub(self, rhs: Self) -> Self::Output {
Planar64(self.0-rhs.0)
}
}
impl std::ops::Mul<i64> for Planar64{
type Output=Planar64;
#[inline]
fn mul(self, rhs: i64) -> Self::Output {
Planar64(self.0*rhs)
}
}
impl std::ops::Mul<Planar64> for Planar64{
type Output=Planar64;
#[inline]
fn mul(self, rhs: Self) -> Self::Output {
Planar64(((self.0 as i128*rhs.0 as i128)>>32) as i64)
}
}
impl std::ops::Mul<Time> for Planar64{
type Output=Planar64;
#[inline]
fn mul(self,rhs:Time)->Self::Output{
Planar64(((self.0 as i128*rhs.0 as i128)/1_000_000_000) as i64)
}
}
impl std::ops::Div<i64> for Planar64{
type Output=Planar64;
#[inline]
fn div(self, rhs: i64) -> Self::Output {
Planar64(self.0/rhs)
}
}
impl std::ops::Div<Planar64> for Planar64{
type Output=Planar64;
#[inline]
fn div(self, rhs: Planar64) -> Self::Output {
Planar64((((self.0 as i128)<<32)/(rhs.0 as i128)) as i64)
}
}
// impl PartialOrd<i64> for Planar64{
// fn partial_cmp(&self, other: &i64) -> Option<std::cmp::Ordering> {
// self.0.partial_cmp(other)
// }
// }
///[-1.0,1.0] = [-2^32,2^32]
#[derive(Clone,Copy,Debug,Default,Hash,Eq,PartialEq)]
pub struct Planar64Vec3(glam::I64Vec3);
impl Planar64Vec3{
pub const ZERO:Self=Planar64Vec3(glam::I64Vec3::ZERO);
pub const ONE:Self=Self::int(1,1,1);
pub const X:Self=Self::int(1,0,0);
pub const Y:Self=Self::int(0,1,0);
pub const Z:Self=Self::int(0,0,1);
pub const NEG_X:Self=Self::int(-1,0,0);
pub const NEG_Y:Self=Self::int(0,-1,0);
pub const NEG_Z:Self=Self::int(0,0,-1);
pub const MIN:Self=Planar64Vec3(glam::I64Vec3::MIN);
pub const MAX:Self=Planar64Vec3(glam::I64Vec3::MAX);
#[inline]
pub const fn new(x:Planar64,y:Planar64,z:Planar64)->Self{
Self(glam::i64vec3(x.0,y.0,z.0))
}
#[inline]
pub const fn get(self)->glam::I64Vec3{
self.0
}
#[inline]
pub const fn int(x:i32,y:i32,z:i32)->Self{
Self(glam::i64vec3((x as i64)<<32,(y as i64)<<32,(z as i64)<<32))
}
#[inline]
pub const fn raw_xyz(x:i64,y:i64,z:i64)->Self{
Self(glam::i64vec3(x,y,z))
}
#[inline]
pub const fn raw_array(xyz:[i64;3])->Self{
Self(glam::I64Vec3::from_array(xyz))
}
#[inline]
pub const fn raw(xyz:glam::I64Vec3)->Self{
Self(xyz)
}
#[inline]
pub const fn x(&self)->Planar64{
Planar64(self.0.x)
}
#[inline]
pub const fn y(&self)->Planar64{
Planar64(self.0.y)
}
#[inline]
pub const fn z(&self)->Planar64{
Planar64(self.0.z)
}
#[inline]
pub fn min(&self,rhs:Self)->Self{
Self(glam::i64vec3(
self.0.x.min(rhs.0.x),
self.0.y.min(rhs.0.y),
self.0.z.min(rhs.0.z),
))
}
#[inline]
pub fn max(&self,rhs:Self)->Self{
Self(glam::i64vec3(
self.0.x.max(rhs.0.x),
self.0.y.max(rhs.0.y),
self.0.z.max(rhs.0.z),
))
}
#[inline]
pub fn midpoint(&self,rhs:Self)->Self{
Self((self.0+rhs.0)/2)
}
#[inline]
pub fn cmplt(&self,rhs:Self)->glam::BVec3{
self.0.cmplt(rhs.0)
}
#[inline]
pub const fn dot(&self,rhs:Self)->Planar64{
Planar64(((
(self.0.x as i128)*(rhs.0.x as i128)+
(self.0.y as i128)*(rhs.0.y as i128)+
(self.0.z as i128)*(rhs.0.z as i128)
)>>32) as i64)
}
#[inline]
pub const fn dot128(&self,rhs:Self)->i128{
(self.0.x as i128)*(rhs.0.x as i128)+
(self.0.y as i128)*(rhs.0.y as i128)+
(self.0.z as i128)*(rhs.0.z as i128)
}
#[inline]
pub const fn cross(&self,rhs:Self)->Planar64Vec3{
Planar64Vec3(glam::i64vec3(
(((self.0.y as i128)*(rhs.0.z as i128)-(self.0.z as i128)*(rhs.0.y as i128))>>32) as i64,
(((self.0.z as i128)*(rhs.0.x as i128)-(self.0.x as i128)*(rhs.0.z as i128))>>32) as i64,
(((self.0.x as i128)*(rhs.0.y as i128)-(self.0.y as i128)*(rhs.0.x as i128))>>32) as i64,
))
}
#[inline]
pub fn length(&self)->Planar64{
let radicand=(self.0.x as i128)*(self.0.x as i128)+(self.0.y as i128)*(self.0.y as i128)+(self.0.z as i128)*(self.0.z as i128);
Planar64(unsafe{(radicand as f64).sqrt().to_int_unchecked()})
}
#[inline]
pub fn with_length(&self,length:Planar64)->Self{
let radicand=(self.0.x as i128)*(self.0.x as i128)+(self.0.y as i128)*(self.0.y as i128)+(self.0.z as i128)*(self.0.z as i128);
let self_length:i128=unsafe{(radicand as f64).sqrt().to_int_unchecked()};
//self.0*length/self_length
Planar64Vec3(
glam::i64vec3(
((self.0.x as i128)*(length.0 as i128)/self_length) as i64,
((self.0.y as i128)*(length.0 as i128)/self_length) as i64,
((self.0.z as i128)*(length.0 as i128)/self_length) as i64,
)
)
}
}
impl Into<glam::Vec3> for Planar64Vec3{
#[inline]
fn into(self)->glam::Vec3{
glam::vec3(
self.0.x as f32,
self.0.y as f32,
self.0.z as f32,
)*PLANAR64_CONVERT_TO_FLOAT32
}
}
impl TryFrom<[f32;3]> for Planar64Vec3{
type Error=Planar64TryFromFloatError;
#[inline]
fn try_from(value:[f32;3])->Result<Self,Self::Error>{
Ok(Self(glam::i64vec3(
Planar64::try_from(value[0])?.0,
Planar64::try_from(value[1])?.0,
Planar64::try_from(value[2])?.0,
)))
}
}
impl TryFrom<glam::Vec3A> for Planar64Vec3{
type Error=Planar64TryFromFloatError;
#[inline]
fn try_from(value:glam::Vec3A)->Result<Self,Self::Error>{
Ok(Self(glam::i64vec3(
Planar64::try_from(value.x)?.0,
Planar64::try_from(value.y)?.0,
Planar64::try_from(value.z)?.0,
)))
}
}
impl std::fmt::Display for Planar64Vec3{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{:.3},{:.3},{:.3}",
Into::<f32>::into(self.x()),Into::<f32>::into(self.y()),Into::<f32>::into(self.z()),
)
}
}
impl std::ops::Neg for Planar64Vec3{
type Output=Planar64Vec3;
#[inline]
fn neg(self)->Self::Output{
Planar64Vec3(-self.0)
}
}
impl std::ops::Add<Planar64Vec3> for Planar64Vec3{
type Output=Planar64Vec3;
#[inline]
fn add(self,rhs:Planar64Vec3) -> Self::Output {
Planar64Vec3(self.0+rhs.0)
}
}
impl std::ops::AddAssign<Planar64Vec3> for Planar64Vec3{
#[inline]
fn add_assign(&mut self,rhs:Planar64Vec3){
*self=*self+rhs
}
}
impl std::ops::Sub<Planar64Vec3> for Planar64Vec3{
type Output=Planar64Vec3;
#[inline]
fn sub(self,rhs:Planar64Vec3) -> Self::Output {
Planar64Vec3(self.0-rhs.0)
}
}
impl std::ops::SubAssign<Planar64Vec3> for Planar64Vec3{
#[inline]
fn sub_assign(&mut self,rhs:Planar64Vec3){
*self=*self-rhs
}
}
impl std::ops::Mul<Planar64Vec3> for Planar64Vec3{
type Output=Planar64Vec3;
#[inline]
fn mul(self, rhs: Planar64Vec3) -> Self::Output {
Planar64Vec3(glam::i64vec3(
(((self.0.x as i128)*(rhs.0.x as i128))>>32) as i64,
(((self.0.y as i128)*(rhs.0.y as i128))>>32) as i64,
(((self.0.z as i128)*(rhs.0.z as i128))>>32) as i64
))
}
}
impl std::ops::Mul<Planar64> for Planar64Vec3{
type Output=Planar64Vec3;
#[inline]
fn mul(self, rhs: Planar64) -> Self::Output {
Planar64Vec3(glam::i64vec3(
(((self.0.x as i128)*(rhs.0 as i128))>>32) as i64,
(((self.0.y as i128)*(rhs.0 as i128))>>32) as i64,
(((self.0.z as i128)*(rhs.0 as i128))>>32) as i64
))
}
}
impl std::ops::Mul<i64> for Planar64Vec3{
type Output=Planar64Vec3;
#[inline]
fn mul(self,rhs:i64)->Self::Output {
Planar64Vec3(glam::i64vec3(
self.0.x*rhs,
self.0.y*rhs,
self.0.z*rhs
))
}
}
impl std::ops::Mul<Time> for Planar64Vec3{
type Output=Planar64Vec3;
#[inline]
fn mul(self,rhs:Time)->Self::Output{
Planar64Vec3(glam::i64vec3(
(((self.0.x as i128)*(rhs.0 as i128))/1_000_000_000) as i64,
(((self.0.y as i128)*(rhs.0 as i128))/1_000_000_000) as i64,
(((self.0.z as i128)*(rhs.0 as i128))/1_000_000_000) as i64
))
}
}
impl std::ops::Div<Planar64> for Planar64Vec3{
type Output=Planar64Vec3;
#[inline]
fn div(self,rhs:Planar64)->Self::Output{
Planar64Vec3(glam::i64vec3(
(((self.0.x as i128)<<32)/(rhs.0 as i128)) as i64,
(((self.0.y as i128)<<32)/(rhs.0 as i128)) as i64,
(((self.0.z as i128)<<32)/(rhs.0 as i128)) as i64,
))
}
}
impl std::ops::Div<i64> for Planar64Vec3{
type Output=Planar64Vec3;
#[inline]
fn div(self,rhs:i64)->Self::Output{
Planar64Vec3(glam::i64vec3(
self.0.x/rhs,
self.0.y/rhs,
self.0.z/rhs,
))
} }
} }
///[-1.0,1.0] = [-2^32,2^32] pub fn int(value:i32)->Planar64{
#[derive(Clone,Copy,Hash,Eq,PartialEq)] Planar64::from(value)
pub struct Planar64Mat3{
pub x_axis:Planar64Vec3,
pub y_axis:Planar64Vec3,
pub z_axis:Planar64Vec3,
} }
impl Default for Planar64Mat3{ pub mod mat3{
#[inline] use super::*;
fn default() -> Self { pub use linear_ops::types::Matrix3;
Self{ pub fn from_diagonal(diag:Planar64Vec3)->Planar64Mat3{
x_axis:Planar64Vec3::X, Planar64Mat3::new([
y_axis:Planar64Vec3::Y, [diag.x,Planar64::ZERO,Planar64::ZERO],
z_axis:Planar64Vec3::Z, [Planar64::ZERO,diag.y,Planar64::ZERO],
} [Planar64::ZERO,Planar64::ZERO,diag.z],
} ])
}
impl Planar64Mat3{
#[inline]
pub const fn from_cols(x_axis:Planar64Vec3,y_axis:Planar64Vec3,z_axis:Planar64Vec3)->Self{
Self{
x_axis,
y_axis,
z_axis,
}
}
pub const fn int_from_cols_array(array:[i32;9])->Self{
Self{
x_axis:Planar64Vec3::int(array[0],array[1],array[2]),
y_axis:Planar64Vec3::int(array[3],array[4],array[5]),
z_axis:Planar64Vec3::int(array[6],array[7],array[8]),
}
} }
#[inline] #[inline]
pub const fn from_diagonal(diagonal:Planar64Vec3)->Self{ pub fn from_rotation_yx(x:Angle32,y:Angle32)->Planar64Mat3{
Self{ let (xc,xs)=x.cos_sin();
x_axis:Planar64Vec3::raw_xyz(diagonal.0.x,0,0), let (yc,ys)=y.cos_sin();
y_axis:Planar64Vec3::raw_xyz(0,diagonal.0.y,0), Planar64Mat3::from_cols([
z_axis:Planar64Vec3::raw_xyz(0,0,diagonal.0.z), Planar64Vec3::new([xc,Planar64::ZERO,-xs]),
} Planar64Vec3::new([(xs*ys).fix_1(),yc,(xc*ys).fix_1()]),
Planar64Vec3::new([(xs*yc).fix_1(),-ys,(xc*yc).fix_1()]),
])
} }
#[inline] #[inline]
pub fn from_rotation_yx(yaw:Angle32,pitch:Angle32)->Self{ pub fn from_rotation_y(y:Angle32)->Planar64Mat3{
let xtheta=yaw.0 as f64*ANGLE32_TO_FLOAT64_RADIANS; let (c,s)=y.cos_sin();
let (xs,xc)=xtheta.sin_cos(); Planar64Mat3::from_cols([
let (xc,xs)=(xc*PLANAR64_ONE_FLOAT64,xs*PLANAR64_ONE_FLOAT64); Planar64Vec3::new([c,Planar64::ZERO,-s]),
let ytheta=pitch.0 as f64*ANGLE32_TO_FLOAT64_RADIANS; vec3::Y,
let (ys,yc)=ytheta.sin_cos(); Planar64Vec3::new([s,Planar64::ZERO,c]),
let (yc,ys)=(yc*PLANAR64_ONE_FLOAT64,ys*PLANAR64_ONE_FLOAT64); ])
//TODO: fix this rounding towards 0
let (xc,xs):(i64,i64)=(unsafe{xc.to_int_unchecked()},unsafe{xs.to_int_unchecked()});
let (yc,ys):(i64,i64)=(unsafe{yc.to_int_unchecked()},unsafe{ys.to_int_unchecked()});
Self::from_cols(
Planar64Vec3(glam::i64vec3(xc,0,-xs)),
Planar64Vec3(glam::i64vec3(((xs as i128*ys as i128)>>32) as i64,yc,((xc as i128*ys as i128)>>32) as i64)),
Planar64Vec3(glam::i64vec3(((xs as i128*yc as i128)>>32) as i64,-ys,((xc as i128*yc as i128)>>32) as i64)),
)
}
#[inline]
pub fn from_rotation_y(angle:Angle32)->Self{
let theta=angle.0 as f64*ANGLE32_TO_FLOAT64_RADIANS;
let (s,c)=theta.sin_cos();
let (c,s)=(c*PLANAR64_ONE_FLOAT64,s*PLANAR64_ONE_FLOAT64);
//TODO: fix this rounding towards 0
let (c,s):(i64,i64)=(unsafe{c.to_int_unchecked()},unsafe{s.to_int_unchecked()});
Self::from_cols(
Planar64Vec3(glam::i64vec3(c,0,-s)),
Planar64Vec3::Y,
Planar64Vec3(glam::i64vec3(s,0,c)),
)
}
#[inline]
pub const fn inverse(&self)->Self{
let det=(
-self.x_axis.0.z as i128*self.y_axis.0.y as i128*self.z_axis.0.x as i128
+self.x_axis.0.y as i128*self.y_axis.0.z as i128*self.z_axis.0.x as i128
+self.x_axis.0.z as i128*self.y_axis.0.x as i128*self.z_axis.0.y as i128
-self.x_axis.0.x as i128*self.y_axis.0.z as i128*self.z_axis.0.y as i128
-self.x_axis.0.y as i128*self.y_axis.0.x as i128*self.z_axis.0.z as i128
+self.x_axis.0.x as i128*self.y_axis.0.y as i128*self.z_axis.0.z as i128
)>>32;
Self{
x_axis:Planar64Vec3::raw_xyz((((-(self.y_axis.0.z as i128*self.z_axis.0.y as i128)+self.y_axis.0.y as i128*self.z_axis.0.z as i128)<<32)/det) as i64,(((self.x_axis.0.z as i128*self.z_axis.0.y as i128-self.x_axis.0.y as i128*self.z_axis.0.z as i128)<<32)/det) as i64,(((-(self.x_axis.0.z as i128*self.y_axis.0.y as i128)+self.x_axis.0.y as i128*self.y_axis.0.z as i128)<<32)/det) as i64),
y_axis:Planar64Vec3::raw_xyz((((self.y_axis.0.z as i128*self.z_axis.0.x as i128-self.y_axis.0.x as i128*self.z_axis.0.z as i128)<<32)/det) as i64,(((-(self.x_axis.0.z as i128*self.z_axis.0.x as i128)+self.x_axis.0.x as i128*self.z_axis.0.z as i128)<<32)/det) as i64,(((self.x_axis.0.z as i128*self.y_axis.0.x as i128-self.x_axis.0.x as i128*self.y_axis.0.z as i128)<<32)/det) as i64),
z_axis:Planar64Vec3::raw_xyz((((-(self.y_axis.0.y as i128*self.z_axis.0.x as i128)+self.y_axis.0.x as i128*self.z_axis.0.y as i128)<<32)/det) as i64,(((self.x_axis.0.y as i128*self.z_axis.0.x as i128-self.x_axis.0.x as i128*self.z_axis.0.y as i128)<<32)/det) as i64,(((-(self.x_axis.0.y as i128*self.y_axis.0.x as i128)+self.x_axis.0.x as i128*self.y_axis.0.y as i128)<<32)/det) as i64),
}
}
#[inline]
pub const fn inverse_times_det(&self)->Self{
Self{
x_axis:Planar64Vec3::raw_xyz(((-(self.y_axis.0.z as i128*self.z_axis.0.y as i128)+self.y_axis.0.y as i128*self.z_axis.0.z as i128)>>32) as i64,((self.x_axis.0.z as i128*self.z_axis.0.y as i128-self.x_axis.0.y as i128*self.z_axis.0.z as i128)>>32) as i64,((-(self.x_axis.0.z as i128*self.y_axis.0.y as i128)+self.x_axis.0.y as i128*self.y_axis.0.z as i128)>>32) as i64),
y_axis:Planar64Vec3::raw_xyz(((self.y_axis.0.z as i128*self.z_axis.0.x as i128-self.y_axis.0.x as i128*self.z_axis.0.z as i128)>>32) as i64,((-(self.x_axis.0.z as i128*self.z_axis.0.x as i128)+self.x_axis.0.x as i128*self.z_axis.0.z as i128)>>32) as i64,((self.x_axis.0.z as i128*self.y_axis.0.x as i128-self.x_axis.0.x as i128*self.y_axis.0.z as i128)>>32) as i64),
z_axis:Planar64Vec3::raw_xyz(((-(self.y_axis.0.y as i128*self.z_axis.0.x as i128)+self.y_axis.0.x as i128*self.z_axis.0.y as i128)>>32) as i64,((self.x_axis.0.y as i128*self.z_axis.0.x as i128-self.x_axis.0.x as i128*self.z_axis.0.y as i128)>>32) as i64,((-(self.x_axis.0.y as i128*self.y_axis.0.x as i128)+self.x_axis.0.x as i128*self.y_axis.0.y as i128)>>32) as i64),
}
}
#[inline]
pub const fn transpose(&self)->Self{
Self{
x_axis:Planar64Vec3::raw_xyz(self.x_axis.0.x,self.y_axis.0.x,self.z_axis.0.x),
y_axis:Planar64Vec3::raw_xyz(self.x_axis.0.y,self.y_axis.0.y,self.z_axis.0.y),
z_axis:Planar64Vec3::raw_xyz(self.x_axis.0.z,self.y_axis.0.z,self.z_axis.0.z),
}
}
#[inline]
pub const fn determinant(&self)->Planar64{
Planar64(((
-self.x_axis.0.z as i128*self.y_axis.0.y as i128*self.z_axis.0.x as i128
+self.x_axis.0.y as i128*self.y_axis.0.z as i128*self.z_axis.0.x as i128
+self.x_axis.0.z as i128*self.y_axis.0.x as i128*self.z_axis.0.y as i128
-self.x_axis.0.x as i128*self.y_axis.0.z as i128*self.z_axis.0.y as i128
-self.x_axis.0.y as i128*self.y_axis.0.x as i128*self.z_axis.0.z as i128
+self.x_axis.0.x as i128*self.y_axis.0.y as i128*self.z_axis.0.z as i128
)>>64) as i64)
}
}
impl Into<glam::Mat3> for Planar64Mat3{
#[inline]
fn into(self)->glam::Mat3{
glam::Mat3::from_cols(
self.x_axis.into(),
self.y_axis.into(),
self.z_axis.into(),
)
}
}
impl TryFrom<glam::Mat3A> for Planar64Mat3{
type Error=Planar64TryFromFloatError;
#[inline]
fn try_from(value:glam::Mat3A)->Result<Self,Self::Error>{
Ok(Self{
x_axis:Planar64Vec3::try_from(value.x_axis)?,
y_axis:Planar64Vec3::try_from(value.y_axis)?,
z_axis:Planar64Vec3::try_from(value.z_axis)?,
})
}
}
impl std::fmt::Display for Planar64Mat3{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"\n{:.3},{:.3},{:.3}\n{:.3},{:.3},{:.3}\n{:.3},{:.3},{:.3}",
Into::<f32>::into(self.x_axis.x()),Into::<f32>::into(self.x_axis.y()),Into::<f32>::into(self.x_axis.z()),
Into::<f32>::into(self.y_axis.x()),Into::<f32>::into(self.y_axis.y()),Into::<f32>::into(self.y_axis.z()),
Into::<f32>::into(self.z_axis.x()),Into::<f32>::into(self.z_axis.y()),Into::<f32>::into(self.z_axis.z()),
)
}
}
impl std::ops::Mul<Planar64Vec3> for Planar64Mat3{
type Output=Planar64Vec3;
#[inline]
fn mul(self,rhs:Planar64Vec3) -> Self::Output {
self.x_axis*rhs.x()
+self.y_axis*rhs.y()
+self.z_axis*rhs.z()
}
}
impl std::ops::Div<i64> for Planar64Mat3{
type Output=Planar64Mat3;
#[inline]
fn div(self,rhs:i64)->Self::Output{
Planar64Mat3{
x_axis:self.x_axis/rhs,
y_axis:self.y_axis/rhs,
z_axis:self.z_axis/rhs,
}
} }
} }
///[-1.0,1.0] = [-2^32,2^32]
#[derive(Clone,Copy,Default,Hash,Eq,PartialEq)] #[derive(Clone,Copy,Default,Hash,Eq,PartialEq)]
pub struct Planar64Affine3{ pub struct Planar64Affine3{
pub matrix3:Planar64Mat3,//includes scale above 1 pub matrix3:Planar64Mat3,//includes scale above 1
pub translation:Planar64Vec3, pub translation:Planar64Vec3,
} }
impl Planar64Affine3{ impl Planar64Affine3{
#[inline] #[inline]
pub fn new(matrix3:Planar64Mat3,translation:Planar64Vec3)->Self{ pub const fn new(matrix3:Planar64Mat3,translation:Planar64Vec3)->Self{
Self{matrix3,translation} Self{matrix3,translation}
} }
#[inline] #[inline]
pub fn transform_point3(&self,point:Planar64Vec3) -> Planar64Vec3{ pub fn transform_point3(&self,point:Planar64Vec3)->vec3::Vector3<Fixed<2,64>>{
Planar64Vec3( self.translation.fix_2()+self.matrix3*point
self.translation.0
+(self.matrix3.x_axis*point.x()).0
+(self.matrix3.y_axis*point.y()).0
+(self.matrix3.z_axis*point.z()).0
)
} }
} }
impl Into<glam::Mat4> for Planar64Affine3{ impl Into<glam::Mat4> for Planar64Affine3{
#[inline] #[inline]
fn into(self)->glam::Mat4{ fn into(self)->glam::Mat4{
let matrix3=self.matrix3.to_array().map(|row|row.map(Into::<f32>::into));
let translation=self.translation.to_array().map(Into::<f32>::into);
glam::Mat4::from_cols_array(&[ glam::Mat4::from_cols_array(&[
self.matrix3.x_axis.0.x as f32,self.matrix3.x_axis.0.y as f32,self.matrix3.x_axis.0.z as f32,0.0, matrix3[0][0],matrix3[0][1],matrix3[0][2],0.0,
self.matrix3.y_axis.0.x as f32,self.matrix3.y_axis.0.y as f32,self.matrix3.y_axis.0.z as f32,0.0, matrix3[1][0],matrix3[1][1],matrix3[1][2],0.0,
self.matrix3.z_axis.0.x as f32,self.matrix3.z_axis.0.y as f32,self.matrix3.z_axis.0.z as f32,0.0, matrix3[2][0],matrix3[2][1],matrix3[2][2],0.0,
self.translation.0.x as f32,self.translation.0.y as f32,self.translation.0.z as f32,PLANAR64_ONE_FLOAT32 translation[0],translation[1],translation[2],1.0
])*PLANAR64_CONVERT_TO_FLOAT32 ])
}
}
impl TryFrom<glam::Affine3A> for Planar64Affine3{
type Error=Planar64TryFromFloatError;
fn try_from(value: glam::Affine3A)->Result<Self, Self::Error> {
Ok(Self{
matrix3:Planar64Mat3::try_from(value.matrix3)?,
translation:Planar64Vec3::try_from(value.translation)?
})
}
}
impl std::fmt::Display for Planar64Affine3{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"translation: {:.3},{:.3},{:.3}\nmatrix3:\n{:.3},{:.3},{:.3}\n{:.3},{:.3},{:.3}\n{:.3},{:.3},{:.3}",
Into::<f32>::into(self.translation.x()),Into::<f32>::into(self.translation.y()),Into::<f32>::into(self.translation.z()),
Into::<f32>::into(self.matrix3.x_axis.x()),Into::<f32>::into(self.matrix3.x_axis.y()),Into::<f32>::into(self.matrix3.x_axis.z()),
Into::<f32>::into(self.matrix3.y_axis.x()),Into::<f32>::into(self.matrix3.y_axis.y()),Into::<f32>::into(self.matrix3.y_axis.z()),
Into::<f32>::into(self.matrix3.z_axis.x()),Into::<f32>::into(self.matrix3.z_axis.y()),Into::<f32>::into(self.matrix3.z_axis.z()),
)
} }
} }
#[test] #[test]
fn test_sqrt(){ fn test_sqrt(){
let r=Planar64::int(400); let r=int(400);
assert_eq!(1717986918400,r.get()); assert_eq!(r,Planar64::raw(1717986918400));
let s=r.sqrt(); let s=r.sqrt();
assert_eq!(85899345920,s.get()); assert_eq!(s,Planar64::raw(85899345920));
} }

View File

@ -5,7 +5,6 @@ pub mod aabb;
pub mod model; pub mod model;
pub mod mouse; pub mod mouse;
pub mod timer; pub mod timer;
pub mod zeroes;
pub mod integer; pub mod integer;
pub mod physics; pub mod physics;
pub mod updatable; pub mod updatable;

View File

@ -283,19 +283,19 @@ mod test{
#[test] #[test]
fn test_timerfixed_scaled(){ fn test_timerfixed_scaled(){
//create a paused timer that reads 0s //create a paused timer that reads 0s
let timer=TimerFixed::<Scaled,Paused>::new(sec!(0),sec!(0)); let timer=TimerFixed::<Scaled,Paused>::from_state(Scaled{scale:0.5f32.try_into().unwrap(),offset:sec!(0)});
//the paused timer at 1 second should read 0s //the paused timer at 1 second should read 0s
assert_eq!(timer.time(sec!(1)),sec!(0)); assert_eq!(timer.time(sec!(1)),sec!(0));
//unpause it after one second //unpause it after one second
let timer=timer.into_unpaused(sec!(1)); let timer=timer.into_unpaused(sec!(1));
//the timer at 6 seconds should read 5s //the timer at 6 seconds should read 2.5s
assert_eq!(timer.time(sec!(6)),sec!(5)); assert_eq!(timer.time(sec!(6)),Time::from_millis(2500));
//pause the timer after 11 seconds //pause the timer after 11 seconds
let timer=timer.into_paused(sec!(11)); let timer=timer.into_paused(sec!(11));
//the paused timer at 20 seconds should read 10s //the paused timer at 20 seconds should read 5s
assert_eq!(timer.time(sec!(20)),sec!(10)); assert_eq!(timer.time(sec!(20)),sec!(5));
} }
#[test] #[test]
fn test_timer()->Result<(),Error>{ fn test_timer()->Result<(),Error>{

View File

@ -1,41 +0,0 @@
//find roots of polynomials
use arrayvec::ArrayVec;
use crate::integer::Planar64;
#[inline]
pub fn zeroes2(a0:Planar64,a1:Planar64,a2:Planar64)->ArrayVec<Planar64,2>{
if a2==Planar64::ZERO{
return zeroes1(a0,a1);
}
let radicand=a1.get() as i128*a1.get() as i128-a2.get() as i128*a0.get() as i128*4;
if 0<radicand{
//start with f64 sqrt
//failure case: 2^63 < sqrt(2^127)
let planar_radicand=Planar64::raw(unsafe{(radicand as f64).sqrt().to_int_unchecked()});
//TODO: one or two newtons
//sort roots ascending and avoid taking the difference of large numbers
match (Planar64::ZERO<a2,Planar64::ZERO<a1){
(true, true )=>[(-a1-planar_radicand)/(a2*2),(a0*2)/(-a1-planar_radicand)].into(),
(true, false)=>[(a0*2)/(-a1+planar_radicand),(-a1+planar_radicand)/(a2*2)].into(),
(false,true )=>[(a0*2)/(-a1-planar_radicand),(-a1-planar_radicand)/(a2*2)].into(),
(false,false)=>[(-a1+planar_radicand)/(a2*2),(a0*2)/(-a1+planar_radicand)].into(),
}
}else if radicand==0{
return ArrayVec::from_iter([a1/(a2*-2)]);
}else{
return ArrayVec::new_const();
}
}
#[inline]
pub fn zeroes1(a0:Planar64,a1:Planar64)->ArrayVec<Planar64,2>{
if a1==Planar64::ZERO{
return ArrayVec::new_const();
}else{
let q=((-a0.get() as i128)<<32)/(a1.get() as i128);
if i64::MIN as i128<=q&&q<=i64::MAX as i128{
return ArrayVec::from_iter([Planar64::raw(q as i64)]);
}else{
return ArrayVec::new_const();
}
}
}