forked from StrafesNET/strafe-client
Compare commits
20 Commits
master
...
tickless-p
Author | SHA1 | Date | |
---|---|---|---|
171e3c34fc | |||
35058436a7 | |||
e9622a7716 | |||
bbf93ad6e3 | |||
f2461c4f39 | |||
2a13c57553 | |||
e936be4f3c | |||
543ff75be9 | |||
4978341b5b | |||
2d0e80323b | |||
5cac0e6153 | |||
943c3ca1c2 | |||
5174b13ba0 | |||
c7da64cc64 | |||
6ada08ef6f | |||
4c079068d6 | |||
f60a370228 | |||
79053420cd | |||
ea9ac948f8 | |||
7f841427dd |
517
src/body.rs
517
src/body.rs
@ -1,10 +1,12 @@
|
|||||||
use crate::instruction::TimedInstruction;
|
use crate::{instruction::{InstructionEmitter, InstructionConsumer, TimedInstruction}, zeroes::zeroes2};
|
||||||
|
|
||||||
pub enum PhysicsInstruction {
|
pub enum PhysicsInstruction {
|
||||||
CollisionStart(RelativeCollision),
|
CollisionStart(RelativeCollision),
|
||||||
CollisionEnd(RelativeCollision),
|
CollisionEnd(RelativeCollision),
|
||||||
StrafeTick,
|
StrafeTick,
|
||||||
Jump,
|
Jump,
|
||||||
|
SetWalkTargetVelocity(glam::Vec3),
|
||||||
|
ReachWalkTargetVelocity,
|
||||||
// Water,
|
// Water,
|
||||||
// Spawn(
|
// Spawn(
|
||||||
// Option<SpawnId>,
|
// Option<SpawnId>,
|
||||||
@ -14,15 +16,91 @@ pub enum PhysicsInstruction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct Body {
|
pub struct Body {
|
||||||
pub position: glam::Vec3,//I64 where 2^32 = 1 u
|
position: glam::Vec3,//I64 where 2^32 = 1 u
|
||||||
pub velocity: glam::Vec3,//I64 where 2^32 = 1 u/s
|
velocity: glam::Vec3,//I64 where 2^32 = 1 u/s
|
||||||
pub time: TIME,//nanoseconds x xxxxD!
|
acceleration: glam::Vec3,//I64 where 2^32 = 1 u/s/s
|
||||||
|
time: TIME,//nanoseconds x xxxxD!
|
||||||
|
//origin_time = timestamp of position and velocity
|
||||||
|
//processed_time = starting time for new events. prevents colliding with the analytic euqation in the past
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum MoveRestriction {
|
||||||
|
Air,
|
||||||
|
Water,
|
||||||
|
Ground,
|
||||||
|
Ladder,//multiple ladders how
|
||||||
|
}
|
||||||
|
|
||||||
|
enum MouseInterpolation {
|
||||||
|
First,//just checks the last value
|
||||||
|
Lerp,//lerps between
|
||||||
|
}
|
||||||
|
|
||||||
|
enum InputInstruction {
|
||||||
|
MoveMouse(glam::IVec2),
|
||||||
|
Jump(bool),
|
||||||
|
}
|
||||||
|
|
||||||
|
struct InputState {
|
||||||
|
controls: u32,
|
||||||
|
mouse_interpolation: MouseInterpolation,
|
||||||
|
time: TIME,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InputState {
|
||||||
|
pub fn get_control(&self,control:u32) -> bool {
|
||||||
|
self.controls&control!=0
|
||||||
|
}
|
||||||
|
pub fn process_instruction(&mut self,ins:InputInstruction){
|
||||||
|
match ins {
|
||||||
|
InputInstruction::MoveMouse(m) => todo!("set mouse_interpolation"),
|
||||||
|
InputInstruction::Jump(b) => todo!("how does info about style modifiers get here"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct MouseInterpolationState {
|
||||||
|
interpolation: MouseInterpolation,
|
||||||
|
time0: TIME,
|
||||||
|
time1: TIME,
|
||||||
|
mouse0: glam::IVec2,
|
||||||
|
mouse1: glam::IVec2,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MouseInterpolationState {
|
||||||
|
pub fn move_mouse(&mut self,time:TIME,pos:glam::IVec2){
|
||||||
|
self.time0=self.time1;
|
||||||
|
self.mouse0=self.mouse1;
|
||||||
|
self.time1=time;
|
||||||
|
self.mouse1=pos;
|
||||||
|
}
|
||||||
|
pub fn interpolated_position(&self,time:TIME) -> glam::IVec2 {
|
||||||
|
match self.interpolation {
|
||||||
|
MouseInterpolation::First => self.mouse0,
|
||||||
|
MouseInterpolation::Lerp => {
|
||||||
|
let m0=self.mouse0.as_i64vec2();
|
||||||
|
let m1=self.mouse1.as_i64vec2();
|
||||||
|
//these are deltas
|
||||||
|
let t1t=(self.time1-time) as i64;
|
||||||
|
let tt0=(time-self.time0) as i64;
|
||||||
|
let dt=(self.time1-self.time0) as i64;
|
||||||
|
((m0*t1t+m1*tt0)/dt).as_ivec2()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PhysicsState {
|
pub struct PhysicsState {
|
||||||
pub body: Body,
|
pub body: Body,
|
||||||
pub contacts: Vec<RelativeCollision>,
|
pub hitbox_halfsize: glam::Vec3,
|
||||||
|
pub contacts: std::collections::HashSet::<RelativeCollision>,
|
||||||
|
//pub intersections: Vec<ModelId>,
|
||||||
|
//temp
|
||||||
pub models_cringe_clone: Vec<Model>,
|
pub models_cringe_clone: Vec<Model>,
|
||||||
|
pub temp_control_dir: glam::Vec3,
|
||||||
|
//camera must exist in state because wormholes modify the camera, also camera punch
|
||||||
|
//pub camera: Camera,
|
||||||
|
//pub mouse_interpolation: MouseInterpolationState,
|
||||||
pub time: TIME,
|
pub time: TIME,
|
||||||
pub strafe_tick_num: TIME,
|
pub strafe_tick_num: TIME,
|
||||||
pub strafe_tick_den: TIME,
|
pub strafe_tick_den: TIME,
|
||||||
@ -30,12 +108,13 @@ pub struct PhysicsState {
|
|||||||
pub mv: f32,
|
pub mv: f32,
|
||||||
pub walkspeed: f32,
|
pub walkspeed: f32,
|
||||||
pub friction: f32,
|
pub friction: f32,
|
||||||
|
pub walk_target_velocity: glam::Vec3,
|
||||||
pub gravity: glam::Vec3,
|
pub gravity: glam::Vec3,
|
||||||
pub grounded: bool,
|
pub grounded: bool,
|
||||||
pub jump_trying: bool,
|
pub jump_trying: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone,Copy)]
|
#[derive(Clone,Copy,Hash,Eq,PartialEq)]
|
||||||
pub enum AabbFace{
|
pub enum AabbFace{
|
||||||
Right,//+X
|
Right,//+X
|
||||||
Top,
|
Top,
|
||||||
@ -59,6 +138,16 @@ impl Aabb {
|
|||||||
// [0.0f32, 1., 0.],
|
// [0.0f32, 1., 0.],
|
||||||
// [0.0f32, -1., 0.],
|
// [0.0f32, -1., 0.],
|
||||||
// ];
|
// ];
|
||||||
|
const VERTEX_DATA: [glam::Vec3; 8] = [
|
||||||
|
glam::vec3(1., -1., -1.),
|
||||||
|
glam::vec3(1., 1., -1.),
|
||||||
|
glam::vec3(1., 1., 1.),
|
||||||
|
glam::vec3(1., -1., 1.),
|
||||||
|
glam::vec3(-1., -1., 1.),
|
||||||
|
glam::vec3(-1., 1., 1.),
|
||||||
|
glam::vec3(-1., 1., -1.),
|
||||||
|
glam::vec3(-1., -1., -1.),
|
||||||
|
];
|
||||||
const VERTEX_DATA_RIGHT: [glam::Vec3; 4] = [
|
const VERTEX_DATA_RIGHT: [glam::Vec3; 4] = [
|
||||||
glam::vec3(1., -1., -1.),
|
glam::vec3(1., -1., -1.),
|
||||||
glam::vec3(1., 1., -1.),
|
glam::vec3(1., 1., -1.),
|
||||||
@ -115,7 +204,10 @@ impl Aabb {
|
|||||||
AabbFace::Front => glam::vec3(0.,0.,-1.),
|
AabbFace::Front => glam::vec3(0.,0.,-1.),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn face_vertices(face:AabbFace) -> [glam::Vec3;4] {
|
pub fn unit_vertices() -> [glam::Vec3;8] {
|
||||||
|
return Self::VERTEX_DATA;
|
||||||
|
}
|
||||||
|
pub fn unit_face_vertices(face:AabbFace) -> [glam::Vec3;4] {
|
||||||
match face {
|
match face {
|
||||||
AabbFace::Right => Self::VERTEX_DATA_RIGHT,
|
AabbFace::Right => Self::VERTEX_DATA_RIGHT,
|
||||||
AabbFace::Top => Self::VERTEX_DATA_TOP,
|
AabbFace::Top => Self::VERTEX_DATA_TOP,
|
||||||
@ -127,7 +219,8 @@ impl Aabb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Face = AabbFace;
|
//pretend to be using what we want to eventually do
|
||||||
|
type TreyMeshFace = AabbFace;
|
||||||
type TreyMesh = Aabb;
|
type TreyMesh = Aabb;
|
||||||
|
|
||||||
pub struct Model {
|
pub struct Model {
|
||||||
@ -140,25 +233,36 @@ impl Model {
|
|||||||
pub fn new(transform:glam::Mat4) -> Self {
|
pub fn new(transform:glam::Mat4) -> Self {
|
||||||
Self{transform}
|
Self{transform}
|
||||||
}
|
}
|
||||||
pub fn face_vertices(&self,face:Face) -> [glam::Vec3;4] {
|
pub fn unit_vertices(&self) -> [glam::Vec3;8] {
|
||||||
Aabb::face_vertices(face)
|
Aabb::unit_vertices()
|
||||||
}
|
}
|
||||||
pub fn face_mesh(&self,face:Face) -> TreyMesh {
|
pub fn mesh(&self) -> TreyMesh {
|
||||||
let mut aabb=Aabb::new();
|
let mut aabb=Aabb::new();
|
||||||
for &vertex in self.face_vertices(face).iter() {
|
for &vertex in self.unit_vertices().iter() {
|
||||||
aabb.grow(vertex);
|
aabb.grow(glam::Vec4Swizzles::xyz(self.transform*vertex.extend(1.0)));
|
||||||
}
|
}
|
||||||
return aabb;
|
return aabb;
|
||||||
}
|
}
|
||||||
pub fn face_normal(&self,face:Face) -> glam::Vec3 {
|
pub fn unit_face_vertices(&self,face:TreyMeshFace) -> [glam::Vec3;4] {
|
||||||
let mut n=glam::Vec3Swizzles::xyzz(Aabb::normal(face));
|
Aabb::unit_face_vertices(face)
|
||||||
n.w=0.0;//what a man will do to avoid writing out the components
|
}
|
||||||
glam::Vec4Swizzles::xyz(self.transform*n)//this is wrong for scale
|
pub fn face_mesh(&self,face:TreyMeshFace) -> TreyMesh {
|
||||||
|
let mut aabb=Aabb::new();
|
||||||
|
for &vertex in self.unit_face_vertices(face).iter() {
|
||||||
|
aabb.grow(glam::Vec4Swizzles::xyz(self.transform*vertex.extend(1.0)));
|
||||||
|
}
|
||||||
|
return aabb;
|
||||||
|
}
|
||||||
|
pub fn face_normal(&self,face:TreyMeshFace) -> glam::Vec3 {
|
||||||
|
glam::Vec4Swizzles::xyz(self.transform*Aabb::normal(face).extend(0.0))//this is wrong for scale
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//need non-face (full model) variant for CanCollide false objects
|
||||||
|
//OR have a separate list from contacts for model intersection
|
||||||
|
#[derive(Eq, Hash, PartialEq)]
|
||||||
pub struct RelativeCollision {
|
pub struct RelativeCollision {
|
||||||
face: Face,//just an id
|
face: TreyMeshFace,//just an id
|
||||||
model: u32,//using id to avoid lifetimes
|
model: u32,//using id to avoid lifetimes
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,50 +277,41 @@ impl RelativeCollision {
|
|||||||
|
|
||||||
pub type TIME = i64;
|
pub type TIME = i64;
|
||||||
|
|
||||||
const CONTROL_JUMP:u32 = 0b01000000;//temp DATA NORMALIZATION!@#$
|
impl Body {
|
||||||
|
pub fn with_position(position:glam::Vec3) -> Self {
|
||||||
|
Self{
|
||||||
|
position: position,
|
||||||
|
velocity: glam::Vec3::ZERO,
|
||||||
|
acceleration: glam::Vec3::ZERO,
|
||||||
|
time: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn extrapolated_position(&self,time: TIME)->glam::Vec3{
|
||||||
|
let dt=(time-self.time) as f64/1_000_000_000f64;
|
||||||
|
self.position+self.velocity*(dt as f32)+self.acceleration*((0.5*dt*dt) as f32)
|
||||||
|
}
|
||||||
|
pub fn advance_time(&mut self, time: TIME){
|
||||||
|
self.position=self.extrapolated_position(time);
|
||||||
|
self.time=time;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl PhysicsState {
|
impl PhysicsState {
|
||||||
//delete this, we are tickless gamers
|
//tickless gaming
|
||||||
pub fn run(&mut self, time: TIME, control_dir: glam::Vec3, controls: u32){
|
pub fn run(&mut self, time: TIME){
|
||||||
let target_tick = (time*self.strafe_tick_num/self.strafe_tick_den) as u32;
|
//prepare is ommitted - everything is done via instructions.
|
||||||
//the game code can run for 1 month before running out of ticks
|
while let Some(instruction) = self.next_instruction(time) {//collect
|
||||||
while self.tick<target_tick {
|
//advance
|
||||||
self.tick += 1;
|
//self.advance_time(instruction.time);
|
||||||
let dt=0.01;
|
//process
|
||||||
let d=self.body.velocity.dot(control_dir);
|
self.process_instruction(instruction);
|
||||||
if d<self.mv {
|
//write hash lol
|
||||||
self.body.velocity+=(self.mv-d)*control_dir;
|
|
||||||
}
|
|
||||||
self.body.velocity+=self.gravity*dt;
|
|
||||||
self.body.position+=self.body.velocity*dt;
|
|
||||||
if self.body.position.y<0.0{
|
|
||||||
self.body.position.y=0.0;
|
|
||||||
self.body.velocity.y=0.0;
|
|
||||||
self.grounded=true;
|
|
||||||
}
|
|
||||||
if self.grounded&&(controls&CONTROL_JUMP)!=0 {
|
|
||||||
self.grounded=false;
|
|
||||||
self.body.velocity+=glam::Vec3::new(0.0,0.715588/2.0*100.0,0.0);
|
|
||||||
}
|
|
||||||
if self.grounded {
|
|
||||||
let applied_friction=self.friction*dt;
|
|
||||||
let targetv=control_dir*self.walkspeed;
|
|
||||||
let diffv=targetv-self.body.velocity;
|
|
||||||
if applied_friction*applied_friction<diffv.length_squared() {
|
|
||||||
self.body.velocity+=applied_friction*diffv.normalize();
|
|
||||||
} else {
|
|
||||||
//PhysicsInstruction::WalkTargetReached
|
|
||||||
self.body.velocity=targetv;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.body.time=target_tick as TIME*self.strafe_tick_den/self.strafe_tick_num;
|
pub fn advance_time(&mut self, time: TIME){
|
||||||
}
|
self.body.advance_time(time);
|
||||||
|
self.time=time;
|
||||||
//delete this
|
|
||||||
pub fn extrapolate_position(&self, time: TIME) -> glam::Vec3 {
|
|
||||||
let dt=(time-self.body.time) as f64/1_000_000_000f64;
|
|
||||||
self.body.position+self.body.velocity*(dt as f32)+self.gravity*((0.5*dt*dt) as f32)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_strafe_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
|
fn next_strafe_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
|
||||||
@ -262,20 +357,259 @@ impl PhysicsState {
|
|||||||
//check if you are accelerating towards a walk target velocity and create an instruction
|
//check if you are accelerating towards a walk target velocity and create an instruction
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
fn predict_collision_end(&self,model:&Model) -> Option<TimedInstruction<PhysicsInstruction>> {
|
fn mesh(&self) -> TreyMesh {
|
||||||
|
let mut aabb=Aabb::new();
|
||||||
|
for vertex in Aabb::unit_vertices(){
|
||||||
|
aabb.grow(self.body.position+self.hitbox_halfsize*vertex);
|
||||||
|
}
|
||||||
|
aabb
|
||||||
|
}
|
||||||
|
fn predict_collision_end(&self,model:&Model,time_limit:TIME,model_id:u32) -> Option<TimedInstruction<PhysicsInstruction>> {
|
||||||
//must treat cancollide false objects differently: you may not exit through the same face you entered.
|
//must treat cancollide false objects differently: you may not exit through the same face you entered.
|
||||||
|
//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
|
||||||
|
let mut best_delta_time=time_limit-self.body.time;
|
||||||
|
let mut best_face:Option<TreyMeshFace>=None;
|
||||||
|
let mesh0=self.mesh();
|
||||||
|
let mesh1=model.mesh();
|
||||||
|
let (p,v,a)=(self.body.position,-self.body.velocity,self.body.acceleration);
|
||||||
|
//collect x
|
||||||
|
for t in zeroes2(mesh0.max.x-mesh1.min.x, v.x, a.x) {
|
||||||
|
//negative t = back in time
|
||||||
|
//must be moving towards surface to collide
|
||||||
|
//must beat the current soonest collision time
|
||||||
|
//must be moving towards surface
|
||||||
|
let t_time=((-t as f64)*1_000_000_000f64) as TIME;
|
||||||
|
if 0<=t_time&&t_time<best_delta_time&&0f32<v.x+a.x*-t{
|
||||||
|
let dp=self.body.extrapolated_position(self.body.time+t_time)-p;
|
||||||
|
//faces must be overlapping
|
||||||
|
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 {
|
||||||
|
//collect valid t
|
||||||
|
best_delta_time=t_time;
|
||||||
|
best_face=Some(TreyMeshFace::Left);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for t in zeroes2(mesh0.min.x-mesh1.max.x, v.x, a.x) {
|
||||||
|
//negative t = back in time
|
||||||
|
//must be moving towards surface to collide
|
||||||
|
//must beat the current soonest collision time
|
||||||
|
//must be moving towards surface
|
||||||
|
let t_time=((-t as f64)*1_000_000_000f64) as TIME;
|
||||||
|
if 0<=t_time&&t_time<best_delta_time&&v.x+a.x*-t<0f32{
|
||||||
|
let dp=self.body.extrapolated_position(self.body.time+t_time)-p;
|
||||||
|
//faces must be overlapping
|
||||||
|
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 {
|
||||||
|
//collect valid t
|
||||||
|
best_delta_time=t_time;
|
||||||
|
best_face=Some(TreyMeshFace::Right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//collect y
|
||||||
|
for t in zeroes2(mesh0.max.y-mesh1.min.y, v.y, a.y) {
|
||||||
|
//negative t = back in time
|
||||||
|
//must be moving towards surface to collide
|
||||||
|
//must beat the current soonest collision time
|
||||||
|
//must be moving towards surface
|
||||||
|
let t_time=((-t as f64)*1_000_000_000f64) as TIME;
|
||||||
|
if 0<=t_time&&t_time<best_delta_time&&0f32<v.y+a.y*-t{
|
||||||
|
let dp=self.body.extrapolated_position(self.body.time+t_time)-p;
|
||||||
|
//faces must be overlapping
|
||||||
|
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 {
|
||||||
|
//collect valid t
|
||||||
|
best_delta_time=t_time;
|
||||||
|
best_face=Some(TreyMeshFace::Top);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for t in zeroes2(mesh0.min.y-mesh1.max.y, v.y, a.y) {
|
||||||
|
//negative t = back in time
|
||||||
|
//must be moving towards surface to collide
|
||||||
|
//must beat the current soonest collision time
|
||||||
|
//must be moving towards surface
|
||||||
|
let t_time=((-t as f64)*1_000_000_000f64) as TIME;
|
||||||
|
if 0<=t_time&&t_time<best_delta_time&&v.y+a.y*-t<0f32{
|
||||||
|
let dp=self.body.extrapolated_position(self.body.time+t_time)-p;
|
||||||
|
//faces must be overlapping
|
||||||
|
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 {
|
||||||
|
//collect valid t
|
||||||
|
best_delta_time=t_time;
|
||||||
|
best_face=Some(TreyMeshFace::Bottom);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//collect z
|
||||||
|
for t in zeroes2(mesh0.max.z-mesh1.min.z, v.z, a.z) {
|
||||||
|
//negative t = back in time
|
||||||
|
//must be moving towards surface to collide
|
||||||
|
//must beat the current soonest collision time
|
||||||
|
//must be moving towards surface
|
||||||
|
let t_time=((-t as f64)*1_000_000_000f64) as TIME;
|
||||||
|
if 0<=t_time&&t_time<best_delta_time&&0f32<v.z+a.z*-t{
|
||||||
|
let dp=self.body.extrapolated_position(self.body.time+t_time)-p;
|
||||||
|
//faces must be overlapping
|
||||||
|
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 {
|
||||||
|
//collect valid t
|
||||||
|
best_delta_time=t_time;
|
||||||
|
best_face=Some(TreyMeshFace::Back);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for t in zeroes2(mesh0.min.z-mesh1.max.z, v.z, a.z) {
|
||||||
|
//negative t = back in time
|
||||||
|
//must be moving towards surface to collide
|
||||||
|
//must beat the current soonest collision time
|
||||||
|
//must be moving towards surface
|
||||||
|
let t_time=((-t as f64)*1_000_000_000f64) as TIME;
|
||||||
|
if 0<=t_time&&t_time<best_delta_time&&v.z+a.z*-t<0f32{
|
||||||
|
let dp=self.body.extrapolated_position(self.body.time+t_time)-p;
|
||||||
|
//faces must be overlapping
|
||||||
|
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 {
|
||||||
|
//collect valid t
|
||||||
|
best_delta_time=t_time;
|
||||||
|
best_face=Some(TreyMeshFace::Front);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//generate instruction
|
||||||
|
if let Some(face) = best_face{
|
||||||
|
return Some(TimedInstruction {
|
||||||
|
time: self.body.time+best_delta_time,
|
||||||
|
instruction: PhysicsInstruction::CollisionStart(RelativeCollision {
|
||||||
|
face,
|
||||||
|
model: model_id
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
fn predict_collision_start(&self,model:&Model) -> Option<TimedInstruction<PhysicsInstruction>> {
|
fn predict_collision_start(&self,model:&Model,time_limit:TIME,model_id:u32) -> Option<TimedInstruction<PhysicsInstruction>> {
|
||||||
|
//find best t
|
||||||
|
let mut best_delta_time=time_limit-self.body.time;
|
||||||
|
let mut best_face:Option<TreyMeshFace>=None;
|
||||||
|
let mesh0=self.mesh();
|
||||||
|
let mesh1=model.mesh();
|
||||||
|
let (p,v,a)=(self.body.position,self.body.velocity,self.body.acceleration);
|
||||||
|
//collect x
|
||||||
|
for t in zeroes2(mesh0.max.x-mesh1.min.x, v.x, a.x) {
|
||||||
|
//negative t = back in time
|
||||||
|
//must be moving towards surface to collide
|
||||||
|
//must beat the current soonest collision time
|
||||||
|
//must be moving towards surface
|
||||||
|
let t_time=((t as f64)*1_000_000_000f64) as TIME;
|
||||||
|
if 0<=t_time&&t_time<best_delta_time&&0f32<v.x+a.x*t{
|
||||||
|
let dp=self.body.extrapolated_position(self.body.time+t_time)-p;
|
||||||
|
//faces must be overlapping
|
||||||
|
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 {
|
||||||
|
//collect valid t
|
||||||
|
best_delta_time=t_time;
|
||||||
|
best_face=Some(TreyMeshFace::Left);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for t in zeroes2(mesh0.min.x-mesh1.max.x, v.x, a.x) {
|
||||||
|
//negative t = back in time
|
||||||
|
//must be moving towards surface to collide
|
||||||
|
//must beat the current soonest collision time
|
||||||
|
//must be moving towards surface
|
||||||
|
let t_time=((t as f64)*1_000_000_000f64) as TIME;
|
||||||
|
if 0<=t_time&&t_time<best_delta_time&&v.x+a.x*t<0f32{
|
||||||
|
let dp=self.body.extrapolated_position(self.body.time+t_time)-p;
|
||||||
|
//faces must be overlapping
|
||||||
|
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 {
|
||||||
|
//collect valid t
|
||||||
|
best_delta_time=t_time;
|
||||||
|
best_face=Some(TreyMeshFace::Right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//collect y
|
||||||
|
for t in zeroes2(mesh0.max.y-mesh1.min.y, v.y, a.y) {
|
||||||
|
//negative t = back in time
|
||||||
|
//must be moving towards surface to collide
|
||||||
|
//must beat the current soonest collision time
|
||||||
|
//must be moving towards surface
|
||||||
|
let t_time=((t as f64)*1_000_000_000f64) as TIME;
|
||||||
|
if 0<=t_time&&t_time<best_delta_time&&0f32<v.y+a.y*t{
|
||||||
|
let dp=self.body.extrapolated_position(self.body.time+t_time)-p;
|
||||||
|
//faces must be overlapping
|
||||||
|
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 {
|
||||||
|
//collect valid t
|
||||||
|
best_delta_time=t_time;
|
||||||
|
best_face=Some(TreyMeshFace::Top);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for t in zeroes2(mesh0.min.y-mesh1.max.y, v.y, a.y) {
|
||||||
|
//negative t = back in time
|
||||||
|
//must be moving towards surface to collide
|
||||||
|
//must beat the current soonest collision time
|
||||||
|
//must be moving towards surface
|
||||||
|
let t_time=((t as f64)*1_000_000_000f64) as TIME;
|
||||||
|
if 0<=t_time&&t_time<best_delta_time&&v.y+a.y*t<0f32{
|
||||||
|
let dp=self.body.extrapolated_position(self.body.time+t_time)-p;
|
||||||
|
//faces must be overlapping
|
||||||
|
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 {
|
||||||
|
//collect valid t
|
||||||
|
best_delta_time=t_time;
|
||||||
|
best_face=Some(TreyMeshFace::Bottom);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//collect z
|
||||||
|
for t in zeroes2(mesh0.max.z-mesh1.min.z, v.z, a.z) {
|
||||||
|
//negative t = back in time
|
||||||
|
//must be moving towards surface to collide
|
||||||
|
//must beat the current soonest collision time
|
||||||
|
//must be moving towards surface
|
||||||
|
let t_time=((t as f64)*1_000_000_000f64) as TIME;
|
||||||
|
if 0<=t_time&&t_time<best_delta_time&&0f32<v.z+a.z*t{
|
||||||
|
let dp=self.body.extrapolated_position(self.body.time+t_time)-p;
|
||||||
|
//faces must be overlapping
|
||||||
|
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 {
|
||||||
|
//collect valid t
|
||||||
|
best_delta_time=t_time;
|
||||||
|
best_face=Some(TreyMeshFace::Back);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for t in zeroes2(mesh0.min.z-mesh1.max.z, v.z, a.z) {
|
||||||
|
//negative t = back in time
|
||||||
|
//must be moving towards surface to collide
|
||||||
|
//must beat the current soonest collision time
|
||||||
|
//must be moving towards surface
|
||||||
|
let t_time=((t as f64)*1_000_000_000f64) as TIME;
|
||||||
|
if 0<=t_time&&t_time<best_delta_time&&v.z+a.z*t<0f32{
|
||||||
|
let dp=self.body.extrapolated_position(self.body.time+t_time)-p;
|
||||||
|
//faces must be overlapping
|
||||||
|
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 {
|
||||||
|
//collect valid t
|
||||||
|
best_delta_time=t_time;
|
||||||
|
best_face=Some(TreyMeshFace::Front);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//generate instruction
|
||||||
|
if let Some(face) = best_face{
|
||||||
|
return Some(TimedInstruction {
|
||||||
|
time: self.body.time+best_delta_time,
|
||||||
|
instruction: PhysicsInstruction::CollisionStart(RelativeCollision {
|
||||||
|
face,
|
||||||
|
model: model_id
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl crate::instruction::InstructionEmitter<PhysicsInstruction> for PhysicsState {
|
impl crate::instruction::InstructionEmitter<PhysicsInstruction> for PhysicsState {
|
||||||
//this little next instruction function can cache its return value and invalidate the cached value by watching the State.
|
//this little next instruction function can cache its return value and invalidate the cached value by watching the State.
|
||||||
fn next_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
|
fn next_instruction(&self,time_limit:TIME) -> Option<TimedInstruction<PhysicsInstruction>> {
|
||||||
//JUST POLLING!!! NO MUTATION
|
//JUST POLLING!!! NO MUTATION
|
||||||
let mut collector = crate::instruction::InstructionCollector::new();
|
let mut collector = crate::instruction::InstructionCollector::new(time_limit);
|
||||||
//autohop (already pressing spacebar; the signal to begin trying to jump is different)
|
//autohop (already pressing spacebar; the signal to begin trying to jump is different)
|
||||||
if self.grounded&&self.jump_trying {
|
if self.grounded&&self.jump_trying {
|
||||||
//scroll will be implemented with InputInstruction::Jump(true) but it blocks setting self.jump_trying=true
|
//scroll will be implemented with InputInstruction::Jump(true) but it blocks setting self.jump_trying=true
|
||||||
@ -286,11 +620,11 @@ impl crate::instruction::InstructionEmitter<PhysicsInstruction> for PhysicsState
|
|||||||
}
|
}
|
||||||
//check for collision stop instructions with curent contacts
|
//check for collision stop instructions with curent contacts
|
||||||
for collision_data in self.contacts.iter() {
|
for collision_data in self.contacts.iter() {
|
||||||
collector.collect(self.predict_collision_end(self.models_cringe_clone.get(collision_data.model as usize).unwrap()));
|
collector.collect(self.predict_collision_end(self.models_cringe_clone.get(collision_data.model as usize).unwrap(),time_limit,collision_data.model));
|
||||||
}
|
}
|
||||||
//check for collision start instructions (against every part in the game with no optimization!!)
|
//check for collision start instructions (against every part in the game with no optimization!!)
|
||||||
for model in &self.models_cringe_clone {
|
for (i,model) in self.models_cringe_clone.iter().enumerate() {
|
||||||
collector.collect(self.predict_collision_start(model));
|
collector.collect(self.predict_collision_start(model,time_limit,i as u32));
|
||||||
}
|
}
|
||||||
if self.grounded {
|
if self.grounded {
|
||||||
//walk maintenance
|
//walk maintenance
|
||||||
@ -304,7 +638,56 @@ impl crate::instruction::InstructionEmitter<PhysicsInstruction> for PhysicsState
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsState {
|
impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsState {
|
||||||
fn process_instruction(&mut self, instruction:TimedInstruction<PhysicsInstruction>) {
|
fn process_instruction(&mut self, ins:TimedInstruction<PhysicsInstruction>) {
|
||||||
//
|
//mutate position and velocity and time
|
||||||
|
self.advance_time(ins.time);//should this be in run?
|
||||||
|
match ins.instruction {
|
||||||
|
PhysicsInstruction::CollisionStart(c) => {
|
||||||
|
//flatten v
|
||||||
|
let n=c.normal(&self.models_cringe_clone);
|
||||||
|
let d=self.body.velocity.dot(n)/n.length_squared();
|
||||||
|
self.body.velocity-=d*n;
|
||||||
|
//check ground
|
||||||
|
match c.face {
|
||||||
|
AabbFace::Top => {
|
||||||
|
//ground
|
||||||
|
self.grounded=true;
|
||||||
|
self.body.acceleration=glam::Vec3::ZERO;
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
self.contacts.insert(c);
|
||||||
|
},
|
||||||
|
PhysicsInstruction::CollisionEnd(c) => {
|
||||||
|
//check ground
|
||||||
|
match c.face {
|
||||||
|
AabbFace::Top => {
|
||||||
|
//ground
|
||||||
|
self.body.acceleration=self.gravity;
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
self.contacts.remove(&c);
|
||||||
|
},
|
||||||
|
PhysicsInstruction::StrafeTick => {
|
||||||
|
//let control_dir=self.get_control_dir();//this should respect your mouse interpolation settings
|
||||||
|
let d=self.body.velocity.dot(self.temp_control_dir);
|
||||||
|
if d<self.mv {
|
||||||
|
self.body.velocity+=(self.mv-d)*self.temp_control_dir;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PhysicsInstruction::Jump => {
|
||||||
|
self.grounded=false;//do I need this?
|
||||||
|
self.body.velocity+=glam::Vec3::new(0.0,0.715588/2.0*100.0,0.0);
|
||||||
|
}
|
||||||
|
PhysicsInstruction::ReachWalkTargetVelocity => {
|
||||||
|
//precisely set velocity
|
||||||
|
self.body.velocity=self.walk_target_velocity;
|
||||||
|
}
|
||||||
|
PhysicsInstruction::SetWalkTargetVelocity(v) => {
|
||||||
|
self.walk_target_velocity=v;
|
||||||
|
//calculate acceleration yada yada
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -279,11 +279,6 @@ fn start<E: Example>(
|
|||||||
log::info!("Initializing the example...");
|
log::info!("Initializing the example...");
|
||||||
let mut example = E::init(&config, &adapter, &device, &queue);
|
let mut example = E::init(&config, &adapter, &device, &queue);
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
|
||||||
let mut last_frame_inst = Instant::now();
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
|
||||||
let (mut frame_count, mut accum_time) = (0, 0.0);
|
|
||||||
|
|
||||||
log::info!("Entering render loop...");
|
log::info!("Entering render loop...");
|
||||||
event_loop.run(move |event, _, control_flow| {
|
event_loop.run(move |event, _, control_flow| {
|
||||||
let _ = (&instance, &adapter); // force ownership by the closure
|
let _ = (&instance, &adapter); // force ownership by the closure
|
||||||
@ -364,20 +359,6 @@ fn start<E: Example>(
|
|||||||
example.move_mouse(delta);
|
example.move_mouse(delta);
|
||||||
},
|
},
|
||||||
event::Event::RedrawRequested(_) => {
|
event::Event::RedrawRequested(_) => {
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
|
||||||
{
|
|
||||||
accum_time += last_frame_inst.elapsed().as_secs_f32();
|
|
||||||
last_frame_inst = Instant::now();
|
|
||||||
frame_count += 1;
|
|
||||||
if frame_count == 100 {
|
|
||||||
println!(
|
|
||||||
"Avg frame time {}ms",
|
|
||||||
accum_time * 1000.0 / frame_count as f32
|
|
||||||
);
|
|
||||||
accum_time = 0.0;
|
|
||||||
frame_count = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let frame = match surface.get_current_texture() {
|
let frame = match surface.get_current_texture() {
|
||||||
Ok(frame) => frame,
|
Ok(frame) => frame,
|
||||||
|
@ -4,7 +4,7 @@ pub struct TimedInstruction<I> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait InstructionEmitter<I> {
|
pub trait InstructionEmitter<I> {
|
||||||
fn next_instruction(&self) -> Option<TimedInstruction<I>>;
|
fn next_instruction(&self, time:crate::body::TIME) -> Option<TimedInstruction<I>>;
|
||||||
}
|
}
|
||||||
pub trait InstructionConsumer<I> {
|
pub trait InstructionConsumer<I> {
|
||||||
fn process_instruction(&mut self, instruction:TimedInstruction<I>);
|
fn process_instruction(&mut self, instruction:TimedInstruction<I>);
|
||||||
@ -12,26 +12,36 @@ pub trait InstructionConsumer<I> {
|
|||||||
|
|
||||||
//PROPER PRIVATE FIELDS!!!
|
//PROPER PRIVATE FIELDS!!!
|
||||||
pub struct InstructionCollector<I> {
|
pub struct InstructionCollector<I> {
|
||||||
instruction: Option<TimedInstruction<I>>,
|
time: crate::body::TIME,
|
||||||
|
instruction: Option<I>,
|
||||||
}
|
}
|
||||||
impl<I> InstructionCollector<I> {
|
impl<I> InstructionCollector<I> {
|
||||||
pub fn new() -> Self {
|
pub fn new(time:crate::body::TIME) -> Self {
|
||||||
Self{instruction:None}
|
Self{
|
||||||
|
time,
|
||||||
|
instruction:None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn collect(&mut self,instruction:Option<TimedInstruction<I>>){
|
pub fn collect(&mut self,instruction:Option<TimedInstruction<I>>){
|
||||||
match &instruction {
|
match instruction {
|
||||||
Some(unwrap_instruction) => match &self.instruction {
|
Some(unwrap_instruction) => {
|
||||||
Some(unwrap_best_instruction) => if unwrap_instruction.time<unwrap_best_instruction.time {
|
if unwrap_instruction.time<self.time {
|
||||||
self.instruction=instruction;
|
self.time=unwrap_instruction.time;
|
||||||
},
|
self.instruction=Some(unwrap_instruction.instruction);
|
||||||
None => self.instruction=instruction,
|
}
|
||||||
},
|
},
|
||||||
None => (),
|
None => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn instruction(self) -> Option<TimedInstruction<I>> {
|
pub fn instruction(self) -> Option<TimedInstruction<I>> {
|
||||||
//STEAL INSTRUCTION AND DESTROY INSTRUCTIONCOLLECTOR
|
//STEAL INSTRUCTION AND DESTROY INSTRUCTIONCOLLECTOR
|
||||||
return self.instruction
|
match self.instruction {
|
||||||
|
Some(instruction)=>Some(TimedInstruction{
|
||||||
|
time:self.time,
|
||||||
|
instruction
|
||||||
|
}),
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,3 +1,4 @@
|
|||||||
pub mod framework;
|
pub mod framework;
|
||||||
pub mod body;
|
pub mod body;
|
||||||
|
pub mod zeroes;
|
||||||
pub mod instruction;
|
pub mod instruction;
|
||||||
|
21
src/main.rs
21
src/main.rs
@ -292,11 +292,7 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
controls:0,
|
controls:0,
|
||||||
};
|
};
|
||||||
let physics = strafe_client::body::PhysicsState {
|
let physics = strafe_client::body::PhysicsState {
|
||||||
body: strafe_client::body::Body {
|
body: strafe_client::body::Body::with_position(glam::Vec3::new(5.0,2.0,5.0)),
|
||||||
position: glam::Vec3::new(5.0,0.0,5.0),
|
|
||||||
velocity: glam::Vec3::new(0.0,0.0,0.0),
|
|
||||||
time: 0,
|
|
||||||
},
|
|
||||||
time: 0,
|
time: 0,
|
||||||
tick: 0,
|
tick: 0,
|
||||||
strafe_tick_num: 100,//100t
|
strafe_tick_num: 100,//100t
|
||||||
@ -304,14 +300,17 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
gravity: glam::Vec3::new(0.0,-100.0,0.0),
|
gravity: glam::Vec3::new(0.0,-100.0,0.0),
|
||||||
friction: 90.0,
|
friction: 90.0,
|
||||||
mv: 2.7,
|
mv: 2.7,
|
||||||
grounded: true,
|
grounded: false,
|
||||||
jump_trying: false,
|
jump_trying: false,
|
||||||
|
temp_control_dir: glam::Vec3::ZERO,
|
||||||
walkspeed: 18.0,
|
walkspeed: 18.0,
|
||||||
contacts: Vec::<strafe_client::body::RelativeCollision>::new(),
|
contacts: std::collections::HashSet::new(),
|
||||||
models_cringe_clone: modeldatas.iter().map(|m|strafe_client::body::Model::new(m.transform)).collect(),
|
models_cringe_clone: modeldatas.iter().map(|m|strafe_client::body::Model::new(m.transform)).collect(),
|
||||||
|
walk_target_velocity: glam::Vec3::ZERO,
|
||||||
|
hitbox_halfsize: glam::vec3(1.0,2.5,1.0),
|
||||||
};
|
};
|
||||||
|
|
||||||
let camera_uniforms = camera.to_uniform_data(physics.extrapolate_position(0));
|
let camera_uniforms = camera.to_uniform_data(physics.body.extrapolated_position(0));
|
||||||
let camera_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
let camera_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
label: Some("Camera"),
|
label: Some("Camera"),
|
||||||
contents: bytemuck::cast_slice(&camera_uniforms),
|
contents: bytemuck::cast_slice(&camera_uniforms),
|
||||||
@ -638,13 +637,15 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
|
|
||||||
let time=self.start_time.elapsed().as_nanos() as i64;
|
let time=self.start_time.elapsed().as_nanos() as i64;
|
||||||
|
|
||||||
self.physics.run(time,control_dir,self.camera.controls);
|
self.physics.temp_control_dir=control_dir;
|
||||||
|
self.physics.jump_trying=self.camera.controls&CONTROL_JUMP!=0;
|
||||||
|
self.physics.run(time);
|
||||||
|
|
||||||
let mut encoder =
|
let mut encoder =
|
||||||
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
|
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
|
||||||
|
|
||||||
// update rotation
|
// update rotation
|
||||||
let camera_uniforms = self.camera.to_uniform_data(self.physics.extrapolate_position(time));
|
let camera_uniforms = self.camera.to_uniform_data(self.physics.body.extrapolated_position(time));
|
||||||
self.staging_belt
|
self.staging_belt
|
||||||
.write_buffer(
|
.write_buffer(
|
||||||
&mut encoder,
|
&mut encoder,
|
||||||
|
27
src/zeroes.rs
Normal file
27
src/zeroes.rs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
//find roots of polynomials
|
||||||
|
pub fn zeroes2(a0:f32,a1:f32,a2:f32) -> Vec<f32>{
|
||||||
|
if a2==0f32{
|
||||||
|
return zeroes1(a0, a1);
|
||||||
|
}
|
||||||
|
let mut radicand=a1*a1-4f32*a2*a0;
|
||||||
|
if 0f32<radicand {
|
||||||
|
radicand=radicand.sqrt();
|
||||||
|
if 0f32<a2 {
|
||||||
|
return vec![(-a1-radicand)/(2f32*a2),(-a1+radicand)/(2f32*a2)];
|
||||||
|
} else {
|
||||||
|
return vec![(-a1+radicand)/(2f32*a2),(-a1-radicand)/(2f32*a2)];
|
||||||
|
}
|
||||||
|
} else if radicand==0f32 {
|
||||||
|
return vec![-a1/(2f32*a2)];
|
||||||
|
} else {
|
||||||
|
return vec![];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
pub fn zeroes1(a0:f32,a1:f32) -> Vec<f32> {
|
||||||
|
if a1==0f32{
|
||||||
|
return vec![];
|
||||||
|
} else {
|
||||||
|
return vec![-a0/a1];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user