Compare commits
7 Commits
bvh
...
physics-th
| Author | SHA1 | Date | |
|---|---|---|---|
| 0a5a0f4c66 | |||
| c61b6cfb90 | |||
| e8cb4a6f70 | |||
| 8a81a57036 | |||
| 17331ba609 | |||
| a24f8f5ff1 | |||
| e90520bb89 |
91
src/aabb.rs
91
src/aabb.rs
@@ -1,91 +0,0 @@
|
||||
#[derive(Debug,Clone,Copy,Hash,Eq,PartialEq)]
|
||||
pub enum AabbFace{
|
||||
Right,//+X
|
||||
Top,
|
||||
Back,
|
||||
Left,
|
||||
Bottom,
|
||||
Front,
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct Aabb {
|
||||
pub min: glam::Vec3,
|
||||
pub max: glam::Vec3,
|
||||
}
|
||||
|
||||
impl Default for Aabb {
|
||||
fn default() -> Self {
|
||||
Aabb::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Aabb {
|
||||
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.),
|
||||
];
|
||||
|
||||
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 join(&mut self, aabb:&Aabb){
|
||||
self.min=self.min.min(aabb.min);
|
||||
self.max=self.max.max(aabb.max);
|
||||
}
|
||||
pub fn inflate(&mut self, hs:glam::Vec3){
|
||||
self.min-=hs;
|
||||
self.max+=hs;
|
||||
}
|
||||
pub fn intersects(&self,aabb:&Aabb)->bool{
|
||||
(self.min.cmplt(aabb.max)&aabb.min.cmplt(self.max)).all()
|
||||
}
|
||||
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 face(&self,face:AabbFace) -> Aabb {
|
||||
let mut aabb=self.clone();
|
||||
//in this implementation face = worldspace aabb face
|
||||
match face {
|
||||
AabbFace::Right => aabb.min.x=aabb.max.x,
|
||||
AabbFace::Top => aabb.min.y=aabb.max.y,
|
||||
AabbFace::Back => aabb.min.z=aabb.max.z,
|
||||
AabbFace::Left => aabb.max.x=aabb.min.x,
|
||||
AabbFace::Bottom => aabb.max.y=aabb.min.y,
|
||||
AabbFace::Front => aabb.max.z=aabb.min.z,
|
||||
}
|
||||
return aabb;
|
||||
}
|
||||
pub fn center(&self)->glam::Vec3{
|
||||
return (self.min+self.max)/2.0
|
||||
}
|
||||
//probably use floats for area & volume because we don't care about precision
|
||||
pub fn area_weight(&self)->f32{
|
||||
let d=self.max-self.min;
|
||||
d.x*d.y+d.y*d.z+d.z*d.x
|
||||
}
|
||||
pub fn volume(&self)->f32{
|
||||
let d=self.max-self.min;
|
||||
d.x*d.y*d.z
|
||||
}
|
||||
}
|
||||
107
src/bvh.rs
107
src/bvh.rs
@@ -1,107 +0,0 @@
|
||||
use crate::aabb::Aabb;
|
||||
|
||||
//da algaritum
|
||||
//lista boxens
|
||||
//sort by {minx,maxx,miny,maxy,minz,maxz} (6 lists)
|
||||
//find the sets that minimizes the sum of surface areas
|
||||
//splitting is done when the minimum split sum of surface areas is larger than the node's own surface area
|
||||
|
||||
//start with bisection into octrees because a bad bvh is still 1000x better than no bvh
|
||||
//sort the centerpoints on each axis (3 lists)
|
||||
//bv is put into octant based on whether it is upper or lower in each list
|
||||
#[derive(Default)]
|
||||
pub struct BvhNode{
|
||||
children:Vec<Self>,
|
||||
models:Vec<u32>,
|
||||
aabb:Aabb,
|
||||
}
|
||||
|
||||
impl BvhNode{
|
||||
pub fn the_tester<F:FnMut(u32)>(&self,aabb:&Aabb,f:&mut F){
|
||||
for &model in &self.models{
|
||||
f(model);
|
||||
}
|
||||
for child in &self.children{
|
||||
if aabb.intersects(&child.aabb){
|
||||
child.the_tester(aabb,f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_bvh(boxen:Vec<Aabb>)->BvhNode{
|
||||
generate_bvh_node(boxen.into_iter().enumerate().collect())
|
||||
}
|
||||
|
||||
fn generate_bvh_node(boxen:Vec<(usize,Aabb)>)->BvhNode{
|
||||
let n=boxen.len();
|
||||
if n<20{
|
||||
let mut aabb=Aabb::new();
|
||||
let models=boxen.into_iter().map(|b|{aabb.join(&b.1);b.0 as u32}).collect();
|
||||
BvhNode{
|
||||
children:Vec::new(),
|
||||
models,
|
||||
aabb,
|
||||
}
|
||||
}else{
|
||||
let mut octant=std::collections::HashMap::with_capacity(n);//this ids which octant the boxen is put in
|
||||
let mut sort_x=Vec::with_capacity(n);
|
||||
let mut sort_y=Vec::with_capacity(n);
|
||||
let mut sort_z=Vec::with_capacity(n);
|
||||
for (i,aabb) in boxen.iter(){
|
||||
let center=aabb.center();
|
||||
octant.insert(*i,0);
|
||||
sort_x.push((*i,center.x));
|
||||
sort_y.push((*i,center.y));
|
||||
sort_z.push((*i,center.z));
|
||||
}
|
||||
sort_x.sort_by(|tup0,tup1|tup0.1.partial_cmp(&tup1.1).unwrap());
|
||||
sort_y.sort_by(|tup0,tup1|tup0.1.partial_cmp(&tup1.1).unwrap());
|
||||
sort_z.sort_by(|tup0,tup1|tup0.1.partial_cmp(&tup1.1).unwrap());
|
||||
let h=n/2;
|
||||
let median_x=sort_x[h].1;
|
||||
let median_y=sort_y[h].1;
|
||||
let median_z=sort_z[h].1;
|
||||
for (i,c) in sort_x{
|
||||
if median_x<c{
|
||||
octant.insert(i,octant[&i]+1<<0);
|
||||
}
|
||||
}
|
||||
for (i,c) in sort_y{
|
||||
if median_y<c{
|
||||
octant.insert(i,octant[&i]+1<<1);
|
||||
}
|
||||
}
|
||||
for (i,c) in sort_z{
|
||||
if median_z<c{
|
||||
octant.insert(i,octant[&i]+1<<2);
|
||||
}
|
||||
}
|
||||
//generate lists for unique octant values
|
||||
let mut list_list=Vec::with_capacity(8);
|
||||
let mut octant_list=Vec::with_capacity(8);
|
||||
for (i,aabb) in boxen.into_iter(){
|
||||
let octant_id=octant[&i];
|
||||
let list_id=if let Some(list_id)=octant_list.iter().position(|&id|id==octant_id){
|
||||
list_id
|
||||
}else{
|
||||
let list_id=list_list.len();
|
||||
octant_list.push(octant_id);
|
||||
list_list.push(Vec::new());
|
||||
list_id
|
||||
};
|
||||
list_list[list_id].push((i,aabb));
|
||||
}
|
||||
let mut aabb=Aabb::new();
|
||||
let children=list_list.into_iter().map(|b|{
|
||||
let node=generate_bvh_node(b);
|
||||
aabb.join(&node.aabb);
|
||||
node
|
||||
}).collect();
|
||||
BvhNode{
|
||||
children,
|
||||
models:Vec::new(),
|
||||
aabb,
|
||||
}
|
||||
}
|
||||
}
|
||||
11
src/main.rs
11
src/main.rs
@@ -2,10 +2,10 @@ use std::{borrow::Cow, time::Instant};
|
||||
use wgpu::{util::DeviceExt, AstcBlock, AstcChannel};
|
||||
use model::{Vertex,ModelInstance,ModelGraphicsInstance};
|
||||
use physics::{InputInstruction, PhysicsInstruction};
|
||||
use instruction::{TimedInstruction, InstructionConsumer};
|
||||
use instruction::TimedInstruction;
|
||||
|
||||
use crate::instruction::InstructionConsumer;
|
||||
|
||||
mod bvh;
|
||||
mod aabb;
|
||||
mod model;
|
||||
mod zeroes;
|
||||
mod worker;
|
||||
@@ -121,7 +121,7 @@ pub struct GlobalState{
|
||||
manual_mouse_lock:bool,
|
||||
mouse:physics::MouseState,
|
||||
graphics:GraphicsState,
|
||||
physics_thread:worker::CompatWorker<TimedInstruction<InputInstruction>,physics::PhysicsOutputState,Box<dyn FnMut(TimedInstruction<InputInstruction>)->physics::PhysicsOutputState>>,
|
||||
physics_thread:worker::Worker<TimedInstruction<InputInstruction>,physics::PhysicsOutputState>,
|
||||
}
|
||||
|
||||
impl GlobalState{
|
||||
@@ -848,6 +848,7 @@ impl framework::Example for GlobalState {
|
||||
//.snf = "SNMF"
|
||||
//.snf = "SNBF"
|
||||
if let (Ok(()),Ok(()))=(std::io::Read::read_exact(&mut input, &mut first_8),std::io::Seek::rewind(&mut input)){
|
||||
//
|
||||
if let Some(indexed_model_instances)={
|
||||
match &first_8[0..4]{
|
||||
b"<rob"=>{
|
||||
@@ -881,7 +882,7 @@ impl framework::Example for GlobalState {
|
||||
physics.spawn_point=spawn_point;
|
||||
physics.process_instruction(instruction::TimedInstruction{
|
||||
time:physics.time,
|
||||
instruction: PhysicsInstruction::Input(physics::PhysicsInputInstruction::Reset),
|
||||
instruction: PhysicsInstruction::Input(InputInstruction::Reset),
|
||||
});
|
||||
physics.generate_models(&indexed_model_instances);
|
||||
self.physics_thread=physics.into_worker();
|
||||
|
||||
367
src/physics.rs
367
src/physics.rs
@@ -13,22 +13,7 @@ pub enum PhysicsInstruction {
|
||||
// bool,//true = Force
|
||||
// )
|
||||
//InputInstructions conditionally activate RefreshWalkTarget (by doing what SetWalkTargetVelocity used to do and then flagging it)
|
||||
Input(PhysicsInputInstruction),
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub enum PhysicsInputInstruction {
|
||||
ReplaceMouse(MouseState,MouseState),
|
||||
SetNextMouse(MouseState),
|
||||
SetMoveForward(bool),
|
||||
SetMoveLeft(bool),
|
||||
SetMoveBack(bool),
|
||||
SetMoveRight(bool),
|
||||
SetMoveUp(bool),
|
||||
SetMoveDown(bool),
|
||||
SetJump(bool),
|
||||
SetZoom(bool),
|
||||
Reset,
|
||||
Idle,
|
||||
Input(InputInstruction),
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub enum InputInstruction {
|
||||
@@ -107,7 +92,7 @@ impl crate::instruction::InstructionConsumer<InputInstruction> for InputState{
|
||||
*/
|
||||
|
||||
//hey dumbass just use a delta
|
||||
#[derive(Clone,Debug)]
|
||||
#[derive(Clone)]
|
||||
pub struct MouseState {
|
||||
pub pos: glam::IVec2,
|
||||
pub time: TIME,
|
||||
@@ -121,6 +106,10 @@ impl Default for MouseState{
|
||||
}
|
||||
}
|
||||
impl MouseState {
|
||||
pub fn move_mouse(&mut self,pos:glam::IVec2,time:TIME){
|
||||
self.time=time;
|
||||
self.pos=pos;
|
||||
}
|
||||
pub fn lerp(&self,target:&MouseState,time:TIME)->glam::IVec2 {
|
||||
let m0=self.pos.as_i64vec2();
|
||||
let m1=target.pos.as_i64vec2();
|
||||
@@ -297,7 +286,6 @@ pub struct PhysicsState{
|
||||
pub grounded:bool,
|
||||
//all models
|
||||
pub models:Vec<ModelPhysics>,
|
||||
pub bvh:crate::bvh::BvhNode,
|
||||
|
||||
pub modes:Vec<crate::model::ModeDescription>,
|
||||
pub mode_from_mode_id:std::collections::HashMap::<u32,usize>,
|
||||
@@ -312,13 +300,118 @@ pub struct PhysicsOutputState{
|
||||
}
|
||||
impl PhysicsOutputState{
|
||||
pub fn adjust_mouse(&self,mouse:&MouseState)->(glam::Vec3,glam::Vec2){
|
||||
(self.body.extrapolated_position(mouse.time)+self.camera.offset,self.camera.simulate_move_angles(mouse.pos).as_vec2())
|
||||
(self.body.extrapolated_position(mouse.time),self.camera.simulate_move_angles(mouse.pos).as_vec2())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Copy,Hash,Eq,PartialEq)]
|
||||
pub enum AabbFace{
|
||||
Right,//+X
|
||||
Top,
|
||||
Back,
|
||||
Left,
|
||||
Bottom,
|
||||
Front,
|
||||
}
|
||||
#[derive(Clone)]
|
||||
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 = crate::aabb::AabbFace;
|
||||
type TreyMesh = crate::aabb::Aabb;
|
||||
type TreyMeshFace = AabbFace;
|
||||
type TreyMesh = Aabb;
|
||||
|
||||
enum PhysicsCollisionAttributes{
|
||||
Contact{//track whether you are contacting the object
|
||||
@@ -341,7 +434,7 @@ pub struct ModelPhysics {
|
||||
|
||||
impl ModelPhysics {
|
||||
fn from_model_transform_attributes(model:&crate::model::IndexedModel,transform:&glam::Affine3A,attributes:PhysicsCollisionAttributes)->Self{
|
||||
let mut aabb=TreyMesh::new();
|
||||
let mut aabb=Aabb::new();
|
||||
for indexed_vertex in &model.unique_vertices {
|
||||
aabb.grow(transform.transform_point3(glam::Vec3::from_array(model.unique_pos[indexed_vertex.pos as usize])));
|
||||
}
|
||||
@@ -359,16 +452,29 @@ impl ModelPhysics {
|
||||
}
|
||||
}
|
||||
pub fn unit_vertices(&self) -> [glam::Vec3;8] {
|
||||
TreyMesh::unit_vertices()
|
||||
Aabb::unit_vertices()
|
||||
}
|
||||
pub fn mesh(&self) -> &TreyMesh {
|
||||
return &self.mesh;
|
||||
}
|
||||
pub fn face_mesh(&self,face:TreyMeshFace)->TreyMesh{
|
||||
self.mesh.face(face)
|
||||
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=self.mesh.clone();
|
||||
//in this implementation face = worldspace aabb face
|
||||
match face {
|
||||
AabbFace::Right => aabb.min.x=aabb.max.x,
|
||||
AabbFace::Top => aabb.min.y=aabb.max.y,
|
||||
AabbFace::Back => aabb.min.z=aabb.max.z,
|
||||
AabbFace::Left => aabb.max.x=aabb.min.x,
|
||||
AabbFace::Bottom => aabb.max.y=aabb.min.y,
|
||||
AabbFace::Front => aabb.max.z=aabb.min.z,
|
||||
}
|
||||
return aabb;
|
||||
}
|
||||
pub fn face_normal(&self,face:TreyMeshFace) -> glam::Vec3 {
|
||||
TreyMesh::normal(face)//this is wrong for scale
|
||||
Aabb::normal(face)//this is wrong for scale
|
||||
}
|
||||
}
|
||||
|
||||
@@ -429,7 +535,6 @@ impl Default for PhysicsState{
|
||||
contacts: std::collections::HashMap::new(),
|
||||
intersects: std::collections::HashMap::new(),
|
||||
models: Vec::new(),
|
||||
bvh:crate::bvh::BvhNode::default(),
|
||||
walk: WalkState::new(),
|
||||
camera: PhysicsCamera::from_offset(glam::vec3(0.0,4.5-2.5,0.0)),
|
||||
next_mouse: MouseState::default(),
|
||||
@@ -450,80 +555,53 @@ impl PhysicsState {
|
||||
self.intersects.clear();
|
||||
}
|
||||
|
||||
pub fn into_worker(mut self)->crate::worker::CompatWorker<TimedInstruction<InputInstruction>,PhysicsOutputState,Box<dyn FnMut(TimedInstruction<InputInstruction>)->PhysicsOutputState>>{
|
||||
let mut mouse_blocking=true;
|
||||
let mut last_mouse_time=self.next_mouse.time;
|
||||
pub fn into_worker(mut self)->crate::worker::Worker<TimedInstruction<InputInstruction>,PhysicsOutputState>{
|
||||
let mut last_time=0;
|
||||
//last_time: this indicates the last time the mouse position was known.
|
||||
//Only used to generate a MouseState right before mouse movement
|
||||
//to finalize a long period of no movement and avoid interpolating from a long out-of-date MouseState.
|
||||
let mut mouse_blocking=true;//waiting for next_mouse to be written
|
||||
let mut timeline=std::collections::VecDeque::new();
|
||||
crate::worker::CompatWorker::new(self.output(),Box::new(move |ins:TimedInstruction<InputInstruction>|{
|
||||
if if let Some(phys_input)=match ins.instruction{
|
||||
InputInstruction::MoveMouse(m)=>{
|
||||
if mouse_blocking{
|
||||
//tell the game state which is living in the past about its future
|
||||
timeline.push_front(TimedInstruction{
|
||||
time:last_mouse_time,
|
||||
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:ins.time,pos:m}),
|
||||
});
|
||||
}else{
|
||||
//mouse has just started moving again after being still for longer than 10ms.
|
||||
//replace the entire mouse interpolation state to avoid an intermediate state with identical m0.t m1.t timestamps which will divide by zero
|
||||
timeline.push_front(TimedInstruction{
|
||||
time:last_mouse_time,
|
||||
instruction:PhysicsInputInstruction::ReplaceMouse(
|
||||
MouseState{time:last_mouse_time,pos:self.next_mouse.pos},
|
||||
MouseState{time:ins.time,pos:m}
|
||||
),
|
||||
});
|
||||
//delay physics execution until we have an interpolation target
|
||||
mouse_blocking=true;
|
||||
crate::worker::Worker::new(self.output(),move |ins:TimedInstruction<InputInstruction>|{
|
||||
let run_queue=match &ins.instruction{
|
||||
InputInstruction::MoveMouse(_)=>{
|
||||
//I FORGOT TO EDIT THE MOVE MOUSE TIMESTAMPS
|
||||
if !mouse_blocking{
|
||||
//mouse has not been moving for a while.
|
||||
//make sure not to interpolate between two distant MouseStates.
|
||||
//generate a mouse instruction with no movement timestamped at last InputInstruction
|
||||
//Idle instructions are CRITICAL to keeping this value up to date
|
||||
//interpolate normally (now that prev mouse pos is up to date)
|
||||
// timeline.push_back(TimedInstruction{
|
||||
// time:last_time,
|
||||
// instruction:InputInstruction::MoveMouse(self.next_mouse.pos),
|
||||
// });
|
||||
}
|
||||
last_mouse_time=ins.time;
|
||||
None
|
||||
mouse_blocking=true;//block physics until the next mouse event or mouse event timeout.
|
||||
true//empty queue
|
||||
},
|
||||
InputInstruction::MoveForward(s)=>Some(PhysicsInputInstruction::SetMoveForward(s)),
|
||||
InputInstruction::MoveLeft(s)=>Some(PhysicsInputInstruction::SetMoveLeft(s)),
|
||||
InputInstruction::MoveBack(s)=>Some(PhysicsInputInstruction::SetMoveBack(s)),
|
||||
InputInstruction::MoveRight(s)=>Some(PhysicsInputInstruction::SetMoveRight(s)),
|
||||
InputInstruction::MoveUp(s)=>Some(PhysicsInputInstruction::SetMoveUp(s)),
|
||||
InputInstruction::MoveDown(s)=>Some(PhysicsInputInstruction::SetMoveDown(s)),
|
||||
InputInstruction::Jump(s)=>Some(PhysicsInputInstruction::SetJump(s)),
|
||||
InputInstruction::Zoom(s)=>Some(PhysicsInputInstruction::SetZoom(s)),
|
||||
InputInstruction::Reset=>Some(PhysicsInputInstruction::Reset),
|
||||
InputInstruction::Idle=>Some(PhysicsInputInstruction::Idle),
|
||||
}{
|
||||
//non-mouse event
|
||||
timeline.push_back(TimedInstruction{
|
||||
time:ins.time,
|
||||
instruction:phys_input,
|
||||
});
|
||||
|
||||
if mouse_blocking{
|
||||
//assume the mouse has stopped moving after 10ms.
|
||||
//shitty mice are 125Hz which is 8ms so this should cover that.
|
||||
//setting this to 100us still doesn't print even though it's 10x lower than the polling rate,
|
||||
//so mouse events are probably not handled separately from drawing and fire right before it :(
|
||||
if 10_000_000<ins.time-self.next_mouse.time{
|
||||
//push an event to extrapolate no movement from
|
||||
timeline.push_front(TimedInstruction{
|
||||
time:last_mouse_time,
|
||||
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:ins.time,pos:self.next_mouse.pos}),
|
||||
});
|
||||
last_mouse_time=ins.time;
|
||||
//stop blocking. the mouse is not moving so the physics does not need to live in the past and wait for interpolation targets.
|
||||
mouse_blocking=false;
|
||||
true
|
||||
_=>{
|
||||
if mouse_blocking{
|
||||
//maybe I can turn this inside out by making this anotehr state machine where 50_000_000 is an instruction timestamp
|
||||
//check if last mouse move is within 50ms
|
||||
if ins.time-self.next_mouse.time<50_000_000{
|
||||
false//do not empty queue
|
||||
}else{
|
||||
mouse_blocking=false;
|
||||
// timeline.push_back(TimedInstruction{
|
||||
// time:ins.time,
|
||||
// instruction:InputInstruction::MoveMouse(self.next_mouse.pos),
|
||||
// });
|
||||
true
|
||||
}
|
||||
}else{
|
||||
false
|
||||
true
|
||||
}
|
||||
}else{
|
||||
//keep this up to date so that it can be used as a known-timestamp
|
||||
//that the mouse was not moving when the mouse starts moving again
|
||||
last_mouse_time=ins.time;
|
||||
true
|
||||
}
|
||||
}else{
|
||||
//mouse event
|
||||
true
|
||||
}{
|
||||
},
|
||||
};
|
||||
last_time=ins.time;
|
||||
timeline.push_back(ins);
|
||||
if run_queue{
|
||||
//empty queue
|
||||
while let Some(instruction)=timeline.pop_front(){
|
||||
self.run(instruction.time);
|
||||
@@ -534,7 +612,7 @@ impl PhysicsState {
|
||||
}
|
||||
}
|
||||
self.output()
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn output(&self)->PhysicsOutputState{
|
||||
@@ -566,7 +644,6 @@ impl PhysicsState {
|
||||
}
|
||||
}
|
||||
}
|
||||
self.bvh=crate::bvh::generate_bvh(self.models.iter().map(|m|m.mesh().clone()).collect());
|
||||
//I don't wanna write structs for temporary structures
|
||||
//this code builds ModeDescriptions from the unsorted lists at the top of the function
|
||||
starts.sort_by_key(|tup|tup.0);
|
||||
@@ -746,8 +823,8 @@ impl PhysicsState {
|
||||
}
|
||||
}
|
||||
fn mesh(&self) -> TreyMesh {
|
||||
let mut aabb=TreyMesh::new();
|
||||
for vertex in TreyMesh::unit_vertices(){
|
||||
let mut aabb=Aabb::new();
|
||||
for vertex in Aabb::unit_vertices(){
|
||||
aabb.grow(self.body.position+self.style.hitbox_halfsize*vertex);
|
||||
}
|
||||
aabb
|
||||
@@ -764,7 +841,7 @@ impl PhysicsState {
|
||||
let (v,a)=(-self.body.velocity,self.body.acceleration);
|
||||
//collect x
|
||||
match collision_data.face {
|
||||
TreyMeshFace::Top|TreyMeshFace::Back|TreyMeshFace::Bottom|TreyMeshFace::Front=>{
|
||||
AabbFace::Top|AabbFace::Back|AabbFace::Bottom|AabbFace::Front=>{
|
||||
for t in zeroes2(mesh0.max.x-mesh1.min.x,v.x,0.5*a.x) {
|
||||
//negative t = back in time
|
||||
//must be moving towards surface to collide
|
||||
@@ -792,14 +869,14 @@ impl PhysicsState {
|
||||
}
|
||||
}
|
||||
},
|
||||
TreyMeshFace::Left=>{
|
||||
AabbFace::Left=>{
|
||||
//generate event if v.x<0||a.x<0
|
||||
if -v.x<0f32{
|
||||
best_time=time;
|
||||
exit_face=Some(TreyMeshFace::Left);
|
||||
}
|
||||
},
|
||||
TreyMeshFace::Right=>{
|
||||
AabbFace::Right=>{
|
||||
//generate event if 0<v.x||0<a.x
|
||||
if 0f32<(-v.x){
|
||||
best_time=time;
|
||||
@@ -809,7 +886,7 @@ impl PhysicsState {
|
||||
}
|
||||
//collect y
|
||||
match collision_data.face {
|
||||
TreyMeshFace::Left|TreyMeshFace::Back|TreyMeshFace::Right|TreyMeshFace::Front=>{
|
||||
AabbFace::Left|AabbFace::Back|AabbFace::Right|AabbFace::Front=>{
|
||||
for t in zeroes2(mesh0.max.y-mesh1.min.y,v.y,0.5*a.y) {
|
||||
//negative t = back in time
|
||||
//must be moving towards surface to collide
|
||||
@@ -837,14 +914,14 @@ impl PhysicsState {
|
||||
}
|
||||
}
|
||||
},
|
||||
TreyMeshFace::Bottom=>{
|
||||
AabbFace::Bottom=>{
|
||||
//generate event if v.y<0||a.y<0
|
||||
if -v.y<0f32{
|
||||
best_time=time;
|
||||
exit_face=Some(TreyMeshFace::Bottom);
|
||||
}
|
||||
},
|
||||
TreyMeshFace::Top=>{
|
||||
AabbFace::Top=>{
|
||||
//generate event if 0<v.y||0<a.y
|
||||
if 0f32<(-v.y){
|
||||
best_time=time;
|
||||
@@ -854,7 +931,7 @@ impl PhysicsState {
|
||||
}
|
||||
//collect z
|
||||
match collision_data.face {
|
||||
TreyMeshFace::Left|TreyMeshFace::Bottom|TreyMeshFace::Right|TreyMeshFace::Top=>{
|
||||
AabbFace::Left|AabbFace::Bottom|AabbFace::Right|AabbFace::Top=>{
|
||||
for t in zeroes2(mesh0.max.z-mesh1.min.z,v.z,0.5*a.z) {
|
||||
//negative t = back in time
|
||||
//must be moving towards surface to collide
|
||||
@@ -882,14 +959,14 @@ impl PhysicsState {
|
||||
}
|
||||
}
|
||||
},
|
||||
TreyMeshFace::Front=>{
|
||||
AabbFace::Front=>{
|
||||
//generate event if v.z<0||a.z<0
|
||||
if -v.z<0f32{
|
||||
best_time=time;
|
||||
exit_face=Some(TreyMeshFace::Front);
|
||||
}
|
||||
},
|
||||
TreyMeshFace::Back=>{
|
||||
AabbFace::Back=>{
|
||||
//generate event if 0<v.z||0<a.z
|
||||
if 0f32<(-v.z){
|
||||
best_time=time;
|
||||
@@ -907,18 +984,18 @@ impl PhysicsState {
|
||||
None
|
||||
}
|
||||
fn predict_collision_start(&self,time:TIME,time_limit:TIME,model_id:u32) -> Option<TimedInstruction<PhysicsInstruction>> {
|
||||
let mesh0=self.mesh();
|
||||
let mesh1=self.models.get(model_id as usize).unwrap().mesh();
|
||||
let (p,v,a,time)=(self.body.position,self.body.velocity,self.body.acceleration,self.body.time);
|
||||
//find best t
|
||||
let mut best_time=time_limit;
|
||||
let mut best_face:Option<TreyMeshFace>=None;
|
||||
let mesh0=self.mesh();
|
||||
let mesh1=self.models.get(model_id as usize).unwrap().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,0.5*a.x) {
|
||||
//must collide now or in the future
|
||||
//must beat the current soonest collision time
|
||||
//must be moving towards surface
|
||||
let t_time=time+((t as f64)*1_000_000_000f64) as TIME;
|
||||
let t_time=self.body.time+((t as f64)*1_000_000_000f64) as TIME;
|
||||
if time<=t_time&&t_time<best_time&&0f32<v.x+a.x*t{
|
||||
let dp=self.body.extrapolated_position(t_time)-p;
|
||||
//faces must be overlapping
|
||||
@@ -934,7 +1011,7 @@ impl PhysicsState {
|
||||
//must collide now or in the future
|
||||
//must beat the current soonest collision time
|
||||
//must be moving towards surface
|
||||
let t_time=time+((t as f64)*1_000_000_000f64) as TIME;
|
||||
let t_time=self.body.time+((t as f64)*1_000_000_000f64) as TIME;
|
||||
if time<=t_time&&t_time<best_time&&v.x+a.x*t<0f32{
|
||||
let dp=self.body.extrapolated_position(t_time)-p;
|
||||
//faces must be overlapping
|
||||
@@ -951,7 +1028,7 @@ impl PhysicsState {
|
||||
//must collide now or in the future
|
||||
//must beat the current soonest collision time
|
||||
//must be moving towards surface
|
||||
let t_time=time+((t as f64)*1_000_000_000f64) as TIME;
|
||||
let t_time=self.body.time+((t as f64)*1_000_000_000f64) as TIME;
|
||||
if time<=t_time&&t_time<best_time&&0f32<v.y+a.y*t{
|
||||
let dp=self.body.extrapolated_position(t_time)-p;
|
||||
//faces must be overlapping
|
||||
@@ -967,7 +1044,7 @@ impl PhysicsState {
|
||||
//must collide now or in the future
|
||||
//must beat the current soonest collision time
|
||||
//must be moving towards surface
|
||||
let t_time=time+((t as f64)*1_000_000_000f64) as TIME;
|
||||
let t_time=self.body.time+((t as f64)*1_000_000_000f64) as TIME;
|
||||
if time<=t_time&&t_time<best_time&&v.y+a.y*t<0f32{
|
||||
let dp=self.body.extrapolated_position(t_time)-p;
|
||||
//faces must be overlapping
|
||||
@@ -984,7 +1061,7 @@ impl PhysicsState {
|
||||
//must collide now or in the future
|
||||
//must beat the current soonest collision time
|
||||
//must be moving towards surface
|
||||
let t_time=time+((t as f64)*1_000_000_000f64) as TIME;
|
||||
let t_time=self.body.time+((t as f64)*1_000_000_000f64) as TIME;
|
||||
if time<=t_time&&t_time<best_time&&0f32<v.z+a.z*t{
|
||||
let dp=self.body.extrapolated_position(t_time)-p;
|
||||
//faces must be overlapping
|
||||
@@ -1000,7 +1077,7 @@ impl PhysicsState {
|
||||
//must collide now or in the future
|
||||
//must beat the current soonest collision time
|
||||
//must be moving towards surface
|
||||
let t_time=time+((t as f64)*1_000_000_000f64) as TIME;
|
||||
let t_time=self.body.time+((t as f64)*1_000_000_000f64) as TIME;
|
||||
if time<=t_time&&t_time<best_time&&v.z+a.z*t<0f32{
|
||||
let dp=self.body.extrapolated_position(t_time)-p;
|
||||
//faces must be overlapping
|
||||
@@ -1039,15 +1116,13 @@ impl crate::instruction::InstructionEmitter<PhysicsInstruction> for PhysicsState
|
||||
// collector.collect(self.predict_collision_end2(self.time,time_limit,collision_data));
|
||||
// }
|
||||
//check for collision start instructions (against every part in the game with no optimization!!)
|
||||
let mut aabb=crate::aabb::Aabb::new();
|
||||
aabb.grow(self.body.extrapolated_position(self.time));
|
||||
aabb.grow(self.body.extrapolated_position(time_limit));
|
||||
aabb.inflate(self.style.hitbox_halfsize);
|
||||
self.bvh.the_tester(&aabb,&mut |id|{
|
||||
if !(self.contacts.contains_key(&id)||self.intersects.contains_key(&id)){
|
||||
collector.collect(self.predict_collision_start(self.time,time_limit,id));
|
||||
for i in 0..self.models.len() {
|
||||
let i=i as u32;
|
||||
if self.contacts.contains_key(&i)||self.intersects.contains_key(&i){
|
||||
continue;
|
||||
}
|
||||
});
|
||||
collector.collect(self.predict_collision_start(self.time,time_limit,i));
|
||||
}
|
||||
if self.grounded {
|
||||
//walk maintenance
|
||||
collector.collect(self.next_walk_instruction());
|
||||
@@ -1062,11 +1137,10 @@ impl crate::instruction::InstructionEmitter<PhysicsInstruction> for PhysicsState
|
||||
impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsState {
|
||||
fn process_instruction(&mut self, ins:TimedInstruction<PhysicsInstruction>) {
|
||||
match &ins.instruction {
|
||||
PhysicsInstruction::Input(PhysicsInputInstruction::Idle)
|
||||
|PhysicsInstruction::Input(PhysicsInputInstruction::SetNextMouse(_))
|
||||
|PhysicsInstruction::Input(PhysicsInputInstruction::ReplaceMouse(_,_))
|
||||
PhysicsInstruction::Input(InputInstruction::Idle)
|
||||
|PhysicsInstruction::Input(InputInstruction::MoveMouse(_))
|
||||
|PhysicsInstruction::StrafeTick => (),
|
||||
_=>println!("{}|{:?}",ins.time,ins.instruction),
|
||||
_=>println!("{:?}",ins),
|
||||
}
|
||||
//selectively update body
|
||||
match &ins.instruction {
|
||||
@@ -1085,7 +1159,7 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
|
||||
match &contacting.surf{
|
||||
Some(surf)=>println!("I'm surfing!"),
|
||||
None=>match &c.face {
|
||||
TreyMeshFace::Top => {
|
||||
AabbFace::Top => {
|
||||
//ground
|
||||
self.grounded=true;
|
||||
},
|
||||
@@ -1155,7 +1229,7 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
|
||||
self.body.acceleration=a;
|
||||
//check ground
|
||||
match &c.face {
|
||||
TreyMeshFace::Top => {
|
||||
AabbFace::Top => {
|
||||
self.grounded=false;
|
||||
},
|
||||
_ => (),
|
||||
@@ -1191,32 +1265,29 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
|
||||
let mut refresh_walk_target=true;
|
||||
let mut refresh_walk_target_velocity=true;
|
||||
match input_instruction{
|
||||
PhysicsInputInstruction::SetNextMouse(m) => {
|
||||
InputInstruction::MoveMouse(m) => {
|
||||
self.camera.angles=self.camera.simulate_move_angles(self.next_mouse.pos);
|
||||
(self.camera.mouse,self.next_mouse)=(self.next_mouse.clone(),m);
|
||||
self.camera.mouse.move_mouse(self.next_mouse.pos,self.next_mouse.time);
|
||||
self.next_mouse.move_mouse(m,self.time);
|
||||
},
|
||||
PhysicsInputInstruction::ReplaceMouse(m0,m1) => {
|
||||
self.camera.angles=self.camera.simulate_move_angles(m0.pos);
|
||||
(self.camera.mouse,self.next_mouse)=(m0,m1);
|
||||
},
|
||||
PhysicsInputInstruction::SetMoveForward(s) => self.set_control(StyleModifiers::CONTROL_MOVEFORWARD,s),
|
||||
PhysicsInputInstruction::SetMoveLeft(s) => self.set_control(StyleModifiers::CONTROL_MOVELEFT,s),
|
||||
PhysicsInputInstruction::SetMoveBack(s) => self.set_control(StyleModifiers::CONTROL_MOVEBACK,s),
|
||||
PhysicsInputInstruction::SetMoveRight(s) => self.set_control(StyleModifiers::CONTROL_MOVERIGHT,s),
|
||||
PhysicsInputInstruction::SetMoveUp(s) => self.set_control(StyleModifiers::CONTROL_MOVEUP,s),
|
||||
PhysicsInputInstruction::SetMoveDown(s) => self.set_control(StyleModifiers::CONTROL_MOVEDOWN,s),
|
||||
PhysicsInputInstruction::SetJump(s) => {
|
||||
InputInstruction::MoveForward(s) => self.set_control(StyleModifiers::CONTROL_MOVEFORWARD,s),
|
||||
InputInstruction::MoveLeft(s) => self.set_control(StyleModifiers::CONTROL_MOVELEFT,s),
|
||||
InputInstruction::MoveBack(s) => self.set_control(StyleModifiers::CONTROL_MOVEBACK,s),
|
||||
InputInstruction::MoveRight(s) => self.set_control(StyleModifiers::CONTROL_MOVERIGHT,s),
|
||||
InputInstruction::MoveUp(s) => self.set_control(StyleModifiers::CONTROL_MOVEUP,s),
|
||||
InputInstruction::MoveDown(s) => self.set_control(StyleModifiers::CONTROL_MOVEDOWN,s),
|
||||
InputInstruction::Jump(s) => {
|
||||
self.set_control(StyleModifiers::CONTROL_JUMP,s);
|
||||
if self.grounded{
|
||||
self.jump();
|
||||
}
|
||||
refresh_walk_target_velocity=false;
|
||||
},
|
||||
PhysicsInputInstruction::SetZoom(s) => {
|
||||
InputInstruction::Zoom(s) => {
|
||||
self.set_control(StyleModifiers::CONTROL_ZOOM,s);
|
||||
refresh_walk_target=false;
|
||||
},
|
||||
PhysicsInputInstruction::Reset => {
|
||||
InputInstruction::Reset => {
|
||||
//temp
|
||||
self.body.position=self.spawn_point;
|
||||
self.body.velocity=glam::Vec3::ZERO;
|
||||
@@ -1227,7 +1298,7 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
|
||||
self.grounded=false;
|
||||
refresh_walk_target=false;
|
||||
},
|
||||
PhysicsInputInstruction::Idle => {refresh_walk_target=false;},//literally idle!
|
||||
InputInstruction::Idle => {refresh_walk_target=false;},//literally idle!
|
||||
}
|
||||
if refresh_walk_target{
|
||||
//calculate walk target velocity
|
||||
|
||||
@@ -45,31 +45,6 @@ impl<Task:Send+'static,Value:Clone+Send+'static> Worker<Task,Value> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CompatWorker<Task,Value:Clone,F>{
|
||||
data:std::marker::PhantomData<Task>,
|
||||
f:F,
|
||||
value:Value,
|
||||
}
|
||||
|
||||
impl<Task,Value:Clone,F:FnMut(Task)->Value> CompatWorker<Task,Value,F> {
|
||||
pub fn new(value:Value,f:F) -> Self {
|
||||
Self {
|
||||
f,
|
||||
value,
|
||||
data:std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send(&mut self,task:Task)->Result<(),()>{
|
||||
self.value=(self.f)(task);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn grab_clone(&self)->Value{
|
||||
self.value.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]//How to run this test with printing: cargo test --release -- --nocapture
|
||||
fn test_worker() {
|
||||
println!("hiiiii");
|
||||
|
||||
Reference in New Issue
Block a user