forked from StrafesNET/strafe-client
Face Crawler™
This commit is contained in:
parent
c201a1a626
commit
5cedf91709
53
src/aabb.rs
53
src/aabb.rs
@ -1,18 +1,9 @@
|
|||||||
use crate::integer::Planar64Vec3;
|
use crate::integer::Planar64Vec3;
|
||||||
|
|
||||||
#[derive(Debug,Clone,Copy,Hash,Eq,PartialEq)]
|
|
||||||
pub enum AabbFace{
|
|
||||||
Right,//+X
|
|
||||||
Top,
|
|
||||||
Back,
|
|
||||||
Left,
|
|
||||||
Bottom,
|
|
||||||
Front,
|
|
||||||
}
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Aabb{
|
pub struct Aabb{
|
||||||
pub min:Planar64Vec3,
|
min:Planar64Vec3,
|
||||||
pub max:Planar64Vec3,
|
max:Planar64Vec3,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Aabb {
|
impl Default for Aabb {
|
||||||
@ -22,17 +13,6 @@ impl Default for Aabb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Aabb{
|
impl Aabb{
|
||||||
const VERTEX_DATA:[Planar64Vec3;8]=[
|
|
||||||
Planar64Vec3::int( 1,-1,-1),
|
|
||||||
Planar64Vec3::int( 1, 1,-1),
|
|
||||||
Planar64Vec3::int( 1, 1, 1),
|
|
||||||
Planar64Vec3::int( 1,-1, 1),
|
|
||||||
Planar64Vec3::int(-1,-1, 1),
|
|
||||||
Planar64Vec3::int(-1, 1, 1),
|
|
||||||
Planar64Vec3::int(-1, 1,-1),
|
|
||||||
Planar64Vec3::int(-1,-1,-1),
|
|
||||||
];
|
|
||||||
|
|
||||||
pub fn grow(&mut self,point:Planar64Vec3){
|
pub fn grow(&mut self,point:Planar64Vec3){
|
||||||
self.min=self.min.min(point);
|
self.min=self.min.min(point);
|
||||||
self.max=self.max.max(point);
|
self.max=self.max.max(point);
|
||||||
@ -48,34 +28,11 @@ impl Aabb{
|
|||||||
pub fn intersects(&self,aabb:&Aabb)->bool{
|
pub fn intersects(&self,aabb:&Aabb)->bool{
|
||||||
(self.min.cmplt(aabb.max)&aabb.min.cmplt(self.max)).all()
|
(self.min.cmplt(aabb.max)&aabb.min.cmplt(self.max)).all()
|
||||||
}
|
}
|
||||||
pub fn normal(face:AabbFace)->Planar64Vec3{
|
pub fn size(&self)->Planar64Vec3{
|
||||||
match face {
|
self.max-self.min
|
||||||
AabbFace::Right=>Planar64Vec3::int(1,0,0),
|
|
||||||
AabbFace::Top=>Planar64Vec3::int(0,1,0),
|
|
||||||
AabbFace::Back=>Planar64Vec3::int(0,0,1),
|
|
||||||
AabbFace::Left=>Planar64Vec3::int(-1,0,0),
|
|
||||||
AabbFace::Bottom=>Planar64Vec3::int(0,-1,0),
|
|
||||||
AabbFace::Front=>Planar64Vec3::int(0,0,-1),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pub fn unit_vertices()->[Planar64Vec3;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)->Planar64Vec3{
|
pub fn center(&self)->Planar64Vec3{
|
||||||
return self.min.midpoint(self.max)
|
self.min.midpoint(self.max)
|
||||||
}
|
}
|
||||||
//probably use floats for area & volume because we don't care about precision
|
//probably use floats for area & volume because we don't care about precision
|
||||||
// pub fn area_weight(&self)->f32{
|
// pub fn area_weight(&self)->f32{
|
||||||
|
121
src/face_crawler.rs
Normal file
121
src/face_crawler.rs
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
use crate::physics::Body;
|
||||||
|
use crate::model_physics::{FEV,MeshQuery,DirectedEdge};
|
||||||
|
use crate::integer::{Time,Planar64};
|
||||||
|
use crate::zeroes::zeroes2;
|
||||||
|
|
||||||
|
enum Transition<F,E:DirectedEdge,V>{
|
||||||
|
Miss,
|
||||||
|
Next(FEV<F,E,V>,Time),
|
||||||
|
Hit(F,Time),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next_transition<F:Copy,E:Copy+DirectedEdge,V:Copy>(fev:&FEV<F,E,V>,time:Time,mesh:&impl MeshQuery<F,E,V>,body:&Body,time_limit:Time)->Transition<F,E,V>{
|
||||||
|
//conflicting derivative means it crosses in the wrong direction.
|
||||||
|
//if the transition time is equal to an already tested transition, do not replace the current best.
|
||||||
|
let mut best_time=time_limit;
|
||||||
|
let mut best_transtition=Transition::Miss;
|
||||||
|
match fev{
|
||||||
|
&FEV::<F,E,V>::Face(face_id)=>{
|
||||||
|
//test own face collision time, ignoring roots with zero or conflicting derivative
|
||||||
|
//n=face.normal d=face.dot
|
||||||
|
//n.a t^2+n.v t+n.p-d==0
|
||||||
|
let (n,d)=mesh.face_nd(face_id);
|
||||||
|
for t in zeroes2((n.dot(body.position)-d)*2,n.dot(body.velocity)*2,n.dot(body.acceleration)){
|
||||||
|
let t=body.time+Time::from(t);
|
||||||
|
if time<=t&&t<best_time&&n.dot(body.extrapolated_velocity(t))<Planar64::ZERO{
|
||||||
|
best_time=t;
|
||||||
|
best_transtition=Transition::Hit(face_id,t);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//test each edge collision time, ignoring roots with zero or conflicting derivative
|
||||||
|
for &directed_edge_id in mesh.face_edges(face_id).iter(){
|
||||||
|
let edge_n=mesh.directed_edge_n(directed_edge_id);
|
||||||
|
let n=n.cross(edge_n);
|
||||||
|
let verts=mesh.edge_verts(directed_edge_id.as_undirected());
|
||||||
|
let d=n.dot(mesh.vert(verts[0])+mesh.vert(verts[1]));
|
||||||
|
//WARNING: d is moved out of the *2 block because of adding two vertices!
|
||||||
|
for t in zeroes2(n.dot(body.position)*2-d,n.dot(body.velocity)*2,n.dot(body.acceleration)){
|
||||||
|
let t=body.time+Time::from(t);
|
||||||
|
if time<=t&&t<best_time&&n.dot(body.extrapolated_velocity(t))<Planar64::ZERO{
|
||||||
|
best_time=t;
|
||||||
|
best_transtition=Transition::Next(FEV::<F,E,V>::Edge(directed_edge_id.as_undirected()),t);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//if none:
|
||||||
|
},
|
||||||
|
&FEV::<F,E,V>::Edge(edge_id)=>{
|
||||||
|
//test each face collision time, ignoring roots with zero or conflicting derivative
|
||||||
|
let edge_n=mesh.edge_n(edge_id);
|
||||||
|
let edge_verts=mesh.edge_verts(edge_id);
|
||||||
|
let vert_sum=mesh.vert(edge_verts[0])+mesh.vert(edge_verts[1]);
|
||||||
|
for (i,&edge_face_id) in mesh.edge_faces(edge_id).iter().enumerate(){
|
||||||
|
let face_n=mesh.face_nd(edge_face_id).0;
|
||||||
|
//edge_n gets parity from the order of edge_faces
|
||||||
|
let n=face_n.cross(edge_n)*((i as i64)*2-1);
|
||||||
|
let d=n.dot(vert_sum);
|
||||||
|
//WARNING yada yada d *2
|
||||||
|
for t in zeroes2((n.dot(body.position))*2-d,n.dot(body.velocity)*2,n.dot(body.acceleration)){
|
||||||
|
let t=body.time+Time::from(t);
|
||||||
|
if time<=t&&t<best_time&&n.dot(body.extrapolated_velocity(t))<Planar64::ZERO{
|
||||||
|
best_time=t;
|
||||||
|
best_transtition=Transition::Next(FEV::<F,E,V>::Face(edge_face_id),t);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//test each vertex collision time, ignoring roots with zero or conflicting derivative
|
||||||
|
for (i,&vert_id) in edge_verts.iter().enumerate(){
|
||||||
|
//vertex normal gets parity from vert index
|
||||||
|
let n=edge_n*(1-2*(i as i64));
|
||||||
|
let d=n.dot(mesh.vert(vert_id));
|
||||||
|
for t in zeroes2((n.dot(body.position)-d)*2,n.dot(body.velocity)*2,n.dot(body.acceleration)){
|
||||||
|
let t=body.time+Time::from(t);
|
||||||
|
if time<=t&&t<best_time&&n.dot(body.extrapolated_velocity(t))<Planar64::ZERO{
|
||||||
|
best_time=t;
|
||||||
|
best_transtition=Transition::Next(FEV::<F,E,V>::Vert(vert_id),t);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//if none:
|
||||||
|
},
|
||||||
|
&FEV::<F,E,V>::Vert(vert_id)=>{
|
||||||
|
//test each edge collision time, ignoring roots with zero or conflicting derivative
|
||||||
|
for &directed_edge_id in mesh.vert_edges(vert_id).iter(){
|
||||||
|
//edge is directed away from vertex, but we want the dot product to turn out negative
|
||||||
|
let n=-mesh.directed_edge_n(directed_edge_id);
|
||||||
|
let d=n.dot(mesh.vert(vert_id));
|
||||||
|
for t in zeroes2((n.dot(body.position)-d)*2,n.dot(body.velocity)*2,n.dot(body.acceleration)){
|
||||||
|
let t=body.time+Time::from(t);
|
||||||
|
if time<=t&&t<best_time&&n.dot(body.extrapolated_velocity(t))<Planar64::ZERO{
|
||||||
|
best_time=t;
|
||||||
|
best_transtition=Transition::Next(FEV::<F,E,V>::Edge(directed_edge_id.as_undirected()),t);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//if none:
|
||||||
|
},
|
||||||
|
}
|
||||||
|
best_transtition
|
||||||
|
}
|
||||||
|
pub enum CrawlResult<F,E:DirectedEdge,V>{
|
||||||
|
Miss(FEV<F,E,V>),
|
||||||
|
Hit(F,Time),
|
||||||
|
}
|
||||||
|
pub fn crawl_fev<F:Copy,E:Copy+DirectedEdge,V:Copy>(mut fev:FEV<F,E,V>,mesh:&impl MeshQuery<F,E,V>,relative_body:&Body,start_time:Time,time_limit:Time)->CrawlResult<F,E,V>{
|
||||||
|
let mut time=start_time;
|
||||||
|
for _ in 0..20{
|
||||||
|
match next_transition(&fev,time,mesh,relative_body,time_limit){
|
||||||
|
Transition::Miss=>return CrawlResult::Miss(fev),
|
||||||
|
Transition::Next(next_fev,next_time)=>(fev,time)=(next_fev,next_time),
|
||||||
|
Transition::Hit(face,time)=>return CrawlResult::Hit(face,time),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//TODO: fix all bugs
|
||||||
|
println!("Too many iterations! Using default behaviour instead of crashing...");
|
||||||
|
CrawlResult::Miss(fev)
|
||||||
|
}
|
@ -45,14 +45,19 @@ fn get_attributes(name:&str,can_collide:bool,velocity:Planar64Vec3,force_interse
|
|||||||
"Water"=>{
|
"Water"=>{
|
||||||
force_can_collide=false;
|
force_can_collide=false;
|
||||||
//TODO: read stupid CustomPhysicalProperties
|
//TODO: read stupid CustomPhysicalProperties
|
||||||
intersecting.water=Some(crate::model::IntersectingWater{density:Planar64::ONE,viscosity:Planar64::ONE/10,current:velocity});
|
intersecting.water=Some(crate::model::IntersectingWater{density:Planar64::ONE,viscosity:Planar64::ONE/10,velocity});
|
||||||
},
|
},
|
||||||
"Accelerator"=>{
|
"Accelerator"=>{
|
||||||
//although the new game supports collidable accelerators, this is a roblox compatability map loader
|
//although the new game supports collidable accelerators, this is a roblox compatability map loader
|
||||||
force_can_collide=false;
|
force_can_collide=false;
|
||||||
general.accelerator=Some(crate::model::GameMechanicAccelerator{acceleration:velocity});
|
general.accelerator=Some(crate::model::GameMechanicAccelerator{acceleration:velocity});
|
||||||
},
|
},
|
||||||
"UnorderedCheckpoint"=>general.checkpoint=Some(crate::model::GameMechanicCheckpoint::Unordered{mode_id:0}),
|
// "UnorderedCheckpoint"=>general.teleport_behaviour=Some(crate::model::TeleportBehaviour::StageElement(crate::model::GameMechanicStageElement{
|
||||||
|
// mode_id:0,
|
||||||
|
// stage_id:0,
|
||||||
|
// force:false,
|
||||||
|
// behaviour:crate::model::StageElementBehaviour::Unordered
|
||||||
|
// })),
|
||||||
"SetVelocity"=>general.trajectory=Some(crate::model::GameMechanicSetTrajectory::Velocity(velocity)),
|
"SetVelocity"=>general.trajectory=Some(crate::model::GameMechanicSetTrajectory::Velocity(velocity)),
|
||||||
"MapFinish"=>{force_can_collide=false;general.zone=Some(crate::model::GameMechanicZone{mode_id:0,behaviour:crate::model::ZoneBehaviour::Finish})},
|
"MapFinish"=>{force_can_collide=false;general.zone=Some(crate::model::GameMechanicZone{mode_id:0,behaviour:crate::model::ZoneBehaviour::Finish})},
|
||||||
"MapAnticheat"=>{force_can_collide=false;general.zone=Some(crate::model::GameMechanicZone{mode_id:0,behaviour:crate::model::ZoneBehaviour::Anitcheat})},
|
"MapAnticheat"=>{force_can_collide=false;general.zone=Some(crate::model::GameMechanicZone{mode_id:0,behaviour:crate::model::ZoneBehaviour::Anitcheat})},
|
||||||
@ -111,13 +116,14 @@ fn get_attributes(name:&str,can_collide:bool,velocity:Planar64Vec3,force_interse
|
|||||||
"WormholeIn"=>general.teleport_behaviour=Some(crate::model::TeleportBehaviour::Wormhole(crate::model::GameMechanicWormhole{destination_model_id:captures[2].parse::<u32>().unwrap()})),
|
"WormholeIn"=>general.teleport_behaviour=Some(crate::model::TeleportBehaviour::Wormhole(crate::model::GameMechanicWormhole{destination_model_id:captures[2].parse::<u32>().unwrap()})),
|
||||||
_=>panic!("regex3[1] messed up bad"),
|
_=>panic!("regex3[1] messed up bad"),
|
||||||
}
|
}
|
||||||
}else if let Some(captures)=lazy_regex::regex!(r"^(OrderedCheckpoint)(\d+)$")
|
|
||||||
.captures(other){
|
|
||||||
match &captures[1]{
|
|
||||||
"OrderedCheckpoint"=>general.checkpoint=Some(crate::model::GameMechanicCheckpoint::Ordered{mode_id:0,checkpoint_id:captures[2].parse::<u32>().unwrap()}),
|
|
||||||
_=>panic!("regex3[1] messed up bad"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// else if let Some(captures)=lazy_regex::regex!(r"^(OrderedCheckpoint)(\d+)$")
|
||||||
|
// .captures(other){
|
||||||
|
// match &captures[1]{
|
||||||
|
// "OrderedCheckpoint"=>general.checkpoint=Some(crate::model::GameMechanicCheckpoint::Ordered{mode_id:0,checkpoint_id:captures[2].parse::<u32>().unwrap()}),
|
||||||
|
// _=>panic!("regex3[1] messed up bad"),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//need some way to skip this
|
//need some way to skip this
|
||||||
|
@ -12,7 +12,9 @@ mod settings;
|
|||||||
mod primitives;
|
mod primitives;
|
||||||
mod instruction;
|
mod instruction;
|
||||||
mod load_roblox;
|
mod load_roblox;
|
||||||
|
mod face_crawler;
|
||||||
mod compat_worker;
|
mod compat_worker;
|
||||||
|
mod model_physics;
|
||||||
mod model_graphics;
|
mod model_graphics;
|
||||||
mod physics_worker;
|
mod physics_worker;
|
||||||
mod graphics_worker;
|
mod graphics_worker;
|
||||||
|
118
src/model.rs
118
src/model.rs
@ -83,73 +83,78 @@ pub enum TempIndexedAttributes{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//you have this effect while in contact
|
//you have this effect while in contact
|
||||||
#[derive(Clone)]
|
#[derive(Clone,Hash,Eq,PartialEq)]
|
||||||
pub struct ContactingLadder{
|
pub struct ContactingLadder{
|
||||||
pub sticky:bool
|
pub sticky:bool
|
||||||
}
|
}
|
||||||
#[derive(Clone)]
|
#[derive(Clone,Hash,Eq,PartialEq)]
|
||||||
pub enum ContactingBehaviour{
|
pub enum ContactingBehaviour{
|
||||||
Surf,
|
Surf,
|
||||||
Ladder(ContactingLadder),
|
Cling,//usable as a zipline, or other weird and wonderful things
|
||||||
Elastic(u32),//[1/2^32,1] 0=None (elasticity+1)/2^32
|
Ladder(ContactingLadder),
|
||||||
|
Elastic(u32),//[1/2^32,1] 0=None (elasticity+1)/2^32
|
||||||
}
|
}
|
||||||
//you have this effect while intersecting
|
//you have this effect while intersecting
|
||||||
#[derive(Clone)]
|
#[derive(Clone,Hash,Eq,PartialEq)]
|
||||||
pub struct IntersectingWater{
|
pub struct IntersectingWater{
|
||||||
pub viscosity:Planar64,
|
pub viscosity:Planar64,
|
||||||
pub density:Planar64,
|
pub density:Planar64,
|
||||||
pub current:Planar64Vec3,
|
pub velocity:Planar64Vec3,
|
||||||
}
|
}
|
||||||
//All models can be given these attributes
|
//All models can be given these attributes
|
||||||
#[derive(Clone)]
|
#[derive(Clone,Hash,Eq,PartialEq)]
|
||||||
pub struct GameMechanicAccelerator{
|
pub struct GameMechanicAccelerator{
|
||||||
pub acceleration:Planar64Vec3
|
pub acceleration:Planar64Vec3
|
||||||
}
|
}
|
||||||
#[derive(Clone)]
|
#[derive(Clone,Hash,Eq,PartialEq)]
|
||||||
pub enum GameMechanicBooster{
|
pub enum GameMechanicBooster{
|
||||||
Affine(Planar64Affine3),//capable of SetVelocity,DotVelocity,normal booster,bouncy part,redirect velocity, and much more
|
Affine(Planar64Affine3),//capable of SetVelocity,DotVelocity,normal booster,bouncy part,redirect velocity, and much more
|
||||||
Velocity(Planar64Vec3),//straight up boost velocity adds to your current velocity
|
Velocity(Planar64Vec3),//straight up boost velocity adds to your current velocity
|
||||||
Energy{direction:Planar64Vec3,energy:Planar64},//increase energy in direction
|
Energy{direction:Planar64Vec3,energy:Planar64},//increase energy in direction
|
||||||
}
|
}
|
||||||
#[derive(Clone)]
|
#[derive(Clone,Hash,Eq,PartialEq)]
|
||||||
pub enum GameMechanicCheckpoint{
|
|
||||||
Ordered{
|
|
||||||
mode_id:u32,
|
|
||||||
checkpoint_id:u32,
|
|
||||||
},
|
|
||||||
Unordered{
|
|
||||||
mode_id:u32,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum TrajectoryChoice{
|
pub enum TrajectoryChoice{
|
||||||
HighArcLongDuration,//underhand lob at target: less horizontal speed and more air time
|
HighArcLongDuration,//underhand lob at target: less horizontal speed and more air time
|
||||||
LowArcShortDuration,//overhand throw at target: more horizontal speed and less air time
|
LowArcShortDuration,//overhand throw at target: more horizontal speed and less air time
|
||||||
}
|
}
|
||||||
#[derive(Clone)]
|
#[derive(Clone,Hash,Eq,PartialEq)]
|
||||||
pub enum GameMechanicSetTrajectory{
|
pub enum GameMechanicSetTrajectory{
|
||||||
|
//Speed-type SetTrajectory
|
||||||
AirTime(Time),//air time (relative to gravity direction) is invariant across mass and gravity changes
|
AirTime(Time),//air time (relative to gravity direction) is invariant across mass and gravity changes
|
||||||
Height(Planar64),//boost height (relative to gravity direction) is invariant across mass and gravity changes
|
Height(Planar64),//boost height (relative to gravity direction) is invariant across mass and gravity changes
|
||||||
|
DotVelocity{direction:Planar64Vec3,dot:Planar64},//set your velocity in a specific direction without touching other directions
|
||||||
|
//Velocity-type SetTrajectory
|
||||||
TargetPointTime{//launch on a trajectory that will land at a target point in a set amount of time
|
TargetPointTime{//launch on a trajectory that will land at a target point in a set amount of time
|
||||||
target_point:Planar64Vec3,
|
target_point:Planar64Vec3,
|
||||||
time:Time,//short time = fast and direct, long time = launch high in the air, negative time = wrong way
|
time:Time,//short time = fast and direct, long time = launch high in the air, negative time = wrong way
|
||||||
},
|
},
|
||||||
TrajectoryTargetPoint{//launch at a fixed speed and land at a target point
|
TargetPointSpeed{//launch at a fixed speed and land at a target point
|
||||||
target_point:Planar64Vec3,
|
target_point:Planar64Vec3,
|
||||||
speed:Planar64,//if speed is too low this will fail to reach the target. The closest-passing trajectory will be chosen instead
|
speed:Planar64,//if speed is too low this will fail to reach the target. The closest-passing trajectory will be chosen instead
|
||||||
trajectory_choice:TrajectoryChoice,
|
trajectory_choice:TrajectoryChoice,
|
||||||
},
|
},
|
||||||
Velocity(Planar64Vec3),//SetVelocity
|
Velocity(Planar64Vec3),//SetVelocity
|
||||||
DotVelocity{direction:Planar64Vec3,dot:Planar64},//set your velocity in a specific direction without touching other directions
|
|
||||||
}
|
}
|
||||||
#[derive(Clone)]
|
impl GameMechanicSetTrajectory{
|
||||||
|
fn is_velocity(&self)->bool{
|
||||||
|
match self{
|
||||||
|
GameMechanicSetTrajectory::AirTime(_)
|
||||||
|
|GameMechanicSetTrajectory::Height(_)
|
||||||
|
|GameMechanicSetTrajectory::DotVelocity{direction:_,dot:_}=>false,
|
||||||
|
GameMechanicSetTrajectory::TargetPointTime{target_point:_,time:_}
|
||||||
|
|GameMechanicSetTrajectory::TargetPointSpeed{target_point:_,speed:_,trajectory_choice:_}
|
||||||
|
|GameMechanicSetTrajectory::Velocity(_)=>true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Clone,Hash,Eq,PartialEq)]
|
||||||
pub enum ZoneBehaviour{
|
pub enum ZoneBehaviour{
|
||||||
//Start is indexed
|
//Start is indexed
|
||||||
//Checkpoints are indexed
|
//Checkpoints are indexed
|
||||||
Finish,
|
Finish,
|
||||||
Anitcheat,
|
Anitcheat,
|
||||||
}
|
}
|
||||||
#[derive(Clone)]
|
#[derive(Clone,Hash,Eq,PartialEq)]
|
||||||
pub struct GameMechanicZone{
|
pub struct GameMechanicZone{
|
||||||
pub mode_id:u32,
|
pub mode_id:u32,
|
||||||
pub behaviour:ZoneBehaviour,
|
pub behaviour:ZoneBehaviour,
|
||||||
@ -160,31 +165,36 @@ pub struct GameMechanicZone{
|
|||||||
// InRange(Planar64,Planar64),
|
// InRange(Planar64,Planar64),
|
||||||
// OutsideRange(Planar64,Planar64),
|
// OutsideRange(Planar64,Planar64),
|
||||||
// }
|
// }
|
||||||
#[derive(Clone)]
|
#[derive(Clone,Hash,Eq,PartialEq)]
|
||||||
pub enum StageElementBehaviour{
|
pub enum StageElementBehaviour{
|
||||||
//Spawn,//The behaviour of stepping on a spawn setting the spawnid
|
//Spawn,//The behaviour of stepping on a spawn setting the spawnid
|
||||||
SpawnAt,
|
SpawnAt,//must be standing on top to get effect. except cancollide false
|
||||||
Trigger,
|
Trigger,
|
||||||
Teleport,
|
Teleport,
|
||||||
Platform,
|
Platform,
|
||||||
//Acts like a trigger if you haven't hit all the checkpoints.
|
//Checkpoint acts like a trigger if you haven't hit all the checkpoints yet.
|
||||||
Checkpoint{
|
//Note that all stage elements act like this for the next stage.
|
||||||
//if this is 2 you must have hit OrderedCheckpoint(0) OrderedCheckpoint(1) OrderedCheckpoint(2) to pass
|
Checkpoint,
|
||||||
ordered_checkpoint_id:Option<u32>,
|
//OrderedCheckpoint. You must pass through all of these in ascending order.
|
||||||
//if this is 2 you must have hit at least 2 UnorderedCheckpoints to pass
|
//If you hit them out of order it acts like a trigger.
|
||||||
unordered_checkpoint_count:u32,
|
//Do not support backtracking at all for now.
|
||||||
},
|
Ordered{
|
||||||
JumpLimit(u32),
|
checkpoint_id:u32,
|
||||||
//Speedtrap(TrapCondition),//Acts as a trigger with a speed condition
|
},
|
||||||
|
//UnorderedCheckpoint. You must pass through all of these in any order.
|
||||||
|
Unordered,
|
||||||
|
//If you get reset by a jump limit
|
||||||
|
JumpLimit(u32),
|
||||||
|
//Speedtrap(TrapCondition),//Acts as a trigger with a speed condition
|
||||||
}
|
}
|
||||||
#[derive(Clone)]
|
#[derive(Clone,Hash,Eq,PartialEq)]
|
||||||
pub struct GameMechanicStageElement{
|
pub struct GameMechanicStageElement{
|
||||||
pub mode_id:u32,
|
pub mode_id:u32,
|
||||||
pub stage_id:u32,//which spawn to send to
|
pub stage_id:u32,//which spawn to send to
|
||||||
pub force:bool,//allow setting to lower spawn id i.e. 7->3
|
pub force:bool,//allow setting to lower spawn id i.e. 7->3
|
||||||
pub behaviour:StageElementBehaviour
|
pub behaviour:StageElementBehaviour
|
||||||
}
|
}
|
||||||
#[derive(Clone)]
|
#[derive(Clone,Hash,Eq,PartialEq)]
|
||||||
pub struct GameMechanicWormhole{
|
pub struct GameMechanicWormhole{
|
||||||
//destination does not need to be another wormhole
|
//destination does not need to be another wormhole
|
||||||
//this defines a one way portal to a destination model transform
|
//this defines a one way portal to a destination model transform
|
||||||
@ -192,17 +202,16 @@ pub struct GameMechanicWormhole{
|
|||||||
pub destination_model_id:u32,
|
pub destination_model_id:u32,
|
||||||
//(position,angles)*=origin.transform.inverse()*destination.transform
|
//(position,angles)*=origin.transform.inverse()*destination.transform
|
||||||
}
|
}
|
||||||
#[derive(Clone)]
|
#[derive(Clone,Hash,Eq,PartialEq)]
|
||||||
pub enum TeleportBehaviour{
|
pub enum TeleportBehaviour{
|
||||||
StageElement(GameMechanicStageElement),
|
StageElement(GameMechanicStageElement),
|
||||||
Wormhole(GameMechanicWormhole),
|
Wormhole(GameMechanicWormhole),
|
||||||
}
|
}
|
||||||
//attributes listed in order of handling
|
//attributes listed in order of handling
|
||||||
#[derive(Default,Clone)]
|
#[derive(Default,Clone,Hash,Eq,PartialEq)]
|
||||||
pub struct GameMechanicAttributes{
|
pub struct GameMechanicAttributes{
|
||||||
pub zone:Option<GameMechanicZone>,
|
pub zone:Option<GameMechanicZone>,
|
||||||
pub booster:Option<GameMechanicBooster>,
|
pub booster:Option<GameMechanicBooster>,
|
||||||
pub checkpoint:Option<GameMechanicCheckpoint>,
|
|
||||||
pub trajectory:Option<GameMechanicSetTrajectory>,
|
pub trajectory:Option<GameMechanicSetTrajectory>,
|
||||||
pub teleport_behaviour:Option<TeleportBehaviour>,
|
pub teleport_behaviour:Option<TeleportBehaviour>,
|
||||||
pub accelerator:Option<GameMechanicAccelerator>,
|
pub accelerator:Option<GameMechanicAccelerator>,
|
||||||
@ -211,13 +220,26 @@ impl GameMechanicAttributes{
|
|||||||
pub fn any(&self)->bool{
|
pub fn any(&self)->bool{
|
||||||
self.zone.is_some()
|
self.zone.is_some()
|
||||||
||self.booster.is_some()
|
||self.booster.is_some()
|
||||||
||self.checkpoint.is_some()
|
|
||||||
||self.trajectory.is_some()
|
||self.trajectory.is_some()
|
||||||
||self.teleport_behaviour.is_some()
|
||self.teleport_behaviour.is_some()
|
||||||
||self.accelerator.is_some()
|
||self.accelerator.is_some()
|
||||||
}
|
}
|
||||||
|
pub fn is_wrcp(&self,current_mode_id:u32)->bool{
|
||||||
|
self.trajectory.as_ref().map_or(false,|t|t.is_velocity())
|
||||||
|
&&match &self.teleport_behaviour{
|
||||||
|
Some(TeleportBehaviour::StageElement(
|
||||||
|
GameMechanicStageElement{
|
||||||
|
mode_id,
|
||||||
|
stage_id:_,
|
||||||
|
force:true,
|
||||||
|
behaviour:StageElementBehaviour::Trigger|StageElementBehaviour::Teleport
|
||||||
|
}
|
||||||
|
))=>current_mode_id==*mode_id,
|
||||||
|
_=>false,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#[derive(Default,Clone)]
|
#[derive(Default,Clone,Hash,Eq,PartialEq)]
|
||||||
pub struct ContactingAttributes{
|
pub struct ContactingAttributes{
|
||||||
//friction?
|
//friction?
|
||||||
pub contact_behaviour:Option<ContactingBehaviour>,
|
pub contact_behaviour:Option<ContactingBehaviour>,
|
||||||
@ -227,7 +249,7 @@ impl ContactingAttributes{
|
|||||||
self.contact_behaviour.is_some()
|
self.contact_behaviour.is_some()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[derive(Default,Clone)]
|
#[derive(Default,Clone,Hash,Eq,PartialEq)]
|
||||||
pub struct IntersectingAttributes{
|
pub struct IntersectingAttributes{
|
||||||
pub water:Option<IntersectingWater>,
|
pub water:Option<IntersectingWater>,
|
||||||
}
|
}
|
||||||
|
@ -1 +1,738 @@
|
|||||||
//
|
use crate::integer::{Planar64,Planar64Vec3};
|
||||||
|
use std::borrow::{Borrow,Cow};
|
||||||
|
|
||||||
|
#[derive(Debug,Clone,Copy,Hash,Eq,PartialEq)]
|
||||||
|
pub struct VertId(usize);
|
||||||
|
#[derive(Debug,Clone,Copy,Hash,Eq,PartialEq)]
|
||||||
|
pub struct EdgeId(usize);
|
||||||
|
pub trait UndirectedEdge{
|
||||||
|
type DirectedEdge:Copy+DirectedEdge;
|
||||||
|
fn as_directed(&self,parity:bool)->Self::DirectedEdge;
|
||||||
|
}
|
||||||
|
impl UndirectedEdge for EdgeId{
|
||||||
|
type DirectedEdge=DirectedEdgeId;
|
||||||
|
fn as_directed(&self,parity:bool)->DirectedEdgeId{
|
||||||
|
DirectedEdgeId(self.0|((parity as usize)<<(usize::BITS-1)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub trait DirectedEdge{
|
||||||
|
type UndirectedEdge:Copy+UndirectedEdge;
|
||||||
|
fn as_undirected(&self)->Self::UndirectedEdge;
|
||||||
|
fn parity(&self)->bool;
|
||||||
|
//this is stupid but may work fine
|
||||||
|
fn reverse(&self)-><<Self as DirectedEdge>::UndirectedEdge as UndirectedEdge>::DirectedEdge{
|
||||||
|
self.as_undirected().as_directed(!self.parity())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// DirectedEdgeId refers to an EdgeId when undirected.
|
||||||
|
#[derive(Debug,Clone,Copy,Hash,Eq,PartialEq)]
|
||||||
|
pub struct DirectedEdgeId(usize);
|
||||||
|
impl DirectedEdge for DirectedEdgeId{
|
||||||
|
type UndirectedEdge=EdgeId;
|
||||||
|
fn as_undirected(&self)->EdgeId{
|
||||||
|
EdgeId(self.0&!(1<<(usize::BITS-1)))
|
||||||
|
}
|
||||||
|
fn parity(&self)->bool{
|
||||||
|
self.0&(1<<(usize::BITS-1))!=0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Debug,Clone,Copy,Hash,Eq,PartialEq)]
|
||||||
|
pub struct FaceId(usize);
|
||||||
|
|
||||||
|
//Vertex <-> Edge <-> Face -> Collide
|
||||||
|
pub enum FEV<F,E:DirectedEdge,V>{
|
||||||
|
Face(F),
|
||||||
|
Edge(E::UndirectedEdge),
|
||||||
|
Vert(V),
|
||||||
|
}
|
||||||
|
|
||||||
|
//use Unit32 #[repr(C)] for map files
|
||||||
|
struct Face{
|
||||||
|
normal:Planar64Vec3,
|
||||||
|
dot:Planar64,
|
||||||
|
}
|
||||||
|
struct Vert(Planar64Vec3);
|
||||||
|
pub trait MeshQuery<FACE:Clone,EDGE:Clone+DirectedEdge,VERT:Clone>{
|
||||||
|
fn edge_n(&self,edge_id:EDGE::UndirectedEdge)->Planar64Vec3{
|
||||||
|
let verts=self.edge_verts(edge_id);
|
||||||
|
self.vert(verts[1].clone())-self.vert(verts[0].clone())
|
||||||
|
}
|
||||||
|
fn directed_edge_n(&self,directed_edge_id:EDGE)->Planar64Vec3{
|
||||||
|
let verts=self.edge_verts(directed_edge_id.as_undirected());
|
||||||
|
(self.vert(verts[1].clone())-self.vert(verts[0].clone()))*((directed_edge_id.parity() as i64)*2-1)
|
||||||
|
}
|
||||||
|
fn vert(&self,vert_id:VERT)->Planar64Vec3;
|
||||||
|
fn face_nd(&self,face_id:FACE)->(Planar64Vec3,Planar64);
|
||||||
|
fn face_edges(&self,face_id:FACE)->Cow<Vec<EDGE>>;
|
||||||
|
fn edge_faces(&self,edge_id:EDGE::UndirectedEdge)->Cow<[FACE;2]>;
|
||||||
|
fn edge_verts(&self,edge_id:EDGE::UndirectedEdge)->Cow<[VERT;2]>;
|
||||||
|
fn vert_edges(&self,vert_id:VERT)->Cow<Vec<EDGE>>;
|
||||||
|
fn vert_faces(&self,vert_id:VERT)->Cow<Vec<FACE>>;
|
||||||
|
}
|
||||||
|
struct FaceRefs{
|
||||||
|
edges:Vec<DirectedEdgeId>,
|
||||||
|
//verts:Vec<VertId>,
|
||||||
|
}
|
||||||
|
struct EdgeRefs{
|
||||||
|
faces:[FaceId;2],//left, right
|
||||||
|
verts:[VertId;2],//bottom, top
|
||||||
|
}
|
||||||
|
struct VertRefs{
|
||||||
|
faces:Vec<FaceId>,
|
||||||
|
edges:Vec<DirectedEdgeId>,
|
||||||
|
}
|
||||||
|
pub struct PhysicsMesh{
|
||||||
|
faces:Vec<Face>,
|
||||||
|
verts:Vec<Vert>,
|
||||||
|
face_topology:Vec<FaceRefs>,
|
||||||
|
edge_topology:Vec<EdgeRefs>,
|
||||||
|
vert_topology:Vec<VertRefs>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default,Clone)]
|
||||||
|
struct VertRefGuy{
|
||||||
|
edges:std::collections::HashSet<DirectedEdgeId>,
|
||||||
|
faces:std::collections::HashSet<FaceId>,
|
||||||
|
}
|
||||||
|
#[derive(Clone,Hash,Eq,PartialEq)]
|
||||||
|
struct EdgeRefVerts([VertId;2]);
|
||||||
|
impl EdgeRefVerts{
|
||||||
|
fn new(v0:VertId,v1:VertId)->(Self,bool){
|
||||||
|
(if v0.0<v1.0{
|
||||||
|
Self([v0,v1])
|
||||||
|
}else{
|
||||||
|
Self([v1,v0])
|
||||||
|
},v0.0<v1.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
struct EdgeRefFaces([FaceId;2]);
|
||||||
|
impl EdgeRefFaces{
|
||||||
|
fn new()->Self{
|
||||||
|
Self([FaceId(0);2])
|
||||||
|
}
|
||||||
|
fn push(&mut self,i:usize,face_id:FaceId){
|
||||||
|
self.0[i]=face_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
struct FaceRefEdges(Vec<DirectedEdgeId>);
|
||||||
|
#[derive(Default)]
|
||||||
|
struct EdgePool{
|
||||||
|
edge_guys:Vec<(EdgeRefVerts,EdgeRefFaces)>,
|
||||||
|
edge_id_from_guy:std::collections::HashMap<EdgeRefVerts,usize>,
|
||||||
|
}
|
||||||
|
impl EdgePool{
|
||||||
|
fn push(&mut self,edge_ref_verts:EdgeRefVerts)->(&mut EdgeRefFaces,EdgeId){
|
||||||
|
let edge_id=if let Some(&edge_id)=self.edge_id_from_guy.get(&edge_ref_verts){
|
||||||
|
edge_id
|
||||||
|
}else{
|
||||||
|
let edge_id=self.edge_guys.len();
|
||||||
|
self.edge_guys.push((edge_ref_verts.clone(),EdgeRefFaces::new()));
|
||||||
|
self.edge_id_from_guy.insert(edge_ref_verts,edge_id);
|
||||||
|
edge_id
|
||||||
|
};
|
||||||
|
(&mut unsafe{self.edge_guys.get_unchecked_mut(edge_id)}.1,EdgeId(edge_id))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<&crate::model::IndexedModel> for PhysicsMesh{
|
||||||
|
fn from(indexed_model:&crate::model::IndexedModel)->Self{
|
||||||
|
assert!(indexed_model.unique_pos.len()!=0,"Mesh cannot have 0 vertices");
|
||||||
|
let verts=indexed_model.unique_pos.iter().map(|v|Vert(v.clone())).collect();
|
||||||
|
let mut vert_ref_guys=vec![VertRefGuy::default();indexed_model.unique_pos.len()];
|
||||||
|
let mut edge_pool=EdgePool::default();
|
||||||
|
let mut face_i=0;
|
||||||
|
let mut faces=Vec::new();
|
||||||
|
let mut face_ref_guys=Vec::new();
|
||||||
|
for group in indexed_model.groups.iter(){for poly in group.polys.iter(){
|
||||||
|
let face_id=FaceId(face_i);
|
||||||
|
//one face per poly
|
||||||
|
let mut normal=Planar64Vec3::ZERO;
|
||||||
|
let len=poly.vertices.len();
|
||||||
|
let face_edges=poly.vertices.iter().enumerate().map(|(i,&vert_id)|{
|
||||||
|
let vert0_id=indexed_model.unique_vertices[vert_id as usize].pos as usize;
|
||||||
|
let vert1_id=indexed_model.unique_vertices[poly.vertices[(i+1)%len] as usize].pos as usize;
|
||||||
|
//https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal (Newell's Method)
|
||||||
|
let v0=indexed_model.unique_pos[vert0_id];
|
||||||
|
let v1=indexed_model.unique_pos[vert1_id];
|
||||||
|
normal+=Planar64Vec3::new(
|
||||||
|
(v0.y()-v1.y())*(v0.z()+v1.z()),
|
||||||
|
(v0.z()-v1.z())*(v0.x()+v1.x()),
|
||||||
|
(v0.x()-v1.x())*(v0.y()+v1.y()),
|
||||||
|
);
|
||||||
|
//get/create edge and push face into it
|
||||||
|
let (edge_ref_verts,is_sorted)=EdgeRefVerts::new(VertId(vert0_id),VertId(vert1_id));
|
||||||
|
let (edge_ref_faces,edge_id)=edge_pool.push(edge_ref_verts);
|
||||||
|
//polygon vertices as assumed to be listed clockwise
|
||||||
|
//populate the edge face on the left or right depending on how the edge vertices got sorted
|
||||||
|
edge_ref_faces.push(!is_sorted as usize,face_id);
|
||||||
|
//index edges & face into vertices
|
||||||
|
{
|
||||||
|
let vert_ref_guy=unsafe{vert_ref_guys.get_unchecked_mut(vert0_id)};
|
||||||
|
vert_ref_guy.edges.insert(edge_id.as_directed(is_sorted));
|
||||||
|
vert_ref_guy.faces.insert(face_id);
|
||||||
|
unsafe{vert_ref_guys.get_unchecked_mut(vert1_id)}.edges.insert(edge_id.as_directed(!is_sorted));
|
||||||
|
}
|
||||||
|
//return directed_edge_id
|
||||||
|
edge_id.as_directed(is_sorted)
|
||||||
|
}).collect();
|
||||||
|
//choose precision loss randomly idk
|
||||||
|
normal=normal/len as i64;
|
||||||
|
let mut dot=Planar64::ZERO;
|
||||||
|
for &v in poly.vertices.iter(){
|
||||||
|
dot+=normal.dot(indexed_model.unique_pos[indexed_model.unique_vertices[v as usize].pos as usize]);
|
||||||
|
}
|
||||||
|
faces.push(Face{normal,dot:dot/len as i64});
|
||||||
|
face_ref_guys.push(FaceRefEdges(face_edges));
|
||||||
|
face_i+=1;
|
||||||
|
}}
|
||||||
|
//conceivably faces, edges, and vertices exist now
|
||||||
|
Self{
|
||||||
|
faces,
|
||||||
|
verts,
|
||||||
|
face_topology:face_ref_guys.into_iter().map(|face_ref_guy|{
|
||||||
|
FaceRefs{edges:face_ref_guy.0}
|
||||||
|
}).collect(),
|
||||||
|
edge_topology:edge_pool.edge_guys.into_iter().map(|(edge_ref_verts,edge_ref_faces)|
|
||||||
|
EdgeRefs{faces:edge_ref_faces.0,verts:edge_ref_verts.0}
|
||||||
|
).collect(),
|
||||||
|
vert_topology:vert_ref_guys.into_iter().map(|vert_ref_guy|
|
||||||
|
VertRefs{
|
||||||
|
edges:vert_ref_guy.edges.into_iter().collect(),
|
||||||
|
faces:vert_ref_guy.faces.into_iter().collect(),
|
||||||
|
}
|
||||||
|
).collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PhysicsMesh{
|
||||||
|
pub fn verts<'a>(&'a self)->impl Iterator<Item=Planar64Vec3>+'a{
|
||||||
|
self.verts.iter().map(|Vert(pos)|*pos)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl MeshQuery<FaceId,DirectedEdgeId,VertId> for PhysicsMesh{
|
||||||
|
fn face_nd(&self,face_id:FaceId)->(Planar64Vec3,Planar64){
|
||||||
|
(self.faces[face_id.0].normal,self.faces[face_id.0].dot)
|
||||||
|
}
|
||||||
|
//ideally I never calculate the vertex position, but I have to for the graphical meshes...
|
||||||
|
fn vert(&self,vert_id:VertId)->Planar64Vec3{
|
||||||
|
self.verts[vert_id.0].0
|
||||||
|
}
|
||||||
|
fn face_edges(&self,face_id:FaceId)->Cow<Vec<DirectedEdgeId>>{
|
||||||
|
Cow::Borrowed(&self.face_topology[face_id.0].edges)
|
||||||
|
}
|
||||||
|
fn edge_faces(&self,edge_id:EdgeId)->Cow<[FaceId;2]>{
|
||||||
|
Cow::Borrowed(&self.edge_topology[edge_id.0].faces)
|
||||||
|
}
|
||||||
|
fn edge_verts(&self,edge_id:EdgeId)->Cow<[VertId;2]>{
|
||||||
|
Cow::Borrowed(&self.edge_topology[edge_id.0].verts)
|
||||||
|
}
|
||||||
|
fn vert_edges(&self,vert_id:VertId)->Cow<Vec<DirectedEdgeId>>{
|
||||||
|
Cow::Borrowed(&self.vert_topology[vert_id.0].edges)
|
||||||
|
}
|
||||||
|
fn vert_faces(&self,vert_id:VertId)->Cow<Vec<FaceId>>{
|
||||||
|
Cow::Borrowed(&self.vert_topology[vert_id.0].faces)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TransformedMesh<'a>{
|
||||||
|
mesh:&'a PhysicsMesh,
|
||||||
|
transform:&'a crate::integer::Planar64Affine3,
|
||||||
|
normal_transform:&'a crate::integer::Planar64Mat3,
|
||||||
|
}
|
||||||
|
impl TransformedMesh<'_>{
|
||||||
|
pub fn new<'a>(
|
||||||
|
mesh:&'a PhysicsMesh,
|
||||||
|
transform:&'a crate::integer::Planar64Affine3,
|
||||||
|
normal_transform:&'a crate::integer::Planar64Mat3,
|
||||||
|
)->TransformedMesh<'a>{
|
||||||
|
TransformedMesh{
|
||||||
|
mesh,
|
||||||
|
transform,
|
||||||
|
normal_transform,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn farthest_vert(&self,dir:Planar64Vec3)->VertId{
|
||||||
|
let mut best_dot=Planar64::MIN;
|
||||||
|
let mut best_vert=VertId(0);
|
||||||
|
for (i,vert) in self.mesh.verts.iter().enumerate(){
|
||||||
|
let p=self.transform.transform_point3(vert.0);
|
||||||
|
let d=dir.dot(p);
|
||||||
|
if best_dot<d{
|
||||||
|
best_dot=d;
|
||||||
|
best_vert=VertId(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
best_vert
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl MeshQuery<FaceId,DirectedEdgeId,VertId> for TransformedMesh<'_>{
|
||||||
|
fn face_nd(&self,face_id:FaceId)->(Planar64Vec3,Planar64){
|
||||||
|
let (n,d)=self.mesh.face_nd(face_id);
|
||||||
|
let transformed_n=*self.normal_transform*n;
|
||||||
|
let transformed_d=Planar64::raw(((transformed_n.dot128(self.transform.matrix3*n)<<32)/n.dot128(n)) as i64)*d+transformed_n.dot(self.transform.translation);
|
||||||
|
(transformed_n,transformed_d)
|
||||||
|
}
|
||||||
|
fn vert(&self,vert_id:VertId)->Planar64Vec3{
|
||||||
|
self.transform.transform_point3(self.mesh.vert(vert_id))
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
fn face_edges(&self,face_id:FaceId)->Cow<Vec<DirectedEdgeId>>{
|
||||||
|
self.mesh.face_edges(face_id)
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
fn edge_faces(&self,edge_id:EdgeId)->Cow<[FaceId;2]>{
|
||||||
|
self.mesh.edge_faces(edge_id)
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
fn edge_verts(&self,edge_id:EdgeId)->Cow<[VertId;2]>{
|
||||||
|
self.mesh.edge_verts(edge_id)
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
fn vert_edges(&self,vert_id:VertId)->Cow<Vec<DirectedEdgeId>>{
|
||||||
|
self.mesh.vert_edges(vert_id)
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
fn vert_faces(&self,vert_id:VertId)->Cow<Vec<FaceId>>{
|
||||||
|
self.mesh.vert_faces(vert_id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Note that a face on a minkowski mesh refers to a pair of fevs on the meshes it's summed from
|
||||||
|
//(face,vertex)
|
||||||
|
//(edge,edge)
|
||||||
|
//(vertex,face)
|
||||||
|
#[derive(Clone,Copy)]
|
||||||
|
pub enum MinkowskiVert{
|
||||||
|
VertVert(VertId,VertId),
|
||||||
|
}
|
||||||
|
#[derive(Clone,Copy)]
|
||||||
|
pub enum MinkowskiEdge{
|
||||||
|
VertEdge(VertId,EdgeId),
|
||||||
|
EdgeVert(EdgeId,VertId),
|
||||||
|
//EdgeEdge when edges are parallel
|
||||||
|
}
|
||||||
|
impl UndirectedEdge for MinkowskiEdge{
|
||||||
|
type DirectedEdge=MinkowskiDirectedEdge;
|
||||||
|
fn as_directed(&self,parity:bool)->Self::DirectedEdge{
|
||||||
|
match self{
|
||||||
|
MinkowskiEdge::VertEdge(v0,e1)=>MinkowskiDirectedEdge::VertEdge(*v0,e1.as_directed(parity)),
|
||||||
|
MinkowskiEdge::EdgeVert(e0,v1)=>MinkowskiDirectedEdge::EdgeVert(e0.as_directed(parity),*v1),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Clone,Copy)]
|
||||||
|
pub enum MinkowskiDirectedEdge{
|
||||||
|
VertEdge(VertId,DirectedEdgeId),
|
||||||
|
EdgeVert(DirectedEdgeId,VertId),
|
||||||
|
//EdgeEdge when edges are parallel
|
||||||
|
}
|
||||||
|
impl DirectedEdge for MinkowskiDirectedEdge{
|
||||||
|
type UndirectedEdge=MinkowskiEdge;
|
||||||
|
fn as_undirected(&self)->Self::UndirectedEdge{
|
||||||
|
match self{
|
||||||
|
MinkowskiDirectedEdge::VertEdge(v0,e1)=>MinkowskiEdge::VertEdge(*v0,e1.as_undirected()),
|
||||||
|
MinkowskiDirectedEdge::EdgeVert(e0,v1)=>MinkowskiEdge::EdgeVert(e0.as_undirected(),*v1),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn parity(&self)->bool{
|
||||||
|
match self{
|
||||||
|
MinkowskiDirectedEdge::VertEdge(_,e)
|
||||||
|
|MinkowskiDirectedEdge::EdgeVert(e,_)=>e.parity(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Debug,Clone,Copy,Hash,Eq,PartialEq)]
|
||||||
|
pub enum MinkowskiFace{
|
||||||
|
VertFace(VertId,FaceId),
|
||||||
|
EdgeEdge(EdgeId,EdgeId,bool),
|
||||||
|
FaceVert(FaceId,VertId),
|
||||||
|
//EdgeFace
|
||||||
|
//FaceEdge
|
||||||
|
//FaceFace
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct MinkowskiMesh<'a>{
|
||||||
|
mesh0:&'a TransformedMesh<'a>,
|
||||||
|
mesh1:&'a TransformedMesh<'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
//infinity fev algorithm state transition
|
||||||
|
enum Transition{
|
||||||
|
Done,//found closest vert, no edges are better
|
||||||
|
Vert(MinkowskiVert),//transition to vert
|
||||||
|
}
|
||||||
|
enum EV{
|
||||||
|
Vert(MinkowskiVert),
|
||||||
|
Edge(MinkowskiEdge),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MinkowskiMesh<'_>{
|
||||||
|
pub fn minkowski_sum<'a>(mesh0:&'a TransformedMesh,mesh1:&'a TransformedMesh)->MinkowskiMesh<'a>{
|
||||||
|
MinkowskiMesh{
|
||||||
|
mesh0,
|
||||||
|
mesh1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn farthest_vert(&self,dir:Planar64Vec3)->MinkowskiVert{
|
||||||
|
MinkowskiVert::VertVert(self.mesh0.farthest_vert(dir),self.mesh1.farthest_vert(-dir))
|
||||||
|
}
|
||||||
|
fn next_transition_vert(&self,vert_id:MinkowskiVert,best_distance_squared:&mut Planar64,infinity_dir:Planar64Vec3,point:Planar64Vec3)->Transition{
|
||||||
|
let mut best_transition=Transition::Done;
|
||||||
|
for &directed_edge_id in self.vert_edges(vert_id).iter(){
|
||||||
|
let edge_n=self.directed_edge_n(directed_edge_id);
|
||||||
|
//is boundary uncrossable by a crawl from infinity
|
||||||
|
if infinity_dir.dot(edge_n)==Planar64::ZERO{
|
||||||
|
let edge_verts=self.edge_verts(directed_edge_id.as_undirected());
|
||||||
|
//select opposite vertex
|
||||||
|
let test_vert_id=edge_verts[directed_edge_id.parity() as usize];
|
||||||
|
//test if it's closer
|
||||||
|
let diff=point-self.vert(test_vert_id);
|
||||||
|
let distance_squared=diff.dot(diff);
|
||||||
|
if distance_squared<*best_distance_squared{
|
||||||
|
best_transition=Transition::Vert(test_vert_id);
|
||||||
|
*best_distance_squared=distance_squared;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
best_transition
|
||||||
|
}
|
||||||
|
fn final_ev(&self,vert_id:MinkowskiVert,best_distance_squared:&mut Planar64,infinity_dir:Planar64Vec3,point:Planar64Vec3)->EV{
|
||||||
|
let mut best_transition=EV::Vert(vert_id);
|
||||||
|
let diff=point-self.vert(vert_id);
|
||||||
|
for &directed_edge_id in self.vert_edges(vert_id).iter(){
|
||||||
|
let edge_n=self.directed_edge_n(directed_edge_id);
|
||||||
|
//is boundary uncrossable by a crawl from infinity
|
||||||
|
if infinity_dir.dot(edge_n)==Planar64::ZERO{
|
||||||
|
//test the edge
|
||||||
|
let d=diff.dot(edge_n);
|
||||||
|
let edge_nn=edge_n.dot(edge_n);
|
||||||
|
if Planar64::ZERO<=d&&d<=edge_nn{
|
||||||
|
let distance_squared={
|
||||||
|
let c=diff.cross(edge_n);
|
||||||
|
c.dot(c)/edge_nn
|
||||||
|
};
|
||||||
|
if distance_squared<=*best_distance_squared{
|
||||||
|
best_transition=EV::Edge(directed_edge_id.as_undirected());
|
||||||
|
*best_distance_squared=distance_squared;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
best_transition
|
||||||
|
}
|
||||||
|
fn crawl_boundaries(&self,mut vert_id:MinkowskiVert,infinity_dir:Planar64Vec3,point:Planar64Vec3)->EV{
|
||||||
|
let mut best_distance_squared={
|
||||||
|
let diff=point-self.vert(vert_id);
|
||||||
|
diff.dot(diff)
|
||||||
|
};
|
||||||
|
loop{
|
||||||
|
match self.next_transition_vert(vert_id,&mut best_distance_squared,infinity_dir,point){
|
||||||
|
Transition::Done=>return self.final_ev(vert_id,&mut best_distance_squared,infinity_dir,point),
|
||||||
|
Transition::Vert(new_vert_id)=>vert_id=new_vert_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// This function drops a vertex down to an edge or a face if the path from infinity did not cross any vertex-edge boundaries but the point is supposed to have already crossed a boundary down from a vertex
|
||||||
|
fn infinity_fev(&self,infinity_dir:Planar64Vec3,point:Planar64Vec3)->FEV::<MinkowskiFace,MinkowskiDirectedEdge,MinkowskiVert>{
|
||||||
|
//start on any vertex
|
||||||
|
//cross uncrossable vertex-edge boundaries until you find the closest vertex or edge
|
||||||
|
//cross edge-face boundary if it's uncrossable
|
||||||
|
match self.crawl_boundaries(self.farthest_vert(infinity_dir),infinity_dir,point){
|
||||||
|
//if a vert is returned, it is the closest point to the infinity point
|
||||||
|
EV::Vert(vert_id)=>FEV::<MinkowskiFace,MinkowskiDirectedEdge,MinkowskiVert>::Vert(vert_id),
|
||||||
|
EV::Edge(edge_id)=>{
|
||||||
|
//cross to face if the boundary is not crossable and we are on the wrong side
|
||||||
|
let edge_n=self.edge_n(edge_id);
|
||||||
|
let vert_sum={
|
||||||
|
let &[v0,v1]=self.edge_verts(edge_id).borrow();
|
||||||
|
self.vert(v0)+self.vert(v1)
|
||||||
|
};
|
||||||
|
for (i,&face_id) in self.edge_faces(edge_id).iter().enumerate(){
|
||||||
|
let face_n=self.face_nd(face_id).0;
|
||||||
|
//edge-face boundary nd, n facing out of the face towards the edge
|
||||||
|
let boundary_n=face_n.cross(edge_n)*(i as i64*2-1);
|
||||||
|
let boundary_d=boundary_n.dot(vert_sum);
|
||||||
|
// point.dot(boundary_n) is multiplied by two because vert_sum sums two vertices.
|
||||||
|
if infinity_dir.dot(boundary_n)==Planar64::ZERO&&point.dot(boundary_n)*2<=boundary_d{
|
||||||
|
//both faces cannot pass this condition, return early if one does.
|
||||||
|
return FEV::<MinkowskiFace,MinkowskiDirectedEdge,MinkowskiVert>::Face(face_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FEV::<MinkowskiFace,MinkowskiDirectedEdge,MinkowskiVert>::Edge(edge_id)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn closest_fev_not_inside(&self,mut infinity_body:crate::physics::Body)->Option<FEV::<MinkowskiFace,MinkowskiDirectedEdge,MinkowskiVert>>{
|
||||||
|
infinity_body.infinity_dir().map_or(None,|dir|{
|
||||||
|
let infinity_fev=self.infinity_fev(-dir,infinity_body.position);
|
||||||
|
//a line is simpler to solve than a parabola
|
||||||
|
infinity_body.velocity=dir;
|
||||||
|
infinity_body.acceleration=Planar64Vec3::ZERO;
|
||||||
|
//crawl in from negative infinity along a tangent line to get the closest fev
|
||||||
|
match crate::face_crawler::crawl_fev(infinity_fev,self,&infinity_body,crate::integer::Time::MIN,infinity_body.time){
|
||||||
|
crate::face_crawler::CrawlResult::Miss(fev)=>Some(fev),
|
||||||
|
crate::face_crawler::CrawlResult::Hit(_,_)=>None,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
pub fn predict_collision_in(&self,relative_body:&crate::physics::Body,time_limit:crate::integer::Time)->Option<(MinkowskiFace,crate::integer::Time)>{
|
||||||
|
self.closest_fev_not_inside(relative_body.clone()).map_or(None,|fev|{
|
||||||
|
//continue forwards along the body parabola
|
||||||
|
match crate::face_crawler::crawl_fev(fev,self,relative_body,relative_body.time,time_limit){
|
||||||
|
crate::face_crawler::CrawlResult::Miss(_)=>None,
|
||||||
|
crate::face_crawler::CrawlResult::Hit(face,time)=>Some((face,time)),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
pub fn predict_collision_out(&self,relative_body:&crate::physics::Body,time_limit:crate::integer::Time)->Option<(MinkowskiFace,crate::integer::Time)>{
|
||||||
|
//create an extrapolated body at time_limit
|
||||||
|
let infinity_body=crate::physics::Body::new(
|
||||||
|
relative_body.extrapolated_position(time_limit),
|
||||||
|
-relative_body.extrapolated_velocity(time_limit),
|
||||||
|
relative_body.acceleration,
|
||||||
|
-time_limit,
|
||||||
|
);
|
||||||
|
self.closest_fev_not_inside(infinity_body).map_or(None,|fev|{
|
||||||
|
//continue backwards along the body parabola
|
||||||
|
match crate::face_crawler::crawl_fev(fev,self,&-relative_body.clone(),-time_limit,-relative_body.time){
|
||||||
|
crate::face_crawler::CrawlResult::Miss(_)=>None,
|
||||||
|
crate::face_crawler::CrawlResult::Hit(face,time)=>Some((face,-time)),//no need to test -time<time_limit because of the first step
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
pub fn predict_collision_face_out(&self,relative_body:&crate::physics::Body,time_limit:crate::integer::Time,contact_face_id:MinkowskiFace)->Option<(MinkowskiEdge,crate::integer::Time)>{
|
||||||
|
//no algorithm needed, there is only one state and two cases (Edge,None)
|
||||||
|
//determine when it passes an edge ("sliding off" case)
|
||||||
|
let mut best_time=time_limit;
|
||||||
|
let mut best_edge=None;
|
||||||
|
let face_n=self.face_nd(contact_face_id).0;
|
||||||
|
for &directed_edge_id in self.face_edges(contact_face_id).iter(){
|
||||||
|
let edge_n=self.directed_edge_n(directed_edge_id);
|
||||||
|
//f x e points in
|
||||||
|
let n=face_n.cross(edge_n);
|
||||||
|
let verts=self.edge_verts(directed_edge_id.as_undirected());
|
||||||
|
let d=n.dot(self.vert(verts[0])+self.vert(verts[1]));
|
||||||
|
//WARNING! d outside of *2
|
||||||
|
for t in crate::zeroes::zeroes2((n.dot(relative_body.position))*2-d,n.dot(relative_body.velocity)*2,n.dot(relative_body.acceleration)){
|
||||||
|
let t=relative_body.time+crate::integer::Time::from(t);
|
||||||
|
if relative_body.time<t&&t<best_time&&n.dot(relative_body.extrapolated_velocity(t))<Planar64::ZERO{
|
||||||
|
best_time=t;
|
||||||
|
best_edge=Some(directed_edge_id);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
best_edge.map(|e|(e.as_undirected(),best_time))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl MeshQuery<MinkowskiFace,MinkowskiDirectedEdge,MinkowskiVert> for MinkowskiMesh<'_>{
|
||||||
|
fn face_nd(&self,face_id:MinkowskiFace)->(Planar64Vec3,Planar64){
|
||||||
|
match face_id{
|
||||||
|
MinkowskiFace::VertFace(v0,f1)=>{
|
||||||
|
let (n,d)=self.mesh1.face_nd(f1);
|
||||||
|
(-n,d-n.dot(self.mesh0.vert(v0)))
|
||||||
|
},
|
||||||
|
MinkowskiFace::EdgeEdge(e0,e1,parity)=>{
|
||||||
|
let edge0_n=self.mesh0.edge_n(e0);
|
||||||
|
let edge1_n=self.mesh1.edge_n(e1);
|
||||||
|
let &[e0v0,e0v1]=self.mesh0.edge_verts(e0).borrow();
|
||||||
|
let &[e1v0,e1v1]=self.mesh1.edge_verts(e1).borrow();
|
||||||
|
let n=edge0_n.cross(edge1_n);
|
||||||
|
let e0d=n.dot(self.mesh0.vert(e0v0)+self.mesh0.vert(e0v1));
|
||||||
|
let e1d=n.dot(self.mesh1.vert(e1v0)+self.mesh1.vert(e1v1));
|
||||||
|
(n*(parity as i64*4-2),(e0d-e1d)*(parity as i64*2-1))
|
||||||
|
},
|
||||||
|
MinkowskiFace::FaceVert(f0,v1)=>{
|
||||||
|
let (n,d)=self.mesh0.face_nd(f0);
|
||||||
|
(n,d-n.dot(self.mesh1.vert(v1)))
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn vert(&self,vert_id:MinkowskiVert)->Planar64Vec3{
|
||||||
|
match vert_id{
|
||||||
|
MinkowskiVert::VertVert(v0,v1)=>{
|
||||||
|
self.mesh0.vert(v0)-self.mesh1.vert(v1)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn face_edges(&self,face_id:MinkowskiFace)->Cow<Vec<MinkowskiDirectedEdge>>{
|
||||||
|
match face_id{
|
||||||
|
MinkowskiFace::VertFace(v0,f1)=>{
|
||||||
|
Cow::Owned(self.mesh1.face_edges(f1).iter().map(|&edge_id1|{
|
||||||
|
MinkowskiDirectedEdge::VertEdge(v0,edge_id1.reverse())
|
||||||
|
}).collect())
|
||||||
|
},
|
||||||
|
MinkowskiFace::EdgeEdge(e0,e1,parity)=>{
|
||||||
|
let e0v=self.mesh0.edge_verts(e0);
|
||||||
|
let e1v=self.mesh1.edge_verts(e1);
|
||||||
|
//could sort this if ordered edges are needed
|
||||||
|
//probably just need to reverse this list according to parity
|
||||||
|
Cow::Owned(vec![
|
||||||
|
MinkowskiDirectedEdge::VertEdge(e0v[0],e1.as_directed(parity)),
|
||||||
|
MinkowskiDirectedEdge::EdgeVert(e0.as_directed(!parity),e1v[0]),
|
||||||
|
MinkowskiDirectedEdge::VertEdge(e0v[1],e1.as_directed(!parity)),
|
||||||
|
MinkowskiDirectedEdge::EdgeVert(e0.as_directed(parity),e1v[1]),
|
||||||
|
])
|
||||||
|
},
|
||||||
|
MinkowskiFace::FaceVert(f0,v1)=>{
|
||||||
|
Cow::Owned(self.mesh0.face_edges(f0).iter().map(|&edge_id0|{
|
||||||
|
MinkowskiDirectedEdge::EdgeVert(edge_id0,v1)
|
||||||
|
}).collect())
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn edge_faces(&self,edge_id:MinkowskiEdge)->Cow<[MinkowskiFace;2]>{
|
||||||
|
match edge_id{
|
||||||
|
MinkowskiEdge::VertEdge(v0,e1)=>{
|
||||||
|
//faces are listed backwards from the minkowski mesh
|
||||||
|
let v0e=self.mesh0.vert_edges(v0);
|
||||||
|
let &[e1f0,e1f1]=self.mesh1.edge_faces(e1).borrow();
|
||||||
|
Cow::Owned([(e1f1,false),(e1f0,true)].map(|(edge_face_id1,face_parity)|{
|
||||||
|
let mut best_edge=None;
|
||||||
|
let mut best_d=Planar64::ZERO;
|
||||||
|
let edge_face1_n=self.mesh1.face_nd(edge_face_id1).0;
|
||||||
|
let edge_face1_nn=edge_face1_n.dot(edge_face1_n);
|
||||||
|
for &directed_edge_id0 in v0e.iter(){
|
||||||
|
let edge0_n=self.mesh0.directed_edge_n(directed_edge_id0);
|
||||||
|
//must be behind other face.
|
||||||
|
let d=edge_face1_n.dot(edge0_n);
|
||||||
|
if d<Planar64::ZERO{
|
||||||
|
let edge0_nn=edge0_n.dot(edge0_n);
|
||||||
|
//divide by zero???
|
||||||
|
let dd=d*d/(edge_face1_nn*edge0_nn);
|
||||||
|
if best_d<dd{
|
||||||
|
best_d=dd;
|
||||||
|
best_edge=Some(directed_edge_id0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
best_edge.map_or(
|
||||||
|
MinkowskiFace::VertFace(v0,edge_face_id1),
|
||||||
|
|directed_edge_id0|MinkowskiFace::EdgeEdge(directed_edge_id0.as_undirected(),e1,directed_edge_id0.parity()^face_parity)
|
||||||
|
)
|
||||||
|
}))
|
||||||
|
},
|
||||||
|
MinkowskiEdge::EdgeVert(e0,v1)=>{
|
||||||
|
//tracking index with an external variable because .enumerate() is not available
|
||||||
|
let v1e=self.mesh1.vert_edges(v1);
|
||||||
|
let &[e0f0,e0f1]=self.mesh0.edge_faces(e0).borrow();
|
||||||
|
Cow::Owned([(e0f0,true),(e0f1,false)].map(|(edge_face_id0,face_parity)|{
|
||||||
|
let mut best_edge=None;
|
||||||
|
let mut best_d=Planar64::ZERO;
|
||||||
|
let edge_face0_n=self.mesh0.face_nd(edge_face_id0).0;
|
||||||
|
let edge_face0_nn=edge_face0_n.dot(edge_face0_n);
|
||||||
|
for &directed_edge_id1 in v1e.iter(){
|
||||||
|
let edge1_n=self.mesh1.directed_edge_n(directed_edge_id1);
|
||||||
|
let d=edge_face0_n.dot(edge1_n);
|
||||||
|
if d<Planar64::ZERO{
|
||||||
|
let edge1_nn=edge1_n.dot(edge1_n);
|
||||||
|
let dd=d*d/(edge_face0_nn*edge1_nn);
|
||||||
|
if best_d<dd{
|
||||||
|
best_d=dd;
|
||||||
|
best_edge=Some(directed_edge_id1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
best_edge.map_or(
|
||||||
|
MinkowskiFace::FaceVert(edge_face_id0,v1),
|
||||||
|
|directed_edge_id1|MinkowskiFace::EdgeEdge(e0,directed_edge_id1.as_undirected(),directed_edge_id1.parity()^face_parity)
|
||||||
|
)
|
||||||
|
}))
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn edge_verts(&self,edge_id:MinkowskiEdge)->Cow<[MinkowskiVert;2]>{
|
||||||
|
match edge_id{
|
||||||
|
MinkowskiEdge::VertEdge(v0,e1)=>{
|
||||||
|
Cow::Owned(self.mesh1.edge_verts(e1).map(|vert_id1|{
|
||||||
|
MinkowskiVert::VertVert(v0,vert_id1)
|
||||||
|
}))
|
||||||
|
},
|
||||||
|
MinkowskiEdge::EdgeVert(e0,v1)=>{
|
||||||
|
Cow::Owned(self.mesh0.edge_verts(e0).map(|vert_id0|{
|
||||||
|
MinkowskiVert::VertVert(vert_id0,v1)
|
||||||
|
}))
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn vert_edges(&self,vert_id:MinkowskiVert)->Cow<Vec<MinkowskiDirectedEdge>>{
|
||||||
|
match vert_id{
|
||||||
|
MinkowskiVert::VertVert(v0,v1)=>{
|
||||||
|
let mut edges=Vec::new();
|
||||||
|
//detect shared volume when the other mesh is mirrored along a test edge dir
|
||||||
|
let v0f=self.mesh0.vert_faces(v0);
|
||||||
|
let v1f=self.mesh1.vert_faces(v1);
|
||||||
|
let v0f_n:Vec<Planar64Vec3>=v0f.iter().map(|&face_id|self.mesh0.face_nd(face_id).0).collect();
|
||||||
|
let v1f_n:Vec<Planar64Vec3>=v1f.iter().map(|&face_id|self.mesh1.face_nd(face_id).0).collect();
|
||||||
|
let the_len=v0f.len()+v1f.len();
|
||||||
|
for &directed_edge_id in self.mesh0.vert_edges(v0).iter(){
|
||||||
|
let n=self.mesh0.directed_edge_n(directed_edge_id);
|
||||||
|
let nn=n.dot(n);
|
||||||
|
//make a set of faces
|
||||||
|
let mut face_normals=Vec::with_capacity(the_len);
|
||||||
|
//add mesh0 faces as-is
|
||||||
|
face_normals.clone_from(&v0f_n);
|
||||||
|
for face_n in &v1f_n{
|
||||||
|
//add reflected mesh1 faces
|
||||||
|
face_normals.push(*face_n-n*(face_n.dot(n)*2/nn));
|
||||||
|
}
|
||||||
|
if is_empty_volume(face_normals){
|
||||||
|
edges.push(MinkowskiDirectedEdge::EdgeVert(directed_edge_id,v1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for &directed_edge_id in self.mesh1.vert_edges(v1).iter(){
|
||||||
|
let n=self.mesh1.directed_edge_n(directed_edge_id);
|
||||||
|
let nn=n.dot(n);
|
||||||
|
let mut face_normals=Vec::with_capacity(the_len);
|
||||||
|
face_normals.clone_from(&v1f_n);
|
||||||
|
for face_n in &v0f_n{
|
||||||
|
face_normals.push(*face_n-n*(face_n.dot(n)*2/nn));
|
||||||
|
}
|
||||||
|
if is_empty_volume(face_normals){
|
||||||
|
edges.push(MinkowskiDirectedEdge::VertEdge(v0,directed_edge_id));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Cow::Owned(edges)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn vert_faces(&self,_vert_id:MinkowskiVert)->Cow<Vec<MinkowskiFace>>{
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_empty_volume(normals:Vec<Planar64Vec3>)->bool{
|
||||||
|
let len=normals.len();
|
||||||
|
for i in 0..len-1{
|
||||||
|
for j in i+1..len{
|
||||||
|
let n=normals[i].cross(normals[j]);
|
||||||
|
let mut d_comp=None;
|
||||||
|
for k in 0..len{
|
||||||
|
if k!=i&&k!=j{
|
||||||
|
let d=n.dot(normals[k]);
|
||||||
|
if let Some(comp)=&d_comp{
|
||||||
|
if *comp*d<Planar64::ZERO{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
d_comp=Some(d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_is_empty_volume(){
|
||||||
|
assert!(!is_empty_volume([Planar64Vec3::X,Planar64Vec3::Y,Planar64Vec3::Z].to_vec()));
|
||||||
|
assert!(is_empty_volume([Planar64Vec3::X,Planar64Vec3::Y,Planar64Vec3::Z,Planar64Vec3::NEG_X].to_vec()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn build_me_a_cube(){
|
||||||
|
let unit_cube=crate::primitives::unit_cube();
|
||||||
|
let mesh=PhysicsMesh::from(&unit_cube);
|
||||||
|
//println!("mesh={:?}",mesh);
|
||||||
|
}
|
1495
src/physics.rs
1495
src/physics.rs
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user