added the other metacarpals

This commit is contained in:
Jay Christy
2023-10-25 16:54:17 -04:00
parent e5679b4e47
commit 671a947657
2 changed files with 219 additions and 31 deletions

View File

@@ -176,7 +176,7 @@ fn spawn_capsule(
)); ));
} }
#[derive(Component, PartialEq)] #[derive(Component, PartialEq, Debug)]
pub enum PhysicsHandBone { pub enum PhysicsHandBone {
Palm, Palm,
Wrist, Wrist,
@@ -238,48 +238,159 @@ fn spawn_physics_hands(mut commands: Commands) {
// SolverGroups::new(self_group, interaction_group), // SolverGroups::new(self_group, interaction_group),
PhysicsHandBone::ThumbMetacarpal, PhysicsHandBone::ThumbMetacarpal,
BoneInitState::False, BoneInitState::False,
Hand::Right,
));
//index
//spawn the thing
commands.spawn((
SpatialBundle::default(),
Collider::capsule(
Vec3 {
x: 0.0,
y: -0.0575,
z: 0.0,
},
Vec3 {
x: 0.0,
y: 0.0575,
z: 0.0,
},
radius,
),
RigidBody::KinematicPositionBased,
// CollisionGroups::new(self_group, interaction_group),
// SolverGroups::new(self_group, interaction_group),
PhysicsHandBone::IndexMetacarpal,
BoneInitState::False,
Hand::Right,
));
//middle
//spawn the thing
commands.spawn((
SpatialBundle::default(),
Collider::capsule(
Vec3 {
x: 0.0,
y: -0.0575,
z: 0.0,
},
Vec3 {
x: 0.0,
y: 0.0575,
z: 0.0,
},
radius,
),
RigidBody::KinematicPositionBased,
// CollisionGroups::new(self_group, interaction_group),
// SolverGroups::new(self_group, interaction_group),
PhysicsHandBone::MiddleMetacarpal,
BoneInitState::False,
Hand::Right,
));
//ring
//spawn the thing
commands.spawn((
SpatialBundle::default(),
Collider::capsule(
Vec3 {
x: 0.0,
y: -0.0575,
z: 0.0,
},
Vec3 {
x: 0.0,
y: 0.0575,
z: 0.0,
},
radius,
),
RigidBody::KinematicPositionBased,
// CollisionGroups::new(self_group, interaction_group),
// SolverGroups::new(self_group, interaction_group),
PhysicsHandBone::RingMetacarpal,
BoneInitState::False,
Hand::Right,
));
//little
//spawn the thing
commands.spawn((
SpatialBundle::default(),
Collider::capsule(
Vec3 {
x: 0.0,
y: -0.0575,
z: 0.0,
},
Vec3 {
x: 0.0,
y: 0.0575,
z: 0.0,
},
radius,
),
RigidBody::KinematicPositionBased,
// CollisionGroups::new(self_group, interaction_group),
// SolverGroups::new(self_group, interaction_group),
PhysicsHandBone::LittleMetacarpal,
BoneInitState::False,
Hand::Right,
)); ));
} }
fn update_physics_hands( fn update_physics_hands(
HandRes: Option<Res<HandsResource>>, hands_res: Option<Res<HandsResource>>,
mut bone_query: Query<( mut bone_query: Query<(
&mut Transform, &mut Transform,
&mut Collider, &mut Collider,
&PhysicsHandBone, &PhysicsHandBone,
&mut BoneInitState, &mut BoneInitState,
&Hand,
)>, )>,
hand_query: Query<(&Transform, &HandBone, &Hand, Without<PhysicsHandBone>)>, hand_query: Query<(&Transform, &HandBone, &Hand, Without<PhysicsHandBone>)>,
) { ) {
//sanity check do we even have hands? //sanity check do we even have hands?
match HandRes { match hands_res {
Some(res) => { Some(res) => {
//config stuff
let radius = 0.010; let radius = 0.010;
for mut bone in bone_query.iter_mut() {
let hand_res = match bone.4 {
Hand::Left => res.left,
Hand::Right => res.right,
};
//lets just do the Right ThumbMetacarpal for now //lets just do the Right ThumbMetacarpal for now
let right_thumb_meta_entity = res.right.thumb.metacarpal; let (start_entity, end_entity) = get_start_and_end_entities(hand_res, bone.2);
let right_thumb_prox_entity = res.right.thumb.proximal;
//now we need their transforms //now we need their transforms
let rtm = hand_query.get(right_thumb_meta_entity); let start_components = hand_query.get(start_entity);
let rtp = hand_query.get(right_thumb_prox_entity); let end_components = hand_query.get(end_entity);
let end = rtp.unwrap().0.translation - rtm.unwrap().0.translation; let direction =
if(end.length() < 0.001){ //i hate this but we need to skip init if the length is zero end_components.unwrap().0.translation - start_components.unwrap().0.translation;
if direction.length() < 0.001 {
//i hate this but we need to skip init if the length is zero
return; return;
} }
info!("end: {}", end.length());
for mut bone in bone_query.iter_mut() {
match *bone.3 { match *bone.3 {
BoneInitState::True => { BoneInitState::True => {
//if we are init then we just move em? //if we are init then we just move em?
*bone.0 = rtm.unwrap().0.clone().looking_at(rtp.unwrap().0.translation, Vec3::Y); *bone.0 = start_components
.unwrap()
}, .0
.clone()
.looking_at(end_components.unwrap().0.translation, Vec3::Y);
}
BoneInitState::False => { BoneInitState::False => {
if (*bone.2 == PhysicsHandBone::ThumbMetacarpal) {
//build a new collider? //build a new collider?
*bone.1 = Collider::capsule( *bone.1 = Collider::capsule(
Vec3::splat(0.0), Vec3::splat(0.0),
Vec3 { x: 0.0, y: 0.0, z: -end.length() }, Vec3 {
x: 0.0,
y: 0.0,
z: -direction.length(),
},
radius, radius,
); );
*bone.3 = BoneInitState::True; *bone.3 = BoneInitState::True;
@@ -287,11 +398,84 @@ fn update_physics_hands(
} }
} }
} }
}
None => info!("hand states resource not initialized yet"), None => info!("hand states resource not initialized yet"),
} }
} }
fn get_start_and_end_entities(hand_res: HandResource, bone: &PhysicsHandBone) -> (Entity, Entity) {
match bone {
PhysicsHandBone::Palm => return (hand_res.thumb.metacarpal, hand_res.thumb.proximal),
PhysicsHandBone::Wrist => return (hand_res.thumb.metacarpal, hand_res.thumb.proximal),
PhysicsHandBone::ThumbMetacarpal => {
return (hand_res.thumb.metacarpal, hand_res.thumb.proximal)
}
PhysicsHandBone::ThumbProximal => {
return (hand_res.thumb.metacarpal, hand_res.thumb.proximal)
}
PhysicsHandBone::ThumbDistal => {
return (hand_res.thumb.metacarpal, hand_res.thumb.proximal)
}
PhysicsHandBone::ThumbTip => return (hand_res.thumb.metacarpal, hand_res.thumb.proximal),
PhysicsHandBone::IndexMetacarpal => {
return (hand_res.index.metacarpal, hand_res.index.proximal)
}
PhysicsHandBone::IndexProximal => {
return (hand_res.thumb.metacarpal, hand_res.thumb.proximal)
}
PhysicsHandBone::IndexIntermediate => {
return (hand_res.thumb.metacarpal, hand_res.thumb.proximal)
}
PhysicsHandBone::IndexDistal => {
return (hand_res.thumb.metacarpal, hand_res.thumb.proximal)
}
PhysicsHandBone::IndexTip => return (hand_res.thumb.metacarpal, hand_res.thumb.proximal),
PhysicsHandBone::MiddleMetacarpal => {
return (hand_res.middle.metacarpal, hand_res.middle.proximal)
}
PhysicsHandBone::MiddleProximal => {
return (hand_res.thumb.metacarpal, hand_res.thumb.proximal)
}
PhysicsHandBone::MiddleIntermediate => {
return (hand_res.thumb.metacarpal, hand_res.thumb.proximal)
}
PhysicsHandBone::MiddleDistal => {
return (hand_res.thumb.metacarpal, hand_res.thumb.proximal)
}
PhysicsHandBone::MiddleTip => return (hand_res.thumb.metacarpal, hand_res.thumb.proximal),
PhysicsHandBone::RingMetacarpal => {
return (hand_res.ring.metacarpal, hand_res.ring.proximal)
}
PhysicsHandBone::RingProximal => {
return (hand_res.thumb.metacarpal, hand_res.thumb.proximal)
}
PhysicsHandBone::RingIntermediate => {
return (hand_res.thumb.metacarpal, hand_res.thumb.proximal)
}
PhysicsHandBone::RingDistal => return (hand_res.thumb.metacarpal, hand_res.thumb.proximal),
PhysicsHandBone::RingTip => return (hand_res.thumb.metacarpal, hand_res.thumb.proximal),
PhysicsHandBone::LittleMetacarpal => {
return (hand_res.little.metacarpal, hand_res.little.proximal)
}
PhysicsHandBone::LittleProximal => {
return (hand_res.thumb.metacarpal, hand_res.thumb.proximal)
}
PhysicsHandBone::LittleIntermediate => {
return (hand_res.thumb.metacarpal, hand_res.thumb.proximal)
}
PhysicsHandBone::LittleDistal => {
return (hand_res.thumb.metacarpal, hand_res.thumb.proximal)
}
PhysicsHandBone::LittleTip => return (hand_res.thumb.metacarpal, hand_res.thumb.proximal),
};
}
fn get_hand_res(res: &Res<'_, HandsResource>, hand: Hand) -> HandResource {
match hand {
Hand::Left => res.left.clone(),
Hand::Right => res.right.clone(),
}
}
#[derive(Event, Default)] #[derive(Event, Default)]
pub struct SpawnCubeRequest; pub struct SpawnCubeRequest;

View File

@@ -56,12 +56,12 @@ impl Default for HandInputSource {
} }
} }
#[derive(Resource, Default)] #[derive(Resource, Default, Clone, Copy)]
pub struct HandsResource { pub struct HandsResource {
pub left: HandResource, pub left: HandResource,
pub right: HandResource, pub right: HandResource,
} }
#[derive(Clone, Copy)]
pub struct HandResource { pub struct HandResource {
pub palm: Entity, pub palm: Entity,
pub wrist: Entity, pub wrist: Entity,
@@ -85,7 +85,7 @@ impl Default for HandResource {
} }
} }
} }
#[derive(Clone, Copy)]
pub struct ThumbResource { pub struct ThumbResource {
pub metacarpal: Entity, pub metacarpal: Entity,
pub proximal: Entity, pub proximal: Entity,
@@ -103,6 +103,7 @@ impl Default for ThumbResource {
} }
} }
} }
#[derive(Clone, Copy)]
pub struct IndexResource { pub struct IndexResource {
pub metacarpal: Entity, pub metacarpal: Entity,
pub proximal: Entity, pub proximal: Entity,
@@ -122,6 +123,7 @@ impl Default for IndexResource {
} }
} }
} }
#[derive(Clone, Copy)]
pub struct MiddleResource { pub struct MiddleResource {
pub metacarpal: Entity, pub metacarpal: Entity,
pub proximal: Entity, pub proximal: Entity,
@@ -140,6 +142,7 @@ impl Default for MiddleResource {
} }
} }
} }
#[derive(Clone, Copy)]
pub struct RingResource { pub struct RingResource {
pub metacarpal: Entity, pub metacarpal: Entity,
pub proximal: Entity, pub proximal: Entity,
@@ -158,6 +161,7 @@ impl Default for RingResource {
} }
} }
} }
#[derive(Clone, Copy)]
pub struct LittleResource { pub struct LittleResource {
pub metacarpal: Entity, pub metacarpal: Entity,
pub proximal: Entity, pub proximal: Entity,