this works much better, still bad though

This commit is contained in:
Jay Christy
2023-11-07 12:40:14 -05:00
parent 7a29d1bd36
commit a35e7b1330
2 changed files with 31 additions and 30 deletions

View File

@@ -6,12 +6,11 @@ use bevy::{
log::info, log::info,
prelude::{ prelude::{
default, shape, App, Assets, Color, Commands, Component, Entity, Event, EventReader, default, shape, App, Assets, Color, Commands, Component, Entity, Event, EventReader,
EventWriter, FixedUpdate, GlobalTransform, IntoSystemConfigs, EventWriter, FixedUpdate, Gizmos, GlobalTransform, IntoSystemConfigs, IntoSystemSetConfigs,
IntoSystemSetConfigs, Mesh, PbrBundle, PostUpdate, Quat, Query, Res, ResMut, Resource, Mesh, PbrBundle, PostUpdate, Quat, Query, Res, ResMut, Resource, Schedule, SpatialBundle,
Schedule, SpatialBundle, StandardMaterial, Startup, Transform, Update, Vec3, With, Without, StandardMaterial, Startup, Transform, Update, Vec3, Vec3Swizzles, With, Without, World,
World, Vec3Swizzles,
}, },
time::{Time, Timer, Fixed}, time::{Fixed, Time, Timer},
transform::TransformSystem, transform::TransformSystem,
}; };
use bevy_openxr::{ use bevy_openxr::{
@@ -129,7 +128,9 @@ fn main() {
.in_set(PhysicsSet::Writeback), .in_set(PhysicsSet::Writeback),
)); ));
app.add_schedule(physics_schedule) // configure our fixed timestep schedule to run at the rate we want app.add_schedule(physics_schedule) // configure our fixed timestep schedule to run at the rate we want
.insert_resource(Time::<Fixed>::from_duration(Duration::from_secs_f32(FIXED_TIMESTEP))) .insert_resource(Time::<Fixed>::from_duration(Duration::from_secs_f32(
FIXED_TIMESTEP,
)))
.add_systems(FixedUpdate, run_physics_schedule) .add_systems(FixedUpdate, run_physics_schedule)
.add_systems(Startup, configure_physics); .add_systems(Startup, configure_physics);
app.run(); app.run();
@@ -302,7 +303,7 @@ fn spawn_physics_hands(mut commands: Commands) {
), ),
RigidBody::KinematicVelocityBased, RigidBody::KinematicVelocityBased,
Velocity::default(), Velocity::default(),
CollisionGroups::new(Group::from_bits(0b1110).unwrap(), Group::from_bits(0b0001).unwrap()), // CollisionGroups::new(Group::from_bits(0b1110).unwrap(), Group::from_bits(0b0001).unwrap()),
// SolverGroups::new(self_group, interaction_group), // SolverGroups::new(self_group, interaction_group),
bone.clone(), bone.clone(),
BoneInitState::False, BoneInitState::False,
@@ -329,6 +330,7 @@ fn update_physics_hands(
)>, )>,
hand_query: Query<(&Transform, &HandBone, &Hand, Without<PhysicsHandBone>)>, hand_query: Query<(&Transform, &HandBone, &Hand, Without<PhysicsHandBone>)>,
time: Res<Time>, time: Res<Time>,
mut gizmos: Gizmos,
) { ) {
let matching = MatchingType::VelocityMatching; let matching = MatchingType::VelocityMatching;
//sanity check do we even have hands? //sanity check do we even have hands?
@@ -355,7 +357,6 @@ fn update_physics_hands(
return; return;
} }
match *bone.3 { match *bone.3 {
BoneInitState::True => { BoneInitState::True => {
match matching { match matching {
@@ -374,14 +375,27 @@ fn update_physics_hands(
/ time.delta_seconds(); / time.delta_seconds();
bone.5.linvel = diff; bone.5.linvel = diff;
//calculate angular velocity? //calculate angular velocity?
let desired_forward = start_components.unwrap().0.clone().looking_at(end_components.unwrap().0.translation, Vec3::Y).rotation; // gizmos.ray(bone.0.translation, bone.0.forward(), Color::WHITE);
let some: Vec3 = help_me(bone.0.rotation,desired_forward); let desired_forward = start_components
.unwrap()
.0
.clone()
.looking_at(end_components.unwrap().0.translation, Vec3::Y)
.rotation;
// gizmos.ray(
// bone.0.translation,
// desired_forward.mul_vec3(-Vec3::Z),
// Color::GREEN,
// );
let cross =
bone.0.forward().cross(desired_forward.mul_vec3(-Vec3::Z));
if *bone.2 == PhysicsHandBone::IndexIntermediate && *bone.4 == Hand::Right { // gizmos.ray(
info!("{}", some); // bone.0.translation,
// info!("{}", forward); // cross,
} // Color::RED,
bone.5.angvel = 5.0 * some / time.delta_seconds(); // );
bone.5.angvel = cross / time.delta_seconds();
} }
} }
} }
@@ -406,20 +420,6 @@ fn update_physics_hands(
} }
} }
fn help_me(rotation: Quat, desired_forward: Quat) -> Vec3 {
let conj = rotation.conjugate();
let diff = desired_forward.mul_quat(conj);
let mut test = diff.to_axis_angle();
if test.0.x.is_infinite() {
return Vec3::ZERO;
}
// if test.1 > 180.0 {
// test.1 = test.1 - 360.0;
// }
let ang = (0.99 * (PI / 180.0) * test.1) * test.0;
return ang;
}
fn get_start_and_end_entities( fn get_start_and_end_entities(
hand_res: HandResource, hand_res: HandResource,
bone: &PhysicsHandBone, bone: &PhysicsHandBone,

View File

@@ -34,6 +34,7 @@ pub fn setup_scene(
transform: Transform::from_xyz(0.0, ground_height, 0.0), transform: Transform::from_xyz(0.0, ground_height, 0.0),
..default() ..default()
}, },
RigidBody::Fixed,
Collider::cuboid(ground_size, ground_thickness, ground_size), Collider::cuboid(ground_size, ground_thickness, ground_size),
)); ));
// cube // cube