forked from StrafesNET/strafe-project
Compare commits
11 Commits
tickless-p
...
free-body
| Author | SHA1 | Date | |
|---|---|---|---|
| a28e7236d4 | |||
| 03a7552248 | |||
| 5f444841ac | |||
| a8b829c9e5 | |||
| f41be177dc | |||
| b75046601e | |||
| 9428929a99 | |||
| 6cfdb495ae | |||
| 4e83bee60f | |||
| 6262553c02 | |||
| 7a3e1e39dc |
759
src/body.rs
759
src/body.rs
@@ -1,693 +1,120 @@
|
|||||||
use crate::{instruction::{InstructionEmitter, InstructionConsumer, TimedInstruction}, zeroes::zeroes2};
|
|
||||||
|
|
||||||
pub enum PhysicsInstruction {
|
|
||||||
CollisionStart(RelativeCollision),
|
|
||||||
CollisionEnd(RelativeCollision),
|
|
||||||
StrafeTick,
|
|
||||||
Jump,
|
|
||||||
SetWalkTargetVelocity(glam::Vec3),
|
|
||||||
ReachWalkTargetVelocity,
|
|
||||||
// Water,
|
|
||||||
// Spawn(
|
|
||||||
// Option<SpawnId>,
|
|
||||||
// bool,//true = Trigger; false = teleport
|
|
||||||
// bool,//true = Force
|
|
||||||
// )
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Body {
|
pub struct Body {
|
||||||
position: glam::Vec3,//I64 where 2^32 = 1 u
|
pub position: glam::Vec3,//I64 where 2^32 = 1 u
|
||||||
velocity: glam::Vec3,//I64 where 2^32 = 1 u/s
|
pub velocity: glam::Vec3,//I64 where 2^32 = 1 u/s
|
||||||
acceleration: glam::Vec3,//I64 where 2^32 = 1 u/s/s
|
pub time: TIME,//nanoseconds x xxxxD!
|
||||||
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 hitbox_halfsize: glam::Vec3,
|
//pub contacts: Vec<RelativeCollision>,
|
||||||
pub contacts: std::collections::HashSet::<RelativeCollision>,
|
|
||||||
//pub intersections: Vec<ModelId>,
|
|
||||||
//temp
|
|
||||||
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_rate: TIME,
|
||||||
pub strafe_tick_den: TIME,
|
|
||||||
pub tick: u32,
|
pub tick: u32,
|
||||||
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,Hash,Eq,PartialEq)]
|
|
||||||
pub enum AabbFace{
|
|
||||||
Right,//+X
|
|
||||||
Top,
|
|
||||||
Back,
|
|
||||||
Left,
|
|
||||||
Bottom,
|
|
||||||
Front,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Aabb {
|
|
||||||
min: glam::Vec3,
|
|
||||||
max: glam::Vec3,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Aabb {
|
|
||||||
// const FACE_DATA: [[f32; 3]; 6] = [
|
|
||||||
// [0.0f32, 0., 1.],
|
|
||||||
// [0.0f32, 0., -1.],
|
|
||||||
// [1.0f32, 0., 0.],
|
|
||||||
// [-1.0f32, 0., 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] = [
|
|
||||||
glam::vec3(1., -1., -1.),
|
|
||||||
glam::vec3(1., 1., -1.),
|
|
||||||
glam::vec3(1., 1., 1.),
|
|
||||||
glam::vec3(1., -1., 1.),
|
|
||||||
];
|
|
||||||
const VERTEX_DATA_TOP: [glam::Vec3; 4] = [
|
|
||||||
glam::vec3(1., 1., -1.),
|
|
||||||
glam::vec3(-1., 1., -1.),
|
|
||||||
glam::vec3(-1., 1., 1.),
|
|
||||||
glam::vec3(1., 1., 1.),
|
|
||||||
];
|
|
||||||
const VERTEX_DATA_BACK: [glam::Vec3; 4] = [
|
|
||||||
glam::vec3(-1., -1., 1.),
|
|
||||||
glam::vec3(1., -1., 1.),
|
|
||||||
glam::vec3(1., 1., 1.),
|
|
||||||
glam::vec3(-1., 1., 1.),
|
|
||||||
];
|
|
||||||
const VERTEX_DATA_LEFT: [glam::Vec3; 4] = [
|
|
||||||
glam::vec3(-1., -1., 1.),
|
|
||||||
glam::vec3(-1., 1., 1.),
|
|
||||||
glam::vec3(-1., 1., -1.),
|
|
||||||
glam::vec3(-1., -1., -1.),
|
|
||||||
];
|
|
||||||
const VERTEX_DATA_BOTTOM: [glam::Vec3; 4] = [
|
|
||||||
glam::vec3(1., -1., 1.),
|
|
||||||
glam::vec3(-1., -1., 1.),
|
|
||||||
glam::vec3(-1., -1., -1.),
|
|
||||||
glam::vec3(1., -1., -1.),
|
|
||||||
];
|
|
||||||
const VERTEX_DATA_FRONT: [glam::Vec3; 4] = [
|
|
||||||
glam::vec3(-1., 1., -1.),
|
|
||||||
glam::vec3(1., 1., -1.),
|
|
||||||
glam::vec3(1., -1., -1.),
|
|
||||||
glam::vec3(-1., -1., -1.),
|
|
||||||
];
|
|
||||||
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {min: glam::Vec3::INFINITY,max: glam::Vec3::NEG_INFINITY}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn grow(&mut self, point:glam::Vec3){
|
|
||||||
self.min=self.min.min(point);
|
|
||||||
self.max=self.max.max(point);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn normal(face:AabbFace) -> glam::Vec3 {
|
|
||||||
match face {
|
|
||||||
AabbFace::Right => glam::vec3(1.,0.,0.),
|
|
||||||
AabbFace::Top => glam::vec3(0.,1.,0.),
|
|
||||||
AabbFace::Back => glam::vec3(0.,0.,1.),
|
|
||||||
AabbFace::Left => glam::vec3(-1.,0.,0.),
|
|
||||||
AabbFace::Bottom => glam::vec3(0.,-1.,0.),
|
|
||||||
AabbFace::Front => glam::vec3(0.,0.,-1.),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn unit_vertices() -> [glam::Vec3;8] {
|
|
||||||
return Self::VERTEX_DATA;
|
|
||||||
}
|
|
||||||
pub fn unit_face_vertices(face:AabbFace) -> [glam::Vec3;4] {
|
|
||||||
match face {
|
|
||||||
AabbFace::Right => Self::VERTEX_DATA_RIGHT,
|
|
||||||
AabbFace::Top => Self::VERTEX_DATA_TOP,
|
|
||||||
AabbFace::Back => Self::VERTEX_DATA_BACK,
|
|
||||||
AabbFace::Left => Self::VERTEX_DATA_LEFT,
|
|
||||||
AabbFace::Bottom => Self::VERTEX_DATA_BOTTOM,
|
|
||||||
AabbFace::Front => Self::VERTEX_DATA_FRONT,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//pretend to be using what we want to eventually do
|
|
||||||
type TreyMeshFace = AabbFace;
|
|
||||||
type TreyMesh = Aabb;
|
|
||||||
|
|
||||||
pub struct Model {
|
|
||||||
//A model is a thing that has a hitbox. can be represented by a list of TreyMesh-es
|
|
||||||
//in this iteration, all it needs is extents.
|
|
||||||
transform: glam::Mat4,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Model {
|
|
||||||
pub fn new(transform:glam::Mat4) -> Self {
|
|
||||||
Self{transform}
|
|
||||||
}
|
|
||||||
pub fn unit_vertices(&self) -> [glam::Vec3;8] {
|
|
||||||
Aabb::unit_vertices()
|
|
||||||
}
|
|
||||||
pub fn mesh(&self) -> TreyMesh {
|
|
||||||
let mut aabb=Aabb::new();
|
|
||||||
for &vertex in self.unit_vertices().iter() {
|
|
||||||
aabb.grow(glam::Vec4Swizzles::xyz(self.transform*vertex.extend(1.0)));
|
|
||||||
}
|
|
||||||
return aabb;
|
|
||||||
}
|
|
||||||
pub fn unit_face_vertices(&self,face:TreyMeshFace) -> [glam::Vec3;4] {
|
|
||||||
Aabb::unit_face_vertices(face)
|
|
||||||
}
|
|
||||||
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 {
|
|
||||||
face: TreyMeshFace,//just an id
|
|
||||||
model: u32,//using id to avoid lifetimes
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RelativeCollision {
|
|
||||||
pub fn mesh(&self,models:&Vec<Model>) -> TreyMesh {
|
|
||||||
return models.get(self.model as usize).unwrap().face_mesh(self.face)
|
|
||||||
}
|
|
||||||
pub fn normal(&self,models:&Vec<Model>) -> glam::Vec3 {
|
|
||||||
return models.get(self.model as usize).unwrap().face_normal(self.face)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type TIME = i64;
|
pub type TIME = i64;
|
||||||
|
|
||||||
impl Body {
|
const CONTROL_JUMP:u32 = 0b01000000;//temp
|
||||||
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 {
|
||||||
//tickless gaming
|
//delete this, we are tickless gamers
|
||||||
pub fn run(&mut self, time: TIME){
|
pub fn run(&mut self, time: TIME, control_dir: glam::Vec3, controls: u32){
|
||||||
//prepare is ommitted - everything is done via instructions.
|
let target_tick = (time/10_000_000) as u32;//100t
|
||||||
while let Some(instruction) = self.next_instruction(time) {//collect
|
//the game code can run for 1 month before running out of ticks
|
||||||
//advance
|
while self.tick<target_tick {
|
||||||
//self.advance_time(instruction.time);
|
self.tick += 1;
|
||||||
//process
|
let dt=0.01;
|
||||||
self.process_instruction(instruction);
|
let d=self.body.velocity.dot(control_dir);
|
||||||
//write hash lol
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn advance_time(&mut self, time: TIME){
|
|
||||||
self.body.advance_time(time);
|
|
||||||
self.time=time;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn next_strafe_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
|
|
||||||
return Some(TimedInstruction{
|
|
||||||
time:(self.time*self.strafe_tick_num/self.strafe_tick_den+1)*self.strafe_tick_den/self.strafe_tick_num,
|
|
||||||
//only poll the physics if there is a before and after mouse event
|
|
||||||
instruction:PhysicsInstruction::StrafeTick
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
//state mutated on collision:
|
|
||||||
//Accelerator
|
|
||||||
//stair step-up
|
|
||||||
|
|
||||||
//state mutated on instruction
|
|
||||||
//change fly acceleration (fly_sustain)
|
|
||||||
//change fly velocity
|
|
||||||
|
|
||||||
//generic event emmiters
|
|
||||||
//PlatformStandTime
|
|
||||||
//walk/swim/air/ladder sounds
|
|
||||||
//VState?
|
|
||||||
|
|
||||||
//falling under the map
|
|
||||||
// fn next_respawn_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
|
|
||||||
// if self.body.position<self.world.min_y {
|
|
||||||
// return Some(TimedInstruction{
|
|
||||||
// time:self.time,
|
|
||||||
// instruction:PhysicsInstruction::Trigger(None)
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fn next_water_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
|
|
||||||
// return Some(TimedInstruction{
|
|
||||||
// time:(self.time*self.strafe_tick_num/self.strafe_tick_den+1)*self.strafe_tick_den/self.strafe_tick_num,
|
|
||||||
// //only poll the physics if there is a before and after mouse event
|
|
||||||
// instruction:PhysicsInstruction::Water
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
fn next_walk_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
|
|
||||||
//check if you are accelerating towards a walk target velocity and create an instruction
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
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.
|
|
||||||
//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
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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.
|
|
||||||
fn next_instruction(&self,time_limit:TIME) -> Option<TimedInstruction<PhysicsInstruction>> {
|
|
||||||
//JUST POLLING!!! NO MUTATION
|
|
||||||
let mut collector = crate::instruction::InstructionCollector::new(time_limit);
|
|
||||||
//autohop (already pressing spacebar; the signal to begin trying to jump is different)
|
|
||||||
if self.grounded&&self.jump_trying {
|
|
||||||
//scroll will be implemented with InputInstruction::Jump(true) but it blocks setting self.jump_trying=true
|
|
||||||
collector.collect(Some(TimedInstruction{
|
|
||||||
time:self.time,
|
|
||||||
instruction:PhysicsInstruction::Jump
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
//check for collision stop instructions with curent contacts
|
|
||||||
for collision_data in self.contacts.iter() {
|
|
||||||
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!!)
|
|
||||||
for (i,model) in self.models_cringe_clone.iter().enumerate() {
|
|
||||||
collector.collect(self.predict_collision_start(model,time_limit,i as u32));
|
|
||||||
}
|
|
||||||
if self.grounded {
|
|
||||||
//walk maintenance
|
|
||||||
collector.collect(self.next_walk_instruction());
|
|
||||||
}else{
|
|
||||||
//check to see when the next strafe tick is
|
|
||||||
collector.collect(self.next_strafe_instruction());
|
|
||||||
}
|
|
||||||
collector.instruction()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsState {
|
|
||||||
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 {
|
if d<self.mv {
|
||||||
self.body.velocity+=(self.mv-d)*self.temp_control_dir;
|
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;
|
||||||
}
|
}
|
||||||
PhysicsInstruction::Jump => {
|
if self.grounded&&(controls&CONTROL_JUMP)!=0 {
|
||||||
self.grounded=false;//do I need this?
|
self.grounded=false;
|
||||||
self.body.velocity+=glam::Vec3::new(0.0,0.715588/2.0*100.0,0.0);
|
self.body.velocity+=glam::Vec3::new(0.0,0.715588/2.0*100.0,0.0);
|
||||||
}
|
}
|
||||||
PhysicsInstruction::ReachWalkTargetVelocity => {
|
if self.grounded {
|
||||||
//precisely set velocity
|
let applied_friction=self.friction*dt;
|
||||||
self.body.velocity=self.walk_target_velocity;
|
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 {
|
||||||
|
self.body.velocity=targetv;
|
||||||
}
|
}
|
||||||
PhysicsInstruction::SetWalkTargetVelocity(v) => {
|
|
||||||
self.walk_target_velocity=v;
|
|
||||||
//calculate acceleration yada yada
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.body.time=target_tick as TIME*10_000_000;
|
||||||
|
}
|
||||||
|
|
||||||
|
//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_event(&self) -> Option<crate::event::EventStruct> {
|
||||||
|
return Some(crate::event::EventStruct{
|
||||||
|
time:self.time/self.strafe_tick_rate*self.strafe_tick_rate,//this is floor(n) I need ceil(n)+1
|
||||||
|
event:crate::event::EventEnum::StrafeTick
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl crate::event::EventTrait for PhysicsState {
|
||||||
|
//this little next event function can cache its return value and invalidate the cached value by watching the State.
|
||||||
|
fn next_event(&self) -> Option<crate::event::EventStruct> {
|
||||||
|
//JUST POLLING!!! NO MUTATION
|
||||||
|
let mut best_event: Option<crate::event::EventStruct> = None;
|
||||||
|
let collect_event = |test_event:Option<crate::event::EventStruct>|{
|
||||||
|
match test_event {
|
||||||
|
Some(unwrap_test_event) => match best_event {
|
||||||
|
Some(unwrap_best_event) => if unwrap_test_event.time<unwrap_best_event.time {
|
||||||
|
best_event=test_event;
|
||||||
|
},
|
||||||
|
None => best_event=test_event,
|
||||||
|
},
|
||||||
|
None => (),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
//check to see if yee need to jump (this is not the way lol)
|
||||||
|
if self.grounded&&self.jump_trying {
|
||||||
|
//scroll will be implemented with InputEvent::InstantJump rather than InputEvent::Jump(true)
|
||||||
|
collect_event(Some(crate::event::EventStruct{
|
||||||
|
time:self.time,
|
||||||
|
event:crate::event::EventEnum::Jump
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
//check for collision stop events with curent contacts
|
||||||
|
for collision_data in self.contacts.iter() {
|
||||||
|
collect_event(self.model.predict_collision(collision_data.model));
|
||||||
|
}
|
||||||
|
//check for collision start events (against every part in the game with no optimization!!)
|
||||||
|
for &model in self.world.models {
|
||||||
|
collect_event(self.model.predict_collision(&model));
|
||||||
|
}
|
||||||
|
//check to see when the next strafe tick is
|
||||||
|
collect_event(self.next_strafe_event());
|
||||||
|
best_event
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//something that implements body + hitbox can predict collision
|
||||||
|
impl crate::sweep::PredictCollision for Model {
|
||||||
|
fn predict_collision(&self,other:&Model) -> Option<crate::event::EventStruct> {
|
||||||
|
//math!
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
15
src/event.rs
Normal file
15
src/event.rs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
pub struct EventStruct {
|
||||||
|
pub time: crate::body::TIME,
|
||||||
|
pub event: EventEnum,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum EventEnum {
|
||||||
|
CollisionStart,//(Collideable),//Body::CollisionStart
|
||||||
|
CollisionEnd,//(Collideable),//Body::CollisionEnd
|
||||||
|
StrafeTick,
|
||||||
|
Jump,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait EventTrait {
|
||||||
|
fn next_event(&self) -> Option<EventStruct>;
|
||||||
|
}
|
||||||
@@ -279,6 +279,11 @@ 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
|
||||||
@@ -359,6 +364,20 @@ 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,
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
pub struct TimedInstruction<I> {
|
|
||||||
pub time: crate::body::TIME,
|
|
||||||
pub instruction: I,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait InstructionEmitter<I> {
|
|
||||||
fn next_instruction(&self, time:crate::body::TIME) -> Option<TimedInstruction<I>>;
|
|
||||||
}
|
|
||||||
pub trait InstructionConsumer<I> {
|
|
||||||
fn process_instruction(&mut self, instruction:TimedInstruction<I>);
|
|
||||||
}
|
|
||||||
|
|
||||||
//PROPER PRIVATE FIELDS!!!
|
|
||||||
pub struct InstructionCollector<I> {
|
|
||||||
time: crate::body::TIME,
|
|
||||||
instruction: Option<I>,
|
|
||||||
}
|
|
||||||
impl<I> InstructionCollector<I> {
|
|
||||||
pub fn new(time:crate::body::TIME) -> Self {
|
|
||||||
Self{
|
|
||||||
time,
|
|
||||||
instruction:None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn collect(&mut self,instruction:Option<TimedInstruction<I>>){
|
|
||||||
match instruction {
|
|
||||||
Some(unwrap_instruction) => {
|
|
||||||
if unwrap_instruction.time<self.time {
|
|
||||||
self.time=unwrap_instruction.time;
|
|
||||||
self.instruction=Some(unwrap_instruction.instruction);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn instruction(self) -> Option<TimedInstruction<I>> {
|
|
||||||
//STEAL INSTRUCTION AND DESTROY INSTRUCTIONCOLLECTOR
|
|
||||||
match self.instruction {
|
|
||||||
Some(instruction)=>Some(TimedInstruction{
|
|
||||||
time:self.time,
|
|
||||||
instruction
|
|
||||||
}),
|
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
pub mod framework;
|
pub mod framework;
|
||||||
pub mod body;
|
pub mod body;
|
||||||
pub mod zeroes;
|
pub mod event;
|
||||||
pub mod instruction;
|
|
||||||
|
|||||||
87
src/main.rs
87
src/main.rs
@@ -24,7 +24,7 @@ struct ModelData {
|
|||||||
entities: Vec<Entity>,
|
entities: Vec<Entity>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ModelGraphics {
|
struct Model {
|
||||||
transform: glam::Mat4,
|
transform: glam::Mat4,
|
||||||
vertex_buf: wgpu::Buffer,
|
vertex_buf: wgpu::Buffer,
|
||||||
entities: Vec<Entity>,
|
entities: Vec<Entity>,
|
||||||
@@ -122,7 +122,7 @@ pub struct Skybox {
|
|||||||
ground_pipeline: wgpu::RenderPipeline,
|
ground_pipeline: wgpu::RenderPipeline,
|
||||||
main_bind_group: wgpu::BindGroup,
|
main_bind_group: wgpu::BindGroup,
|
||||||
camera_buf: wgpu::Buffer,
|
camera_buf: wgpu::Buffer,
|
||||||
models: Vec<ModelGraphics>,
|
models: Vec<Model>,
|
||||||
depth_view: wgpu::TextureView,
|
depth_view: wgpu::TextureView,
|
||||||
staging_belt: wgpu::util::StagingBelt,
|
staging_belt: wgpu::util::StagingBelt,
|
||||||
}
|
}
|
||||||
@@ -292,60 +292,28 @@ 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::with_position(glam::Vec3::new(5.0,2.0,5.0)),
|
body: strafe_client::body::Body {
|
||||||
|
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
|
tick_rate: 100,
|
||||||
strafe_tick_den: 1_000_000_000,
|
|
||||||
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: false,
|
grounded: true,
|
||||||
jump_trying: false,
|
|
||||||
temp_control_dir: glam::Vec3::ZERO,
|
|
||||||
walkspeed: 18.0,
|
walkspeed: 18.0,
|
||||||
contacts: std::collections::HashSet::new(),
|
|
||||||
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.body.extrapolated_position(0));
|
let camera_uniforms = camera.to_uniform_data(physics.extrapolate_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),
|
||||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||||
});
|
});
|
||||||
|
|
||||||
//drain the modeldata vec so entities can be /moved/ to models.entities
|
|
||||||
let mut models = Vec::<ModelGraphics>::with_capacity(modeldatas.len());
|
|
||||||
for (i,modeldata) in modeldatas.drain(..).enumerate() {
|
|
||||||
let model_uniforms = get_transform_uniform_data(&modeldata.transform);
|
|
||||||
let model_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
||||||
label: Some(format!("ModelGraphics{}",i).as_str()),
|
|
||||||
contents: bytemuck::cast_slice(&model_uniforms),
|
|
||||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
|
||||||
});
|
|
||||||
let model_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
||||||
layout: &model_bind_group_layout,
|
|
||||||
entries: &[
|
|
||||||
wgpu::BindGroupEntry {
|
|
||||||
binding: 0,
|
|
||||||
resource: model_buf.as_entire_binding(),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
label: Some(format!("ModelGraphics{}",i).as_str()),
|
|
||||||
});
|
|
||||||
//all of these are being moved here
|
|
||||||
models.push(ModelGraphics{
|
|
||||||
transform: modeldata.transform,
|
|
||||||
vertex_buf:modeldata.vertex_buf,
|
|
||||||
entities: modeldata.entities,
|
|
||||||
bind_group: model_bind_group,
|
|
||||||
model_buf,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||||
label: None,
|
label: None,
|
||||||
bind_group_layouts: &[&main_bind_group_layout, &model_bind_group_layout],
|
bind_group_layouts: &[&main_bind_group_layout, &model_bind_group_layout],
|
||||||
@@ -541,6 +509,35 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
label: Some("Camera"),
|
label: Some("Camera"),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//drain the modeldata vec so entities can be /moved/ to models.entities
|
||||||
|
let mut models = Vec::<Model>::with_capacity(modeldatas.len());
|
||||||
|
for (i,modeldata) in modeldatas.drain(..).enumerate() {
|
||||||
|
let model_uniforms = get_transform_uniform_data(&modeldata.transform);
|
||||||
|
let model_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
|
label: Some(format!("Model{}",i).as_str()),
|
||||||
|
contents: bytemuck::cast_slice(&model_uniforms),
|
||||||
|
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||||
|
});
|
||||||
|
let model_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
|
layout: &model_bind_group_layout,
|
||||||
|
entries: &[
|
||||||
|
wgpu::BindGroupEntry {
|
||||||
|
binding: 0,
|
||||||
|
resource: model_buf.as_entire_binding(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
label: Some(format!("Model{}",i).as_str()),
|
||||||
|
});
|
||||||
|
//all of these are being moved here
|
||||||
|
models.push(Model{
|
||||||
|
transform: modeldata.transform,
|
||||||
|
vertex_buf:modeldata.vertex_buf,
|
||||||
|
entities: modeldata.entities,
|
||||||
|
bind_group: model_bind_group,
|
||||||
|
model_buf,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
let depth_view = Self::create_depth_texture(config, device);
|
let depth_view = Self::create_depth_texture(config, device);
|
||||||
|
|
||||||
Skybox {
|
Skybox {
|
||||||
@@ -637,15 +634,13 @@ 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.temp_control_dir=control_dir;
|
self.physics.run(time,control_dir,self.camera.controls);
|
||||||
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.body.extrapolated_position(time));
|
let camera_uniforms = self.camera.to_uniform_data(self.physics.extrapolate_position(time));
|
||||||
self.staging_belt
|
self.staging_belt
|
||||||
.write_buffer(
|
.write_buffer(
|
||||||
&mut encoder,
|
&mut encoder,
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
|
|
||||||
//something that implements body + hitbox + transform can predict collision
|
|
||||||
impl crate::sweep::PredictCollision for Model {
|
|
||||||
fn predict_collision(&self,other:&Model) -> Option<crate::event::EventStruct> {
|
|
||||||
//math!
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
//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];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user