forked from StrafesNET/strafe-client
delete aabb physics
This commit is contained in:
parent
3c443b6b6a
commit
d155517587
282
src/physics.rs
282
src/physics.rs
@ -1,7 +1,6 @@
|
|||||||
use crate::zeroes::zeroes2;
|
|
||||||
use crate::instruction::{InstructionEmitter,InstructionConsumer,TimedInstruction};
|
use crate::instruction::{InstructionEmitter,InstructionConsumer,TimedInstruction};
|
||||||
use crate::integer::{Time,Planar64,Planar64Vec3,Planar64Mat3,Angle32,Ratio64,Ratio64Vec2};
|
use crate::integer::{Time,Planar64,Planar64Vec3,Planar64Mat3,Angle32,Ratio64,Ratio64Vec2};
|
||||||
use crate::model_physics::{PhysicsMesh,TransformedMesh,MinkowskiMesh};
|
use crate::model_physics::{PhysicsMesh,TransformedMesh};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum PhysicsInstruction {
|
pub enum PhysicsInstruction {
|
||||||
@ -970,285 +969,6 @@ impl PhysicsState {
|
|||||||
MoveState::Water=>None,//TODO
|
MoveState::Water=>None,//TODO
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn mesh(&self) -> TreyMesh {
|
|
||||||
let mut aabb=TreyMesh::default();
|
|
||||||
for vertex in TreyMesh::unit_vertices(){
|
|
||||||
aabb.grow(self.body.position+self.style.hitbox_halfsize*vertex);
|
|
||||||
}
|
|
||||||
aabb
|
|
||||||
}
|
|
||||||
fn predict_collision_end(&self,time:Time,time_limit:Time,collision_data:&ContactCollision) -> 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_time=time_limit;
|
|
||||||
let mut exit_face:Option<TreyMeshFace>=None;
|
|
||||||
let mesh0=self.mesh();
|
|
||||||
let mesh1=self.models.get(collision_data.model as usize).unwrap().mesh();
|
|
||||||
let (v,a)=(-self.body.velocity,self.body.acceleration);
|
|
||||||
//collect x
|
|
||||||
match collision_data.face {
|
|
||||||
TreyMeshFace::Top|TreyMeshFace::Back|TreyMeshFace::Bottom|TreyMeshFace::Front=>{
|
|
||||||
for t in zeroes2(mesh0.max.x()-mesh1.min.x(),v.x(),a.x()/2) {
|
|
||||||
//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=self.body.time-Time::from(t);
|
|
||||||
if time<=t_time&&t_time<best_time&&Planar64::ZERO<v.x()+a.x()*-t{
|
|
||||||
//collect valid t
|
|
||||||
best_time=t_time;
|
|
||||||
exit_face=Some(TreyMeshFace::Left);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for t in zeroes2(mesh0.min.x()-mesh1.max.x(),v.x(),a.x()/2) {
|
|
||||||
//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=self.body.time-Time::from(t);
|
|
||||||
if time<=t_time&&t_time<best_time&&v.x()+a.x()*-t<Planar64::ZERO{
|
|
||||||
//collect valid t
|
|
||||||
best_time=t_time;
|
|
||||||
exit_face=Some(TreyMeshFace::Right);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
TreyMeshFace::Left=>{
|
|
||||||
//generate event if v.x<0||a.x<0
|
|
||||||
if -v.x()<Planar64::ZERO{
|
|
||||||
best_time=time;
|
|
||||||
exit_face=Some(TreyMeshFace::Left);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
TreyMeshFace::Right=>{
|
|
||||||
//generate event if 0<v.x||0<a.x
|
|
||||||
if Planar64::ZERO<(-v.x()){
|
|
||||||
best_time=time;
|
|
||||||
exit_face=Some(TreyMeshFace::Right);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
//collect y
|
|
||||||
match collision_data.face {
|
|
||||||
TreyMeshFace::Left|TreyMeshFace::Back|TreyMeshFace::Right|TreyMeshFace::Front=>{
|
|
||||||
for t in zeroes2(mesh0.max.y()-mesh1.min.y(),v.y(),a.y()/2) {
|
|
||||||
//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=self.body.time-Time::from(t);
|
|
||||||
if time<=t_time&&t_time<best_time&&Planar64::ZERO<v.y()+a.y()*-t{
|
|
||||||
//collect valid t
|
|
||||||
best_time=t_time;
|
|
||||||
exit_face=Some(TreyMeshFace::Bottom);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for t in zeroes2(mesh0.min.y()-mesh1.max.y(),v.y(),a.y()/2) {
|
|
||||||
//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=self.body.time-Time::from(t);
|
|
||||||
if time<=t_time&&t_time<best_time&&v.y()+a.y()*-t<Planar64::ZERO{
|
|
||||||
//collect valid t
|
|
||||||
best_time=t_time;
|
|
||||||
exit_face=Some(TreyMeshFace::Top);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
TreyMeshFace::Bottom=>{
|
|
||||||
//generate event if v.y<0||a.y<0
|
|
||||||
if -v.y()<Planar64::ZERO{
|
|
||||||
best_time=time;
|
|
||||||
exit_face=Some(TreyMeshFace::Bottom);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
TreyMeshFace::Top=>{
|
|
||||||
//generate event if 0<v.y||0<a.y
|
|
||||||
if Planar64::ZERO<(-v.y()){
|
|
||||||
best_time=time;
|
|
||||||
exit_face=Some(TreyMeshFace::Top);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
//collect z
|
|
||||||
match collision_data.face {
|
|
||||||
TreyMeshFace::Left|TreyMeshFace::Bottom|TreyMeshFace::Right|TreyMeshFace::Top=>{
|
|
||||||
for t in zeroes2(mesh0.max.z()-mesh1.min.z(),v.z(),a.z()/2) {
|
|
||||||
//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=self.body.time-Time::from(t);
|
|
||||||
if time<=t_time&&t_time<best_time&&Planar64::ZERO<v.z()+a.z()*-t{
|
|
||||||
//collect valid t
|
|
||||||
best_time=t_time;
|
|
||||||
exit_face=Some(TreyMeshFace::Front);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for t in zeroes2(mesh0.min.z()-mesh1.max.z(),v.z(),a.z()/2) {
|
|
||||||
//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=self.body.time-Time::from(t);
|
|
||||||
if time<=t_time&&t_time<best_time&&v.z()+a.z()*-t<Planar64::ZERO{
|
|
||||||
//collect valid t
|
|
||||||
best_time=t_time;
|
|
||||||
exit_face=Some(TreyMeshFace::Back);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
TreyMeshFace::Front=>{
|
|
||||||
//generate event if v.z<0||a.z<0
|
|
||||||
if -v.z()<Planar64::ZERO{
|
|
||||||
best_time=time;
|
|
||||||
exit_face=Some(TreyMeshFace::Front);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
TreyMeshFace::Back=>{
|
|
||||||
//generate event if 0<v.z||0<a.z
|
|
||||||
if Planar64::ZERO<(-v.z()){
|
|
||||||
best_time=time;
|
|
||||||
exit_face=Some(TreyMeshFace::Back);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
//generate instruction
|
|
||||||
if let Some(_face) = exit_face{
|
|
||||||
return Some(TimedInstruction {
|
|
||||||
time: best_time,
|
|
||||||
instruction: PhysicsInstruction::CollisionEnd(collision_data.clone())
|
|
||||||
})
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
fn predict_collision_start(&self,time:Time,time_limit:Time,model_id:usize) -> Option<TimedInstruction<PhysicsInstruction>> {
|
|
||||||
let mesh0=self.mesh();
|
|
||||||
let mesh1=self.models.get(model_id).unwrap().mesh();
|
|
||||||
let (p,v,a,body_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;
|
|
||||||
//collect x
|
|
||||||
for t in zeroes2(mesh0.max.x()-mesh1.min.x(),v.x(),a.x()/2) {
|
|
||||||
//must collide now or in the future
|
|
||||||
//must beat the current soonest collision time
|
|
||||||
//must be moving towards surface
|
|
||||||
let t_time=body_time+Time::from(t);
|
|
||||||
if time<=t_time&&t_time<best_time&&Planar64::ZERO<v.x()+a.x()*t{
|
|
||||||
let dp=self.body.extrapolated_position(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_time=t_time;
|
|
||||||
best_face=Some(TreyMeshFace::Left);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for t in zeroes2(mesh0.min.x()-mesh1.max.x(),v.x(),a.x()/2) {
|
|
||||||
//must collide now or in the future
|
|
||||||
//must beat the current soonest collision time
|
|
||||||
//must be moving towards surface
|
|
||||||
let t_time=body_time+Time::from(t);
|
|
||||||
if time<=t_time&&t_time<best_time&&v.x()+a.x()*t<Planar64::ZERO{
|
|
||||||
let dp=self.body.extrapolated_position(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_time=t_time;
|
|
||||||
best_face=Some(TreyMeshFace::Right);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//collect y
|
|
||||||
for t in zeroes2(mesh0.max.y()-mesh1.min.y(),v.y(),a.y()/2) {
|
|
||||||
//must collide now or in the future
|
|
||||||
//must beat the current soonest collision time
|
|
||||||
//must be moving towards surface
|
|
||||||
let t_time=body_time+Time::from(t);
|
|
||||||
if time<=t_time&&t_time<best_time&&Planar64::ZERO<v.y()+a.y()*t{
|
|
||||||
let dp=self.body.extrapolated_position(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_time=t_time;
|
|
||||||
best_face=Some(TreyMeshFace::Bottom);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for t in zeroes2(mesh0.min.y()-mesh1.max.y(),v.y(),a.y()/2) {
|
|
||||||
//must collide now or in the future
|
|
||||||
//must beat the current soonest collision time
|
|
||||||
//must be moving towards surface
|
|
||||||
let t_time=body_time+Time::from(t);
|
|
||||||
if time<=t_time&&t_time<best_time&&v.y()+a.y()*t<Planar64::ZERO{
|
|
||||||
let dp=self.body.extrapolated_position(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_time=t_time;
|
|
||||||
best_face=Some(TreyMeshFace::Top);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//collect z
|
|
||||||
for t in zeroes2(mesh0.max.z()-mesh1.min.z(),v.z(),a.z()/2) {
|
|
||||||
//must collide now or in the future
|
|
||||||
//must beat the current soonest collision time
|
|
||||||
//must be moving towards surface
|
|
||||||
let t_time=body_time+Time::from(t);
|
|
||||||
if time<=t_time&&t_time<best_time&&Planar64::ZERO<v.z()+a.z()*t{
|
|
||||||
let dp=self.body.extrapolated_position(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_time=t_time;
|
|
||||||
best_face=Some(TreyMeshFace::Front);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for t in zeroes2(mesh0.min.z()-mesh1.max.z(),v.z(),a.z()/2) {
|
|
||||||
//must collide now or in the future
|
|
||||||
//must beat the current soonest collision time
|
|
||||||
//must be moving towards surface
|
|
||||||
let t_time=body_time+Time::from(t);
|
|
||||||
if time<=t_time&&t_time<best_time&&v.z()+a.z()*t<Planar64::ZERO{
|
|
||||||
let dp=self.body.extrapolated_position(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_time=t_time;
|
|
||||||
best_face=Some(TreyMeshFace::Back);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//generate instruction
|
|
||||||
if let Some(face)=best_face{
|
|
||||||
return Some(TimedInstruction{
|
|
||||||
time: best_time,
|
|
||||||
instruction:PhysicsInstruction::CollisionStart(ContactCollision{
|
|
||||||
face,
|
|
||||||
model:model_id
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl crate::instruction::InstructionEmitter<PhysicsInstruction> for PhysicsState {
|
impl crate::instruction::InstructionEmitter<PhysicsInstruction> for PhysicsState {
|
||||||
|
Loading…
Reference in New Issue
Block a user